Skip to content

🛡️ Sentinel: Redact sensitive query parameters in logs#157

Closed
abhimehro wants to merge 1 commit intomainfrom
sentinel/redact-sensitive-logs-17711217631418324444
Closed

🛡️ Sentinel: Redact sensitive query parameters in logs#157
abhimehro wants to merge 1 commit intomainfrom
sentinel/redact-sensitive-logs-17711217631418324444

Conversation

@abhimehro
Copy link
Owner

Redacts common sensitive query parameters (token, key, secret, etc.) from URLs in log messages to prevent accidental information disclosure. This is a defense-in-depth measure.


PR created automatically by Jules for task 17711217631418324444 started by @abhimehro

Co-authored-by: abhimehro <84992105+abhimehro@users.noreply.github.com>
@google-labs-jules
Copy link

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

Copilot AI review requested due to automatic review settings February 3, 2026 10:58
@trunk-io
Copy link

trunk-io bot commented Feb 3, 2026

Merging to main in this repository is managed by Trunk.

  • To merge this pull request, check the box to the left or comment /trunk merge below.

@github-actions github-actions bot added the python label Feb 3, 2026
@gemini-code-assist
Copy link

Summary of Changes

Hello @abhimehro, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a significant security enhancement by automatically redacting sensitive query parameters from URLs before they are written to logs. This measure prevents the inadvertent exposure of confidential information such as API keys or tokens, thereby strengthening the application's security posture and ensuring log integrity.

Highlights

  • Enhanced Log Security: Implemented redaction of common sensitive query parameters (e.g., 'token', 'key', 'secret') from URLs in log messages to prevent accidental information disclosure, acting as a defense-in-depth measure.
  • New Unit Tests: Added a new test file, 'tests/test_security_log.py', to thoroughly validate the new query parameter redaction logic with various test cases.
Changelog
  • main.py
    • Modified the 'sanitize_for_log' function to include a regular expression-based redaction for common sensitive URL query parameters like 'token', 'key', 'secret', 'password', 'auth', 'access_token', and 'api_key'.
    • Reordered the redaction logic within 'sanitize_for_log' to first handle general sensitive query parameters, followed by the specific global 'TOKEN' redaction.
  • tests/test_security_log.py
    • Added a new test file to introduce comprehensive unit tests for the 'sanitize_for_log' function's new query parameter redaction capabilities.
    • Implemented 'test_redact_query_params' with multiple scenarios to ensure sensitive parameters are correctly identified and replaced with '[REDACTED]' in various URL formats.
Activity
  • The pull request was automatically created by Jules for a task (17711217631418324444) initiated by @abhimehro.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.


self.assertEqual(result_content, expected, f"Failed for input: {input_str}")

if __name__ == "__main__":

Check warning

Code scanning / Prospector (reported by Codacy)

expected 2 blank lines after class or function definition, found 1 (E305) Warning test

expected 2 blank lines after class or function definition, found 1 (E305)
# 1. Redact common sensitive query parameters in URLs (Defense in Depth)
# Matches ?param=value or &param=value
# Stops at &, whitespace, or quotes
s = re.sub(

Check warning

Code scanning / Pylint (reported by Codacy)

Variable name "s" doesn't conform to snake_case naming style Warning

Variable name "s" doesn't conform to snake_case naming style
@@ -0,0 +1,56 @@
import unittest

Check warning

Code scanning / Pylint (reported by Codacy)

Missing module docstring Warning test

Missing module docstring
import unittest
from main import sanitize_for_log

class TestSecurityLog(unittest.TestCase):

Check warning

Code scanning / Pylint (reported by Codacy)

Missing class docstring Warning test

Missing class docstring
from main import sanitize_for_log

class TestSecurityLog(unittest.TestCase):
def test_redact_query_params(self):

Check warning

Code scanning / Pylint (reported by Codacy)

Missing method docstring Warning test

Missing method docstring
# Also repr() escapes things.
# Our expected strings don't have special chars that repr escapes (except maybe quotes).
# But the proposed implementation applies redaction BEFORE repr.
# So sanitizing "url?token=s" -> "url?token=[REDACTED]" -> repr() -> "'url?token=[REDACTED]'"

Check warning

Code scanning / Pylint (reported by Codacy)

Line too long (105/100) Warning test

Line too long (105/100)
# 1. Redact common sensitive query parameters in URLs (Defense in Depth)
# Matches ?param=value or &param=value
# Stops at &, whitespace, or quotes
s = re.sub(

Check warning

Code scanning / Pylintpython3 (reported by Codacy)

Variable name "s" doesn't conform to snake_case naming style Warning

Variable name "s" doesn't conform to snake_case naming style
@@ -0,0 +1,56 @@
import unittest

Check warning

Code scanning / Pylintpython3 (reported by Codacy)

Missing module docstring Warning test

Missing module docstring
import unittest
from main import sanitize_for_log

class TestSecurityLog(unittest.TestCase):

Check warning

Code scanning / Pylintpython3 (reported by Codacy)

Missing class docstring Warning test

Missing class docstring
from main import sanitize_for_log

class TestSecurityLog(unittest.TestCase):
def test_redact_query_params(self):

Check warning

Code scanning / Pylintpython3 (reported by Codacy)

Missing function or method docstring Warning test

Missing function or method docstring
# Also repr() escapes things.
# Our expected strings don't have special chars that repr escapes (except maybe quotes).
# But the proposed implementation applies redaction BEFORE repr.
# So sanitizing "url?token=s" -> "url?token=[REDACTED]" -> repr() -> "'url?token=[REDACTED]'"

Check warning

Code scanning / Pylintpython3 (reported by Codacy)

Line too long (105/100) Warning test

Line too long (105/100)
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds defense-in-depth log sanitization by redacting common sensitive URL query parameters (e.g., token/key/secret/password) before emitting log messages.

Changes:

  • Extend sanitize_for_log to redact sensitive query parameter values via regex substitution.
  • Add a new unit test module covering expected query-parameter redaction behavior.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
main.py Adds regex-based query-parameter redaction in sanitize_for_log prior to existing TOKEN redaction and control-char escaping.
tests/test_security_log.py Introduces tests for URL query parameter redaction behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@@ -149,8 +149,21 @@ def check_env_permissions(env_path: str = ".env") -> None:
def sanitize_for_log(text: Any) -> str:
"""Sanitize text for logging, ensuring TOKEN is redacted and control chars are escaped."""
Copy link

Copilot AI Feb 3, 2026

Choose a reason for hiding this comment

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

sanitize_for_log now redacts common sensitive URL query parameters in addition to the global TOKEN, but the docstring still only mentions TOKEN redaction and control-char escaping. Please update the docstring to reflect the expanded behavior so callers/tests have an accurate contract.

Suggested change
"""Sanitize text for logging, ensuring TOKEN is redacted and control chars are escaped."""
"""
Sanitize text for logging.
This helper:
- Redacts values of common sensitive URL query parameters
(e.g. token, key, secret, password, auth, access_token, api_key).
- Redacts the global TOKEN value, if present.
- Escapes control characters to reduce log-injection and terminal issues.
"""

Copilot uses AI. Check for mistakes.
Comment on lines +27 to +30
),
(
"'https://quoted.com?password=pass'",
"https://quoted.com?password=[REDACTED]"
Copy link

Copilot AI Feb 3, 2026

Choose a reason for hiding this comment

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

This test case expects sanitize_for_log to strip the surrounding quotes from the input URL. With the current implementation, the outer quotes become escaped by repr() (e.g., \'...\') and will not be removed by the helper logic below, so this assertion will fail. Either remove this case, or change the expected value to match the actual sanitized output, or adjust sanitize_for_log to normalize/strip surrounding quotes before applying repr() if that’s desired behavior.

Suggested change
),
(
"'https://quoted.com?password=pass'",
"https://quoted.com?password=[REDACTED]"

Copilot uses AI. Check for mistakes.
Comment on lines +35 to +54
# sanitize_for_log uses repr() which adds quotes and escapes.
# We need to handle that in our expectation or strip it.
# The current implementation of sanitize_for_log returns a repr() string (quoted).
# If our expected string is the *content* inside the quotes, we should match that.

result = sanitize_for_log(input_str)

# Remove surrounding quotes for easier comparison if present
if len(result) >= 2 and result[0] == result[-1] and result[0] in ("'", '"'):
result_content = result[1:-1]
else:
result_content = result

# Also repr() escapes things.
# Our expected strings don't have special chars that repr escapes (except maybe quotes).
# But the proposed implementation applies redaction BEFORE repr.
# So sanitizing "url?token=s" -> "url?token=[REDACTED]" -> repr() -> "'url?token=[REDACTED]'"

self.assertEqual(result_content, expected, f"Failed for input: {input_str}")

Copy link

Copilot AI Feb 3, 2026

Choose a reason for hiding this comment

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

The inline comments here describe sanitize_for_log as returning a quoted repr() string, but the current implementation strips matching outer quotes before returning. This makes the comments misleading and the extra quote-stripping logic below unnecessary for most cases; consider simplifying the test to compare sanitize_for_log(input_str) directly to expected (and update comments accordingly).

Suggested change
# sanitize_for_log uses repr() which adds quotes and escapes.
# We need to handle that in our expectation or strip it.
# The current implementation of sanitize_for_log returns a repr() string (quoted).
# If our expected string is the *content* inside the quotes, we should match that.
result = sanitize_for_log(input_str)
# Remove surrounding quotes for easier comparison if present
if len(result) >= 2 and result[0] == result[-1] and result[0] in ("'", '"'):
result_content = result[1:-1]
else:
result_content = result
# Also repr() escapes things.
# Our expected strings don't have special chars that repr escapes (except maybe quotes).
# But the proposed implementation applies redaction BEFORE repr.
# So sanitizing "url?token=s" -> "url?token=[REDACTED]" -> repr() -> "'url?token=[REDACTED]'"
self.assertEqual(result_content, expected, f"Failed for input: {input_str}")
# sanitize_for_log is expected to return the sanitized string directly,
# with any sensitive query parameters redacted.
result = sanitize_for_log(input_str)
self.assertEqual(result, expected, f"Failed for input: {input_str}")

Copilot uses AI. Check for mistakes.
Comment on lines +156 to +161
s = re.sub(
r"([?&](?:token|key|secret|password|auth|access_token|api_key)=)([^&\s\"']+)",
r"\1[REDACTED]",
s,
flags=re.IGNORECASE,
)
Copy link

Copilot AI Feb 3, 2026

Choose a reason for hiding this comment

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

The new query-param redaction regex does not stop at URL fragments ('#'). Because # is allowed in the value character class, an input like ...?token=abc#section will redact the entire abc#section and drop the fragment text. Consider treating # as a terminator (and potentially other common delimiters) so only the parameter value is replaced and the rest of the message is preserved.

Copilot uses AI. Check for mistakes.
Copy link

@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

The pull request introduces a valuable security enhancement by redacting sensitive query parameters in logs, which is a good defense-in-depth measure. The addition of a new test file tests/test_security_log.py is also a positive step towards ensuring the correctness of this feature. However, there are critical issues identified related to the completeness of sensitive data redaction due to an overly restrictive regular expression, and an incorrect test case input that misrepresents expected behavior. Additionally, there is redundant logic in the new test file that can be streamlined for better maintainability.

# Matches ?param=value or &param=value
# Stops at &, whitespace, or quotes
s = re.sub(
r"([?&](?:token|key|secret|password|auth|access_token|api_key)=)([^&\s\"']+)",

Choose a reason for hiding this comment

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

critical

The regular expression r"([?&](?:token|key|secret|password|auth|access_token|api_key)=)([^&\s\"']+)" used for redacting sensitive query parameters is too restrictive. The character class [^&\s\"'] explicitly excludes single quotes ('), double quotes ("), and whitespace. This means if a sensitive query parameter's value contains any of these characters (e.g., token=abc'def or token=abc def), the redaction will stop prematurely, leaving a portion of the sensitive information exposed in the logs. This defeats the purpose of redacting sensitive data and poses a significant security risk. A more robust approach would be to redact the entire value until the next URL parameter delimiter (&) or the end of the string.

Suggested change
r"([?&](?:token|key|secret|password|auth|access_token|api_key)=)([^&\s\"']+)",
r"([?&](?:token|key|secret|password|auth|access_token|api_key)=)([^&]+)",

"https://safe.com?public=data"
),
(
"'https://quoted.com?password=pass'",

Choose a reason for hiding this comment

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

high

The test case at this line uses an input string that is already quoted: "'https://quoted.com?password=pass'". This format implies that the input text to sanitize_for_log is a string literal containing quotes, rather than a raw URL string. The sanitize_for_log function is designed to apply repr() for escaping control characters and then strip the outermost quotes. When the input itself is already quoted, repr() will escape these inner quotes, leading to an output that still contains escaped quotes (e.g., "'https://quoted.com?password=[REDACTED]'") which does not match the expected value of "https://quoted.com?password=[REDACTED]". To properly test the redaction of a URL containing a sensitive parameter with a value that includes a single quote, the input should be a raw URL string.

Suggested change
"'https://quoted.com?password=pass'",
"https://quoted.com?password=pass'",

Comment on lines +43 to +47
if len(result) >= 2 and result[0] == result[-1] and result[0] in ("'", '"'):
result_content = result[1:-1]
else:
result_content = result

Choose a reason for hiding this comment

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

medium

The sanitize_for_log function in main.py now includes logic (lines 170-172) to remove the outermost quotes added by repr(). Therefore, the result_content extraction logic within this test is redundant. The result returned by sanitize_for_log should already be the unquoted string.

            # The sanitize_for_log function now handles stripping repr() quotes.
            # So, result should be directly comparable to expected.
            result_content = result

# But the proposed implementation applies redaction BEFORE repr.
# So sanitizing "url?token=s" -> "url?token=[REDACTED]" -> repr() -> "'url?token=[REDACTED]'"

self.assertEqual(result_content, expected, f"Failed for input: {input_str}")

Choose a reason for hiding this comment

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

medium

Following the removal of redundant repr() handling logic, the comparison in the assertion should directly use the result from sanitize_for_log, as it already returns the unquoted string.

            self.assertEqual(result, expected, f"Failed for input: {input_str}")

Copilot AI added a commit that referenced this pull request Feb 8, 2026
…itization, add dry-run plan details

Incorporates the best changes from 36 Jules PRs, addressing review feedback:

Bolt (Performance) - from PR #173:
- Pre-compile PROFILE_ID_PATTERN and RULE_PATTERN at module level
- Use compiled patterns in is_valid_profile_id_format, validate_profile_id, and is_valid_rule
- Supersedes PRs: #140, #143, #152, #155, #158, #161, #167, #170, #173

Sentinel (Security) - from PR #172 with review feedback:
- Enhance sanitize_for_log to redact Basic Auth credentials in URLs
- Redact sensitive query parameters (token, key, secret, password, etc.)
- Handle fragment separators (#) per Gemini Code Assist review
- Use [^&#\s]* pattern per Copilot reviewer suggestion
- Update docstring per reviewer suggestion
- Supersedes PRs: #142, #145, #148, #151, #154, #157, #160, #169, #172

Palette (UX) - from PR #174 with lint fixes:
- Add print_plan_details function for dry-run visibility
- Fix duplicate render_progress_bar definition bug
- Supersedes PRs: #139, #141, #144, #147, #150, #153, #156, #159, #162, #165, #168, #171, #174

Also: #146, #149, #164 (parallel folder deletion) and #166 (auto-fix .env perms) are independent features not consolidated here.

Co-authored-by: abhimehro <84992105+abhimehro@users.noreply.github.com>
@abhimehro abhimehro closed this Feb 9, 2026
@abhimehro abhimehro deleted the sentinel/redact-sensitive-logs-17711217631418324444 branch February 9, 2026 00:19
abhimehro added a commit that referenced this pull request Feb 9, 2026
…itization, add dry-run plan details

Incorporates the best changes from 36 Jules PRs, addressing review feedback:

Bolt (Performance) - from PR #173:
- Pre-compile PROFILE_ID_PATTERN and RULE_PATTERN at module level
- Use compiled patterns in is_valid_profile_id_format, validate_profile_id, and is_valid_rule
- Supersedes PRs: #140, #143, #152, #155, #158, #161, #167, #170, #173

Sentinel (Security) - from PR #172 with review feedback:
- Enhance sanitize_for_log to redact Basic Auth credentials in URLs
- Redact sensitive query parameters (token, key, secret, password, etc.)
- Handle fragment separators (#) per Gemini Code Assist review
- Use [^&#\s]* pattern per Copilot reviewer suggestion
- Update docstring per reviewer suggestion
- Supersedes PRs: #142, #145, #148, #151, #154, #157, #160, #169, #172

Palette (UX) - from PR #174 with lint fixes:
- Add print_plan_details function for dry-run visibility
- Fix duplicate render_progress_bar definition bug
- Supersedes PRs: #139, #141, #144, #147, #150, #153, #156, #159, #162, #165, #168, #171, #174

Also: #146, #149, #164 (parallel folder deletion) and #166 (auto-fix .env perms) are independent features not consolidated here.

Co-authored-by: abhimehro <84992105+abhimehro@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants