-
Notifications
You must be signed in to change notification settings - Fork 23
HYPERFLEET-1259 - fix: SQL injection protection for order queries #244
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
Open
ma-hill
wants to merge
1
commit into
openshift-hyperfleet:main
Choose a base branch
from
ma-hill:HYPERFLEET-1259
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.
+515
−38
Open
Changes from all commits
Commits
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 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 |
|---|---|---|
|
|
@@ -834,3 +834,177 @@ func TestConditionStatusValidation(t *testing.T) { | |
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestArgsToOrder(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| errorContains string | ||
| input []string | ||
| expected []string | ||
| expectError bool | ||
| }{ | ||
| { | ||
| name: "single field with asc", | ||
| input: []string{"name asc"}, | ||
| expected: []string{"name asc"}, | ||
| }, | ||
| { | ||
| name: "single field with desc", | ||
| input: []string{"created_time desc"}, | ||
| expected: []string{"created_time desc"}, | ||
| }, | ||
| { | ||
| name: "single field without direction defaults to asc", | ||
| input: []string{"created_time"}, | ||
| expected: []string{"created_time asc"}, | ||
| }, | ||
| { | ||
| name: "multiple fields", | ||
| input: []string{"name asc", "created_time desc"}, | ||
| expected: []string{"name asc", "created_time desc"}, | ||
| }, | ||
| { | ||
| name: "field with extra spaces", | ||
| input: []string{" name asc "}, | ||
| expected: []string{"name asc"}, | ||
| }, | ||
| { | ||
| name: "field with tabs and spaces", | ||
| input: []string{"name \t desc"}, | ||
| expected: []string{"name desc"}, | ||
| }, | ||
| { | ||
| name: "all allowed fields", | ||
| input: []string{"id", "name", "created_time", "updated_time", "kind"}, | ||
| expected: []string{"id asc", "name asc", "created_time asc", "updated_time asc", "kind asc"}, | ||
| }, | ||
| { | ||
| name: "invalid direction", | ||
| input: []string{"name ascending"}, | ||
| expectError: true, | ||
| errorContains: "invalid order format", | ||
| }, | ||
| { | ||
| name: "SQL injection attempt - semicolon", | ||
| input: []string{"name; DROP TABLE resources"}, | ||
| expectError: true, | ||
| errorContains: "invalid order format", | ||
| }, | ||
| { | ||
| name: "SQL injection attempt - comment", | ||
| input: []string{"name-- asc"}, | ||
| expectError: true, | ||
| errorContains: "invalid order format", | ||
| }, | ||
| { | ||
| name: "uppercase field name", | ||
| input: []string{"NAME asc"}, | ||
| expectError: true, | ||
| errorContains: "invalid order format", | ||
| }, | ||
| { | ||
| name: "uppercase direction", | ||
| input: []string{"name ASC"}, | ||
| expectError: true, | ||
| errorContains: "invalid order format", | ||
| }, | ||
| { | ||
| name: "empty string in array is skipped", | ||
| input: []string{""}, | ||
| expected: nil, | ||
| }, | ||
| { | ||
| name: "empty string in array with field at end", | ||
| input: []string{"", "", "", "", "", "kind asc", "href desc"}, | ||
| expected: []string{"kind asc", "href desc"}, | ||
| }, | ||
| { | ||
| name: "whitespace only string is skipped", | ||
| input: []string{" "}, | ||
| expected: nil, | ||
| }, | ||
| { | ||
| name: "mixed valid and empty strings and tabs", | ||
| input: []string{"name asc", "", "created_time desc", " ", "\t"}, | ||
| expected: []string{"name asc", "created_time desc"}, | ||
| }, | ||
| { | ||
| name: "mixed valid and invalid field", | ||
| input: []string{"created_time desc", "name", "wrong_field"}, | ||
| expectError: true, | ||
| errorContains: "not allowed for ordering", | ||
| }, | ||
| { | ||
| name: "field not in whitelist", | ||
| input: []string{"custom_field asc"}, | ||
| expectError: true, | ||
| errorContains: "not allowed for ordering", | ||
| }, | ||
| { | ||
| name: "deleted_time field", | ||
| input: []string{"deleted_time desc"}, | ||
| expected: []string{"deleted_time desc"}, | ||
| }, | ||
| { | ||
| name: "generation field", | ||
| input: []string{"generation asc"}, | ||
| expected: []string{"generation asc"}, | ||
| }, | ||
| { | ||
| name: "too many parts", | ||
| input: []string{"name asc extra"}, | ||
| expectError: true, | ||
| errorContains: "invalid order format", | ||
| }, | ||
| { | ||
| name: "empty array", | ||
| input: []string{}, | ||
| expected: nil, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| RegisterTestingT(t) | ||
|
|
||
| result, err := ArgsToOrder(tt.input) | ||
|
|
||
| if tt.expectError { | ||
| Expect(err).ToNot(BeNil(), "expected error but got nil") | ||
| if tt.errorContains != "" { | ||
| Expect(err.Reason).To(ContainSubstring(tt.errorContains)) | ||
| } | ||
| } else { | ||
| Expect(err).To(BeNil(), "unexpected error: %v", err) | ||
| Expect(result).To(Equal(tt.expected)) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestArgsToOrder_SecurityValidation(t *testing.T) { | ||
| RegisterTestingT(t) | ||
|
|
||
| // SQL injection attempts that should all fail | ||
| injectionAttempts := []struct { | ||
| name string | ||
| input string | ||
| }{ | ||
| {"union injection", "name UNION SELECT password FROM users"}, | ||
| {"comment injection", "name--"}, | ||
| {"semicolon terminator", "name; DROP TABLE resources;"}, | ||
| {"quote escape", "name' OR '1'='1"}, | ||
| {"parentheses", "name) OR (1=1"}, | ||
| {"wildcard", "name*"}, | ||
| {"backtick", "name`"}, | ||
| } | ||
|
Comment on lines
+989
to
+1000
Contributor
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. Consider adding a test case for uppercase rejection to verify the lowercase-only enforcement. |
||
|
|
||
| for _, tt := range injectionAttempts { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| RegisterTestingT(t) | ||
|
|
||
| _, err := ArgsToOrder([]string{tt.input}) | ||
| Expect(err).ToNot(BeNil(), "injection attempt '%s' should be rejected", tt.input) | ||
| }) | ||
| } | ||
| } | ||
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
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 list is missing deleted_time and deleted_by from the orderByAllowedFields map, so 2 of 11 allowed fields don't have integration coverage. Consider adding them so the test lives up to its "all whitelisted fields" intent.
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.
deleted_time ( line 563 ) and deleted-by ( line 567 )
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.
The allowlist in sql_helpers.go is correct. My comment was about the integration test TestOrderByAllowedFields in order_field_mapping_test.go, which only tests 9 of 11 fields. deleted_time and deleted_by aren't in the test's allowedFields slice, so they don't have integration coverage.