Skip to content

Secure coding improvements, pr number 1#391

Merged
TomasTomecek merged 4 commits intopackit:mainfrom
TomasTomecek:secure-coding-1
Mar 31, 2026
Merged

Secure coding improvements, pr number 1#391
TomasTomecek merged 4 commits intopackit:mainfrom
TomasTomecek:secure-coding-1

Conversation

@TomasTomecek
Copy link
Copy Markdown
Member

I will send more with followup PRs. There were even more changes here but decided to split/cut them because I didn't like Claude's proposals.

Claude's detailed overview:

2. Add credential redaction to MCP gateway logs (HIGH)

Files: mcp_server/gateway.py, mcp_server/tests/unit/test_gateway.py

Added _redact() function with regex patterns matching common credential formats:

  • GitLab PATs (glpat-...)
  • Anthropic API keys (sk-ant-...)
  • Google API keys (AIzaSy...)
  • OAuth2 tokens in URLs (oauth2:...@)
  • Generic long tokens (20+ chars with keywords like token/key/password)

The log_tool_call decorator now redacts all tool arguments and error messages before logging.

Test coverage: 20 unit tests covering pattern matching, edge cases, and real-world scenarios.

3. Add LITELLM_DEBUG environment variable guard (HIGH)

Files: agents/utils.py, templates/beeai-agent.env

set_litellm_debug() now checks for LITELLM_DEBUG env var before enabling debug mode. If not set, logs a warning and returns without enabling token-leaking debug output.

Added comprehensive documentation explaining the security risk and usage.

5. Add SecurityContext to K8s deployments (LOW)

Files: openshift/deployment-*.yml (10 files)

Replaced empty securityContext: {} with:

securityContext:
  runAsNonRoot: true
  seccompProfile:
    type: RuntimeDefault

Matches the pattern already used in cronjob manifests.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request significantly enhances application security by implementing credential redaction in logs and strengthening Kubernetes deployment configurations. Specifically, a new _redact utility and associated regex patterns were introduced in mcp_server/gateway.py to prevent sensitive data like API keys and tokens from appearing in log outputs, with comprehensive unit tests added for validation. The set_litellm_debug function was updated to prevent token leakage by requiring an explicit environment variable for debug logging and adding a clear warning. Furthermore, all Kubernetes deployment files were updated to enforce runAsNonRoot: true and apply a default seccompProfile for improved runtime security. A review comment suggested making the _REDACT_PATTERNS a frozenset to ensure its immutability.

Signed-off-by: Tomas Tomecek <ttomecek@redhat.com>
Assisted-by: Claude
Signed-off-by: Tomas Tomecek <ttomecek@redhat.com>
Assisted-by: Claude
@TomasTomecek
Copy link
Copy Markdown
Member Author

/gemini review

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces several security enhancements, including the integration of detect-secrets into the pre-commit workflow, the addition of a redaction mechanism to mask sensitive credentials in logs, and the hardening of OpenShift deployments with non-root security contexts. Feedback focuses on refining the redaction regex to prevent destructive string replacement, consolidating duplicate sanitization logic into a shared utility, and strengthening unit tests to verify that redacted strings preserve their original structure.

Signed-off-by: Tomas Tomecek <ttomecek@redhat.com>
Assisted-by: Claude
Signed-off-by: Tomas Tomecek <ttomecek@redhat.com>
Assisted-by: Claude
# Bearer tokens in URLs or strings
re.compile(r"oauth2:[^@\s]+@"),
# Testing Farm API tokens (UUID format)
re.compile(r"[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}", re.IGNORECASE),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm curious if this won't cause trouble when logging e.g. Celery task IDs (if we decide to use Celery).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

great point Nikola; I expect that we'll update the regexes in the future or even the logging mechanism

@TomasTomecek TomasTomecek merged commit 9092507 into packit:main Mar 31, 2026
7 checks passed
@TomasTomecek TomasTomecek deleted the secure-coding-1 branch March 31, 2026 09:59
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