@rebasepro/rls-check 0.31.0 → 0.32.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,129 @@
1
+ /**
2
+ * Policies Postgres refuses to expand: `infinite recursion detected in policy
3
+ * for relation "…"` (SQLSTATE 42P17).
4
+ *
5
+ * How the rewriter decides — `fireRIRrules` in
6
+ * `src/backend/rewrite/rewriteHandler.c`. For each table a statement names, it
7
+ * collects the policies that apply to the command and the current role. When
8
+ * any of them contains a sub-select (a SubLink: `EXISTS (SELECT …)`,
9
+ * `x IN (SELECT …)`, a scalar `(SELECT …)`), it raises 42P17 if that table is
10
+ * already being expanded; otherwise it marks the table and expands the tables
11
+ * those sub-selects read, as SELECTs, the same way. A table whose policies
12
+ * contain no sub-select is neither checked nor walked into. A function call is
13
+ * never expanded — its body is planned when it runs — which is why moving the
14
+ * read into a `SECURITY DEFINER` function is the way out.
15
+ *
16
+ * So a statement on T fails when a policy it applies reads, in a sub-select, a
17
+ * table from which SELECT policies lead back to T, every table on the way — T
18
+ * included — having a sub-select among its SELECT policies. Since 0.25 Rebase
19
+ * writes `rebase.uid()` as `(SELECT rebase.uid())`, and the admin baseline is a
20
+ * sub-select too, so nearly every table's policies qualify: a rule that reads
21
+ * its own table, or two tables whose rules read each other, fails on every such
22
+ * statement — for signed-in users, the service key and `dataAsAdmin` alike.
23
+ *
24
+ * Pure, like the checks: policies in, recursions out. The policy text is read
25
+ * with this package's scanner (`checks/sql.ts`), so a `FROM` inside a string
26
+ * literal, a comment or `extract(… FROM …)` is never a table. What it cannot
27
+ * see, it does not guess, so each of these is a miss and never a false error:
28
+ *
29
+ * - a view a policy reads (Postgres expands the view's query as well);
30
+ * - a table the input carries no policies for;
31
+ * - a table read inside a function — which is also what Postgres does not
32
+ * expand, so that one is not a miss at all;
33
+ * - a policy written `TO` a role no {@link RoleContext} holds.
34
+ *
35
+ * A finding is `heuristic` rather than `certain` only when its loop passes
36
+ * through a table named without a schema that the search path does not settle.
37
+ */
38
+ import type { DbPolicy, PolicyCommand } from "./types.js";
39
+ /** The parts of a policy the analysis reads: a {@link DbPolicy}, or a policy a compiler planned. */
40
+ export type RecursionPolicy = Pick<DbPolicy, "schema" | "table" | "name" | "roles" | "command" | "using" | "withCheck">;
41
+ /** A role requests run as, by the policy roles that apply to it. */
42
+ export interface RoleContext {
43
+ /** How a finding names it: `rebase_user`, `rebase_user_admin`. */
44
+ name: string;
45
+ /**
46
+ * The roles whose policies apply to it besides PUBLIC: itself and every
47
+ * role it inherits. A policy applies when its `TO` list names PUBLIC or
48
+ * one of these.
49
+ */
50
+ roles: readonly string[];
51
+ }
52
+ /** What {@link findPolicyRecursion} assumes about the database the policies live in. */
53
+ export interface PolicyRecursionOptions {
54
+ /**
55
+ * The roles requests run as. Each is walked on its own, because a policy
56
+ * applies only to the roles its `TO` list reaches. Without any, only the
57
+ * policies written `TO PUBLIC` are walked: they apply to every role.
58
+ */
59
+ contexts?: readonly RoleContext[];
60
+ /**
61
+ * Where a table named without a schema resolves, first match wins: the
62
+ * search path the policies were created under. Defaults to `["public"]`,
63
+ * the one Rebase pins on its connections.
64
+ */
65
+ searchPath?: readonly string[];
66
+ }
67
+ /** One table the rewriter expands, and the policy whose sub-select reads the next one. */
68
+ export interface RecursionStep {
69
+ schema: string;
70
+ table: string;
71
+ policy: string;
72
+ }
73
+ /** A policy whose expansion Postgres refuses with 42P17. */
74
+ export interface PolicyRecursion {
75
+ schema: string;
76
+ table: string;
77
+ /** The policy that starts the loop. */
78
+ policy: string;
79
+ command: PolicyCommand;
80
+ /** The table the policy's sub-select reads, where the loop starts. */
81
+ reads: {
82
+ schema: string;
83
+ table: string;
84
+ };
85
+ /**
86
+ * The expansion, in order: the policy's table first, then every table the
87
+ * loop passes through, each with the policy that reads the next. The
88
+ * table after the last step is {@link table} again, where Postgres finds
89
+ * a table it is already expanding.
90
+ */
91
+ path: RecursionStep[];
92
+ /** The {@link RoleContext} names it recurses for. */
93
+ contexts: string[];
94
+ /** `certain` unless the loop passes through a table name the search path does not settle. */
95
+ confidence: "certain" | "heuristic";
96
+ /** Why it is heuristic, one sentence each. Empty when certain. */
97
+ caveats: string[];
98
+ }
99
+ /** A table a sub-select names, as written: schema-qualified or bare. */
100
+ export interface TableRef {
101
+ schema: string | null;
102
+ name: string;
103
+ }
104
+ /** What a policy expression's sub-selects amount to. */
105
+ export interface SubLinks {
106
+ /** Whether the expression has any sub-select at all — what makes Postgres walk into the table. */
107
+ hasSubLink: boolean;
108
+ /** The tables named in a FROM or JOIN of any of them, CTE names left out. */
109
+ reads: TableRef[];
110
+ }
111
+ /**
112
+ * Every sub-select in a policy expression: whether there is one, and the
113
+ * tables they read.
114
+ *
115
+ * Sub-selects nest, and each is read at its own level, so `FROM` inside
116
+ * `extract(year FROM x)` — one level deeper than the sub-select it sits in —
117
+ * is never a FROM clause.
118
+ */
119
+ export declare function readSubLinks(expr: string | null | undefined): SubLinks;
120
+ /**
121
+ * Every policy whose sub-selects lead back to its own table through tables
122
+ * Postgres walks into — each one a statement the database refuses with 42P17.
123
+ *
124
+ * One finding per policy and table it reads, with the shortest loop and every
125
+ * role context it recurses for. A loop between two other tables that a policy
126
+ * merely passes through is reported from those tables' own policies, not
127
+ * again from every policy that reaches it.
128
+ */
129
+ export declare function findPolicyRecursion(policies: readonly RecursionPolicy[], options?: PolicyRecursionOptions): PolicyRecursion[];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rebasepro/rls-check",
3
- "version": "0.31.0",
3
+ "version": "0.32.0",
4
4
  "description": "Audit Row-Level Security on any PostgreSQL database — finds tables served without RLS, permissive tautologies, unqualified columns in policy subqueries, RLS-bypassing views and unprotected junction tables. Read-only.",
5
5
  "keywords": [
6
6
  "postgres",