Skip to content

🛡️ Sentinel: Enforce strict whitelist for rule validation#136

Merged
abhimehro merged 1 commit intomainfrom
sentinel-whitelist-validation-5659401450376526586
Jan 27, 2026
Merged

🛡️ Sentinel: Enforce strict whitelist for rule validation#136
abhimehro merged 1 commit intomainfrom
sentinel-whitelist-validation-5659401450376526586

Conversation

@abhimehro
Copy link
Owner

@abhimehro abhimehro commented Jan 27, 2026

✅ Merged via local merge and push to main.

Changes integrated:

  • Replaced blacklist validation with strict whitelist regex (^[a-zA-Z0-9.\-_:*\/]+$)
  • Added comprehensive parametrized tests covering valid and invalid inputs
  • Updated .jules/sentinel.md with the security learning

Test Results: 56/56 tests passing.

Replaced fragile blacklist with strict regex whitelist to prevent injection.
Added comprehensive security tests.

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 January 27, 2026 10:58
@trunk-io
Copy link

trunk-io bot commented Jan 27, 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.

@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 significantly bolsters the security of rule validation by transitioning from a fragile blacklist to a robust whitelist mechanism. The is_valid_rule function now strictly enforces allowed characters, effectively preventing various injection attack vectors. This change is thoroughly tested with new, comprehensive test cases and documented as a security learning.

Highlights

  • Security Enhancement: Replaced a vulnerable blacklist approach with a strict whitelist for validating input rules in the is_valid_rule function, significantly reducing the attack surface for injection.
  • Validation Logic Update: The is_valid_rule function in main.py now uses a regular expression ^[a-zA-Z0-9.\-_:*\/]+$ to permit only alphanumeric characters and specific safe symbols (hyphen, dot, underscore, colon, asterisk, slash).
  • Comprehensive Testing: New parameterized tests were added to tests/test_security.py to thoroughly verify the new whitelist validation against both valid and various invalid (injection) inputs, ensuring robustness.
  • Documentation Update: A new security learning entry was added to .jules/sentinel.md to document the vulnerability of blacklists and the robustness of whitelists, along with prevention strategies.
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.

"""
Tests the is_valid_rule function against a strict whitelist of inputs.
"""
assert is_valid_rule(rule) == expected_validity, f"Failed for rule: {rule}"

Check notice

Code scanning / Bandit

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
"""
Tests the is_valid_rule function against a strict whitelist of inputs.
"""
assert is_valid_rule(rule) == expected_validity, f"Failed for rule: {rule}"

Check notice

Code scanning / Bandit (reported by Codacy)

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
@@ -1,6 +1,8 @@

Check warning

Code scanning / Pylintpython3 (reported by Codacy)

Missing module docstring Warning test

Missing module docstring
@@ -1,6 +1,8 @@

Check warning

Code scanning / Pylint (reported by Codacy)

Missing module docstring Warning test

Missing module docstring
1. Implement strict validation on all data items from external lists (`is_valid_rule`).
2. Filter out items containing dangerous characters (`<`, `>`, `"` etc.) or control codes.

## 2026-05-24 - [Weak Blacklist Validation for Rules]

Check notice

Code scanning / Remark-lint (reported by Codacy)

Warn when references to undefined definitions are found. Note

[no-undefined-references] Found reference to undefined definition
1. Implement strict validation on all data items from external lists (`is_valid_rule`).
2. Filter out items containing dangerous characters (`<`, `>`, `"` etc.) or control codes.

## 2026-05-24 - [Weak Blacklist Validation for Rules]

Check notice

Code scanning / Remark-lint (reported by Codacy)

Warn when shortcut reference links are used. Note

[no-shortcut-reference-link] Use the trailing [] on reference links
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

This pull request makes a significant security improvement by replacing a fragile blacklist with a strict whitelist for rule validation. The change in is_valid_rule effectively mitigates a class of injection vulnerabilities. The accompanying tests are thorough, though I've pointed out a couple of cases that seem to validate incorrect rule formats. I've also suggested a minor performance optimization by pre-compiling the new regular expression. Overall, this is a solid and important change.

Comment on lines +110 to +111
("http://example.com", True), # Safe from injection (contains only allowed chars)
("example.com/path", True), # Allowed (CIDR uses /)

Choose a reason for hiding this comment

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

high

These two test cases seem to assert that invalid rule formats are valid, which could lead to incorrect behavior or mask future bugs.

  • http://example.com is a URL, not a valid domain/IP/CIDR rule for DNS filtering.
  • example.com/path is also not a valid rule format. The / is intended for CIDR notation with IPs, not for paths with domains.

These should likely be expected to fail validation. This would also mean tightening the regex in is_valid_rule to be more semantically strict, which would make the function more robust.

Suggested change
("http://example.com", True), # Safe from injection (contains only allowed chars)
("example.com/path", True), # Allowed (CIDR uses /)
("http://example.com", False), # Not a valid rule format (URL, not domain/IP)
("example.com/path", False), # Not a valid rule format (domain with path)

if any(c in dangerous_chars for c in rule):
# Strict whitelist to prevent injection
# ^[a-zA-Z0-9.\-_:*\/]+$
if not re.match(r"^[a-zA-Z0-9.\-_:*\/]+$", rule):

Choose a reason for hiding this comment

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

medium

This is a great security improvement! For a performance optimization, consider pre-compiling this regular expression at the module level. Since is_valid_rule is called inside a loop for every rule, pre-compiling avoids the overhead of parsing the regex pattern on each call.

You could do something like this:

# At the top of main.py
_VALID_RULE_RE = re.compile(r"^[a-zA-Z0-9.\-_:*\/]+$")

# Then in is_valid_rule()
if not _VALID_RULE_RE.match(rule):
    return False

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

This PR strengthens input validation for rule processing by replacing a fragile blacklist approach with a strict whitelist regex pattern, significantly improving security against injection attacks.

Changes:

  • Replaced blacklist validation in is_valid_rule() with a whitelist regex pattern (^[a-zA-Z0-9.\-_:*\/]+$)
  • Added comprehensive test coverage for both valid inputs (domains, IPs, IPv6, CIDR, wildcards) and malicious inputs (injection payloads)
  • Documented the vulnerability and fix in the sentinel log

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
main.py Replaced blacklist with strict whitelist regex in is_valid_rule() function
tests/test_security.py Added parametrized test cases covering valid and malicious input patterns
.jules/sentinel.md Documented the vulnerability, learning, and prevention measures

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

@abhimehro abhimehro merged commit a279fcb into main Jan 27, 2026
24 checks passed
@abhimehro abhimehro deleted the sentinel-whitelist-validation-5659401450376526586 branch January 27, 2026 13:02
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