@nest-boot/row-level-security 7.2.4 → 7.2.5
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.
- package/dist/decorators/policy.decorator.js +2 -2
- package/dist/decorators/policy.decorator.js.map +1 -1
- package/dist/row-level-security-migration-generator.js +204 -271
- package/dist/row-level-security-migration-generator.js.map +1 -1
- package/dist/row-level-security-migration-generator.spec.js +65 -0
- package/dist/row-level-security-migration-generator.spec.js.map +1 -1
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/dist/utils/normalize-postgres-type-alias.spec.d.ts +1 -0
- package/dist/utils/normalize-postgres-type-alias.spec.js +28 -0
- package/dist/utils/normalize-postgres-type-alias.spec.js.map +1 -0
- package/dist/utils/{normalize-postgres-type-alias.js → normalize-postgres-type-alias.util.js} +1 -1
- package/dist/utils/normalize-postgres-type-alias.util.js.map +1 -0
- package/package.json +7 -4
- package/dist/utils/normalize-postgres-type-alias.js.map +0 -1
- /package/dist/utils/{normalize-postgres-type-alias.d.ts → normalize-postgres-type-alias.util.d.ts} +0 -0
|
@@ -9,7 +9,7 @@ const policy_mode_enum_1 = require("../enums/policy-mode.enum");
|
|
|
9
9
|
const assert_identifier_1 = require("../utils/assert-identifier");
|
|
10
10
|
const escape_sql_literal_1 = require("../utils/escape-sql-literal");
|
|
11
11
|
const is_postgres_keyword_requiring_quote_1 = require("../utils/is-postgres-keyword-requiring-quote");
|
|
12
|
-
const
|
|
12
|
+
const normalize_postgres_type_alias_util_1 = require("../utils/normalize-postgres-type-alias.util");
|
|
13
13
|
const quote_identifier_1 = require("../utils/quote-identifier");
|
|
14
14
|
const policyMetadata = new WeakMap();
|
|
15
15
|
/**
|
|
@@ -227,7 +227,7 @@ function normalizePostgresType(entityMetadata, propertyName, postgresType) {
|
|
|
227
227
|
if (!/^[A-Za-z0-9_."()[\],\s[\]]+$/.test(normalized)) {
|
|
228
228
|
throw new Error(`Policy property "${propertyName}" on entity ${entityMetadata.entityName} has an unsupported database column type: ${postgresType}`);
|
|
229
229
|
}
|
|
230
|
-
return (0,
|
|
230
|
+
return (0, normalize_postgres_type_alias_util_1.normalizePostgresTypeAlias)(normalized);
|
|
231
231
|
}
|
|
232
232
|
function mapPropertyTypeToPostgresType(propertyType) {
|
|
233
233
|
const normalized = propertyType?.trim().toLowerCase();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"policy.decorator.js","sourceRoot":"","sources":["../../src/decorators/policy.decorator.ts"],"names":[],"mappings":";;AAiCA,wBA0DC;AAGD,8CAEC;AAGD,oDAOC;AAGD,8CAKC;AAlHD,sEAA6D;AAC7D,gEAAuD;AAOvD,kEAA8D;AAC9D,oEAA+D;AAC/D,sGAA+F;AAC/F,
|
|
1
|
+
{"version":3,"file":"policy.decorator.js","sourceRoot":"","sources":["../../src/decorators/policy.decorator.ts"],"names":[],"mappings":";;AAiCA,wBA0DC;AAGD,8CAEC;AAGD,oDAOC;AAGD,8CAKC;AAlHD,sEAA6D;AAC7D,gEAAuD;AAOvD,kEAA8D;AAC9D,oEAA+D;AAC/D,sGAA+F;AAC/F,oGAAyF;AACzF,gEAA4D;AAY5D,MAAM,cAAc,GAAG,IAAI,OAAO,EAAiC,CAAC;AAEpE;;;;;;GAMG;AACH,SAAgB,MAAM,CAAC,OAAsB;IAC3C,MAAM,IAAI,GAAG,eAAe,CAAC,OAAO,CAAC,IAAI,EAAE,yBAAyB,CAAC,CAAC;IACtE,MAAM,OAAO,GAAG,OAAO,CAAC,OAAO,IAAI,mCAAa,CAAC,GAAG,CAAC;IACrD,MAAM,QAAQ,GAAG,eAAe,CAC9B,OAAO,CAAC,QAAQ,EAChB,6BAA6B,CAC9B,CAAC;IACF,MAAM,OAAO,GAAG,eAAe,CAC7B,OAAO,CAAC,OAAO,EACf,4BAA4B,CAC7B,CAAC;IACF,MAAM,KAAK,GAAG,mBAAmB,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;IACjD,MAAM,SAAS,GAAG,mB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AA0B,EAAC,UAAU,CAAC,CAAC;AAChD,CAAC;AAED,SAAS,6BAA6B,CAAC,YAAgC;IACrE,MAAM,UAAU,GAAG,YAAY,EAAE,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC;IAEtD,QAAQ,UAAU,EAAE,CAAC;QACnB,KAAK,QAAQ,CAAC;QACd,KAAK,YAAY;YACf,OAAO,QAAQ,CAAC;QAClB,KAAK,SAAS,CAAC;QACf,KAAK,aAAa;YAChB,OAAO,SAAS,CAAC;QACnB,KAAK,MAAM,CAAC;QACZ,KAAK,UAAU,CAAC;QAChB,KAAK,cAAc;YACjB,OAAO,0BAA0B,CAAC;QACpC,KAAK,KAAK,CAAC;QACX,KAAK,SAAS,CAAC;QACf,KAAK,QAAQ,CAAC;QACd,KAAK,YAAY;YACf,OAAO,SAAS,CAAC;QACnB,KAAK,QAAQ,CAAC;QACd,KAAK,YAAY,CAAC;QAClB,KAAK,MAAM,CAAC;QACZ,KAAK,UAAU;YACb,OAAO,MAAM,CAAC;QAChB,KAAK,MAAM,CAAC;QACZ,KAAK,UAAU;YACb,OAAO,MAAM,CAAC;QAChB;YACE,OAAO,SAAS,CAAC;IACrB,CAAC;AACH,CAAC;AAED,SAAS,wBAAwB,CAAC,MAAc;IAC9C,OAAO,cAAc,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC;AAC1C,CAAC;AAED,SAAS,gBAAgB,CACvB,QAA6B;IAE7B,OAAO,OAAO,QAAQ,KAAK,UAAU,CAAC;AACxC,CAAC"}
|
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.RowLevelSecurityMigrationGenerator = void 0;
|
|
4
4
|
const node_crypto_1 = require("node:crypto");
|
|
5
5
|
const migrations_1 = require("@mikro-orm/migrations");
|
|
6
|
+
const pgsql_ast_parser_1 = require("pgsql-ast-parser");
|
|
6
7
|
const policy_decorator_1 = require("./decorators/policy.decorator");
|
|
7
8
|
const policy_command_enum_1 = require("./enums/policy-command.enum");
|
|
8
9
|
const policy_mode_enum_1 = require("./enums/policy-mode.enum");
|
|
@@ -11,8 +12,7 @@ const create_policy_down_sql_1 = require("./utils/create-policy-down-sql");
|
|
|
11
12
|
const create_policy_privilege_down_sql_statements_1 = require("./utils/create-policy-privilege-down-sql-statements");
|
|
12
13
|
const create_policy_role_sql_statements_1 = require("./utils/create-policy-role-sql-statements");
|
|
13
14
|
const create_policy_up_sql_statements_1 = require("./utils/create-policy-up-sql-statements");
|
|
14
|
-
const
|
|
15
|
-
const normalize_postgres_type_alias_1 = require("./utils/normalize-postgres-type-alias");
|
|
15
|
+
const normalize_postgres_type_alias_util_1 = require("./utils/normalize-postgres-type-alias.util");
|
|
16
16
|
const POSTGRES_IDENTIFIER_MAX_LENGTH = 63;
|
|
17
17
|
const POLICY_IDENTIFIER_TYPE = "policy";
|
|
18
18
|
/** MikroORM TypeScript migration generator that injects generated RLS SQL. */
|
|
@@ -262,10 +262,8 @@ function hasSamePolicyDefinitionContent(left, right) {
|
|
|
262
262
|
return (normalizePolicyMode(left.mode) === normalizePolicyMode(right.mode) &&
|
|
263
263
|
normalizePolicyCommand(left.command) ===
|
|
264
264
|
normalizePolicyCommand(right.command) &&
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
normalizePolicyExpression(left.withCheck) ===
|
|
268
|
-
normalizePolicyExpression(right.withCheck) &&
|
|
265
|
+
hasSamePolicyExpression(left.using, right.using) &&
|
|
266
|
+
hasSamePolicyExpression(left.withCheck, right.withCheck) &&
|
|
269
267
|
normalizePolicyRoles(left.roles).join("\n") ===
|
|
270
268
|
normalizePolicyRoles(right.roles).join("\n"));
|
|
271
269
|
}
|
|
@@ -275,6 +273,17 @@ function normalizePolicyMode(mode) {
|
|
|
275
273
|
function normalizePolicyCommand(command) {
|
|
276
274
|
return command ?? policy_command_enum_1.PolicyCommand.ALL;
|
|
277
275
|
}
|
|
276
|
+
function hasSamePolicyExpression(left, right) {
|
|
277
|
+
const normalizedLeft = normalizePolicyExpression(left);
|
|
278
|
+
const normalizedRight = normalizePolicyExpression(right);
|
|
279
|
+
if (normalizedLeft !== undefined && normalizedRight !== undefined) {
|
|
280
|
+
return normalizedLeft === normalizedRight;
|
|
281
|
+
}
|
|
282
|
+
if (normalizedLeft === undefined && normalizedRight === undefined) {
|
|
283
|
+
return left?.trim() === right?.trim();
|
|
284
|
+
}
|
|
285
|
+
return false;
|
|
286
|
+
}
|
|
278
287
|
function normalizePolicyExpression(expression) {
|
|
279
288
|
const normalized = expression?.trim();
|
|
280
289
|
if (!normalized) {
|
|
@@ -283,273 +292,96 @@ function normalizePolicyExpression(expression) {
|
|
|
283
292
|
return normalizePostgresExpression(normalized);
|
|
284
293
|
}
|
|
285
294
|
function normalizePostgresExpression(expression) {
|
|
286
|
-
const
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
function stripRedundantCurrentSettingCastParentheses(expression) {
|
|
290
|
-
return expression.replace(/\((current_setting\('(?:''|[^'])*', true\))\)::/gi, "$1::");
|
|
291
|
-
}
|
|
292
|
-
function normalizePostgresExpressionSegment(segment) {
|
|
293
|
-
return normalizeSqlOperatorSpacing(normalizeNullTypeCasts(segment
|
|
294
|
-
.replace(/\s+/g, " ")
|
|
295
|
-
.replace(/\b(select|and|or|not|is|true|false|null|as)\b/gi, (match) => match.toLowerCase())
|
|
296
|
-
.replace(/\s*,\s*/g, ", ")))
|
|
297
|
-
.replace(/\(\s+/g, "(")
|
|
298
|
-
.replace(/\s+\)/g, ")");
|
|
299
|
-
}
|
|
300
|
-
function normalizeNullTypeCasts(segment) {
|
|
301
|
-
return segment
|
|
302
|
-
.replace(/\bnull::(?:character\s+varying|varchar)(?:\s*\([^)]*\))?/gi, "null::character varying")
|
|
303
|
-
.replace(/\bnull::(?:timestamp\s+with\s+time\s+zone|timestamptz)\b/gi, "null::timestamp with time zone")
|
|
304
|
-
.replace(/\bnull::([a-z_][a-z0-9_.]*)/gi, (_match, type) => `null::${(0, normalize_postgres_type_alias_1.normalizePostgresTypeAlias)(type.toLowerCase())}`);
|
|
305
|
-
}
|
|
306
|
-
function normalizeSqlOperatorSpacing(segment) {
|
|
307
|
-
const jsonOperators = [];
|
|
308
|
-
const protectedSegment = segment.replace(/\s*(#>>|#>|->>|->|@>|<@)\s*/g, (_match, operator) => {
|
|
309
|
-
const placeholder = `__rls_json_operator_${String(jsonOperators.length)}__`;
|
|
310
|
-
jsonOperators.push(` ${operator} `);
|
|
311
|
-
return placeholder;
|
|
312
|
-
});
|
|
313
|
-
return protectedSegment
|
|
314
|
-
.replace(/\s*(<>|!=|<=|>=|=|<|>)\s*/g, (_match, operator) => ` ${operator === "!=" ? "<>" : operator} `)
|
|
315
|
-
.replace(/__rls_json_operator_(\d+)__/g, (_match, index) => jsonOperators[Number(index)]);
|
|
316
|
-
}
|
|
317
|
-
function stripSelectProjectionAliases(expression) {
|
|
318
|
-
let normalized = "";
|
|
319
|
-
const selectDepths = new Set();
|
|
320
|
-
let depth = 0;
|
|
321
|
-
let index = 0;
|
|
322
|
-
while (index < expression.length) {
|
|
323
|
-
const character = expression[index];
|
|
324
|
-
if (character === "'") {
|
|
325
|
-
const literalEnd = readSqlStringLiteralEnd(expression, index);
|
|
326
|
-
normalized += expression.slice(index, literalEnd);
|
|
327
|
-
index = literalEnd;
|
|
328
|
-
continue;
|
|
329
|
-
}
|
|
330
|
-
if (character === '"') {
|
|
331
|
-
const identifierEnd = readQuotedIdentifierEnd(expression, index);
|
|
332
|
-
normalized += expression.slice(index, identifierEnd);
|
|
333
|
-
index = identifierEnd;
|
|
334
|
-
continue;
|
|
335
|
-
}
|
|
336
|
-
if (character === "(") {
|
|
337
|
-
depth += 1;
|
|
338
|
-
normalized += character;
|
|
339
|
-
index += 1;
|
|
340
|
-
continue;
|
|
341
|
-
}
|
|
342
|
-
if (character === ")") {
|
|
343
|
-
selectDepths.delete(depth);
|
|
344
|
-
depth -= 1;
|
|
345
|
-
normalized += character;
|
|
346
|
-
index += 1;
|
|
347
|
-
continue;
|
|
348
|
-
}
|
|
349
|
-
const wordEnd = readSqlWordEnd(expression, index);
|
|
350
|
-
if (wordEnd > index) {
|
|
351
|
-
const word = expression.slice(index, wordEnd);
|
|
352
|
-
const lowerWord = word.toLowerCase();
|
|
353
|
-
if (lowerWord === "select") {
|
|
354
|
-
selectDepths.add(depth);
|
|
355
|
-
}
|
|
356
|
-
if (lowerWord === "as" && selectDepths.has(depth)) {
|
|
357
|
-
const aliasEnd = readSelectProjectionAliasEnd(expression, wordEnd);
|
|
358
|
-
if (aliasEnd !== undefined) {
|
|
359
|
-
normalized = normalized.replace(/\s+$/, "");
|
|
360
|
-
index = aliasEnd;
|
|
361
|
-
continue;
|
|
362
|
-
}
|
|
363
|
-
}
|
|
364
|
-
normalized += word;
|
|
365
|
-
index = wordEnd;
|
|
366
|
-
continue;
|
|
367
|
-
}
|
|
368
|
-
normalized += character;
|
|
369
|
-
index += 1;
|
|
295
|
+
const parsed = parsePolicyExpressionAst(expression);
|
|
296
|
+
if (!parsed) {
|
|
297
|
+
return undefined;
|
|
370
298
|
}
|
|
371
|
-
return
|
|
299
|
+
return JSON.stringify(canonicalizePolicyExpressionAst(parsed.ast, parsed.sql));
|
|
372
300
|
}
|
|
373
|
-
function
|
|
374
|
-
const
|
|
375
|
-
|
|
376
|
-
if (aliasEnd === aliasStart) {
|
|
301
|
+
function parsePolicyExpressionAst(expression) {
|
|
302
|
+
const parsed = parsePolicyExpressionStatement(expression);
|
|
303
|
+
if (parsed?.statement.type !== "select" || !parsed.statement.where) {
|
|
377
304
|
return undefined;
|
|
378
305
|
}
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
let normalized = expression;
|
|
384
|
-
while (isWrappedInParentheses(normalized)) {
|
|
385
|
-
normalized = normalized.slice(1, -1).trim();
|
|
386
|
-
}
|
|
387
|
-
return normalized;
|
|
388
|
-
}
|
|
389
|
-
function stripRedundantJsonOperandParentheses(expression) {
|
|
390
|
-
let normalized = expression;
|
|
391
|
-
let previous;
|
|
392
|
-
do {
|
|
393
|
-
previous = normalized;
|
|
394
|
-
normalized = stripRedundantJsonOperandParenthesesOnce(normalized);
|
|
395
|
-
} while (normalized !== previous);
|
|
396
|
-
return normalized;
|
|
397
|
-
}
|
|
398
|
-
function stripRedundantJsonOperandParenthesesOnce(expression) {
|
|
399
|
-
const removals = new Set();
|
|
400
|
-
const parenthesisPairs = findParenthesisPairs(expression);
|
|
401
|
-
for (const [start, end] of parenthesisPairs) {
|
|
402
|
-
const innerExpression = expression.slice(start + 1, end);
|
|
403
|
-
if (hasTopLevelJsonOperator(innerExpression) &&
|
|
404
|
-
(hasComparisonOperatorBefore(expression, start) ||
|
|
405
|
-
hasComparisonOperatorAfter(expression, end))) {
|
|
406
|
-
removals.add(start);
|
|
407
|
-
removals.add(end);
|
|
408
|
-
}
|
|
409
|
-
}
|
|
410
|
-
if (removals.size === 0) {
|
|
411
|
-
return expression;
|
|
412
|
-
}
|
|
413
|
-
return [...expression]
|
|
414
|
-
.filter((_character, index) => !removals.has(index))
|
|
415
|
-
.join("");
|
|
306
|
+
return {
|
|
307
|
+
ast: parsed.statement.where,
|
|
308
|
+
sql: parsed.sql,
|
|
309
|
+
};
|
|
416
310
|
}
|
|
417
|
-
function
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
const character = expression[index];
|
|
425
|
-
if (character === "'") {
|
|
426
|
-
index = readSqlStringLiteralEnd(expression, index);
|
|
427
|
-
continue;
|
|
428
|
-
}
|
|
429
|
-
if (character === '"') {
|
|
430
|
-
index = readQuotedIdentifierEnd(expression, index);
|
|
431
|
-
continue;
|
|
432
|
-
}
|
|
433
|
-
if (character === "(") {
|
|
434
|
-
depth += 1;
|
|
435
|
-
}
|
|
436
|
-
else if (character === ")") {
|
|
437
|
-
depth -= 1;
|
|
438
|
-
if (depth === 0 && index < expression.length - 1) {
|
|
439
|
-
return false;
|
|
440
|
-
}
|
|
441
|
-
}
|
|
442
|
-
if (depth < 0) {
|
|
443
|
-
return false;
|
|
444
|
-
}
|
|
445
|
-
index += 1;
|
|
311
|
+
function parsePolicyExpressionStatement(expression) {
|
|
312
|
+
const sql = `select 1 where ${expression}`;
|
|
313
|
+
try {
|
|
314
|
+
return {
|
|
315
|
+
sql,
|
|
316
|
+
statement: (0, pgsql_ast_parser_1.parse)(sql, { locationTracking: true })[0],
|
|
317
|
+
};
|
|
446
318
|
}
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
}
|
|
459
|
-
if (character === '"') {
|
|
460
|
-
index = readQuotedIdentifierEnd(expression, index);
|
|
461
|
-
continue;
|
|
462
|
-
}
|
|
463
|
-
if (character === "(") {
|
|
464
|
-
stack.push(index);
|
|
465
|
-
}
|
|
466
|
-
else if (character === ")") {
|
|
467
|
-
const start = stack.pop();
|
|
468
|
-
if (start !== undefined) {
|
|
469
|
-
pairs.push([start, index]);
|
|
319
|
+
catch {
|
|
320
|
+
const parserCompatibleExpression = getPolicyExpressionForAstParser(expression);
|
|
321
|
+
if (parserCompatibleExpression !== expression) {
|
|
322
|
+
try {
|
|
323
|
+
const parserCompatibleSql = `select 1 where ${parserCompatibleExpression}`;
|
|
324
|
+
return {
|
|
325
|
+
sql: parserCompatibleSql,
|
|
326
|
+
statement: (0, pgsql_ast_parser_1.parse)(parserCompatibleSql, {
|
|
327
|
+
locationTracking: true,
|
|
328
|
+
})[0],
|
|
329
|
+
};
|
|
470
330
|
}
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
}
|
|
474
|
-
return pairs;
|
|
475
|
-
}
|
|
476
|
-
function hasTopLevelJsonOperator(expression) {
|
|
477
|
-
let depth = 0;
|
|
478
|
-
let index = 0;
|
|
479
|
-
while (index < expression.length) {
|
|
480
|
-
const character = expression[index];
|
|
481
|
-
if (character === "'") {
|
|
482
|
-
index = readSqlStringLiteralEnd(expression, index);
|
|
483
|
-
continue;
|
|
484
|
-
}
|
|
485
|
-
if (character === '"') {
|
|
486
|
-
index = readQuotedIdentifierEnd(expression, index);
|
|
487
|
-
continue;
|
|
488
|
-
}
|
|
489
|
-
if (character === "(") {
|
|
490
|
-
depth += 1;
|
|
491
|
-
index += 1;
|
|
492
|
-
continue;
|
|
493
|
-
}
|
|
494
|
-
if (character === ")") {
|
|
495
|
-
depth -= 1;
|
|
496
|
-
index += 1;
|
|
497
|
-
continue;
|
|
498
|
-
}
|
|
499
|
-
if (depth === 0) {
|
|
500
|
-
const jsonOperator = readJsonOperatorAt(expression, index);
|
|
501
|
-
if (jsonOperator) {
|
|
502
|
-
return true;
|
|
331
|
+
catch {
|
|
332
|
+
return undefined;
|
|
503
333
|
}
|
|
504
334
|
}
|
|
505
|
-
|
|
335
|
+
return undefined;
|
|
506
336
|
}
|
|
507
|
-
return false;
|
|
508
337
|
}
|
|
509
|
-
function
|
|
510
|
-
|
|
511
|
-
return
|
|
338
|
+
function getPolicyExpressionForAstParser(expression) {
|
|
339
|
+
// pgsql-ast-parser treats these multi-word casts in select projections as ambiguous.
|
|
340
|
+
return replaceSqlOutsideQuotedTokens(expression, (segment) => segment
|
|
341
|
+
.replace(/::\s*character\s+varying\b/gi, "::varchar")
|
|
342
|
+
.replace(/::\s*bit\s+varying\b/gi, "::varbit"));
|
|
512
343
|
}
|
|
513
|
-
function
|
|
514
|
-
const operatorStart = skipSqlWhitespace(expression, endIndex + 1);
|
|
515
|
-
return Boolean(readComparisonOperatorAt(expression, operatorStart));
|
|
516
|
-
}
|
|
517
|
-
function mapSqlOutsideQuotedTokens(expression, transform) {
|
|
344
|
+
function replaceSqlOutsideQuotedTokens(expression, transform) {
|
|
518
345
|
const parts = [];
|
|
519
346
|
let segmentStart = 0;
|
|
520
347
|
let index = 0;
|
|
521
348
|
while (index < expression.length) {
|
|
522
|
-
|
|
349
|
+
const tokenEnd = readSqlQuotedTokenEnd(expression, index);
|
|
350
|
+
if (tokenEnd === undefined) {
|
|
523
351
|
index += 1;
|
|
524
352
|
continue;
|
|
525
353
|
}
|
|
526
354
|
parts.push(transform(expression.slice(segmentStart, index)));
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
parts.push(expression.slice(literalStart, index));
|
|
531
|
-
}
|
|
532
|
-
else {
|
|
533
|
-
const identifierStart = index;
|
|
534
|
-
index = readQuotedIdentifierEnd(expression, index);
|
|
535
|
-
parts.push(normalizeQuotedIdentifier(expression.slice(identifierStart, index)));
|
|
536
|
-
}
|
|
537
|
-
segmentStart = index;
|
|
355
|
+
parts.push(expression.slice(index, tokenEnd));
|
|
356
|
+
segmentStart = tokenEnd;
|
|
357
|
+
index = tokenEnd;
|
|
538
358
|
}
|
|
539
359
|
parts.push(transform(expression.slice(segmentStart)));
|
|
540
360
|
return parts.join("");
|
|
541
361
|
}
|
|
542
|
-
function
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
362
|
+
function readSqlQuotedTokenEnd(expression, startIndex) {
|
|
363
|
+
if (expression[startIndex] === "'") {
|
|
364
|
+
return readSqlStringLiteralEnd(expression, startIndex, hasPostgresEscapeStringPrefix(expression, startIndex));
|
|
365
|
+
}
|
|
366
|
+
if (expression[startIndex] === '"') {
|
|
367
|
+
return readQuotedIdentifierEnd(expression, startIndex);
|
|
368
|
+
}
|
|
369
|
+
return readDollarQuotedStringEnd(expression, startIndex);
|
|
370
|
+
}
|
|
371
|
+
function hasPostgresEscapeStringPrefix(expression, quoteIndex) {
|
|
372
|
+
const prefixIndex = quoteIndex - 1;
|
|
373
|
+
if (!/[eE]/.test(expression[prefixIndex] ?? "")) {
|
|
374
|
+
return false;
|
|
547
375
|
}
|
|
548
|
-
return
|
|
376
|
+
return prefixIndex === 0 || !isSqlIdentifierPart(expression[prefixIndex - 1]);
|
|
549
377
|
}
|
|
550
|
-
function readSqlStringLiteralEnd(expression, startIndex) {
|
|
378
|
+
function readSqlStringLiteralEnd(expression, startIndex, allowBackslashEscapes = false) {
|
|
551
379
|
let index = startIndex + 1;
|
|
552
380
|
while (index < expression.length) {
|
|
381
|
+
if (allowBackslashEscapes && expression[index] === "\\") {
|
|
382
|
+
index += 2;
|
|
383
|
+
continue;
|
|
384
|
+
}
|
|
553
385
|
if (expression[index] !== "'") {
|
|
554
386
|
index += 1;
|
|
555
387
|
continue;
|
|
@@ -563,6 +395,9 @@ function readSqlStringLiteralEnd(expression, startIndex) {
|
|
|
563
395
|
}
|
|
564
396
|
return index;
|
|
565
397
|
}
|
|
398
|
+
function isSqlIdentifierPart(character) {
|
|
399
|
+
return character !== undefined && /[A-Za-z0-9_$]/.test(character);
|
|
400
|
+
}
|
|
566
401
|
function readQuotedIdentifierEnd(expression, startIndex) {
|
|
567
402
|
let index = startIndex + 1;
|
|
568
403
|
while (index < expression.length) {
|
|
@@ -579,41 +414,139 @@ function readQuotedIdentifierEnd(expression, startIndex) {
|
|
|
579
414
|
}
|
|
580
415
|
return index;
|
|
581
416
|
}
|
|
582
|
-
function
|
|
583
|
-
|
|
584
|
-
|
|
417
|
+
function readDollarQuotedStringEnd(expression, startIndex) {
|
|
418
|
+
const delimiter = /^\$[a-z_][a-z0-9_]*\$|^\$\$/i.exec(expression.slice(startIndex))?.[0];
|
|
419
|
+
if (!delimiter) {
|
|
420
|
+
return undefined;
|
|
585
421
|
}
|
|
586
|
-
|
|
422
|
+
const endIndex = expression.indexOf(delimiter, startIndex + delimiter.length);
|
|
423
|
+
return endIndex === -1 ? expression.length : endIndex + delimiter.length;
|
|
587
424
|
}
|
|
588
|
-
function
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
425
|
+
function canonicalizePolicyExpressionAst(value, sourceSql) {
|
|
426
|
+
if (value === null || typeof value !== "object") {
|
|
427
|
+
return value === undefined
|
|
428
|
+
? null
|
|
429
|
+
: value;
|
|
430
|
+
}
|
|
431
|
+
if (Array.isArray(value)) {
|
|
432
|
+
return value.map((item) => canonicalizePolicyExpressionAst(item, sourceSql));
|
|
433
|
+
}
|
|
434
|
+
if (isRedundantTextCast(value)) {
|
|
435
|
+
return canonicalizePolicyExpressionAst(value.operand, sourceSql);
|
|
436
|
+
}
|
|
437
|
+
const node = value;
|
|
438
|
+
const entries = [];
|
|
439
|
+
for (const key of Object.keys(node).sort()) {
|
|
440
|
+
const property = node[key];
|
|
441
|
+
if (property === undefined || key === "_location") {
|
|
442
|
+
continue;
|
|
443
|
+
}
|
|
444
|
+
if (key === "alias" && "expr" in node) {
|
|
445
|
+
continue;
|
|
446
|
+
}
|
|
447
|
+
if (key === "value" && isNumericPolicyExpressionAstNode(node)) {
|
|
448
|
+
entries.push([
|
|
449
|
+
key,
|
|
450
|
+
getPolicyExpressionAstNodeSource(node, sourceSql) ??
|
|
451
|
+
canonicalizePolicyExpressionAst(property, sourceSql),
|
|
452
|
+
]);
|
|
453
|
+
continue;
|
|
454
|
+
}
|
|
455
|
+
if (key === "op" && typeof property === "string") {
|
|
456
|
+
entries.push([key, normalizePolicyExpressionOperator(property)]);
|
|
457
|
+
continue;
|
|
458
|
+
}
|
|
459
|
+
if (key === "to" && node.type === "cast") {
|
|
460
|
+
entries.push([
|
|
461
|
+
key,
|
|
462
|
+
canonicalizePolicyExpressionDataType(property, sourceSql),
|
|
463
|
+
]);
|
|
464
|
+
continue;
|
|
465
|
+
}
|
|
466
|
+
entries.push([key, canonicalizePolicyExpressionAst(property, sourceSql)]);
|
|
467
|
+
}
|
|
468
|
+
return Object.fromEntries(entries);
|
|
593
469
|
}
|
|
594
|
-
function
|
|
595
|
-
if (
|
|
596
|
-
return
|
|
470
|
+
function canonicalizePolicyExpressionDataType(value, sourceSql) {
|
|
471
|
+
if (value === null || typeof value !== "object") {
|
|
472
|
+
return value === undefined
|
|
473
|
+
? null
|
|
474
|
+
: value;
|
|
597
475
|
}
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
index += 1;
|
|
476
|
+
if (Array.isArray(value)) {
|
|
477
|
+
return value.map((item) => canonicalizePolicyExpressionDataType(item, sourceSql));
|
|
601
478
|
}
|
|
602
|
-
|
|
479
|
+
const node = value;
|
|
480
|
+
const entries = [];
|
|
481
|
+
const isDoubleQuoted = node.doubleQuoted === true;
|
|
482
|
+
for (const key of Object.keys(node).sort()) {
|
|
483
|
+
const property = node[key];
|
|
484
|
+
if (property === undefined || key === "_location") {
|
|
485
|
+
continue;
|
|
486
|
+
}
|
|
487
|
+
if (key === "name" && typeof property === "string" && !isDoubleQuoted) {
|
|
488
|
+
entries.push([key, normalizePolicyExpressionDataTypeName(property)]);
|
|
489
|
+
continue;
|
|
490
|
+
}
|
|
491
|
+
if (key === "arrayOf") {
|
|
492
|
+
entries.push([
|
|
493
|
+
key,
|
|
494
|
+
canonicalizePolicyExpressionDataType(property, sourceSql),
|
|
495
|
+
]);
|
|
496
|
+
continue;
|
|
497
|
+
}
|
|
498
|
+
entries.push([key, canonicalizePolicyExpressionAst(property, sourceSql)]);
|
|
499
|
+
}
|
|
500
|
+
return Object.fromEntries(entries);
|
|
603
501
|
}
|
|
604
|
-
function
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
index += 1;
|
|
502
|
+
function isRedundantTextCast(value) {
|
|
503
|
+
if (!isPolicyExpressionAstNode(value, "cast") || !isTextType(value.to)) {
|
|
504
|
+
return false;
|
|
608
505
|
}
|
|
609
|
-
return
|
|
506
|
+
return (isPolicyExpressionAstNode(value.operand, "string") ||
|
|
507
|
+
isTextJsonMember(value.operand));
|
|
610
508
|
}
|
|
611
|
-
function
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
509
|
+
function isTextJsonMember(value) {
|
|
510
|
+
return isPolicyExpressionAstNode(value, "member") && value.op === "->>";
|
|
511
|
+
}
|
|
512
|
+
function isTextType(value) {
|
|
513
|
+
if (value === null || typeof value !== "object" || Array.isArray(value)) {
|
|
514
|
+
return false;
|
|
615
515
|
}
|
|
616
|
-
|
|
516
|
+
const node = value;
|
|
517
|
+
const typeName = node.name;
|
|
518
|
+
return (node.doubleQuoted !== true &&
|
|
519
|
+
typeof typeName === "string" &&
|
|
520
|
+
normalizePolicyExpressionDataTypeName(typeName) === "text");
|
|
521
|
+
}
|
|
522
|
+
function isNumericPolicyExpressionAstNode(value) {
|
|
523
|
+
return value.type === "integer" || value.type === "numeric";
|
|
524
|
+
}
|
|
525
|
+
function getPolicyExpressionAstNodeSource(value, sourceSql) {
|
|
526
|
+
const location = value._location;
|
|
527
|
+
if (location === null ||
|
|
528
|
+
typeof location !== "object" ||
|
|
529
|
+
Array.isArray(location)) {
|
|
530
|
+
return undefined;
|
|
531
|
+
}
|
|
532
|
+
const start = location.start;
|
|
533
|
+
const end = location.end;
|
|
534
|
+
if (typeof start !== "number" || typeof end !== "number") {
|
|
535
|
+
return undefined;
|
|
536
|
+
}
|
|
537
|
+
return sourceSql.slice(start, end);
|
|
538
|
+
}
|
|
539
|
+
function isPolicyExpressionAstNode(value, type) {
|
|
540
|
+
return (value !== null &&
|
|
541
|
+
typeof value === "object" &&
|
|
542
|
+
!Array.isArray(value) &&
|
|
543
|
+
value.type === type);
|
|
544
|
+
}
|
|
545
|
+
function normalizePolicyExpressionDataTypeName(typeName) {
|
|
546
|
+
return (0, normalize_postgres_type_alias_util_1.normalizePostgresTypeAlias)(typeName.toLowerCase().replace(/\s+/g, " "));
|
|
547
|
+
}
|
|
548
|
+
function normalizePolicyExpressionOperator(operator) {
|
|
549
|
+
return operator === "!=" ? "<>" : operator;
|
|
617
550
|
}
|
|
618
551
|
function normalizePolicyRoles(roles) {
|
|
619
552
|
return [
|