@rebasepro/rls-check 0.23.0 → 0.24.1-canary.g56af4a4
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/README.md +12 -12
- package/dist/checks/anonymous-write-allowed.d.ts +3 -2
- package/dist/checks/current-setting-throws.d.ts +6 -0
- package/dist/checks/index.d.ts +5 -0
- package/dist/checks/policy-always-true.d.ts +18 -1
- package/dist/checks/policy-anonymous-tautology.d.ts +52 -1
- package/dist/checks/policy-authenticated-tautology.d.ts +6 -0
- package/dist/checks/sql.d.ts +12 -5
- package/dist/checks/unqualified-column-in-subquery.d.ts +15 -5
- package/dist/checks/util.d.ts +48 -9
- package/dist/checks/view-bypasses-rls.d.ts +14 -0
- package/dist/index.es.js +883 -121
- package/dist/index.es.js.map +1 -1
- package/dist/introspect.d.ts +39 -1
- package/dist/types.d.ts +12 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -16,7 +16,7 @@ It also takes the connection string as an argument, but prefer not to: npm echoe
|
|
|
16
16
|
|
|
17
17
|
It works against Supabase, Neon, RDS, Cloud SQL, Postgres in a container on your laptop, or anything else that speaks the wire protocol. It knows nothing about your framework and asks nothing of your codebase.
|
|
18
18
|
|
|
19
|
-
**It is read-only.** It issues `SELECT`s against the system catalogs
|
|
19
|
+
**It is read-only.** It sets two options for its own session, `statement_timeout` and `default_transaction_read_only = on`, and then issues `SELECT`s against the system catalogs (`pg_class` and its ACLs, `pg_policies`, `pg_proc`, `pg_roles` …) and nothing else. It writes nothing, changes nothing beyond its own session, and sends nothing anywhere. There is no telemetry, no upload, and no network access beyond the connection you give it.
|
|
20
20
|
|
|
21
21
|
---
|
|
22
22
|
|
|
@@ -28,7 +28,7 @@ The failures that make a Postgres database leak in practice, rather than the one
|
|
|
28
28
|
- policies that look like access control but evaluate to `true` for every row;
|
|
29
29
|
- `auth.uid() IS NOT NULL`-shaped policies, which separate signed-in from signed-out callers and scope nothing;
|
|
30
30
|
- views and materialized views that read past the RLS on their base tables because they run as their owner;
|
|
31
|
-
- a bare column inside an `EXISTS` subquery that Postgres silently binds to the *inner* table, turning a tenant filter into a tautology;
|
|
31
|
+
- a bare column inside an `EXISTS` subquery that Postgres silently binds to the *inner* table, turning a tenant filter into a tautology — which the catalog stores as the column compared with itself;
|
|
32
32
|
- many-to-many join tables left unprotected between two protected endpoints — the whole edge list, readable;
|
|
33
33
|
- `SECURITY DEFINER` routines with an unpinned `search_path`;
|
|
34
34
|
- `GRANT`s to `PUBLIC`, and policies pointed at roles nothing can connect as.
|
|
@@ -144,7 +144,7 @@ rls-check is free and maintained by the team behind Rebase — https://rebase.pr
|
|
|
144
144
|
|
|
145
145
|
That run found nothing heuristic. When it does, the heuristic findings go in a `WORTH CHECKING` section of their own, after the confident ones — mixing "this table is public" with "this might be a join table" is how a scanner teaches people to ignore it.
|
|
146
146
|
|
|
147
|
-
The `Exposed` line is worth reading before the findings:
|
|
147
|
+
The `Exposed` line is worth reading before the findings: a check that calls a table exposed does so only when one of those roles can reach it — holds a privilege on it, and `USAGE` on its schema (an unconditional policy one `GRANT` short of that is reported as latent `info`, not as an exposure) — so if the role your application connects as is not listed, name it with `--role` and run again.
|
|
148
148
|
|
|
149
149
|
## The checks
|
|
150
150
|
|
|
@@ -153,13 +153,13 @@ Run `npx @rebasepro/rls-check --list-checks` for the catalog on your installed v
|
|
|
153
153
|
| id | typical severity | confidence | what it looks for |
|
|
154
154
|
| --- | --- | --- | --- |
|
|
155
155
|
| `rls-disabled` | critical | certain | A table with RLS off that grants SELECT/INSERT/UPDATE/DELETE to a role an untrusted caller can reach, or a foreign table (which cannot have RLS) with such a grant. |
|
|
156
|
-
| `policy-always-true` | critical | certain | A permissive policy whose `USING` or `WITH CHECK` expression is always true. Downgraded to medium, and to heuristic, when `RESTRICTIVE` policies on the same command apply to every role it reaches. |
|
|
157
|
-
| `view-bypasses-rls` | critical | certain | A view granted to an untrusted role that selects from an RLS-protected table and runs with its owner's privileges. Heuristic on servers before PG15, where `security_invoker` does not exist. |
|
|
158
|
-
| `policy-anonymous-tautology` | varies | heuristic | An `auth.uid() IS NOT NULL`-shaped policy: it separates signed-in from signed-out callers and scopes no rows. Critical on Supabase
|
|
159
|
-
| `policy-authenticated-tautology` | high | heuristic | The corrected form of the above — `auth.uid() IS NOT NULL AND auth.uid() <> 'anonymous'` — which excludes signed-out callers and still scopes no rows. Every account reads every row; with open registration that is everybody. |
|
|
160
|
-
| `anonymous-write-allowed` | high | certain | A permissive INSERT/UPDATE/DELETE policy reachable without authentication whose check expression accepts any row, backed by a matching grant. |
|
|
156
|
+
| `policy-always-true` | critical | certain | A permissive policy whose `USING` or `WITH CHECK` expression is always true. High when only an UPDATE policy's `WITH CHECK` is constant: `USING` still scopes which rows it touches, and the check lets them become anything. Downgraded to medium, and to heuristic, when `RESTRICTIVE` policies on the same command apply to every role it reaches. Info, titled *latent*, when the roles it names cannot reach the table yet (no privilege for the command, or no `USAGE` on the schema): one `GRANT` away from critical. |
|
|
157
|
+
| `view-bypasses-rls` | critical | certain | A view granted to an untrusted role that selects from an RLS-protected table and runs with its owner's privileges. Heuristic on servers before PG15, where `security_invoker` does not exist, and when the view's owner is itself subject to the base table's policies. |
|
|
158
|
+
| `policy-anonymous-tautology` | varies | heuristic | An `auth.uid() IS NOT NULL`-shaped policy: it separates signed-in from signed-out callers and scopes no rows. Critical when a signed-out caller still has a value there — Rebase's and PostgREST's sentinel id, or `auth.role()` / `auth.jwt()` on Supabase, which the anon key fills; low for `auth.uid()` on Supabase, which is NULL for a signed-out caller; medium where the scan cannot tell. |
|
|
159
|
+
| `policy-authenticated-tautology` | high | heuristic | The corrected form of the above — `auth.uid() IS NOT NULL AND auth.uid() <> 'anonymous'` — which excludes signed-out callers and still scopes no rows. Every account reads every row; with open registration that is everybody. Rebase's `policy.registered()` alone (which also excludes guests) is this shape too. So is a role test alone — `auth.role() = 'authenticated'`, or the `role` claim of `auth.jwt()` — reported at `policy-always-true`'s severity (critical), since it is `TO authenticated USING (true)` written as an expression. |
|
|
160
|
+
| `anonymous-write-allowed` | high | certain | A permissive INSERT/UPDATE/DELETE policy reachable without authentication whose check expression accepts any row, backed by a matching grant. A policy for a role that `anon` inherits counts. Heuristic on Rebase when only `rebase_user` reaches it: whether a signed-out caller runs as that role is the API's `AUTH_REQUIRE` setting, which the database cannot see. |
|
|
161
161
|
| `matview-bypasses-rls` | high | certain | A materialized view granted to an untrusted role whose defining query reads an RLS-protected table. Materialized views have no `security_invoker`. |
|
|
162
|
-
| `unqualified-column-in-subquery` | high | heuristic | A bare column name in an `EXISTS`/`IN` subquery that exists on both the inner relation and the policy's own table, so Postgres binds it to the inner one. |
|
|
162
|
+
| `unqualified-column-in-subquery` | high | heuristic | A bare column name in an `EXISTS`/`IN` subquery that exists on both the inner relation and the policy's own table, so Postgres binds it to the inner one. Postgres stores it with every column qualified, so on a live database it is found as the inner column compared with itself (`m.org_id = m.org_id`). |
|
|
163
163
|
| `junction-table-unprotected` | high | heuristic | A table that is essentially two foreign keys pointing at RLS-protected tables, with no row-level security of its own. |
|
|
164
164
|
| `grant-to-public` | medium | certain | A table or foreign-table privilege granted to `PUBLIC`, which includes roles that do not exist yet. |
|
|
165
165
|
| `rls-enabled-no-policies` | medium | certain | RLS enabled and not a single policy defined, so the table denies everything. |
|
|
@@ -216,7 +216,7 @@ If your password contains `/`, `?` or `#`, percent-encode it. Those three end th
|
|
|
216
216
|
| --- | --- |
|
|
217
217
|
| `0` | Clean, or nothing at or above `--fail-on`. |
|
|
218
218
|
| `1` | Findings at or above `--fail-on`. |
|
|
219
|
-
| `2` | The scan did not run: bad arguments, connection refused, authentication failed, timeout. |
|
|
219
|
+
| `2` | The scan did not run: bad arguments, connection refused, authentication failed, timeout. Or it was incomplete: a catalogue read failed or a check crashed (`diagnostics.degraded`). This holds even with `--fail-on none`, since an incomplete scan has no verdict. |
|
|
220
220
|
|
|
221
221
|
`1` and `2` are never conflated. A pipeline that reads a DNS failure as a clean bill of health is worse than no tool at all.
|
|
222
222
|
|
|
@@ -329,7 +329,7 @@ A stock Rebase scaffold reports three `policy-always-true` criticals on its firs
|
|
|
329
329
|
|
|
330
330
|
Findings are sorted worst-first and then by schema, object and id, so two scans of an unchanged database produce an identical file.
|
|
331
331
|
|
|
332
|
-
`exposedRoles` and `diagnostics` are part of the contract, not decoration.
|
|
332
|
+
`exposedRoles` and `diagnostics` are part of the contract, not decoration. A check that calls a table exposed does so only when one of the exposed roles can reach it, and `diagnostics.degraded` is how a consumer tells "nothing was wrong" from "the scan could not look" — `findings: []` without both is half an answer.
|
|
333
333
|
|
|
334
334
|
## What this tool does not do
|
|
335
335
|
|
|
@@ -338,7 +338,7 @@ Being clear about this is the point of the tool. It is a **static audit of the c
|
|
|
338
338
|
- **It does not execute queries as other roles.** It never connects as `anon`, never sets a JWT claim, and never tries to read a row it should not be able to read. Everything it reports is inferred from what the catalogs say, not observed.
|
|
339
339
|
- **It cannot prove a policy is correct.** Deciding whether `owner_id = auth.uid()` is the right rule for your application requires knowing your application. `rls-check` can only tell you that certain *shapes* are wrong — a policy that is always true, a view that runs as its owner, a table with RLS switched off.
|
|
340
340
|
- **A clean report is not a security certification.** It means these fifteen checks found nothing. It does not mean your authorization model is sound, your API layer enforces what it should, or your data is safe.
|
|
341
|
-
- **It recognises app roles by name, and yours may not be one of them.**
|
|
341
|
+
- **It recognises app roles by name, and yours may not be one of them.** A check calls a table exposed only when a role an untrusted caller can arrive as holds privileges on it and `USAGE` on its schema. Out of the box that means `PUBLIC`, Supabase's `anon` and `authenticated`, PostgREST's `web_anon`, and Rebase's `rebase_user`. If your application connects as `app_user`, `api` or anything else, name it — `--role app_user` — or the checks have nothing to gate on. A scan that finds a write-holding role it cannot account for says so in a `Note` rather than printing a clean report.
|
|
342
342
|
- **It does not model your API layer.** Whether a table is actually reachable depends on PostgREST, your server, or your gateway. Findings say "if this table is exposed over an API" when reachability depends on something outside the database — believe that qualifier.
|
|
343
343
|
- **It does not see what your connection sees.** Almost every connection string handed to a tool like this belongs to a superuser or a table owner, which RLS cannot constrain. That is what lets it read the true catalog; it also means the findings describe what *other* roles get. The report says so, prominently, every time it applies.
|
|
344
344
|
- **Heuristic checks produce false positives by design.** Junction-table inference and unqualified-column detection match a shape, not a proof. They are reported in a separate section for exactly that reason.
|
|
@@ -13,8 +13,9 @@ import type { Check } from "../types.js";
|
|
|
13
13
|
* correct one — satisfies condition 1 on almost every project out there.
|
|
14
14
|
* Flagging it would make this check fire on the whole ecosystem.
|
|
15
15
|
*
|
|
16
|
-
* So what is reported is the narrow, certain case: the
|
|
17
|
-
*
|
|
16
|
+
* So what is reported is the narrow, certain case: the clause that decides the
|
|
17
|
+
* command is constant-true, which in Postgres means "accept any row". An
|
|
18
|
+
* *absent* clause is the opposite — see {@link writesAcceptingAnyRow}. Policies
|
|
18
19
|
* whose expression is an anonymous *tautology* rather than a constant are the
|
|
19
20
|
* business of `policy-anonymous-tautology`, which can weigh the platform.
|
|
20
21
|
*/
|
|
@@ -13,3 +13,9 @@ import type { Check } from "../types.js";
|
|
|
13
13
|
* the check cannot see the session that will run the query.
|
|
14
14
|
*/
|
|
15
15
|
export declare const currentSettingThrows: Check;
|
|
16
|
+
/**
|
|
17
|
+
* The expression with every one-argument `current_setting(...)` given
|
|
18
|
+
* `missing_ok`. The text is `pg_policies`' deparse, which spells the argument
|
|
19
|
+
* as a literal, usually cast: `current_setting('app.tenant'::text)`.
|
|
20
|
+
*/
|
|
21
|
+
export declare function withMissingOk(expr: string): string;
|
package/dist/checks/index.d.ts
CHANGED
|
@@ -13,6 +13,11 @@ export interface RunOptions {
|
|
|
13
13
|
only?: string[];
|
|
14
14
|
/** Skip these check ids. */
|
|
15
15
|
skip?: string[];
|
|
16
|
+
/**
|
|
17
|
+
* Told about every check that threw. Its findings are missing, which looks
|
|
18
|
+
* exactly like a clean result unless the caller says so.
|
|
19
|
+
*/
|
|
20
|
+
onCheckError?: (checkId: string, error: unknown) => void;
|
|
16
21
|
}
|
|
17
22
|
/**
|
|
18
23
|
* Run every selected check and return the findings, worst first.
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Check } from "../types.js";
|
|
1
|
+
import type { Check, DbPolicy, DbSnapshot, Severity } from "../types.js";
|
|
2
2
|
/**
|
|
3
3
|
* A PERMISSIVE policy whose expression is a constant truth, targeted at a role
|
|
4
4
|
* an untrusted caller reaches.
|
|
@@ -15,3 +15,20 @@ import type { Check } from "../types.js";
|
|
|
15
15
|
* cover the same rows).
|
|
16
16
|
*/
|
|
17
17
|
export declare const policyAlwaysTrue: Check;
|
|
18
|
+
/**
|
|
19
|
+
* How bad an unconditional clause on `policy` is, by the command it governs and
|
|
20
|
+
* the RESTRICTIVE policies that gate it.
|
|
21
|
+
*
|
|
22
|
+
* Exported because `USING (true)` is not the only way to write it:
|
|
23
|
+
* `policy-authenticated-tautology` grades `auth.role() = 'authenticated'` —
|
|
24
|
+
* the same grant, spelled as an expression — by this same rule, so the two
|
|
25
|
+
* spellings cannot drift apart.
|
|
26
|
+
*/
|
|
27
|
+
export declare function unconditionalReading(snapshot: DbSnapshot, policy: DbPolicy, clauses: string[]): {
|
|
28
|
+
severity: Severity;
|
|
29
|
+
gate: {
|
|
30
|
+
name: string;
|
|
31
|
+
verb: string;
|
|
32
|
+
} | null;
|
|
33
|
+
checkOnly: boolean;
|
|
34
|
+
};
|
|
@@ -9,7 +9,10 @@ import type { Check } from "../types.js";
|
|
|
9
9
|
* - Supabase: `auth.uid()` reads a JWT claim and returns NULL for an anonymous
|
|
10
10
|
* caller, so the expression is a legitimate "signed in" test. Its only real
|
|
11
11
|
* failing is that it does not scope rows to their owner — worth `low`, worded
|
|
12
|
-
* as a design observation rather than a vulnerability.
|
|
12
|
+
* as a design observation rather than a vulnerability. That is a fact about
|
|
13
|
+
* `auth.uid()`, not about Supabase: an anonymous caller there still carries
|
|
14
|
+
* the anon key's JWT, so `auth.role()` and `auth.jwt()` are non-null for it,
|
|
15
|
+
* and the same shape built on them is a bypass — see {@link CallerCall}.
|
|
13
16
|
* - Rebase / PostgREST-style stacks that coerce a missing id to a sentinel
|
|
14
17
|
* (`'anonymous'`, `''`): the expression is true for signed-out callers, so it
|
|
15
18
|
* is a straight authentication bypass. This exact policy shipped in this
|
|
@@ -22,9 +25,34 @@ import type { Check } from "../types.js";
|
|
|
22
25
|
* {@link matchTautology}.
|
|
23
26
|
*/
|
|
24
27
|
export declare const policyAnonymousTautology: Check;
|
|
28
|
+
/**
|
|
29
|
+
* What a caller call hands a signed-out request, which is what decides whether
|
|
30
|
+
* `<call> IS NOT NULL` keeps that request out.
|
|
31
|
+
*/
|
|
32
|
+
export interface CallerCall {
|
|
33
|
+
/**
|
|
34
|
+
* On Supabase, a signed-out request carries the project's anon key — a real
|
|
35
|
+
* JWT with `role: "anon"` and no user in it. So:
|
|
36
|
+
*
|
|
37
|
+
* - `null`: the call reads something that JWT does not carry (`auth.uid()`
|
|
38
|
+
* reads `sub`), and a null test on it does exclude signed-out callers;
|
|
39
|
+
* - `present`: the call reads something it does carry, and a null test on
|
|
40
|
+
* it excludes nobody;
|
|
41
|
+
* - `unknown`: Supabase does not set it, so whatever does decides.
|
|
42
|
+
*/
|
|
43
|
+
onSupabase: "null" | "present" | "unknown";
|
|
44
|
+
/** What the call returns for that request, in prose, when it is `present`. */
|
|
45
|
+
signedOutValue?: string;
|
|
46
|
+
/** What the call names — "id", "role" — for the sentence about its sentinels. */
|
|
47
|
+
noun: string;
|
|
48
|
+
/** Values a signed-out caller arrives with, so a guard excluding one is real. */
|
|
49
|
+
sentinels: string[];
|
|
50
|
+
}
|
|
25
51
|
export interface TautologyMatch {
|
|
26
52
|
/** How to name the caller-id expression in prose, e.g. `auth.uid()`. */
|
|
27
53
|
shape: string;
|
|
54
|
+
/** What that call hands a signed-out request. */
|
|
55
|
+
call: CallerCall;
|
|
28
56
|
/** Literals the policy excludes that exclude nobody. Empty for a bare null test. */
|
|
29
57
|
decoyGuards: string[];
|
|
30
58
|
/**
|
|
@@ -43,6 +71,13 @@ export interface TautologyMatch {
|
|
|
43
71
|
* here, which is why the policy that actually shipped passed silently.
|
|
44
72
|
*/
|
|
45
73
|
guardsSentinel: boolean;
|
|
74
|
+
/**
|
|
75
|
+
* Whether the clause also keeps guests out — `NOT rebase.is_anonymous()`,
|
|
76
|
+
* the conjunct `policy.registered()` adds to `authenticated()`. It scopes
|
|
77
|
+
* no rows, so it does not change which finding this is; only the sentence
|
|
78
|
+
* that describes who is admitted.
|
|
79
|
+
*/
|
|
80
|
+
excludesGuests: boolean;
|
|
46
81
|
}
|
|
47
82
|
/**
|
|
48
83
|
* Recognise "the policy tests that a caller id exists, and nothing that narrows
|
|
@@ -69,3 +104,19 @@ export interface TautologyMatch {
|
|
|
69
104
|
* because it is the one that survives code review.
|
|
70
105
|
*/
|
|
71
106
|
export declare function callerIdOnlyClause(clause: string | null | undefined): TautologyMatch | null;
|
|
107
|
+
/**
|
|
108
|
+
* `auth.role() = 'authenticated'` — and nothing that narrows which rows.
|
|
109
|
+
*
|
|
110
|
+
* That is `TO authenticated USING (true)` written as an expression: every
|
|
111
|
+
* signed-in caller passes it, for every row. The policy-level spelling was
|
|
112
|
+
* graded critical by `policy-always-true` while this one gave no finding at
|
|
113
|
+
* all, which is the gap a scanner must not have. The same two rules as
|
|
114
|
+
* {@link callerIdOnlyClause}: every conjunct must be accounted for (the role
|
|
115
|
+
* test, or the guest exclusion, which scopes nothing either), and any `OR`
|
|
116
|
+
* means the shape no longer describes what the policy admits.
|
|
117
|
+
*
|
|
118
|
+
* Returns how to name the role read in prose, or `null`.
|
|
119
|
+
*/
|
|
120
|
+
export declare function signedInRoleOnlyClause(clause: string | null | undefined): {
|
|
121
|
+
shape: string;
|
|
122
|
+
} | null;
|
|
@@ -23,5 +23,11 @@ import type { Check } from "../types.js";
|
|
|
23
23
|
* different fixes and different severities have to be two ids. Someone who has
|
|
24
24
|
* decided their `countries` table really is world-readable should be able to
|
|
25
25
|
* silence that without also silencing "signed-out callers can read it".
|
|
26
|
+
*
|
|
27
|
+
* The same id also covers `auth.role() = 'authenticated'` (and the `role`
|
|
28
|
+
* claim of `auth.jwt()`, either way round): a role test that says "signed in"
|
|
29
|
+
* and nothing about rows. That one is graded by `policy-always-true`'s rule,
|
|
30
|
+
* not `high`, because it is `TO authenticated USING (true)` spelled as an
|
|
31
|
+
* expression, and the two spellings must not get different answers.
|
|
26
32
|
*/
|
|
27
33
|
export declare const policyAuthenticatedTautology: Check;
|
package/dist/checks/sql.d.ts
CHANGED
|
@@ -9,7 +9,8 @@
|
|
|
9
9
|
* Everything it reads comes from `pg_policies.qual` / `with_check`, which is
|
|
10
10
|
* Postgres's own re-rendering of the parse tree rather than the SQL anyone
|
|
11
11
|
* typed: parenthesised more heavily, casts made explicit, and — importantly for
|
|
12
|
-
* the unqualified-column check —
|
|
12
|
+
* the unqualified-column check — every column inside a subquery qualified, so a
|
|
13
|
+
* bare name written there never reaches this scanner from a live catalog. A hit
|
|
13
14
|
* is strong evidence and a miss proves nothing, which is what the checks say.
|
|
14
15
|
*/
|
|
15
16
|
export type TokenKind = "ident" | "string" | "number" | "op" | "punct";
|
|
@@ -44,10 +45,16 @@ export declare function qualifiedNameEndingAt(tokens: SqlToken[], i: number): st
|
|
|
44
45
|
/**
|
|
45
46
|
* Does this expression normalize to an unconditional truth?
|
|
46
47
|
*
|
|
47
|
-
* Strictly structural: parens and
|
|
48
|
-
*
|
|
49
|
-
* `(is_public = true AND owner = auth.uid())`, which is exactly the
|
|
50
|
-
* false positive this tool cannot afford.
|
|
48
|
+
* Strictly structural: parens and casts are dropped, and the expression is
|
|
49
|
+
* folded only where every part is a constant. A substring match on `true`
|
|
50
|
+
* would flag `(is_public = true AND owner = auth.uid())`, which is exactly the
|
|
51
|
+
* kind of false positive this tool cannot afford.
|
|
52
|
+
*
|
|
53
|
+
* Folded: `true`, `NOT false`, a constant compared with a constant (`1 = 1`,
|
|
54
|
+
* `1 > 0`, `'a'::character varying = 'a'::character varying`), and `AND` /
|
|
55
|
+
* `OR` over those — `true OR x` is true whatever `x` is. Only `true`, `1 = 1`
|
|
56
|
+
* and a one-word cast were, so `NOT false`, `1 > 0` and `true OR …` passed as
|
|
57
|
+
* scoped policies.
|
|
51
58
|
*/
|
|
52
59
|
export declare function isUnconditionalTrue(expr: string | null | undefined): boolean;
|
|
53
60
|
/** Words that are never a column reference in a policy predicate. */
|
|
@@ -24,10 +24,20 @@ import type { Check } from "../types.js";
|
|
|
24
24
|
* flagging it merely because the outer table also has a `user_id` would fire
|
|
25
25
|
* on a large fraction of correct policies.
|
|
26
26
|
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
* it
|
|
30
|
-
*
|
|
31
|
-
*
|
|
27
|
+
* What a live database hands this check is not what anyone typed.
|
|
28
|
+
* `pg_policies.qual` is Postgres's re-rendering of the parse tree, and inside a
|
|
29
|
+
* subquery it qualifies *every* column — so the bare name is never in the text.
|
|
30
|
+
* The bare-name scan below only ever fires on text that did not come out of the
|
|
31
|
+
* catalog. Against a real database the mistake shows up as its effect: the bare
|
|
32
|
+
* `organization_id` bound to the inner relation, so the stored comparison reads
|
|
33
|
+
* `m.organization_id = m.organization_id` — a column compared with itself. That
|
|
34
|
+
* shape is what {@link selfComparisons} looks for, and it is the half of this
|
|
35
|
+
* check that can fire on a scan. It was the half missing until it was noticed
|
|
36
|
+
* that the check could not fire at all; the e2e suite now holds it to that.
|
|
37
|
+
*
|
|
38
|
+
* Confidence is always heuristic, and an absence still proves nothing: a bare
|
|
39
|
+
* name compared with a *different* inner column (`organization_id = id`) comes
|
|
40
|
+
* back as `m.organization_id = m.id`, which is indistinguishable from a
|
|
41
|
+
* comparison somebody meant.
|
|
32
42
|
*/
|
|
33
43
|
export declare const unqualifiedColumnInSubquery: Check;
|
package/dist/checks/util.d.ts
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* noise, and noise is what gets a tool like this uninstalled — so the helpers
|
|
7
7
|
* below err towards "no" whenever the catalog is ambiguous.
|
|
8
8
|
*/
|
|
9
|
-
import type { DbGrant, DbPolicy, DbRelation, DbSnapshot, Finding, Severity } from "../types.js";
|
|
9
|
+
import type { DbForeignKey, DbGrant, DbPolicy, DbRelation, DbRole, DbSnapshot, Finding, PolicyCommand, Severity } from "../types.js";
|
|
10
10
|
export declare const SEVERITY_ORDER: Severity[];
|
|
11
11
|
export declare const docsFor: (id: string) => string;
|
|
12
12
|
export type Privilege = DbGrant["privileges"][number];
|
|
@@ -17,6 +17,12 @@ export declare const WRITE_PRIVILEGES: Privilege[];
|
|
|
17
17
|
export declare const ANONYMOUS_ROLES: string[];
|
|
18
18
|
export declare const isPublicRole: (role: string) => boolean;
|
|
19
19
|
export declare const sameRole: (a: string, b: string) => boolean;
|
|
20
|
+
/** Every grant on one relation, in snapshot order. */
|
|
21
|
+
export declare function grantsOn(snapshot: DbSnapshot, schema: string, table: string): readonly DbGrant[];
|
|
22
|
+
/** The foreign keys declared on one table, in snapshot order. */
|
|
23
|
+
export declare function foreignKeysOf(snapshot: DbSnapshot, schema: string, table: string): readonly DbForeignKey[];
|
|
24
|
+
/** A role by name, compared the way {@link sameRole} compares. */
|
|
25
|
+
export declare function roleNamed(snapshot: DbSnapshot, name: string): DbRole | undefined;
|
|
20
26
|
/**
|
|
21
27
|
* Every role whose grants `role` actually receives: itself, PUBLIC, and the
|
|
22
28
|
* transitive closure of its memberships.
|
|
@@ -27,7 +33,19 @@ export declare const sameRole: (a: string, b: string) => boolean;
|
|
|
27
33
|
* checker that only compares grantee names.
|
|
28
34
|
*/
|
|
29
35
|
export declare function rolesUsableBy(snapshot: DbSnapshot, role: string): Set<string>;
|
|
30
|
-
/**
|
|
36
|
+
/**
|
|
37
|
+
* Can `role` name objects in `schema` at all?
|
|
38
|
+
*
|
|
39
|
+
* Without USAGE on the schema a role gets "permission denied for schema" before
|
|
40
|
+
* any table privilege is looked at, so a grant on a table there reaches
|
|
41
|
+
* nothing. Unknown — the read failed, or the schema has no record — counts as
|
|
42
|
+
* yes: a scanner that cannot tell must report, not stay quiet.
|
|
43
|
+
*/
|
|
44
|
+
export declare function hasSchemaUsage(snapshot: DbSnapshot, schema: string, role: string): boolean;
|
|
45
|
+
/**
|
|
46
|
+
* Privileges `role` effectively holds on a relation, memberships included —
|
|
47
|
+
* and none at all when it cannot use the relation's schema.
|
|
48
|
+
*/
|
|
31
49
|
export declare function effectivePrivileges(snapshot: DbSnapshot, schema: string, table: string, role: string): Set<Privilege>;
|
|
32
50
|
/**
|
|
33
51
|
* Which exposed roles hold at least one of `wanted` on this relation, and which.
|
|
@@ -40,6 +58,19 @@ export declare function exposedGrantees(snapshot: DbSnapshot, schema: string, ta
|
|
|
40
58
|
}[];
|
|
41
59
|
/** Does this policy's TO list name a role an untrusted caller arrives as? */
|
|
42
60
|
export declare function policyTargetsExposedRole(snapshot: DbSnapshot, policy: DbPolicy): string[];
|
|
61
|
+
/** The privileges a policy's command is exercised with. */
|
|
62
|
+
export declare const COMMAND_PRIVILEGES: Record<PolicyCommand, Privilege[]>;
|
|
63
|
+
/**
|
|
64
|
+
* The exposed callers a policy applies to *and* that can reach its table for
|
|
65
|
+
* its command: {@link policyTargetsExposedRole}, kept to the roles holding a
|
|
66
|
+
* privilege the command needs (and USAGE on the schema).
|
|
67
|
+
*
|
|
68
|
+
* A policy nobody can reach the table through is a latent problem, not an
|
|
69
|
+
* exposure, and every check claims an exposure: "a caller can read every row"
|
|
70
|
+
* is false for a role Postgres answers "permission denied". `PUBLIC` stays when
|
|
71
|
+
* the policy is TO PUBLIC and any exposed role reaches the table.
|
|
72
|
+
*/
|
|
73
|
+
export declare function policyReachedBy(snapshot: DbSnapshot, policy: DbPolicy): string[];
|
|
43
74
|
export declare const TABLE_KINDS: DbRelation["kind"][];
|
|
44
75
|
export declare const isTable: (r: DbRelation) => boolean;
|
|
45
76
|
export declare function scannedTables(snapshot: DbSnapshot): DbRelation[];
|
|
@@ -54,18 +85,26 @@ export declare const hasColumn: (r: DbRelation, name: string) => boolean;
|
|
|
54
85
|
/** `≈ 12,000 rows` — only ever used to convey blast radius, never severity. */
|
|
55
86
|
export declare function rowsPhrase(rel: DbRelation | undefined): string;
|
|
56
87
|
/**
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
-
* create a table, a role or a policy, and every fix is printed to be pasted
|
|
61
|
-
* into psql. Doubling `"` keeps a name inside its quotes; a name with a line
|
|
62
|
-
* break in it is written as a Unicode-escape identifier (`U&"a\000Ab"`), which
|
|
63
|
-
* names the same object and has no line break to escape a comment with.
|
|
88
|
+
* The column a printed fix should scope rows by, when the table has one that
|
|
89
|
+
* looks like the owner's id. Fixes named `user_id` on every table, and a table
|
|
90
|
+
* without one rejected the pasted SQL.
|
|
64
91
|
*/
|
|
92
|
+
export declare function ownerColumnOf(rel: DbRelation | undefined): string | undefined;
|
|
65
93
|
export declare function qi(ident: string): string;
|
|
66
94
|
export declare const qrel: (schema: string, name: string) => string;
|
|
67
95
|
/** PUBLIC is a keyword, not an identifier — quoting it changes what it means. */
|
|
68
96
|
export declare const qrole: (role: string) => string;
|
|
97
|
+
/**
|
|
98
|
+
* The REVOKEs that take `privileges` on a relation away from the exposed roles:
|
|
99
|
+
* one per grant that reaches them, made to the role the grant names.
|
|
100
|
+
*
|
|
101
|
+
* Revoking from the exposed role by name is not the same thing. A grant to
|
|
102
|
+
* `app_reader` that `anon` inherits survives `REVOKE … FROM anon`, which is a
|
|
103
|
+
* no-op, and a relation granted to `anon` and `authenticated` keeps the second
|
|
104
|
+
* grant when the fix names only the first. Every fix that takes a grant away
|
|
105
|
+
* builds it here, so applying it and rescanning clears the finding.
|
|
106
|
+
*/
|
|
107
|
+
export declare function revokesReaching(snapshot: DbSnapshot, schema: string, table: string, exposed: string[], privileges: Privilege[]): string[];
|
|
69
108
|
export declare function finding(f: Omit<Finding, "docs"> & {
|
|
70
109
|
docs?: string;
|
|
71
110
|
}): Finding;
|
|
@@ -1,6 +1,14 @@
|
|
|
1
1
|
import type { Check, DbRelation, DbSnapshot, DbView } from "../types.js";
|
|
2
2
|
/** Base relations of `view` that have RLS turned on. */
|
|
3
3
|
export declare function protectedBaseTables(snapshot: DbSnapshot, view: DbView): DbRelation[];
|
|
4
|
+
/**
|
|
5
|
+
* Whether `owner` reads `base` past its policies: as a superuser, with
|
|
6
|
+
* BYPASSRLS, or as the table's owner (a member of the owning role counts, as
|
|
7
|
+
* Postgres counts it) while FORCE ROW LEVEL SECURITY is off. A role the
|
|
8
|
+
* catalog read did not return is assumed to — the scan must not go quiet on
|
|
9
|
+
* what it could not see.
|
|
10
|
+
*/
|
|
11
|
+
export declare function ownerReadsPast(snapshot: DbSnapshot, owner: string, base: DbRelation): boolean;
|
|
4
12
|
/**
|
|
5
13
|
* A view granted to an untrusted role that reads an RLS-protected table without
|
|
6
14
|
* `security_invoker`.
|
|
@@ -14,5 +22,11 @@ export declare function protectedBaseTables(snapshot: DbSnapshot, view: DbView):
|
|
|
14
22
|
* Before PG 15 the option does not exist, which means every view behaves this
|
|
15
23
|
* way. That is worth saying, but it is not evidence that *this* view is a
|
|
16
24
|
* mistake, so those findings are marked heuristic.
|
|
25
|
+
*
|
|
26
|
+
* The owner is *normally* exempt, not always. A view owned by a role the base
|
|
27
|
+
* tables' policies do apply to returns what those policies give that role —
|
|
28
|
+
* to every caller alike, which may be nothing at all. Still reported, because
|
|
29
|
+
* the rows do not depend on who asks, but heuristic: whether it hands out more
|
|
30
|
+
* than a caller should see depends on the owner's policies.
|
|
17
31
|
*/
|
|
18
32
|
export declare const viewBypassesRls: Check;
|