-
Notifications
You must be signed in to change notification settings - Fork 0
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
Fix false positive for single or double quotes that are safely escaped #49
Open
hansott
wants to merge
12
commits into
main
Choose a base branch
from
escaped-single-double
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 8 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
6eaf27c
Fix false positive for single or double quotes that are safely escaped
hansott 0f7c34f
Simplify code
hansott 2c15426
Simplify
hansott 4636745
Improve comment
hansott 8c22d44
Rename function
hansott ac4a831
Format code
hansott 3d9d135
Simplify
hansott 6285cb9
Add comment back
hansott eef88aa
Add some more tests
hansott 3c407bc
Format code
hansott 87f8e79
Make sure one spot has safely escaped double quote in test
hansott 10fac23
Should be three double quotes
hansott 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
There are no files selected for viewing
This file contains 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,12 @@ | ||
pub fn find_all_matches(haystack: &str, needle: &str) -> Vec<usize> { | ||
let mut positions = Vec::new(); | ||
let mut start = 0; | ||
|
||
while let Some(pos) = haystack[start..].find(needle) { | ||
let match_pos = start + pos; | ||
positions.push(match_pos); | ||
start = match_pos + 1; // Allow overlapping matches | ||
} | ||
|
||
positions | ||
} |
This file contains 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,67 @@ | ||
#[cfg(test)] | ||
mod tests { | ||
use crate::helpers::find_all_matches::find_all_matches; | ||
|
||
#[test] | ||
fn test_find_all_matches() { | ||
let haystack = "hello world hello world hello world"; | ||
let needle = "world"; | ||
let expected = vec![6, 18, 30]; | ||
let result = find_all_matches(haystack, needle); | ||
assert_eq!(result, expected); | ||
} | ||
|
||
#[test] | ||
fn test_no_matches() { | ||
let haystack = "hello world hello world hello world"; | ||
let needle = "foo"; | ||
let expected = vec![]; | ||
let result = find_all_matches(haystack, needle); | ||
assert_eq!(result, expected); | ||
} | ||
|
||
#[test] | ||
fn test_empty_haystack() { | ||
let haystack = ""; | ||
let needle = "world"; | ||
let expected = vec![]; | ||
let result = find_all_matches(haystack, needle); | ||
assert_eq!(result, expected); | ||
} | ||
|
||
#[test] | ||
fn test_empty_needle() { | ||
let haystack = "hello world hello world hello world"; | ||
let needle = ""; | ||
let expected = vec![]; | ||
let result = find_all_matches(haystack, needle); | ||
assert_eq!(result, expected); | ||
} | ||
|
||
#[test] | ||
fn test_empty_haystack_and_needle() { | ||
let haystack = ""; | ||
let needle = ""; | ||
let expected = vec![]; | ||
let result = find_all_matches(haystack, needle); | ||
assert_eq!(result, expected); | ||
} | ||
|
||
#[test] | ||
fn test_single_char() { | ||
let haystack = "hello world hello world hello world"; | ||
let needle = "w"; | ||
let expected = vec![6, 18, 30]; | ||
let result = find_all_matches(haystack, needle); | ||
assert_eq!(result, expected); | ||
} | ||
|
||
#[test] | ||
fn test_overlapping_matches() { | ||
let haystack = "aaa"; | ||
let needle = "aa"; | ||
let expected = vec![0, 1]; | ||
let result = find_all_matches(haystack, needle); | ||
assert_eq!(result, expected); | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod diff_in_vec_len; | ||
pub mod find_all_matches; |
This file contains 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 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 |
---|---|---|
|
@@ -691,4 +691,29 @@ mod tests { | |
); | ||
is_injection!("SELECT * FROM users WHERE id IN (-1,571,639)", "-1,571,639"); | ||
} | ||
|
||
#[test] | ||
fn test_partial_match_single_quote() { | ||
let starts_with = r#" | ||
SELECT "foo" WHERE "bar" = '''; sleep 15 ;"' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's add is injection cases |
||
"#; | ||
|
||
// r#"..."# is a raw string literal | ||
not_injection!(starts_with, r#"'; sleep 15 ;""#); | ||
} | ||
|
||
#[test] | ||
fn test_multiple_partial_match_single_quote() { | ||
not_injection!("SELECT '''abc', '''abc' FROM table", "'abc"); | ||
not_injection!("SELECT 'abc''', 'abc''' FROM table", "abc'"); | ||
not_injection!("SELECT '''abc''', '''abc''' FROM table", "'abc'"); | ||
} | ||
|
||
#[test] | ||
fn test_multiple_partial_match_double_quote() { | ||
// r#"..."# is a raw string literal | ||
not_injection!(r#"SELECT """"abc", """"abc"" FROM table"#, r#""abc"#); | ||
not_injection!(r#"SELECT "abc"""", "abc""" FROM table"#, r#"abc""#); | ||
not_injection!(r#"SELECT """"abc""", """"abc"" FROM table"#, r#""abc"#); | ||
} | ||
} |
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.
starts_with?