Skip to content

Potential fix for code scanning alert no. 6: Log Injection - #16

Merged
tormachris merged 2 commits into
mainfrom
alert-autofix-6
Nov 12, 2026
Merged

tormachris merged 2 commits into
mainfrom
alert-autofix-6

Conversation

@tormachris

Copy link
Copy Markdown
Member

Potential fix for https://github.com/Ctrl-Alt-GG/streams/security/code-scanning/6

General fix: sanitize untrusted values before logging by removing line-break/control characters (at least \r and \n) so attacker input cannot split or forge log entries.

Best targeted fix here: in src/catalog/services/cache.py, sanitize key at the log site in read_public_cache. Add a small helper (e.g., _sanitize_for_log) that converts input to string and strips \r/\n, then use it when logging key. This preserves existing behavior (same key used for cache operations) while making logging safe.

Changes needed:

  • File: src/catalog/services/cache.py
  • Edits:
    1. Add helper function near _now():
      • Accepts object, returns str.
      • Replaces \r and \n with empty string.
    2. Update line with logger.warning(...) in read_public_cache to log sanitized key.

No new imports or external dependencies are required.

Suggested fixes powered by Copilot Autofix. Review carefully before merging.

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
Signed-off-by: Torma Kristóf <[email protected]>
@tormachris
tormachris marked this pull request as ready for review November 12, 2026 21:31
Copilot AI balanced review requested due to automatic review settings November 12, 2026 21:31

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The log-injection protection lacks regression coverage verifying that warnings strip carriage returns and newlines while the original key is passed to cache.get.

1 open issue
What changed in this PR

This PR addresses log injection in the catalog cache service by sanitizing cache keys before logging Redis read failures, while preserving the original keys for cache operations.

Changes:

  • Add _sanitize_for_log to convert values to strings and remove carriage returns and newlines.
  • Apply sanitization to the key in read_public_cache warning messages.
File Description
src/​catalog/​services/​cache.py Add a sanitization helper and use it when logging failed cache reads.

Review effort: Balanced

return cache.get(key)
except (RedisConnectionError, RedisTimeoutError) as error:
logger.warning("catalog_cache_read_failed key=%s error=%s", key, error)
logger.warning("catalog_cache_read_failed key=%s error=%s", _sanitize_for_log(key), error)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Added the regression test in commit 668a645. It verifies that a key containing both CR and LF is passed unchanged to cache.get while the formatted warning contains neither character.

@tormachris
tormachris merged commit 09b03fc into main Nov 12, 2026
7 checks passed
@tormachris
tormachris deleted the alert-autofix-6 branch November 12, 2026 21:42
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.

3 participants