Skip to content

fix(ai-red-teaming): JSON-encode prompt in generated agent target bodies - #152

Merged
rdheekonda merged 1 commit into
mainfrom
fix/agentic-target-prompt-json-escaping
Sep 22, 2026
Merged

rdheekonda merged 1 commit into
mainfrom
fix/agentic-target-prompt-json-escaping

Conversation

@rdheekonda

Copy link
Copy Markdown
Contributor

Summary

The generated HTTP-agent target built its request body with
{template}.replace('{prompt}', prompt.replace('"', '\\"')) - escaping only
double quotes. Multi-line / backslash-heavy adversarial prompts (GOAT, TAP,
crescendo) then produced invalid JSON, so json.loads(body_str) failed and
the target returned empty responses. Attacks recorded 0 tool_calls / empty
content and trials failed even against a vulnerable agent.

Found by running a GOAT attack via the TUI against a dict-returning agent that
returns populated tool_calls on a direct curl but empty responses through the
attack.

Fix

Substitute the prompt with json.dumps(prompt)[1:-1] so quotes, newlines, and
backslashes are escaped correctly (valid JSON for any prompt). Applied to both
generated-target builders (agentic + custom). Bump 1.17.1 -> 1.17.2.

Validation

  • New TestAgentTargetPromptEscaping (old escaping fails json.loads on a nasty
    multi-line prompt; new escaping round-trips exactly) + existing normalization
    tests -> pass.
  • Verified: '{"message":"{prompt}"}'.replace('{prompt}', json.dumps(nasty)[1:-1])
    parses and preserves the prompt for a prompt containing newlines, quotes, and
    backslashes.

The generated HTTP-agent target built its request body with
`{template}.replace('{prompt}', prompt.replace('"', '\\"'))`, escaping only
double quotes. Multi-line / backslash-heavy adversarial prompts (GOAT, TAP,
crescendo) then produced invalid JSON, so json.loads failed and the target
returned empty responses - attacks recorded 0 tool_calls / empty content and
trials failed even against a vulnerable agent. Substitute the prompt with
json.dumps(prompt)[1:-1] so quotes, newlines, and backslashes are escaped
correctly. Bump 1.17.1 -> 1.17.2.

Found by running a GOAT attack via the TUI against a dict-returning agent that
returns populated tool_calls on a direct curl but empty responses through the
attack.
@rdheekonda
rdheekonda merged commit 885a50f into main Sep 22, 2026
5 checks passed
@rdheekonda
rdheekonda deleted the fix/agentic-target-prompt-json-escaping branch September 22, 2026 21:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant