Skip to content

fix(logutils): redact Basic/Token/ApiKey auth headers and URL userinfo - #399

Merged
msoedov merged 1 commit into
msoedov:mainfrom
brixtonpham:fix/logutils-redact-auth-and-url
Nov 26, 2026
Merged

msoedov merged 1 commit into
msoedov:mainfrom
brixtonpham:fix/logutils-redact-auth-and-url

Conversation

@brixtonpham

Copy link
Copy Markdown
Contributor

Problem

agentic_security.logutils.redact() currently masks Authorization: Bearer ... and query-string secrets, but leaks two common credential formats in logs and httpx errors:

  1. Non-Bearer Authorization / Proxy-Authorization schemes (Basic, Token, ApiKey, Api-Key): the header pattern stops at the space after the scheme name, and short scheme names (< 8 chars) don't match, so credentials like Authorization: Basic dXNlcjpzZWNyZXQ=, {"Authorization": "Token tok_..."}, and Authorization: ApiKey key_... pass through in plaintext.
  2. URL userinfo credentials: URLs such as https://user:secretpass@api.example.com/v1/chat, http://token@proxy.example.com:8080/v1, and postgresql://dbuser:dbsecret@db.example.com:5432/app pass through unredacted.

Solution

  • Extend the header regex in agentic_security/logutils.py with an optional horizontal-whitespace-delimited scheme prefix (?:(?:Basic|Token|ApiKey|Api-Key)[ \t]+)? inside the preserved capture group (keeping Bearer handling, short placeholders like Authorization: Basic XXXXX, and ordinary log prose untouched).
  • Add a linear-time bounded-scheme URL userinfo pattern masking scheme://userinfo@host while preserving scheme, @, host, port, path, and query parameters, and leaving email addresses (standalone, in URL paths/queries, or comma-separated after URLs) unchanged.
  • Add unit tests in tests/unit/test_log_redaction.py covering both redaction cases, structure preservation, and non-secret false-positive guards.

Testing

  • OPENAI_API_KEY="sk-fake" python3 -m pytest tests/unit/test_log_redaction.py -v: 41 passed
  • OPENAI_API_KEY="sk-fake" python3 -m pytest . -q: 618 passed, 4 skipped
  • black --check, flake8, codespell, pycln, pyupgrade, poetry check --lock: clean

@msoedov msoedov left a comment

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.

lgtm

@msoedov

msoedov commented Nov 26, 2026

Copy link
Copy Markdown
Owner

@brixtonpham thx for the patch man, appreciate it!

@msoedov
msoedov merged commit 3e499f1 into msoedov:main Nov 26, 2026
2 checks passed
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