Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

fix(jit): normalize whitespace symmetrically in access-grant query match (#20139)

Vincent Huang2371f5929315
diff --git a/backend/store/access_grant.go b/backend/store/access_grant.go
index 4d864eba73..dc3c3ceda0 100644
--- a/backend/store/access_grant.go
+++ b/backend/store/access_grant.go
@@ -366,7 +366,26 @@ func GetListAccessGrantFilter(filter string) (*qb.Query, error) {
if !ok {
return nil, errors.Errorf("query value must be a string")
}
- return qb.Q().Space("btrim(access_grant.payload->>'query') = ?", strings.TrimSpace(queryStr)), nil
+ // Trim the same whitespace set on both sides (boundary
+ // only) so the run-time JIT match in preCheckAccess
+ // survives invisible boundary differences — most
+ // commonly a trailing \n that Monaco's getValue() emits
+ // in the request drawer but that the editor's
+ // getActiveStatement() doesn't.
+ //
+ // We deliberately do NOT collapse internal whitespace.
+ // Doing so would let "SELECT * FROM t --\nWHERE x=1"
+ // compare equal to "SELECT * FROM t -- WHERE x=1",
+ // silently authorizing a query with the WHERE clause
+ // commented out — a privilege escalation, not a
+ // usability nit. Internal whitespace differences
+ // (reformatting, CRLF↔LF in the body) intentionally
+ // fall through to the IAM check; users can re-request.
+ //
+ // The contains branch below collapses internal
+ // whitespace; that's safe there because it's used for
+ // search/listing, not authorization.
+ return qb.Q().Space("btrim(access_grant.payload->>'query', E' \\t\\n\\r\\v\\f') = ?", strings.TrimSpace(queryStr)), nil
case "issue":
issueStr, ok := value.(string)
if !ok {
diff --git a/backend/store/access_grant_filter_test.go b/backend/store/access_grant_filter_test.go
index 0a29634b56..95c9efb0d6 100644
--- a/backend/store/access_grant_filter_test.go
+++ b/backend/store/access_grant_filter_test.go
@@ -42,6 +42,70 @@ func TestGetListAccessGrantFilter(t *testing.T) {
wantErr: true,
errContains: "unsupported function matches",
},
+ // query equality must trim the *same* set of characters on both
+ // sides so an "approved" JIT grant whose stored SQL differs from
+ // the submitted SQL only by boundary whitespace (most commonly a
+ // trailing \n that Monaco's getValue() emits in the request drawer
+ // but that the editor's getActiveStatement() doesn't) still
+ // matches at run time. Critically, *internal* whitespace must be
+ // preserved byte-for-byte: collapsing internal whitespace would
+ // let "SELECT * FROM t --\nWHERE tenant=1" compare equal to
+ // "SELECT * FROM t -- WHERE tenant=1", which means an approved
+ // query gets stripped of its WHERE clause at run time. That's a
+ // privilege escalation, not a usability nit. Boundary-only trim
+ // fixes the reported customer bug without enabling that class.
+ {
+ name: "query equality strips trailing newline",
+ filter: `query == "SELECT 1\n"`,
+ wantSQL: "(btrim(access_grant.payload->>'query', E' \\t\\n\\r\\v\\f') = $1)",
+ wantArgs: []any{"SELECT 1"},
+ wantErr: false,
+ },
+ {
+ name: "query equality strips leading whitespace",
+ filter: `query == "\n\tSELECT 1"`,
+ wantSQL: "(btrim(access_grant.payload->>'query', E' \\t\\n\\r\\v\\f') = $1)",
+ wantArgs: []any{"SELECT 1"},
+ wantErr: false,
+ },
+ {
+ name: "query equality strips trailing CRLF",
+ filter: `query == "SELECT 1\r\n"`,
+ wantSQL: "(btrim(access_grant.payload->>'query', E' \\t\\n\\r\\v\\f') = $1)",
+ wantArgs: []any{"SELECT 1"},
+ wantErr: false,
+ },
+ {
+ name: "query equality preserves internal whitespace byte-for-byte",
+ filter: `query == "SELECT *\n FROM t"`,
+ wantSQL: "(btrim(access_grant.payload->>'query', E' \\t\\n\\r\\v\\f') = $1)",
+ wantArgs: []any{"SELECT *\n FROM t"},
+ wantErr: false,
+ },
+ // Anti-collision regression for the privilege-escalation class
+ // described above. The internal \n inside the SQL must survive
+ // the normalization so that comment termination is preserved.
+ {
+ name: "query equality preserves -- comment newline (no privilege escalation)",
+ filter: `query == "SELECT * FROM t --\nWHERE tenant=1"`,
+ wantSQL: "(btrim(access_grant.payload->>'query', E' \\t\\n\\r\\v\\f') = $1)",
+ wantArgs: []any{"SELECT * FROM t --\nWHERE tenant=1"},
+ wantErr: false,
+ },
+ {
+ name: "query equality preserves whitespace inside string literals",
+ filter: `query == "SELECT 'a b'"`,
+ wantSQL: "(btrim(access_grant.payload->>'query', E' \\t\\n\\r\\v\\f') = $1)",
+ wantArgs: []any{"SELECT 'a b'"},
+ wantErr: false,
+ },
+ {
+ name: "query equality preserves single spaces between tokens",
+ filter: `query == "SELECT 1 FROM t"`,
+ wantSQL: "(btrim(access_grant.payload->>'query', E' \\t\\n\\r\\v\\f') = $1)",
+ wantArgs: []any{"SELECT 1 FROM t"},
+ wantErr: false,
+ },
}
for _, tt := range tests {