Skip to content

fix: CustomVulnerabilityTemplate handles None purpose and special characters - #216

Open
frankentini wants to merge 1 commit into
confident-ai:mainfrom
frankentini:fix/custom-vulnerability-template-escaping
Open

fix: CustomVulnerabilityTemplate handles None purpose and special characters#216
frankentini wants to merge 1 commit into
confident-ai:mainfrom
frankentini:fix/custom-vulnerability-template-escaping

Conversation

@frankentini

Copy link
Copy Markdown
Contributor

Summary

Fixes three bugs in CustomVulnerabilityTemplate that cause crashes or incorrect output when using CustomVulnerability:

Bug 1: AttributeError when purpose is None

_apply_template_variables calls purpose.strip() unconditionally, raising AttributeError: 'NoneType' object has no attribute 'strip' when purpose is omitted.

Bug 2: KeyError/ValueError when purpose contains curly braces

The old code substituted {purpose} directly into the template string before calling str.format(). If the purpose contained curly braces (e.g. "JSON parser {key: value}"), the subsequent .format() call would misinterpret them as format placeholders and crash.

Fix: {purpose} is now passed through str.format() like the other variables (name, type, max_goldens), so special characters in user-supplied values are handled safely.

Bug 3: Literal None rendered in fallback prompt

_generate_fallback_prompt used an f-string with {purpose}, rendering the literal string "None" when purpose was not provided. Now defaults to "general assistant".

Tests

Added 17 focused unit tests covering:

  • Basic placeholder substitution
  • None, empty, and whitespace-only purpose handling
  • Special characters (curly braces) in purpose and name
  • Literal brace preservation in templates
  • Repeated and missing placeholders
  • Fallback prompt generation
  • Entry point routing (custom prompt vs fallback)

All tests pass:

======================== 17 passed in 0.04s =========================

Related

Partially addresses the template-related crash path described in #154 (TypeError: can only join an iterable with custom vulnerability).

…racters

Fix three bugs in CustomVulnerabilityTemplate:

1. _apply_template_variables crashes with AttributeError when purpose
   is None because it calls purpose.strip() unconditionally.

2. _apply_template_variables substitutes {purpose} before str.format(),
   so if purpose contains curly braces (e.g. JSON descriptions), the
   subsequent .format() call raises KeyError or ValueError. Now purpose
   is passed through str.format() like the other variables.

3. _generate_fallback_prompt renders literal 'None' in the output when
   purpose is not provided. Now defaults to 'general assistant'.

Add 17 focused tests for the template module covering placeholder
substitution, None/empty/whitespace purpose, special characters in
user-supplied values, literal braces preservation, and fallback prompt
generation.
@vercel

vercel Bot commented Apr 8, 2026

Copy link
Copy Markdown

@frankentini is attempting to deploy a commit to the Confident AI Team on Vercel.

A member of the Team first needs to authorize it.

@penguine-ip

Copy link
Copy Markdown
Contributor

Hey @frankentini thanks for the fix! One thing I'd like to ask is can we not default to a general assistant purpose? This could lead to incorrectness in certain scnearios.

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.

2 participants