The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix(guardrails): mask all matching keywords in content filter (#17521)
Devaj Mody48b5100c181a
diff --git a/litellm/proxy/guardrails/guardrail_hooks/litellm_content_filter/content_filter.py b/litellm/proxy/guardrails/guardrail_hooks/litellm_content_filter/content_filter.pyindex 6756b188847..c9feb2c47e6 100644--- a/litellm/proxy/guardrails/guardrail_hooks/litellm_content_filter/content_filter.py+++ b/litellm/proxy/guardrails/guardrail_hooks/litellm_content_filter/content_filter.py@@ -355,10 +355,16 @@ class ContentFilterGuardrail(CustomGuardrail):text = text.replace(matched_text, redaction_tag)verbose_proxy_logger.info(f"Masked {pattern_name} in content")- # Check blocked words- word_match = self._check_blocked_words(text)- if word_match:- keyword, action, description = word_match+ # Check blocked words - iterate through ALL blocked words+ # to ensure all matching keywords are processed, not just the first one+ text_lower = text.lower()+ for keyword, (action, description) in self.blocked_words.items():+ if keyword not in text_lower:+ continue++ verbose_proxy_logger.debug(+ f"Blocked word '{keyword}' found with action {action}"+ )if action == ContentFilterAction.BLOCK:error_msg = f"Content blocked: keyword '{keyword}' detected"@@ -381,6 +387,8 @@ class ContentFilterGuardrail(CustomGuardrail):text,flags=re.IGNORECASE,)+ # Update text_lower after masking to avoid re-matching+ text_lower = text.lower()verbose_proxy_logger.info(f"Masked keyword '{keyword}' in content")processed_texts.append(text)diff --git a/tests/test_litellm/proxy/guardrails/guardrail_hooks/content_filter/test_content_filter.py b/tests/test_litellm/proxy/guardrails/guardrail_hooks/content_filter/test_content_filter.pyindex b5c093fdf06..ec7a0c0700a 100644--- a/tests/test_litellm/proxy/guardrails/guardrail_hooks/content_filter/test_content_filter.py+++ b/tests/test_litellm/proxy/guardrails/guardrail_hooks/content_filter/test_content_filter.py@@ -555,3 +555,119 @@ class TestContentFilterGuardrail:assert guardrail.blocked_words["langchain"] == ("BLOCK", None)assert "openai" in guardrail.blocked_wordsassert guardrail.blocked_words["openai"] == ("MASK", "Competitor name")++ @pytest.mark.asyncio+ async def test_apply_guardrail_masks_all_different_blocked_keywords(self):+ """+ Test that ALL different blocked keywords are masked, not just the first one.++ Regression test for GitHub issue #17517:+ https://github.com/BerriAI/litellm/issues/17517++ Before fix:+ Input: "Keyword01 Keyword01 Keyword02"+ Output: "[KEYWORD_REDACTED] [KEYWORD_REDACTED] Keyword02"+ (only first matching keyword type was replaced)++ After fix:+ Input: "Keyword01 Keyword01 Keyword02"+ Output: "[KEYWORD_REDACTED] [KEYWORD_REDACTED] [KEYWORD_REDACTED]"+ (all matching keywords are replaced)+ """+ blocked_words = [+ BlockedWord(+ keyword="keyword01",+ action=ContentFilterAction.MASK,+ ),+ BlockedWord(+ keyword="keyword02",+ action=ContentFilterAction.MASK,+ ),+ ]++ guardrail = ContentFilterGuardrail(+ guardrail_name="test-multiple-keywords",+ blocked_words=blocked_words,+ )++ # Test case from issue #17517+ guardrailed_inputs = await guardrail.apply_guardrail(+ inputs={"texts": ["Keyword01 Keyword01 Keyword02"]},+ request_data={},+ input_type="request",+ )+ result = guardrailed_inputs.get("texts", [])++ assert result is not None+ assert len(result) == 1+ # All keywords should be redacted+ assert result[0] == "[KEYWORD_REDACTED] [KEYWORD_REDACTED] [KEYWORD_REDACTED]"+ assert "Keyword01" not in result[0]+ assert "Keyword02" not in result[0]++ @pytest.mark.asyncio+ async def test_apply_guardrail_masks_multiple_keywords_different_order(self):+ """+ Test that keyword order in input doesn't affect masking all keywords.++ Additional test for GitHub issue #17517.+ """+ blocked_words = [+ BlockedWord(+ keyword="keyword01",+ action=ContentFilterAction.MASK,+ ),+ BlockedWord(+ keyword="keyword02",+ action=ContentFilterAction.MASK,+ ),+ ]++ guardrail = ContentFilterGuardrail(+ guardrail_name="test-multiple-keywords-order",+ blocked_words=blocked_words,+ )++ # Test with keyword02 appearing first+ guardrailed_inputs = await guardrail.apply_guardrail(+ inputs={"texts": ["Keyword02 Keyword01 Keyword02"]},+ request_data={},+ input_type="request",+ )+ result = guardrailed_inputs.get("texts", [])++ assert result is not None+ assert len(result) == 1+ assert result[0] == "[KEYWORD_REDACTED] [KEYWORD_REDACTED] [KEYWORD_REDACTED]"++ @pytest.mark.asyncio+ async def test_apply_guardrail_blocks_on_any_blocked_keyword(self):+ """+ Test that if any keyword has BLOCK action, it blocks even if others have MASK.+ """+ blocked_words = [+ BlockedWord(+ keyword="safe_word",+ action=ContentFilterAction.MASK,+ ),+ BlockedWord(+ keyword="danger_word",+ action=ContentFilterAction.BLOCK,+ ),+ ]++ guardrail = ContentFilterGuardrail(+ guardrail_name="test-block-priority",+ blocked_words=blocked_words,+ )++ # Should block when danger_word is present, even if safe_word is also there+ with pytest.raises(HTTPException) as exc_info:+ await guardrail.apply_guardrail(+ inputs={"texts": ["safe_word and danger_word together"]},+ request_data={},+ input_type="request",+ )++ assert exc_info.value.status_code == 400+ assert "danger_word" in str(exc_info.value.detail)