This is an automated email from the ASF dual-hosted git repository.
Croway pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new e4f06f376561 CAMEL-24607: camel-langchain4j-agent-api -
CodeInjectionGuardrail must count distinct injection types
e4f06f376561 is described below
commit e4f06f3765617b6016ab170a7288f523c564af32
Author: Andrea Cosentino <[email protected]>
AuthorDate: Thu Sep 3 10:07:55 2026 +0200
CAMEL-24607: camel-langchain4j-agent-api - CodeInjectionGuardrail must
count distinct injection types
In non-strict mode CodeInjectionGuardrail collected every matched pattern's
type
into a list without de-duplication and then blocked when the list had two
or more
entries, although the intent (per the inline comment and the sibling
PromptInjectionGuardrail) is to require multiple DIFFERENT types. Because
several
patterns share a type (e.g. two TEMPLATE_INJECTION patterns for {{...}} and
${...}),
a single legitimate templating question such as "render {{name}} and
${value}" was
falsely blocked. Only record a type when it is not already present, so the
"multiple types" check counts distinct types.
Co-Authored-By: Claude Opus 4.8 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Ka4dAcJMpxahMfk3kmG5Ls
Signed-off-by: Andrea Cosentino <[email protected]>
---
.../langchain4j/agent/api/guardrails/CodeInjectionGuardrail.java | 6 +++++-
.../agent/api/guardrails/CodeInjectionGuardrailTest.java | 9 +++++++++
2 files changed, 14 insertions(+), 1 deletion(-)
diff --git
a/components/camel-ai/camel-langchain4j-agent-api/src/main/java/org/apache/camel/component/langchain4j/agent/api/guardrails/CodeInjectionGuardrail.java
b/components/camel-ai/camel-langchain4j-agent-api/src/main/java/org/apache/camel/component/langchain4j/agent/api/guardrails/CodeInjectionGuardrail.java
index 1e4148a67c39..558cac7f4f9f 100644
---
a/components/camel-ai/camel-langchain4j-agent-api/src/main/java/org/apache/camel/component/langchain4j/agent/api/guardrails/CodeInjectionGuardrail.java
+++
b/components/camel-ai/camel-langchain4j-agent-api/src/main/java/org/apache/camel/component/langchain4j/agent/api/guardrails/CodeInjectionGuardrail.java
@@ -226,7 +226,11 @@ public class CodeInjectionGuardrail implements
InputGuardrail {
}
if (pattern.getPattern().matcher(text).find()) {
- detected.add(pattern.getType());
+ // Only record distinct types: the non-strict check below
blocks on "multiple different types",
+ // and several patterns share a type, so counting duplicates
would false-positive on a single type.
+ if (!detected.contains(pattern.getType())) {
+ detected.add(pattern.getType());
+ }
if (strict) {
return failure(String.format(
diff --git
a/components/camel-ai/camel-langchain4j-agent-api/src/test/java/org/apache/camel/component/langchain4j/agent/api/guardrails/CodeInjectionGuardrailTest.java
b/components/camel-ai/camel-langchain4j-agent-api/src/test/java/org/apache/camel/component/langchain4j/agent/api/guardrails/CodeInjectionGuardrailTest.java
index 536b22d65a2f..9da371195a6d 100644
---
a/components/camel-ai/camel-langchain4j-agent-api/src/test/java/org/apache/camel/component/langchain4j/agent/api/guardrails/CodeInjectionGuardrailTest.java
+++
b/components/camel-ai/camel-langchain4j-agent-api/src/test/java/org/apache/camel/component/langchain4j/agent/api/guardrails/CodeInjectionGuardrailTest.java
@@ -122,6 +122,15 @@ class CodeInjectionGuardrailTest {
assertFalse(guardrail.validate(UserMessage.from("Run {{template}} and
../../../etc/passwd")).isSuccess());
}
+ @Test
+ void testNonStrictModeDoesNotBlockMultipleMatchesOfTheSameType() {
+ CodeInjectionGuardrail guardrail = new CodeInjectionGuardrail();
+
+ // Both {{...}} and ${...} are TEMPLATE_INJECTION patterns, i.e. a
single type - a legitimate
+ // templating question must not be blocked just because two same-type
patterns matched.
+ assertTrue(guardrail.validate(UserMessage.from("render {{name}} and
${value}")).isSuccess());
+ }
+
@Test
void testForSpecificTypes() {
// Use builder with strict mode to fail on single match