forked from keksiqc/ctrld-sync
-
Notifications
You must be signed in to change notification settings - Fork 1
🛡️ Sentinel: Fix CSV Injection in log sanitization #222
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
abhimehro
merged 7 commits into
main
from
sentinel-csv-injection-fix-4975783286757750034
Feb 16, 2026
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5c57fbc
🛡️ Sentinel: [MEDIUM] Fix CSV Injection in log sanitization
google-labs-jules[bot] 7fce5c4
Update main.py
abhimehro b860d50
Update test_csv_injection.py
abhimehro 8ec3a43
Update sentinel.md
abhimehro 281de00
Merge branch 'main' into sentinel-csv-injection-fix-4975783286757750034
abhimehro 4a2c5bb
Merge branch 'main' into sentinel-csv-injection-fix-4975783286757750034
abhimehro 5c22248
Merge branch 'main' into sentinel-csv-injection-fix-4975783286757750034
abhimehro File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
|
|
||
| import unittest | ||
| import sys | ||
| import os | ||
|
|
||
| # Add root to path to import main | ||
| sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | ||
|
|
||
| import main | ||
|
|
||
| class TestCSVInjection(unittest.TestCase): | ||
| def test_csv_injection_prevention(self): | ||
| """ | ||
| Verify that sanitize_for_log correctly keeps quotes around strings | ||
| that start with characters known to trigger formula execution in spreadsheets | ||
| (CSV Injection). | ||
| """ | ||
| # Test cases for CSV injection characters | ||
| dangerous_inputs = [ | ||
| "=cmd|' /C calc'!A0", | ||
| "+cmd|' /C calc'!A0", | ||
| "-cmd|' /C calc'!A0", | ||
| "@cmd|' /C calc'!A0", | ||
| ] | ||
|
|
||
| for inp in dangerous_inputs: | ||
| sanitized = main.sanitize_for_log(inp) | ||
| # Should keep quotes (repr adds them) | ||
| # repr("=...") -> "'=...'" | ||
| # So sanitized should start with ' or " | ||
| self.assertTrue(sanitized.startswith("'") or sanitized.startswith('"'), | ||
| f"Input '{inp}' should be quoted to prevent CSV injection. Got: {sanitized}") | ||
|
|
||
| # Should contain the input | ||
| self.assertIn(inp, sanitized) | ||
|
|
||
| def test_normal_string_behavior(self): | ||
| """ | ||
| Verify that normal strings (not starting with =, +, -, @) still have | ||
| their outer quotes stripped, preserving existing behavior. | ||
| """ | ||
| safe_inputs = [ | ||
| "NormalString", | ||
| "Folder Name", | ||
| "12345", | ||
| "<script>alert(1)</script>", # XSS attempt (handled by repr escaping but checked here for quote stripping) | ||
| ] | ||
|
|
||
| for inp in safe_inputs: | ||
| sanitized = main.sanitize_for_log(inp) | ||
| # Should NOT start with quote (unless repr escaped something inside and used different quotes, but for simple strings it shouldn't) | ||
| # Actually, repr("NormalString") is 'NormalString'. Stripped -> NormalString. | ||
| # repr("Folder Name") is 'Folder Name'. Stripped -> Folder Name. | ||
| self.assertFalse(sanitized.startswith(("'", '"')), | ||
| f"Input '{inp}' should have outer quotes stripped. Got: {sanitized}") | ||
|
|
||
| # For strict check: | ||
| self.assertEqual(sanitized, repr(inp)[1:-1]) | ||
|
|
||
| def test_empty_input(self): | ||
| """Verify empty input handling.""" | ||
| self.assertEqual(main.sanitize_for_log(""), "") | ||
| self.assertEqual(main.sanitize_for_log(None), "None") | ||
|
|
||
| if __name__ == '__main__': | ||
| unittest.main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test refactoring appears unrelated to the CSV injection fix described in the PR title and description. The change from decorator-based
@patch("main.is_valid_rule")to context managerwith patch.object(main, "is_valid_rule")is a valid refactoring, but it's unclear why it's included in this PR focused on security fixes. Consider moving unrelated refactorings to a separate PR to maintain clear separation of concerns and make reviews easier.