Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

fix(pg-meta): tighten pg-format keyword() against control-phrase injection (#46076)

Charis64143a5d901d
diff --git a/packages/pg-meta/src/pg-format/index.ts b/packages/pg-meta/src/pg-format/index.ts
index 75e3cd48ca3..314d548606a 100644
--- a/packages/pg-meta/src/pg-format/index.ts
+++ b/packages/pg-meta/src/pg-format/index.ts
@@ -212,18 +212,28 @@ export function literal(value?: unknown): SafeSqlFragment {
return quoted as SafeSqlFragment
}
+// Multi-word SQL keyword phrases callers are allowed to pass to `keyword()`.
+// Compared case-insensitively. Any phrase not listed here must be expressed as
+// separate single-word `keyword()` calls composed via `safeSql`, so that
+// arbitrary control phrases (e.g. "DROP TABLE") can't slip through.
+const ALLOWED_MULTI_WORD_KEYWORDS = new Set(['INSTEAD OF', 'BY DEFAULT'])
+
/**
- * Marks SQL keywords (e.g., 'BEFORE', 'instead of') as safe for interpolation.
- * Only letters, numbers, underscores, and spaces are allowed, to prevent
- * injection.
+ * Marks SQL keywords (e.g., 'BEFORE', 'INSTEAD OF') as safe for interpolation.
+ * Accepts either a single word matching [A-Za-z][A-Za-z0-9_]*, or a
+ * multi-word phrase from `ALLOWED_MULTI_WORD_KEYWORDS`. Any other input —
+ * including arbitrary space-separated tokens like "DROP TABLE" — is rejected.
*/
export function keyword(value: string): SafeSqlFragment {
- if (!/^[A-Za-z][A-Za-z0-9_ ]*$/.test(value)) {
- throw new Error(
- `Not a valid keyword: "${value}". Only letters, numbers, underscores, and spaces are permitted.`
- )
+ if (/^[A-Za-z][A-Za-z0-9_]*$/.test(value)) {
+ return value as SafeSqlFragment
+ }
+ if (ALLOWED_MULTI_WORD_KEYWORDS.has(value.toUpperCase())) {
+ return value as SafeSqlFragment
}
- return value as SafeSqlFragment
+ throw new Error(
+ `Not a valid keyword: "${value}". Must be a single word matching [A-Za-z][A-Za-z0-9_]*, or one of: ${[...ALLOWED_MULTI_WORD_KEYWORDS].join(', ')}.`
+ )
}
type Stringifyable =
diff --git a/packages/pg-meta/test/pg-format.test.ts b/packages/pg-meta/test/pg-format.test.ts
index 48d5e9d0e8c..3e0d2d8884e 100644
--- a/packages/pg-meta/test/pg-format.test.ts
+++ b/packages/pg-meta/test/pg-format.test.ts
@@ -147,9 +147,14 @@ describe('pg-format', () => {
expect(keyword('ROW')).toBe('ROW')
})
- test('accepts lowercase and mixed case', () => {
+ test('accepts allow-listed multi-word keywords', () => {
+ expect(keyword('INSTEAD OF')).toBe('INSTEAD OF')
+ expect(keyword('BY DEFAULT')).toBe('BY DEFAULT')
+ })
+
+ test('multi-word allow-list is case-insensitive', () => {
expect(keyword('instead of')).toBe('instead of')
- expect(keyword('Each Row')).toBe('Each Row')
+ expect(keyword('By Default')).toBe('By Default')
})
test('accepts words with underscores and digits', () => {
@@ -180,6 +185,12 @@ describe('pg-format', () => {
test('rejects strings with parentheses', () => {
expect(() => keyword('fn()')).toThrow('Not a valid keyword')
})
+
+ test('rejects arbitrary multi-word phrases not on the allow-list', () => {
+ expect(() => keyword('DROP TABLE')).toThrow('Not a valid keyword')
+ expect(() => keyword('DELETE FROM users')).toThrow('Not a valid keyword')
+ expect(() => keyword('Each Row')).toThrow('Not a valid keyword')
+ })
})
describe('safeSql', () => {