|
| 1 | +package database |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/url" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +// These are regression tests for SQL-injection sinks where user-controlled |
| 9 | +// tokens were interpolated into SQL text with bare quotes instead of being |
| 10 | +// escaped/parameterized. The request parser lets a client smuggle a literal |
| 11 | +// ' or " (and even separators) into a token via a backslash escape, so every |
| 12 | +// interpolation site must quote/escape its input. |
| 13 | +// |
| 14 | +// Each case asserts the SAFE (properly escaped) SQL: the doubled quote is the |
| 15 | +// evidence that the breakout attempt was neutralized. |
| 16 | + |
| 17 | +func buildFromQuery(t *testing.T, query string) (string, []any) { |
| 18 | + t.Helper() |
| 19 | + u, err := url.Parse(query) |
| 20 | + if err != nil { |
| 21 | + t.Fatal(err) |
| 22 | + } |
| 23 | + parts, err := PostgRestParser{}.parse("table", u.Query()) |
| 24 | + if err != nil { |
| 25 | + t.Fatalf("parse error for %q: %v", query, err) |
| 26 | + } |
| 27 | + sql, values, err := DirectQueryBuilder{}.BuildSelect("table", parts, &QueryOptions{}, nil) |
| 28 | + if err != nil { |
| 29 | + t.Fatalf("build error for %q: %v", query, err) |
| 30 | + } |
| 31 | + return sql, values |
| 32 | +} |
| 33 | + |
| 34 | +// Finding 4: the select alias (AS "...") was built with raw quotes, so a " |
| 35 | +// smuggled into the label broke out of the identifier into the SELECT list. |
| 36 | +func TestInjectionSelectAliasQuoting(t *testing.T) { |
| 37 | + // label token becomes: x" (the \" is an escaped literal double-quote) |
| 38 | + sql, _ := buildFromQuery(t, `?select=x\":a`) |
| 39 | + want := `SELECT "table"."a" AS "x""" FROM "table"` |
| 40 | + if sql != want { |
| 41 | + t.Errorf("alias not escaped\n want: %s\n got: %s", want, sql) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +// Finding 3: JSON-path members were wrapped in bare single quotes, so a ' |
| 46 | +// smuggled into a path member broke out of the string literal. |
| 47 | +func TestInjectionJSONPathQuoting(t *testing.T) { |
| 48 | + // path member token becomes: x'y |
| 49 | + sql, _ := buildFromQuery(t, `?select=data->>x\'y`) |
| 50 | + want := `SELECT ("table"."data"->>'x''y') AS "x'y" FROM "table"` |
| 51 | + if sql != want { |
| 52 | + t.Errorf("json path member not escaped\n want: %s\n got: %s", want, sql) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +// Finding 2: full-text-search config arguments were interpolated with bare |
| 57 | +// single quotes, so a ' in the fts(config) argument broke out of the literal. |
| 58 | +func TestInjectionFTSConfigQuoting(t *testing.T) { |
| 59 | + // fts config token becomes: en'x |
| 60 | + sql, values := buildFromQuery(t, `?body=fts(en\'x).cat`) |
| 61 | + want := `SELECT * FROM "table" WHERE "table"."body" @@ to_tsquery('en''x', $1)` |
| 62 | + if sql != want { |
| 63 | + t.Errorf("fts config not escaped\n want: %s\n got: %s", want, sql) |
| 64 | + } |
| 65 | + if len(values) != 1 || values[0] != "cat" { |
| 66 | + t.Errorf("fts value should be parameterized, got %v", values) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +// SET ROLE takes the role verbatim from the JWT claim. It is built by |
| 71 | +// concatenation (PrepareConnection needs a live connection, so this locks in the |
| 72 | +// quoting contract that line relies on): a malicious role must stay a single |
| 73 | +// quoted identifier and cannot break out into additional statements. |
| 74 | +func TestInjectionSetRoleQuoting(t *testing.T) { |
| 75 | + cases := map[string]string{ |
| 76 | + `postgres; DROP TABLE users; --`: `SET ROLE "postgres; DROP TABLE users; --"`, |
| 77 | + `admin" ; DROP`: `SET ROLE "admin"" ; DROP"`, |
| 78 | + } |
| 79 | + for role, want := range cases { |
| 80 | + got := "SET ROLE " + quote(role) |
| 81 | + if got != want { |
| 82 | + t.Errorf("role %q not neutralized\n want: %s\n got: %s", role, want, got) |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +// Finding 1: embedded-resource filter values take the nmarker == -1 branch of |
| 88 | +// appendValue, which interpolated the value with bare single quotes instead of |
| 89 | +// using a $N placeholder. This exercises that branch directly. |
| 90 | +func TestInjectionEmbeddedValueEscaping(t *testing.T) { |
| 91 | + where, _, _ := appendValue("", `x' OR '1'='1`, nil, -1, false) |
| 92 | + want := `'x'' OR ''1''=''1'` |
| 93 | + if where != want { |
| 94 | + t.Errorf("interpolated value not escaped\n want: %s\n got: %s", want, where) |
| 95 | + } |
| 96 | +} |
0 commit comments