-
Notifications
You must be signed in to change notification settings - Fork 3
feat(storage): implement backend sorting with sql injection protection #145
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
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
0b48b63
feat: add sorting to sql adapter
Lutherwaves bdf9c0a
refactor: add SortingDirection type and SortDirectionKey constant
Lutherwaves c38344b
refactor: use maps.Copy and flatParams in sql extractParams
Lutherwaves c86c808
refactor: use SortingDirection type in sql adapter
Lutherwaves edb279e
refactor: use switch and maps.Equal in sql sorting helpers
Lutherwaves 8f1e293
refactor: use SortingDirection type in cosmosdb adapter
Lutherwaves 1a05bc9
refactor: deduplicate extractParams and extractSortDirection as packa…
Lutherwaves 5d6c85e
feat: add validateSortKey to guard against ORDER BY injection
Lutherwaves 3a314bd
fix: allow underscore-prefixed sort keys, add edge case tests
Lutherwaves 2bc9da7
fix: validate sortKey in CosmosDB executePaginatedQuery to prevent in…
Lutherwaves 7b508f6
fix: validate sortKey in SQL executePaginatedQuery to prevent injection
Lutherwaves 5d5f92c
chore(test): restore sqlAdapterInstance singleton after TestListRejec…
Lutherwaves 83e52f2
fix: warn on cursor field extraction failure, remove redundant else b…
Lutherwaves b72b96e
refactor(style): use snake_case log keys in cursor extraction warnings
Lutherwaves 0cec523
fix: validate sortKey in DynamoDB List and Search to prevent PartiQL …
Lutherwaves b25e988
chore(docs): add godoc to executePaginatedQuery in SQL and CosmosDB a…
Lutherwaves 4b4931c
chore(docs): document sortKey and SortDirectionKey on StorageAdapter …
Lutherwaves d31ee23
refactor: more explicit error msgs on failure to list/search
Lutherwaves 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
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,106 @@ | ||
| package storage | ||
|
|
||
| import ( | ||
| "maps" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestListRejectsMaliciousSortKey(t *testing.T) { | ||
| // Reset singleton so we get a fresh SQLite adapter | ||
| prev := sqlAdapterInstance | ||
| sqlAdapterInstance = nil | ||
| t.Cleanup(func() { sqlAdapterInstance = prev }) | ||
| adapter := GetSQLAdapterInstance(map[string]string{ | ||
| "provider": "sqlite", | ||
| }) | ||
| type Row struct { | ||
| ID string `gorm:"primaryKey"` | ||
| } | ||
| _ = adapter.DB.AutoMigrate(&Row{}) | ||
|
|
||
| var rows []Row | ||
| _, err := adapter.List(&rows, "id; DROP TABLE rows", map[string]any{}, 10, "") | ||
| if err == nil { | ||
| t.Error("expected error for malicious sortKey, got nil") | ||
| } | ||
| } | ||
|
|
||
| func TestValidateSortKey(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| input string | ||
| wantErr bool | ||
| }{ | ||
| {"simple column", "id", false}, | ||
| {"snake_case column", "created_at", false}, | ||
| {"mixed case", "createdAt", false}, | ||
| {"with numbers", "field1", false}, | ||
| {"empty string", "", true}, | ||
| {"leading digit", "1field", true}, | ||
| {"dot notation injection", "id; DROP TABLE users", true}, | ||
| {"semicolon", "id;DROP", true}, | ||
| {"space", "col name", true}, | ||
| {"table.column dot", "t.col", true}, | ||
| {"SQL comment", "id--", true}, | ||
| {"single quote", "id'", true}, | ||
| {"underscore prefix", "_ts", false}, // CosmosDB system fields like _ts are valid | ||
| {"null byte", "id\x00DROP", true}, // null byte injection rejected | ||
| {"unicode lookalike", "iа", true}, // Cyrillic а (U+0430) rejected, not ASCII | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| err := validateSortKey(tt.input) | ||
| if (err != nil) != tt.wantErr { | ||
| t.Errorf("validateSortKey(%q) error = %v, wantErr %v", tt.input, err, tt.wantErr) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestExtractSortDirection(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| input map[string]any | ||
| expected SortingDirection | ||
| wantErr bool | ||
| }{ | ||
| {"default when missing", map[string]any{}, Ascending, false}, | ||
| {"ASC explicit", map[string]any{SortDirectionKey: "ASC"}, Ascending, false}, | ||
| {"DESC", map[string]any{SortDirectionKey: "DESC"}, Descending, false}, | ||
| {"lowercase desc", map[string]any{SortDirectionKey: "desc"}, Descending, false}, | ||
| {"invalid returns error", map[string]any{SortDirectionKey: "SIDEWAYS"}, "", true}, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| got, err := extractSortDirection(tt.input) | ||
| if (err != nil) != tt.wantErr { | ||
| t.Errorf("extractSortDirection(%v) error = %v, wantErr %v", tt.input, err, tt.wantErr) | ||
| return | ||
| } | ||
| if !tt.wantErr && got != tt.expected { | ||
| t.Errorf("extractSortDirection(%v) = %q; want %q", tt.input, got, tt.expected) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestExtractParams(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| input []map[string]any | ||
| expected map[string]any | ||
| }{ | ||
| {"empty input", []map[string]any{}, map[string]any{}}, | ||
| {"single map", []map[string]any{{"a": 1}}, map[string]any{"a": 1}}, | ||
| {"two maps merged", []map[string]any{{"a": 1}, {"b": 2}}, map[string]any{"a": 1, "b": 2}}, | ||
| {"later map wins on collision", []map[string]any{{"a": 1}, {"a": 2}}, map[string]any{"a": 2}}, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| got := extractParams(tt.input...) | ||
| if !maps.Equal(got, tt.expected) { | ||
| t.Errorf("extractParams(%v) = %v; want %v", tt.input, got, tt.expected) | ||
| } | ||
| }) | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.