Skip to content

fix(http_spec): expand secrets in headers and URL and guard None values - #401

Open
brixtonpham wants to merge 2 commits into
msoedov:mainfrom
brixtonpham:fix/http-spec-secret-expansion
Open

brixtonpham wants to merge 2 commits into
msoedov:mainfrom
brixtonpham:fix/http-spec-secret-expansion

Conversation

@brixtonpham

Copy link
Copy Markdown
Contributor

Problem

Two bugs affect secret expansion in HTTP specs:

  1. parse_http_spec() (agentic_security/http_spec.py) only expands $SECRET placeholders in body, ignoring headers and url. Because 18 of 20 built-in LLM_SPECS presets and the default Web UI spec use Authorization: Bearer $OPENAI_API_KEY (or API keys in URL query params), the literal $OPENAI_API_KEY string is sent over the wire and fails with HTTP 401.
  2. expand_secrets() / set_secrets() (agentic_security/core/app.py) crash on repeated calls when an env var is unset. The first set_secrets({"KEY": "$UNSET_VAR"}) stores None in _secrets; the next set_secrets(...) call iterates over _secrets and calls val.startswith("$") on None, raising AttributeError: 'NoneType' object has no attribute 'startswith'. Re-expanding all of _secrets also re-runs env lookup on previously stored secrets whose literal value begins with $.

Solution

  • agentic_security/http_spec.py:
    • Add _build_secret_replacer() using single-pass regex substitution (longest key first, (?![A-Za-z0-9_]) token boundary, callable replacement so $ or \1 inside secret values cannot double-expand or trigger backreferences).
    • Skip None, empty, non-string, and bare "$" entries cleanly.
    • Expand secrets in url, parsed headers values (leaving header names untouched so : in secret values cannot break header parsing), and body.
    • Validate the expanded URL with urlparse, while formatting InvalidHTTPSpecError with the unexpanded raw_url so expanded secrets are not leaked in errors.
  • agentic_security/core/app.py:
    • Guard expand_secrets() with isinstance(val, str) and val.startswith("$") and update set_secrets() to expand a copy of only the incoming dict before _secrets.update(expanded).
  • Tests (tests/unit/test_spec.py, agentic_security/core/test_app.py):
    • Add unit tests covering the built-in OpenAI preset, URL/header/body expansion, longest-key boundary & single-pass $/\1 safety, None/non-string skipping, non-leaking InvalidHTTPSpecError, and repeated set_secrets() calls.

Testing

  • OPENAI_API_KEY="sk-fake" python3 -m pytest tests/unit/test_spec.py agentic_security/core/test_app.py -v: 24 passed
  • OPENAI_API_KEY="sk-fake" python3 -m pytest . -q: 649 passed, 4 skipped
  • pre-commit run --all-files: all hooks passed

return Modality.TEXT


def _build_secret_replacer(

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding this _build_secret_replacer!

However, I would leave get_secrets and set_secrets as they are and use an empty string instead of None.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call — reverted get_secrets and set_secrets to their original signatures/behavior and updated expand_secrets to fall back to "" instead of None.

This branch has not been deployed

No deployments
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