-
Notifications
You must be signed in to change notification settings - Fork 1
fix: Escape single quotes in JSON field names to prevent SQL injection #58
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,147 @@ | ||||||
| package cel2sql_test | ||||||
|
|
||||||
| import ( | ||||||
| "testing" | ||||||
|
|
||||||
| "github.com/google/cel-go/cel" | ||||||
| "github.com/stretchr/testify/require" | ||||||
|
|
||||||
| "github.com/spandigital/cel2sql/v2" | ||||||
| "github.com/spandigital/cel2sql/v2/pg" | ||||||
| ) | ||||||
|
|
||||||
| // TestJSONFieldNameEscaping_SingleQuote tests that single quotes in JSON field names are properly escaped | ||||||
| func TestJSONFieldNameEscaping_SingleQuote(t *testing.T) { | ||||||
| // Create a schema with a JSON field that might have field names with single quotes | ||||||
| testSchema := pg.Schema{ | ||||||
| {Name: "id", Type: "integer"}, | ||||||
| {Name: "metadata", Type: "jsonb"}, | ||||||
| } | ||||||
|
|
||||||
| provider := pg.NewTypeProvider(map[string]pg.Schema{ | ||||||
| "TestTable": testSchema, | ||||||
| }) | ||||||
|
|
||||||
| tests := []struct { | ||||||
| name string | ||||||
| celExpr string | ||||||
| expectedSQL string | ||||||
| description string | ||||||
| }{ | ||||||
| { | ||||||
| name: "JSON field with single quote in name", | ||||||
| celExpr: `obj.metadata.user_name == "test"`, | ||||||
| expectedSQL: `obj->>'metadata'->>'user_name' = 'test'`, | ||||||
| description: "Normal field name without quotes", | ||||||
| }, | ||||||
| { | ||||||
| name: "Nested JSON access", | ||||||
| celExpr: `obj.metadata.settings.theme == "dark"`, | ||||||
| expectedSQL: `obj->>'metadata'->'settings'->>'theme' = 'dark'`, | ||||||
|
||||||
| expectedSQL: `obj->>'metadata'->'settings'->>'theme' = 'dark'`, | |
| expectedSQL: `obj->'metadata'->'settings'->>'theme' = 'dark'`, |
Copilot
AI
Oct 22, 2025
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.
Corrected example text formatting for clarity. The backslashes are unnecessary escape sequences in the raw string and make the example harder to read.
| t.Log("Example: \"user's name\" -> \"user''s name\"") | |
| t.Log(`Example: "user's name" -> "user''s name"`) |
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 expected SQL is incorrect. Based on the JSON path generation rules in the coding guidelines, intermediate navigation should use
->(returns JSON) and only the final extraction should use->>(returns text). The correct expected SQL should beobj.metadata->>'user_name' = 'test'sincemetadatais an intermediate field anduser_nameis the final field being extracted.