@rebasepro/common 0.29.0-canary.g0e3a0e7 → 0.29.1

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.
@@ -1,6 +1,7 @@
1
1
  import { getTableName } from "../relations.es.js";
2
2
  import { securityRuleToConditions } from "./securityRuleToConditions.es.js";
3
3
  import { policyToPostgres } from "./policyToPostgres.es.js";
4
+ import { DEFAULT_DATA_SOURCE_KEY, RLS_IS_REQUEST_ROLE_SQL, declaredRlsPolicyShape } from "@rebasepro/types";
4
5
  import { getPolicyNamesForRule } from "@rebasepro/utils";
5
6
  //#region src/util/policy/compileRulePolicies.ts
6
7
  /**
@@ -20,6 +21,9 @@ function compileRulePolicies(collection, rule, options) {
20
21
  const ops = rule.operations && rule.operations.length > 0 ? rule.operations : [rule.operation ?? "all"];
21
22
  const policyNames = getPolicyNamesForRule(rule, getTableName(collection));
22
23
  const { usingExpr, withCheckExpr } = securityRuleToConditions(rule);
24
+ const mode = rule.mode ?? "permissive";
25
+ const narrowed = mode === "permissive" && declaredRlsPolicyShape(collection.dataSource ?? DEFAULT_DATA_SOURCE_KEY).policyTarget === "requestRole";
26
+ const forRequestRoles = (clause) => narrowed ? `(${clause}) AND ${RLS_IS_REQUEST_ROLE_SQL}` : clause;
23
27
  return ops.map((operation, index) => {
24
28
  const needsUsing = operation !== "insert";
25
29
  const needsWithCheck = operation !== "select" && operation !== "delete";
@@ -30,10 +34,10 @@ function compileRulePolicies(collection, rule, options) {
30
34
  return {
31
35
  name: policyNames[index],
32
36
  operation,
33
- mode: rule.mode ?? "permissive",
37
+ mode,
34
38
  roles: rule.pgRoles ? [...rule.pgRoles].sort() : ["public"],
35
- using,
36
- withCheck
39
+ using: using === null ? null : forRequestRoles(using),
40
+ withCheck: withCheck === null ? null : forRequestRoles(withCheck)
37
41
  };
38
42
  });
39
43
  }
@@ -1 +1 @@
1
- {"version":3,"file":"compileRulePolicies.es.js","names":[],"sources":["../../../src/util/policy/compileRulePolicies.ts"],"sourcesContent":["import type { CollectionConfig, SecurityOperation, SecurityRule } from \"@rebasepro/types\";\nimport { getPolicyNamesForRule } from \"@rebasepro/utils\";\nimport { getTableName } from \"../relations\";\nimport { securityRuleToConditions } from \"./securityRuleToConditions\";\nimport { policyToPostgres, type PolicyCompileOptions } from \"./policyToPostgres\";\n\n/**\n * One Postgres policy a security rule compiles to: its name and the clauses\n * `CREATE POLICY` is given for it.\n */\nexport interface CompiledRulePolicy {\n name: string;\n operation: SecurityOperation;\n /** `permissive` | `restrictive`, lower-case as the rule spells it. */\n mode: \"permissive\" | \"restrictive\";\n /** The `TO` list. Sorted, `[\"public\"]` when the rule names none. */\n roles: string[];\n /** The `USING` clause, or `null` when the operation takes none. */\n using: string | null;\n /** The `WITH CHECK` clause, or `null` when the operation takes none. */\n withCheck: string | null;\n}\n\n/**\n * The policies one security rule compiles to — one per operation.\n *\n * The schema planner writes these into the database, and the Studio's RLS\n * editor compares the database against them; both call this, so \"what the code\n * declares\" has one meaning.\n *\n * The desugaring (`access` / `ownerField` / `roles` / structured condition /\n * raw SQL → `PolicyExpression`) is {@link securityRuleToConditions}, and the SQL\n * is {@link policyToPostgres}. What lives here is only the shape: which\n * operations a rule expands to, which clauses each operation takes, and the\n * deny-all fallback for a clause that compiled to nothing.\n */\nexport function compileRulePolicies(\n collection: CollectionConfig,\n rule: SecurityRule,\n options?: PolicyCompileOptions\n): CompiledRulePolicy[] {\n const ops = rule.operations && rule.operations.length > 0 ? rule.operations : [rule.operation ?? \"all\"];\n const policyNames = getPolicyNamesForRule(rule, getTableName(collection));\n const { usingExpr, withCheckExpr } = securityRuleToConditions(rule);\n\n return ops.map((operation, index) => {\n const needsUsing = operation !== \"insert\";\n const needsWithCheck = operation !== \"select\" && operation !== \"delete\";\n let using = needsUsing && usingExpr ? policyToPostgres(usingExpr, collection, options) : null;\n let withCheck = needsWithCheck && withCheckExpr ? policyToPostgres(withCheckExpr, collection, options) : null;\n // A clause that compiled to nothing denies rather than opens.\n if (!using && needsUsing) using = \"false\";\n if (!withCheck && needsWithCheck) withCheck = \"false\";\n return {\n name: policyNames[index],\n operation,\n mode: rule.mode ?? \"permissive\",\n roles: rule.pgRoles ? [...rule.pgRoles].sort() : [\"public\"],\n using,\n withCheck\n };\n });\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAoCA,SAAgB,oBACZ,YACA,MACA,SACoB;CACpB,MAAM,MAAM,KAAK,cAAc,KAAK,WAAW,SAAS,IAAI,KAAK,aAAa,CAAC,KAAK,aAAa,KAAK;CACtG,MAAM,cAAc,sBAAsB,MAAM,aAAa,UAAU,CAAC;CACxE,MAAM,EAAE,WAAW,kBAAkB,yBAAyB,IAAI;CAElE,OAAO,IAAI,KAAK,WAAW,UAAU;EACjC,MAAM,aAAa,cAAc;EACjC,MAAM,iBAAiB,cAAc,YAAY,cAAc;EAC/D,IAAI,QAAQ,cAAc,YAAY,iBAAiB,WAAW,YAAY,OAAO,IAAI;EACzF,IAAI,YAAY,kBAAkB,gBAAgB,iBAAiB,eAAe,YAAY,OAAO,IAAI;EAEzG,IAAI,CAAC,SAAS,YAAY,QAAQ;EAClC,IAAI,CAAC,aAAa,gBAAgB,YAAY;EAC9C,OAAO;GACH,MAAM,YAAY;GAClB;GACA,MAAM,KAAK,QAAQ;GACnB,OAAO,KAAK,UAAU,CAAC,GAAG,KAAK,OAAO,CAAC,CAAC,KAAK,IAAI,CAAC,QAAQ;GAC1D;GACA;EACJ;CACJ,CAAC;AACL"}
1
+ {"version":3,"file":"compileRulePolicies.es.js","names":[],"sources":["../../../src/util/policy/compileRulePolicies.ts"],"sourcesContent":["import { declaredRlsPolicyShape, DEFAULT_DATA_SOURCE_KEY, RLS_IS_REQUEST_ROLE_SQL, type CollectionConfig, type SecurityOperation, type SecurityRule } from \"@rebasepro/types\";\nimport { getPolicyNamesForRule } from \"@rebasepro/utils\";\nimport { getTableName } from \"../relations\";\nimport { securityRuleToConditions } from \"./securityRuleToConditions\";\nimport { policyToPostgres, type PolicyCompileOptions } from \"./policyToPostgres\";\n\n/**\n * One Postgres policy a security rule compiles to: its name and the clauses\n * `CREATE POLICY` is given for it.\n */\nexport interface CompiledRulePolicy {\n name: string;\n operation: SecurityOperation;\n /** `permissive` | `restrictive`, lower-case as the rule spells it. */\n mode: \"permissive\" | \"restrictive\";\n /** The `TO` list. Sorted, `[\"public\"]` when the rule names none. */\n roles: string[];\n /** The `USING` clause, or `null` when the operation takes none. */\n using: string | null;\n /** The `WITH CHECK` clause, or `null` when the operation takes none. */\n withCheck: string | null;\n}\n\n/**\n * The policies one security rule compiles to — one per operation.\n *\n * The schema planner writes these into the database, and the Studio's RLS\n * editor compares the database against them; both call this, so \"what the code\n * declares\" has one meaning.\n *\n * The desugaring (`access` / `ownerField` / `roles` / structured condition /\n * raw SQL → `PolicyExpression`) is {@link securityRuleToConditions}, and the SQL\n * is {@link policyToPostgres}. What lives here is only the shape: which\n * operations a rule expands to, which clauses each operation takes, and the\n * deny-all fallback for a clause that compiled to nothing.\n */\nexport function compileRulePolicies(\n collection: CollectionConfig,\n rule: SecurityRule,\n options?: PolicyCompileOptions\n): CompiledRulePolicy[] {\n const ops = rule.operations && rule.operations.length > 0 ? rule.operations : [rule.operation ?? \"all\"];\n const policyNames = getPolicyNamesForRule(rule, getTableName(collection));\n const { usingExpr, withCheckExpr } = securityRuleToConditions(rule);\n const mode = rule.mode ?? \"permissive\";\n // `rls.policyTarget: \"requestRole\"`: a grant is for the request roles only.\n // In the compiler, not on the rule, because a policy's name hashes the rule\n // — narrowing it there would rename every policy in every database. Never on\n // a restrictive policy: it can only take access away, from everyone.\n const narrowed = mode === \"permissive\"\n && declaredRlsPolicyShape(collection.dataSource ?? DEFAULT_DATA_SOURCE_KEY).policyTarget === \"requestRole\";\n const forRequestRoles = (clause: string): string => narrowed ? `(${clause}) AND ${RLS_IS_REQUEST_ROLE_SQL}` : clause;\n\n return ops.map((operation, index) => {\n const needsUsing = operation !== \"insert\";\n const needsWithCheck = operation !== \"select\" && operation !== \"delete\";\n let using = needsUsing && usingExpr ? policyToPostgres(usingExpr, collection, options) : null;\n let withCheck = needsWithCheck && withCheckExpr ? policyToPostgres(withCheckExpr, collection, options) : null;\n // A clause that compiled to nothing denies rather than opens.\n if (!using && needsUsing) using = \"false\";\n if (!withCheck && needsWithCheck) withCheck = \"false\";\n return {\n name: policyNames[index],\n operation,\n mode,\n roles: rule.pgRoles ? [...rule.pgRoles].sort() : [\"public\"],\n using: using === null ? null : forRequestRoles(using),\n withCheck: withCheck === null ? null : forRequestRoles(withCheck)\n };\n });\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAoCA,SAAgB,oBACZ,YACA,MACA,SACoB;CACpB,MAAM,MAAM,KAAK,cAAc,KAAK,WAAW,SAAS,IAAI,KAAK,aAAa,CAAC,KAAK,aAAa,KAAK;CACtG,MAAM,cAAc,sBAAsB,MAAM,aAAa,UAAU,CAAC;CACxE,MAAM,EAAE,WAAW,kBAAkB,yBAAyB,IAAI;CAClE,MAAM,OAAO,KAAK,QAAQ;CAK1B,MAAM,WAAW,SAAS,gBACnB,uBAAuB,WAAW,cAAc,uBAAuB,CAAC,CAAC,iBAAiB;CACjG,MAAM,mBAAmB,WAA2B,WAAW,IAAI,OAAO,QAAQ,4BAA4B;CAE9G,OAAO,IAAI,KAAK,WAAW,UAAU;EACjC,MAAM,aAAa,cAAc;EACjC,MAAM,iBAAiB,cAAc,YAAY,cAAc;EAC/D,IAAI,QAAQ,cAAc,YAAY,iBAAiB,WAAW,YAAY,OAAO,IAAI;EACzF,IAAI,YAAY,kBAAkB,gBAAgB,iBAAiB,eAAe,YAAY,OAAO,IAAI;EAEzG,IAAI,CAAC,SAAS,YAAY,QAAQ;EAClC,IAAI,CAAC,aAAa,gBAAgB,YAAY;EAC9C,OAAO;GACH,MAAM,YAAY;GAClB;GACA;GACA,OAAO,KAAK,UAAU,CAAC,GAAG,KAAK,OAAO,CAAC,CAAC,KAAK,IAAI,CAAC,QAAQ;GAC1D,OAAO,UAAU,OAAO,OAAO,gBAAgB,KAAK;GACpD,WAAW,cAAc,OAAO,OAAO,gBAAgB,SAAS;EACpE;CACJ,CAAC;AACL"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rebasepro/common",
3
- "version": "0.29.0-canary.g0e3a0e7",
3
+ "version": "0.29.1",
4
4
  "description": "Rebase shared core — collection registry, data driver adapter and fluent query builder. No React dependency.",
5
5
  "keywords": [
6
6
  "rebase",
@@ -46,8 +46,8 @@
46
46
  "dependencies": {
47
47
  "fast-equals": "6.0.2",
48
48
  "json-logic-js": "^2.0.5",
49
- "@rebasepro/types": "0.29.0-canary.g0e3a0e7",
50
- "@rebasepro/utils": "0.29.0-canary.g0e3a0e7"
49
+ "@rebasepro/types": "0.29.1",
50
+ "@rebasepro/utils": "0.29.1"
51
51
  },
52
52
  "devDependencies": {
53
53
  "@jest/globals": "^30.4.1",