Skip to content

fix(security): resolve Python warning-severity CodeQL alerts (#3164)#3204

Merged
mrveiss merged 2 commits intoDev_new_guifrom
fix/3164-py-warnings
Apr 1, 2026
Merged

fix(security): resolve Python warning-severity CodeQL alerts (#3164)#3204
mrveiss merged 2 commits intoDev_new_guifrom
fix/3164-py-warnings

Conversation

@mrveiss
Copy link
Copy Markdown
Owner

@mrveiss mrveiss commented Apr 1, 2026

Summary

Alert types fixed

Alert Files Fix
py/bad-tag-filter api/knowledge.py Replaced regex re.sub(r"<[^>]+>") with html.parser.HTMLParser
py/regex-injection security/domain_security.py Used re.escape() on config-driven glob patterns
py/polynomial-redos knowledge/metadata.py Fixed email regex backtracking ([a-zA-Z0-9-.]+(?:[a-zA-Z0-9-]+)+)
py/polynomial-redos utils/encoding_utils.py Bounded ANSI OSC patterns with {0,1024} quantifier limit
py/polynomial-redos api/analytics_controller.py Removed lookahead causing exponential backtracking
py/polynomial-redos services/semantic_analyzer.py Bounded [\s\S]* with {0,5000} limit

Not addressed in this PR (false positives / intentional design)

  • py/insecure-protocol (~4) — All http:// URLs are internal service-to-service communication on private networks
  • py/clear-text-logging-sensitive-data (~55) — CodeQL flags variable names like token/password but no actual secrets are logged
  • py/command-line-injection (~3) — Terminal executor and elevation wrapper intentionally run user commands
  • py/full-ssrf (2) — Already fixed in prior PRs (validate_url applied)

Test plan

  • python -c "from html.parser import HTMLParser" passes
  • Email validation still matches valid emails, rejects invalid
  • ANSI stripping still works on terminal output
  • Domain security pattern matching still blocks/allows expected domains

🤖 Generated with Claude Code

- py/bad-tag-filter: replace regex HTML stripping with html.parser in knowledge.py
- py/regex-injection: use re.escape() for config-driven patterns in domain_security.py
- py/polynomial-redos: fix backtracking in email/URL patterns (metadata.py),
  ANSI escape patterns (encoding_utils.py), analytics slug regex, and
  docstring detection patterns (semantic_analyzer.py)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@mrveiss
Copy link
Copy Markdown
Owner Author

mrveiss commented Apr 1, 2026

Code review

Found 2 issues:

  1. Slug regex lookahead removal changes endpoint normalization behavior. The original pattern ^(?=.*[a-z])(?=.*\d)[a-z0-9_-]{8,}$ required both letters AND digits, as documented by the comment directly above: "contains both letters and digits, length ≥ 8. Avoids collapsing short word slugs." The replacement ^[a-z0-9_-]{8,}$ now matches pure-word paths like /dashboard, /settings, /knowledge (all ≥8 chars, all-alpha), which will be incorrectly collapsed to {id} in analytics. A bounded lookahead like (?=[a-z0-9_-]{0,200}[a-z])(?=[a-z0-9_-]{0,200}\d) would fix ReDoS without changing semantics.

re.compile(r"^\d+$"),
# Alphanumeric slugs that look generated: starts with alpha/digit, contains
# both letters and digits, length ≥ 8. Avoids collapsing short word slugs.
re.compile(r"^[a-z0-9_-]{8,}$", re.I),
]

  1. URL validation pattern now rejects valid URLs with ports or query strings without a leading slash. The old pattern ended with .*$ (match anything after host). The new (?:/\S*)?$ requires any suffix to start with /. This rejects RFC 3986-valid URLs like https://api.example.com:8080/path, https://example.com?query=1, and https://docs.example.com#section because port numbers, bare query strings, and fragments are not preceded by / in the new pattern.

# Validation pattern for URL
URL_PATTERN = re.compile(
r"^https?://[a-zA-Z0-9][-a-zA-Z0-9]*(?:\.[a-zA-Z0-9][-a-zA-Z0-9]*)+(?:/\S*)?$"
)

🤖 Generated with Claude Code

- If this code review was useful, please react with 👍. Otherwise, react with 👎.

…upport (#3164)

Restore bounded lookaheads in analytics slug pattern to prevent collapsing
real word paths (e.g. /dashboard, /knowledge) into {id}. Fix URL validation
to accept ports, query strings, and fragments per RFC 3986.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@mrveiss mrveiss merged commit 53ec912 into Dev_new_gui Apr 1, 2026
1 of 3 checks passed
@mrveiss mrveiss deleted the fix/3164-py-warnings branch April 1, 2026 20:25
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