graphile-sql-expression-validator 2.11.1 → 2.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,190 @@
1
+ /**
2
+ * FieldType and FieldDefault: structured JSONB models for PostgreSQL
3
+ * type declarations and default value expressions.
4
+ *
5
+ * These models provide a safe, validated representation that can be:
6
+ * 1. Validated structurally (JSON shape, identifier patterns, allowlists)
7
+ * 2. Converted to pgsql-parser AST nodes
8
+ * 3. Validated at the AST level (reuses the expression validator for defaults)
9
+ * 4. Deparsed to canonical SQL text
10
+ *
11
+ * The key security insight: the structured model eliminates entire attack
12
+ * categories by construction (no subqueries, no column refs, no stacked
13
+ * statements). What remains is identifier validation + allowlist enforcement.
14
+ */
15
+ import type { SqlExpressionValidatorOptions } from './validator';
16
+ /**
17
+ * Structured representation of a PostgreSQL data type.
18
+ *
19
+ * @example Simple type
20
+ * { name: "text" }
21
+ *
22
+ * @example Type with arguments
23
+ * { name: "geometry", args: ["Point", 4326] }
24
+ * { name: "numeric", args: [10, 2] }
25
+ * { name: "vector", args: [1536] }
26
+ *
27
+ * @example Array type
28
+ * { name: "text", array_dimensions: 1 }
29
+ *
30
+ * @example Schema-qualified type
31
+ * { name: "my_type", schema: "my_schema" }
32
+ *
33
+ * @example Interval with field range
34
+ * { name: "interval", range: ["day", "second"] }
35
+ */
36
+ export interface FieldType {
37
+ /** Type name (required). Must be a valid SQL identifier. */
38
+ name: string;
39
+ /** Schema qualifier (optional). Must be a valid SQL identifier. */
40
+ schema?: string;
41
+ /** Type arguments (optional). Each is a string identifier, number, or boolean. */
42
+ args?: (string | number | boolean)[];
43
+ /** Number of array dimensions (optional). 1 = `text[]`, 2 = `text[][]`. */
44
+ array_dimensions?: number;
45
+ /** Interval field range (optional). 1-2 elements: ["day"] or ["day", "second"]. */
46
+ range?: string[];
47
+ }
48
+ /**
49
+ * Argument to a function in a FieldDefault expression.
50
+ * Can be a literal value or a nested FieldDefault (recursive).
51
+ */
52
+ export type FieldDefaultArg = string | number | boolean | null | FieldDefault;
53
+ /**
54
+ * Structured representation of a PostgreSQL default value expression.
55
+ *
56
+ * @example Literal values
57
+ * { value: false }
58
+ * { value: 0 }
59
+ * { value: "pooled" }
60
+ *
61
+ * @example Cast expression
62
+ * { value: "{}", cast: { name: "jsonb" } }
63
+ * { value: "15 minutes", cast: { name: "interval" } }
64
+ *
65
+ * @example Simple function call
66
+ * { function: "now" }
67
+ * { function: "gen_random_uuid" }
68
+ *
69
+ * @example Schema-qualified function
70
+ * { function: "current_user_id", schema: "jwt_public" }
71
+ *
72
+ * @example Function with arguments (nested)
73
+ * { function: "encode", args: [{ function: "gen_random_bytes", args: [16] }, "hex"] }
74
+ *
75
+ * @example Function with cast
76
+ * { function: "lpad", args: ["", 32, "0"], cast: { name: "bit", args: [32] } }
77
+ */
78
+ export interface FieldDefault {
79
+ /** Literal value (string, number, boolean, null, or array). */
80
+ value?: string | number | boolean | null | unknown[];
81
+ /** Function name. Must be a valid SQL identifier. */
82
+ function?: string;
83
+ /** Schema qualifier for function (optional). */
84
+ schema?: string;
85
+ /** Function arguments (optional, recursive). */
86
+ args?: FieldDefaultArg[];
87
+ /** Output type cast (optional). Reuses FieldType shape. */
88
+ cast?: FieldType;
89
+ }
90
+ export interface FieldTypeValidationOptions {
91
+ /** Allowed schemas for schema-qualified types. */
92
+ allowedTypeSchemas?: string[];
93
+ /** Additional forbidden type names beyond the built-in set. */
94
+ additionalForbiddenTypes?: string[];
95
+ }
96
+ export interface FieldDefaultValidationOptions extends SqlExpressionValidatorOptions {
97
+ /** Allowed schemas for schema-qualified types in casts. */
98
+ allowedTypeSchemas?: string[];
99
+ /** Additional forbidden type names beyond the built-in set. */
100
+ additionalForbiddenTypes?: string[];
101
+ /** Maximum nesting depth for recursive args. Defaults to 4. */
102
+ maxNestingDepth?: number;
103
+ /**
104
+ * Map of schema → allowed functions for schema-qualified calls.
105
+ * Functions in this map do NOT need to also appear in allowedFunctions.
106
+ */
107
+ allowedSchemaFunctions?: Record<string, string[]>;
108
+ }
109
+ export interface FieldTypeValidationResult {
110
+ valid: boolean;
111
+ /** Canonical SQL text (e.g., "geometry(Point,4326)", "text[]") */
112
+ canonicalSql?: string;
113
+ /** The TypeName AST node */
114
+ ast?: Record<string, unknown>;
115
+ error?: string;
116
+ }
117
+ export interface FieldDefaultValidationResult {
118
+ valid: boolean;
119
+ /** Canonical SQL text (e.g., "now()", "'{}'::jsonb") */
120
+ canonicalSql?: string;
121
+ /** The expression AST node */
122
+ ast?: Record<string, unknown>;
123
+ error?: string;
124
+ }
125
+ /**
126
+ * PostgreSQL system catalog type casts that are forbidden.
127
+ * These OID-alias types can be used to probe the system catalog.
128
+ * Shared with the expression validator (same list).
129
+ */
130
+ export declare const FORBIDDEN_TYPES: Set<string>;
131
+ /**
132
+ * Convert a FieldType to a pgsql-parser TypeName AST node.
133
+ *
134
+ * The TypeName node format:
135
+ * {
136
+ * names: [{ String: { sval: "schema" } }, { String: { sval: "name" } }],
137
+ * typmods: [<arg AST nodes>],
138
+ * arrayBounds: [{ Integer: { ival: -1 } }, ...],
139
+ * typemod: -1
140
+ * }
141
+ */
142
+ export declare function fieldTypeToAst(ft: FieldType): Record<string, unknown>;
143
+ /**
144
+ * Convert a FieldType to its canonical SQL text representation.
145
+ *
146
+ * Uses the deparser for the base type, then appends array brackets.
147
+ * Handles interval range fields manually since the deparser needs
148
+ * special handling for INTERVAL ... DAY TO SECOND syntax.
149
+ */
150
+ export declare function fieldTypeToSql(ft: FieldType): string;
151
+ /**
152
+ * Convert a FieldDefault to a pgsql-parser expression AST node.
153
+ *
154
+ * Handles:
155
+ * - Literal values → A_Const
156
+ * - Function calls → FuncCall (with recursive args)
157
+ * - Type casts → TypeCast wrapping the inner expression
158
+ * - Combinations (function + cast, value + cast)
159
+ */
160
+ export declare function fieldDefaultToAst(fd: FieldDefault, depth?: number): Record<string, unknown>;
161
+ /**
162
+ * Convert a FieldDefault to its canonical SQL text representation.
163
+ */
164
+ export declare function fieldDefaultToSql(fd: FieldDefault): string;
165
+ /**
166
+ * Validate a FieldType object structurally.
167
+ *
168
+ * Checks:
169
+ * - Object shape (no unknown keys, required keys present)
170
+ * - `name` is a valid SQL identifier
171
+ * - `name` is not a forbidden type (regclass, regtype, etc.)
172
+ * - `schema` (if present) is a valid SQL identifier and on the allowlist
173
+ * - `args` elements are literals (string, number, boolean)
174
+ * - `array_dimensions` is a positive integer
175
+ * - `range` elements are valid interval field qualifiers
176
+ *
177
+ * Then converts to SQL text for canonical output.
178
+ */
179
+ export declare function validateFieldType(ft: unknown, options?: FieldTypeValidationOptions): FieldTypeValidationResult;
180
+ /**
181
+ * Validate a FieldDefault object.
182
+ *
183
+ * Structural validation + AST conversion + expression validation.
184
+ *
185
+ * 1. Validates the JSON structure (shape, types, required fields)
186
+ * 2. Converts to pgsql-parser expression AST
187
+ * 3. Validates the AST through the expression validator (function/schema allowlists)
188
+ * 4. Deparses to canonical SQL text
189
+ */
190
+ export declare function validateFieldDefault(fd: unknown, options?: FieldDefaultValidationOptions): FieldDefaultValidationResult;