@agentic-kit/db-tools 0.12.47 → 0.13.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.
- package/README.md +18 -4
- package/access/findings.d.ts +93 -0
- package/access/findings.js +434 -0
- package/access/index.d.ts +5 -0
- package/access/index.js +27 -0
- package/access/policies.d.ts +142 -0
- package/access/policies.js +338 -0
- package/access/provisioning.d.ts +121 -0
- package/access/provisioning.js +381 -0
- package/access/rule-check.d.ts +52 -0
- package/access/rule-check.js +307 -0
- package/access/words.d.ts +38 -0
- package/access/words.js +530 -0
- package/app-workspace.d.ts +1 -0
- package/app-workspace.js +7 -9
- package/blueprint/blueprint-defaults.d.ts +9 -0
- package/blueprint/blueprint-defaults.js +77 -0
- package/blueprint/blueprint-schema.d.ts +68 -0
- package/blueprint/blueprint-schema.js +139 -0
- package/blueprint/field-type.d.ts +4 -0
- package/blueprint/field-type.js +100 -0
- package/blueprint/internal-policies.d.ts +1 -0
- package/blueprint/internal-policies.js +10 -0
- package/blueprint-construction.d.ts +10 -6
- package/blueprint-construction.js +34 -25
- package/context.d.ts +15 -0
- package/context.js +132 -69
- package/db-probe.d.ts +1 -1
- package/db-probe.js +19 -4
- package/esm/access/findings.d.ts +93 -0
- package/esm/access/findings.js +428 -0
- package/esm/access/index.d.ts +5 -0
- package/esm/access/index.js +5 -0
- package/esm/access/policies.d.ts +142 -0
- package/esm/access/policies.js +315 -0
- package/esm/access/provisioning.d.ts +121 -0
- package/esm/access/provisioning.js +375 -0
- package/esm/access/rule-check.d.ts +52 -0
- package/esm/access/rule-check.js +297 -0
- package/esm/access/words.d.ts +38 -0
- package/esm/access/words.js +524 -0
- package/esm/app-workspace.d.ts +1 -0
- package/esm/app-workspace.js +7 -10
- package/esm/blueprint/blueprint-defaults.d.ts +9 -0
- package/esm/blueprint/blueprint-defaults.js +74 -0
- package/esm/blueprint/blueprint-schema.d.ts +68 -0
- package/esm/blueprint/blueprint-schema.js +134 -0
- package/esm/blueprint/field-type.d.ts +4 -0
- package/esm/blueprint/field-type.js +95 -0
- package/esm/blueprint/internal-policies.d.ts +1 -0
- package/esm/blueprint/internal-policies.js +7 -0
- package/esm/blueprint-construction.d.ts +10 -6
- package/esm/blueprint-construction.js +32 -24
- package/esm/context.d.ts +15 -0
- package/esm/context.js +133 -70
- package/esm/db-probe.d.ts +1 -1
- package/esm/db-probe.js +21 -6
- package/esm/failure.d.ts +89 -0
- package/esm/failure.js +362 -0
- package/esm/gate.d.ts +6 -4
- package/esm/gate.js +42 -14
- package/esm/graphql-http.d.ts +27 -6
- package/esm/graphql-http.js +96 -24
- package/esm/host.d.ts +33 -8
- package/esm/host.js +42 -2
- package/esm/index.d.ts +22 -12
- package/esm/index.js +21 -39
- package/esm/provision-database/env-file.d.ts +7 -0
- package/esm/provision-database/env-file.js +30 -0
- package/esm/provision-database/preset-match.js +1 -2
- package/esm/provision-database/provision.d.ts +21 -4
- package/esm/provision-database/provision.js +330 -85
- package/esm/provision-database/request-database.d.ts +22 -3
- package/esm/provision-database/request-database.js +46 -13
- package/esm/ready.d.ts +20 -0
- package/esm/ready.js +45 -0
- package/esm/run-codegen/barrels.js +1 -1
- package/esm/run-codegen/endpoints.js +5 -6
- package/esm/schema-model/normalize.js +1 -1
- package/esm/schema-model.js +4 -5
- package/esm/schema-resolve.d.ts +50 -0
- package/esm/schema-resolve.js +200 -14
- package/esm/table-access.d.ts +116 -0
- package/esm/table-access.js +418 -0
- package/esm/tools/access.d.ts +218 -0
- package/esm/tools/access.js +651 -0
- package/esm/tools/add-policies.d.ts +44 -12
- package/esm/tools/add-policies.js +84 -84
- package/esm/tools/add-records.d.ts +23 -2
- package/esm/tools/add-records.js +113 -75
- package/esm/tools/add-relation-schema.d.ts +5 -5
- package/esm/tools/add-relation-schema.js +13 -14
- package/esm/tools/add-relation.d.ts +11 -2
- package/esm/tools/add-relation.js +68 -31
- package/esm/tools/create-api-key.d.ts +18 -4
- package/esm/tools/create-api-key.js +105 -54
- package/esm/tools/describe-schema.d.ts +5 -7
- package/esm/tools/describe-schema.js +41 -118
- package/esm/tools/entity-types.d.ts +54 -0
- package/esm/tools/entity-types.js +233 -0
- package/esm/tools/index.d.ts +26 -0
- package/esm/tools/index.js +151 -0
- package/esm/tools/mutations.d.ts +36 -7
- package/esm/tools/mutations.js +245 -99
- package/esm/tools/provision-blueprint.d.ts +25 -1
- package/esm/tools/provision-blueprint.js +184 -42
- package/esm/tools/provision-database.d.ts +7 -5
- package/esm/tools/provision-database.js +31 -17
- package/esm/tools/run-codegen.d.ts +27 -4
- package/esm/tools/run-codegen.js +143 -76
- package/esm/tools/templates.d.ts +30 -7
- package/esm/tools/templates.js +166 -95
- package/failure.d.ts +89 -0
- package/failure.js +375 -0
- package/gate.d.ts +6 -4
- package/gate.js +41 -13
- package/graphql-http.d.ts +27 -6
- package/graphql-http.js +105 -25
- package/host.d.ts +33 -8
- package/host.js +45 -2
- package/index.d.ts +22 -12
- package/index.js +86 -43
- package/package.json +6 -5
- package/provision-database/env-file.d.ts +7 -0
- package/provision-database/env-file.js +32 -1
- package/provision-database/preset-match.js +1 -2
- package/provision-database/provision.d.ts +21 -4
- package/provision-database/provision.js +327 -81
- package/provision-database/request-database.d.ts +22 -3
- package/provision-database/request-database.js +49 -14
- package/ready.d.ts +20 -0
- package/ready.js +52 -0
- package/run-codegen/barrels.js +1 -1
- package/run-codegen/endpoints.js +5 -6
- package/schema-model/normalize.js +4 -4
- package/schema-model.js +4 -5
- package/schema-resolve.d.ts +50 -0
- package/schema-resolve.js +209 -14
- package/table-access.d.ts +116 -0
- package/table-access.js +437 -0
- package/tools/access.d.ts +218 -0
- package/tools/access.js +669 -0
- package/tools/add-policies.d.ts +44 -12
- package/tools/add-policies.js +86 -84
- package/tools/add-records.d.ts +23 -2
- package/tools/add-records.js +113 -74
- package/tools/add-relation-schema.d.ts +5 -5
- package/tools/add-relation-schema.js +13 -14
- package/tools/add-relation.d.ts +11 -2
- package/tools/add-relation.js +69 -31
- package/tools/create-api-key.d.ts +18 -4
- package/tools/create-api-key.js +104 -53
- package/tools/describe-schema.d.ts +5 -7
- package/tools/describe-schema.js +40 -117
- package/tools/entity-types.d.ts +54 -0
- package/tools/entity-types.js +240 -0
- package/tools/index.d.ts +26 -0
- package/tools/index.js +154 -0
- package/tools/mutations.d.ts +36 -7
- package/tools/mutations.js +252 -101
- package/tools/provision-blueprint.d.ts +25 -1
- package/tools/provision-blueprint.js +186 -43
- package/tools/provision-database.d.ts +7 -5
- package/tools/provision-database.js +33 -18
- package/tools/run-codegen.d.ts +27 -4
- package/tools/run-codegen.js +141 -73
- package/tools/templates.d.ts +30 -7
- package/tools/templates.js +167 -94
- package/esm/policy/add-policies-to-table.d.ts +0 -26
- package/esm/policy/add-policies-to-table.js +0 -66
- package/esm/policy/provision-helpers.d.ts +0 -38
- package/esm/policy/provision-helpers.js +0 -50
- package/esm/tools/manage-entity-types.d.ts +0 -40
- package/esm/tools/manage-entity-types.js +0 -201
- package/policy/add-policies-to-table.d.ts +0 -26
- package/policy/add-policies-to-table.js +0 -71
- package/policy/provision-helpers.d.ts +0 -38
- package/policy/provision-helpers.js +0 -58
- package/tools/manage-entity-types.d.ts +0 -40
- package/tools/manage-entity-types.js +0 -206
|
@@ -0,0 +1,428 @@
|
|
|
1
|
+
// Structural findings over the stored rules of one table: shapes the backend docs warn about, read from what each
|
|
2
|
+
// Authz node checks. No finding says who can or cannot reach a row: each one says what may happen, and fires when the
|
|
3
|
+
// rules leave it possible.
|
|
4
|
+
import { appliesTo, AUTHZ_NODES, effectivePolicy, exprOf, lets, OPERATIONS, part, partsFor, PRIVILEGE, PUBLIC, SIGNED_IN, sorted, treeOf, unique } from './policies';
|
|
5
|
+
import { dataNodesForPolicies, fieldsAddedByNodes, ROW_ID, TABLE_NEEDS } from './provisioning';
|
|
6
|
+
import { statesAll } from './words';
|
|
7
|
+
export const ACCESS_CHECKS = {
|
|
8
|
+
AuthzAllowAll: { person: 'anyone', always: true },
|
|
9
|
+
AuthzAppMemberOwner: { person: 'member', scope: 'app', owner: 'owner_field' },
|
|
10
|
+
AuthzAppMembership: { person: 'member', scope: 'app' },
|
|
11
|
+
AuthzColumnSecurity: { person: 'none' },
|
|
12
|
+
AuthzComposite: { person: 'mixed' },
|
|
13
|
+
AuthzDatabaseClaim: { person: 'session' },
|
|
14
|
+
AuthzDenyAll: { person: 'none' },
|
|
15
|
+
AuthzDirectOwner: { person: 'named', owner: 'entity_field' },
|
|
16
|
+
AuthzDirectOwnerAny: { person: 'named', owner: 'entity_fields' },
|
|
17
|
+
AuthzEntityMembership: { person: 'member', scope: 'row', team: 'entity_field' },
|
|
18
|
+
AuthzFilePath: { person: 'shared' },
|
|
19
|
+
AuthzHumanOnly: { person: 'session', limit: true },
|
|
20
|
+
AuthzMemberList: { person: 'named' },
|
|
21
|
+
AuthzMemberOwner: { person: 'member', scope: 'row', owner: 'owner_field', team: 'entity_field' },
|
|
22
|
+
AuthzNotReadOnly: { person: 'member', scope: 'row', team: 'entity_field', limit: true },
|
|
23
|
+
AuthzOrgHierarchy: { person: 'named' },
|
|
24
|
+
AuthzPeerOwnership: { person: 'member', scope: 'peer' },
|
|
25
|
+
AuthzPublishable: { person: 'anyone' },
|
|
26
|
+
AuthzRelatedEntityMembership: { person: 'member', scope: 'row' },
|
|
27
|
+
AuthzRelatedMemberList: { person: 'named' },
|
|
28
|
+
AuthzRelatedMemberOwner: { person: 'member', scope: 'row', owner: 'owner_field' },
|
|
29
|
+
AuthzRelatedPeerOwnership: { person: 'member', scope: 'peer' },
|
|
30
|
+
AuthzSystemOnly: { person: 'session', limit: true },
|
|
31
|
+
AuthzTemporal: { person: 'anyone' },
|
|
32
|
+
AuthzValueAllowed: { person: 'anyone' },
|
|
33
|
+
AuthzValueExists: { person: 'anyone' },
|
|
34
|
+
AuthzValueMatch: { person: 'anyone' }
|
|
35
|
+
};
|
|
36
|
+
const ANONYMOUS = 'anonymous';
|
|
37
|
+
// A field from a caller that does not give its default has a default no one knows, so what depends on it is never a
|
|
38
|
+
// guess.
|
|
39
|
+
function readableFields(fields) {
|
|
40
|
+
return fields?.map((field) => (field.default === undefined ? { ...field, default: 'unknown' } : field));
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* The columns the rules' Data nodes add that the table has with another type, another NOT NULL, or another default. A
|
|
44
|
+
* generator keeps a column the table already has as it is, so the rule then reads a column it did not make.
|
|
45
|
+
*/
|
|
46
|
+
export function columnGaps(given, policies) {
|
|
47
|
+
const fields = readableFields(given);
|
|
48
|
+
const expected = fieldsAddedByNodes(dataNodesForPolicies(policies).nodes).fields;
|
|
49
|
+
return expected.flatMap((column) => {
|
|
50
|
+
const actual = fields.find((field) => field.name === column.name);
|
|
51
|
+
if (!actual)
|
|
52
|
+
return [];
|
|
53
|
+
const gaps = [];
|
|
54
|
+
if (actual.type !== undefined && actual.type !== column.type)
|
|
55
|
+
gaps.push('type');
|
|
56
|
+
if (actual.required === column.nullable)
|
|
57
|
+
gaps.push('required');
|
|
58
|
+
if (actual.default !== 'unknown' && column.default !== 'unknown' && JSON.stringify(sorted(actual.default)) !== JSON.stringify(sorted(column.default))) {
|
|
59
|
+
gaps.push('default');
|
|
60
|
+
}
|
|
61
|
+
return gaps.length ? [{ column: column.name, node: column.node, expected: column, actual, gaps }] : [];
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
// ─── Grants ──────────────────────────────────────────────────────
|
|
65
|
+
function granted(input, role, privilege) {
|
|
66
|
+
if (!input.grants)
|
|
67
|
+
return true;
|
|
68
|
+
return input.grants
|
|
69
|
+
.filter((grant) => grant.roles.includes(role) || grant.roles.includes(PUBLIC))
|
|
70
|
+
.flatMap((grant) => grant.privileges)
|
|
71
|
+
.some((item) => String(Array.isArray(item) ? item[0] : item).toLowerCase() === privilege);
|
|
72
|
+
}
|
|
73
|
+
function rolesOf(input, parts) {
|
|
74
|
+
const named = [...parts.map((item) => item.role), ...(input.grants ?? []).flatMap((grant) => grant.roles)];
|
|
75
|
+
const roles = new Set([SIGNED_IN, ...named.filter((role) => role !== PUBLIC), ...(named.includes(PUBLIC) ? [ANONYMOUS] : [])]);
|
|
76
|
+
return [SIGNED_IN, ...[...roles].filter((role) => role !== SIGNED_IN).sort()];
|
|
77
|
+
}
|
|
78
|
+
const FINDINGS = {
|
|
79
|
+
'owner-outlives-membership': {
|
|
80
|
+
level: 'high',
|
|
81
|
+
summary: 'An owner check here does not check membership, so a person who leaves the team or the app may keep access to the rows they own.',
|
|
82
|
+
risk: 'A removed or banned member may still reach the rows that name them as their owner.',
|
|
83
|
+
fix: 'Use the owner check that also requires membership: AuthzMemberOwner for rows of a team, AuthzAppMemberOwner for rows of the whole app.'
|
|
84
|
+
},
|
|
85
|
+
'app-membership-on-team-rows': {
|
|
86
|
+
level: 'medium',
|
|
87
|
+
summary: 'App membership here may let every member of the app in, whatever team the row belongs to, on a table whose rows belong to teams.',
|
|
88
|
+
risk: 'A member of one team may reach the rows of every other team.',
|
|
89
|
+
fix: 'Check membership of the team the row names (AuthzEntityMembership), and fence the owner check with it (AuthzMemberOwner).'
|
|
90
|
+
},
|
|
91
|
+
'granted-without-policy': {
|
|
92
|
+
level: 'low',
|
|
93
|
+
summary: 'The table grants this operation, but no rule lets anyone do it, so the database refuses it.',
|
|
94
|
+
risk: 'People who expect to do it cannot.',
|
|
95
|
+
fix: 'Add a rule for this operation, or remove the grant.'
|
|
96
|
+
},
|
|
97
|
+
'granted-role-without-policy': {
|
|
98
|
+
level: 'low',
|
|
99
|
+
summary: 'The table grants this operation to a role that no rule names, so the database refuses it for that role unless the role is a member of a role a rule names; these words cannot tell which roles are members of which.',
|
|
100
|
+
risk: 'People of that role who expect to do it may not be able to.',
|
|
101
|
+
fix: 'Add a rule for this operation for that role, or for public, or remove the grant.'
|
|
102
|
+
},
|
|
103
|
+
'policies-without-row-security': {
|
|
104
|
+
level: 'high',
|
|
105
|
+
summary: 'The table has rules, but row security is off, so the rules do not apply.',
|
|
106
|
+
risk: 'Anyone the table grants an operation to can do it to every row.',
|
|
107
|
+
fix: 'Turn row security on for this table.'
|
|
108
|
+
},
|
|
109
|
+
'anyone-can-change': {
|
|
110
|
+
level: 'high',
|
|
111
|
+
summary: 'No part of the rules for this change checks who the person is, so anyone who can use the table may create, change or delete rows.',
|
|
112
|
+
risk: 'Any signed-in person may change or delete rows that are not theirs.',
|
|
113
|
+
fix: 'Use a membership or owner rule for these operations, or add a restrictive one that checks who the person is; keep a rule for anyone for read only, and only when every row is public.'
|
|
114
|
+
},
|
|
115
|
+
'limit-blocks-outside-grant': {
|
|
116
|
+
level: 'medium',
|
|
117
|
+
summary: 'A limit that requires membership may also block the people that another rule lets in on purpose without membership.',
|
|
118
|
+
risk: 'Shares, published rows or listed people outside the team may be cut off, so that rule does nothing for them.',
|
|
119
|
+
fix: 'Remove the membership limit, or require membership inside the rules that need it instead of as a limit on all of them.'
|
|
120
|
+
},
|
|
121
|
+
'write-without-read': {
|
|
122
|
+
level: 'medium',
|
|
123
|
+
summary: 'The rule for this change may let in people who cannot read the row (for create: the row they make), and a change through the API reads it, so they may be refused.',
|
|
124
|
+
risk: 'People the rule names may get an error, and the words of the rule may promise access the table does not give.',
|
|
125
|
+
fix: 'Give read the same rule as the change or a wider one, or narrow the change to the people who can read the row; grant read where the table does not.'
|
|
126
|
+
},
|
|
127
|
+
'raw-part-may-widen': {
|
|
128
|
+
level: 'high',
|
|
129
|
+
summary: 'A rule here holds a part its words may not fully state, in a place where it can let more people in (on its own, under OR, or under NOT); the words of the rule say which part, and why.',
|
|
130
|
+
risk: 'The part may let in people the words do not name, up to every signed-in person for every row.',
|
|
131
|
+
fix: 'Write the rule with Authz nodes, AND, OR and NOT, with the settings and column names its words state, so they state all of it; check the stored rule before you trust its words.'
|
|
132
|
+
},
|
|
133
|
+
'reused-column-differs': {
|
|
134
|
+
level: 'medium',
|
|
135
|
+
summary: 'A column a rule reads is not as the rule\'s Data node makes it, so the table may not behave as the rule\'s words say; the details say how.',
|
|
136
|
+
risk: 'A create may fail for lack of a value, a row may name another owner or team than the words say, or the rule may not work on the column.',
|
|
137
|
+
fix: 'Give the column the type and the default its Data node gives, or set access that adds a column of its own.'
|
|
138
|
+
},
|
|
139
|
+
'required-column-not-set': {
|
|
140
|
+
level: 'medium',
|
|
141
|
+
summary: 'A required column with no default is left from an access rule the table no longer has, so every create must send a value for it.',
|
|
142
|
+
risk: 'A create that does not send it fails, and nothing in the access of the table says why.',
|
|
143
|
+
fix: 'Give the column a default or make it optional, remove it, or set access whose rules use it again.'
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
const MAX_CLAUSES = 64;
|
|
147
|
+
const checkOf = (literal) => (literal.leaf ? ACCESS_CHECKS[literal.leaf.type] : undefined);
|
|
148
|
+
function clausesOf(expr, positive = true) {
|
|
149
|
+
if (expr.kind === 'leaf')
|
|
150
|
+
return [[{ leaf: expr, positive }]];
|
|
151
|
+
if (expr.kind === 'raw')
|
|
152
|
+
return [[{ positive }]];
|
|
153
|
+
if (expr.kind === 'not')
|
|
154
|
+
return clausesOf(expr.arg, !positive);
|
|
155
|
+
const parts = expr.args.map((arg) => clausesOf(arg, positive));
|
|
156
|
+
if (parts.some((entry) => !entry))
|
|
157
|
+
return undefined;
|
|
158
|
+
let out;
|
|
159
|
+
if ((expr.kind === 'and') === positive) {
|
|
160
|
+
out = [[]];
|
|
161
|
+
for (const entry of parts) {
|
|
162
|
+
out = out.flatMap((clause) => entry.map((other) => [...clause, ...other]));
|
|
163
|
+
if (out.length > MAX_CLAUSES)
|
|
164
|
+
return undefined;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
else {
|
|
168
|
+
out = parts.flat();
|
|
169
|
+
}
|
|
170
|
+
return out.length > MAX_CLAUSES ? undefined : out;
|
|
171
|
+
}
|
|
172
|
+
// The expression with each node its words may not fully state read as a part no Authz node describes, so that no
|
|
173
|
+
// finding rests on what those words say.
|
|
174
|
+
function readable(expr, related) {
|
|
175
|
+
if (expr.kind === 'leaf')
|
|
176
|
+
return statesAll(expr.type, expr.data, related) ? expr : { kind: 'raw' };
|
|
177
|
+
if (expr.kind === 'raw')
|
|
178
|
+
return expr;
|
|
179
|
+
if (expr.kind === 'not')
|
|
180
|
+
return { kind: 'not', arg: readable(expr.arg, related) };
|
|
181
|
+
return { kind: expr.kind, args: expr.args.map((arg) => readable(arg, related)) };
|
|
182
|
+
}
|
|
183
|
+
// A part no Authz node describes (a stored expression, a node the registry does not have), or a node its words may not
|
|
184
|
+
// fully state.
|
|
185
|
+
const unreadable = (literal) => !checkOf(literal);
|
|
186
|
+
// A part that limits who passes to people a known node names: a member, a named person or a share.
|
|
187
|
+
const namesKnownPeople = (literal) => literal.positive && ['member', 'named', 'shared'].includes(checkOf(literal)?.person);
|
|
188
|
+
// A literal that never holds: a check that lets no one in, or the negation of one that lets everyone in.
|
|
189
|
+
const never = (literal) => (literal.positive ? checkOf(literal)?.person === 'none' : checkOf(literal)?.always === true);
|
|
190
|
+
// The clauses that can hold; a tree too large to read gives none, so it fires nothing.
|
|
191
|
+
function clausesOfPart(item) {
|
|
192
|
+
return (clausesOf(item.expr) ?? []).filter((clause) => !clause.some(never));
|
|
193
|
+
}
|
|
194
|
+
// The literal limits who passes to people the rule names. A raw part or an unknown node counts as such a check, so that
|
|
195
|
+
// no finding rests on a guess.
|
|
196
|
+
function namesPeople(literal) {
|
|
197
|
+
const check = checkOf(literal);
|
|
198
|
+
if (!check)
|
|
199
|
+
return true;
|
|
200
|
+
return literal.positive && ['member', 'named', 'shared', 'mixed'].includes(check.person);
|
|
201
|
+
}
|
|
202
|
+
const memberOf = (literal, scopes) => literal.positive && checkOf(literal)?.person === 'member' && scopes.includes(checkOf(literal).scope);
|
|
203
|
+
function ownerIsRowId(literal) {
|
|
204
|
+
const owner = checkOf(literal)?.owner;
|
|
205
|
+
const column = owner ? literal.leaf.data[owner] : undefined;
|
|
206
|
+
const columns = Array.isArray(column) ? column : [column];
|
|
207
|
+
return columns.length > 0 && columns.every((name) => name === ROW_ID);
|
|
208
|
+
}
|
|
209
|
+
// An owner check with no membership in it. An owner column named id means the row is the person.
|
|
210
|
+
const bareOwner = (literal) => literal.positive && Boolean(checkOf(literal)?.owner) && checkOf(literal).person !== 'member' && !ownerIsRowId(literal);
|
|
211
|
+
// A literal that admits people with no membership on purpose: row conditions, sessions, shares, listed people. A rule
|
|
212
|
+
// that lets everyone in with no check is not one: fencing it with a limit is how a table says "only these people".
|
|
213
|
+
function letsOutsidersIn(literal) {
|
|
214
|
+
const check = checkOf(literal);
|
|
215
|
+
if (!literal.positive || !check || check.always)
|
|
216
|
+
return false;
|
|
217
|
+
return ['anyone', 'session', 'shared'].includes(check.person) || (check.person === 'named' && !check.owner);
|
|
218
|
+
}
|
|
219
|
+
const permissiveClauses = (parts) => parts.filter(lets).flatMap(clausesOfPart);
|
|
220
|
+
// A restrictive rule every clause of which meets `test` holds back everyone who fails it.
|
|
221
|
+
const fencedBy = (parts, test) => parts.some((item) => !lets(item) && clausesOf(item.expr) !== undefined && clausesOfPart(item).every((clause) => clause.some(test)));
|
|
222
|
+
// Who passes is limited to people the rules name.
|
|
223
|
+
const limitsPeople = (parts) => permissiveClauses(parts).every((clause) => clause.some(namesPeople)) || fencedBy(parts, namesPeople);
|
|
224
|
+
function groups(parts, roles) {
|
|
225
|
+
return roles.map((role) => parts.filter((item) => appliesTo(item, role)));
|
|
226
|
+
}
|
|
227
|
+
// ─── Columns the rules read ──────────────────────────────────────
|
|
228
|
+
// The columns an access rule's Data node adds, at their default names: entity_id, owner_id, is_published, ...
|
|
229
|
+
let accessColumns;
|
|
230
|
+
function accessColumnNames() {
|
|
231
|
+
accessColumns ??= new Set(Object.values(TABLE_NEEDS).flatMap((need) => (need.data_node ? fieldsAddedByNodes([need.data_node.type]).fields.map((field) => field.name) : [])));
|
|
232
|
+
return accessColumns;
|
|
233
|
+
}
|
|
234
|
+
const typeFits = (schema, value) => schema.type === undefined ||
|
|
235
|
+
[schema.type]
|
|
236
|
+
.flat()
|
|
237
|
+
.some((type) => (type === 'array' ? Array.isArray(value) : type === 'object' ? typeof value === 'object' && value !== null && !Array.isArray(value) : typeof value === type || (type === 'integer' && Number.isInteger(value))));
|
|
238
|
+
const join = (path, key) => (path ? `${path}.${key}` : String(key));
|
|
239
|
+
// The columns of this table a value reads under its schema: column parameters, other than another table's, nested
|
|
240
|
+
// expressions included. An object or a list of objects the schema does not describe could read any column.
|
|
241
|
+
function readValue(schema, value, path) {
|
|
242
|
+
const none = { columns: [], opaque: false, nested: [] };
|
|
243
|
+
if (value === undefined || value === null)
|
|
244
|
+
return none;
|
|
245
|
+
if (schema.format === 'authz-expression')
|
|
246
|
+
return { ...none, nested: [treeOf(value)] };
|
|
247
|
+
// Alternative shapes of the value; an anyOf of required key groups constrains keys and is not one.
|
|
248
|
+
const shapes = (schema.anyOf ?? []).filter((entry) => entry.type !== undefined || entry.format !== undefined);
|
|
249
|
+
if (shapes.length) {
|
|
250
|
+
const branch = shapes.find((entry) => typeFits(entry, value));
|
|
251
|
+
return branch ? readValue(branch, value, path) : none;
|
|
252
|
+
}
|
|
253
|
+
if (schema.format === 'column-ref')
|
|
254
|
+
return typeof value === 'string' && schema['x-column-scope'] !== 'foreign' ? { ...none, columns: [{ param: path, column: value }] } : none;
|
|
255
|
+
const merge = (readings) => ({
|
|
256
|
+
columns: readings.flatMap((entry) => entry.columns),
|
|
257
|
+
opaque: readings.some((entry) => entry.opaque),
|
|
258
|
+
nested: readings.flatMap((entry) => entry.nested)
|
|
259
|
+
});
|
|
260
|
+
if (Array.isArray(value)) {
|
|
261
|
+
const items = !Array.isArray(schema.items) ? schema.items : undefined;
|
|
262
|
+
return items ? merge(value.map((item, i) => readValue(items, item, join(path, i)))) : { ...none, opaque: value.length > 0 };
|
|
263
|
+
}
|
|
264
|
+
if (typeof value === 'object') {
|
|
265
|
+
const record = value;
|
|
266
|
+
if (schema.properties) {
|
|
267
|
+
const described = merge(Object.entries(schema.properties).map(([key, inner]) => readValue(inner, record[key], join(path, key))));
|
|
268
|
+
const extra = Object.keys(record).some((key) => !schema.properties[key]);
|
|
269
|
+
return extra ? { ...described, opaque: true } : described;
|
|
270
|
+
}
|
|
271
|
+
return { ...none, opaque: Object.keys(record).length > 0 };
|
|
272
|
+
}
|
|
273
|
+
return none;
|
|
274
|
+
}
|
|
275
|
+
function readExpr(expr, owner, columns, unknown) {
|
|
276
|
+
if (expr.kind !== 'leaf') {
|
|
277
|
+
if (expr.kind === 'raw')
|
|
278
|
+
unknown.add(owner);
|
|
279
|
+
for (const arg of expr.kind === 'not' ? [expr.arg] : expr.kind === 'raw' ? [] : expr.args)
|
|
280
|
+
readExpr(arg, owner, columns, unknown);
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
283
|
+
const node = AUTHZ_NODES.get(expr.type);
|
|
284
|
+
if (!node) {
|
|
285
|
+
unknown.add(expr.type);
|
|
286
|
+
return;
|
|
287
|
+
}
|
|
288
|
+
const reading = readValue(node.parameter_schema, expr.data, '');
|
|
289
|
+
for (const { param, column } of reading.columns)
|
|
290
|
+
columns.push({ type: expr.type, param, column });
|
|
291
|
+
if (reading.opaque)
|
|
292
|
+
unknown.add(expr.type);
|
|
293
|
+
for (const nested of reading.nested)
|
|
294
|
+
readExpr(nested, expr.type, columns, unknown);
|
|
295
|
+
}
|
|
296
|
+
/**
|
|
297
|
+
* The columns of the table the rules read: each rule, its `with_check`, and the nodes nested in them, from the nodes'
|
|
298
|
+
* column parameters (registry defaults apply). `unknown` names the rule types with a part whose columns cannot be told:
|
|
299
|
+
* a stored raw expression, a type the registry does not have, or data its schema does not describe.
|
|
300
|
+
*/
|
|
301
|
+
export function columnsReadByPolicies(policies) {
|
|
302
|
+
const columns = [];
|
|
303
|
+
const unknown = new Set();
|
|
304
|
+
for (const policy of policies) {
|
|
305
|
+
for (const rule of [{ type: policy.type, data: policy.data }, ...(policy.with_check ? [policy.with_check] : [])]) {
|
|
306
|
+
const effective = effectivePolicy(rule.type, rule.data ?? {});
|
|
307
|
+
readExpr(exprOf(effective.type, effective.data), rule.type, columns, unknown);
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
return { columns: unique(columns, (entry) => JSON.stringify([entry.type, entry.param, entry.column])), unknown: [...unknown] };
|
|
311
|
+
}
|
|
312
|
+
// A required column with no default that an access rule's Data node adds, and that no rule of the table reads. When a
|
|
313
|
+
// part of the rules cannot be read, it may read the column, so nothing is reported.
|
|
314
|
+
function strandedColumns(input) {
|
|
315
|
+
if (!input.fields)
|
|
316
|
+
return [];
|
|
317
|
+
const { columns, unknown } = columnsReadByPolicies(input.policies);
|
|
318
|
+
if (unknown.length)
|
|
319
|
+
return [];
|
|
320
|
+
const read = new Set(columns.map((entry) => entry.column));
|
|
321
|
+
return input.fields.filter((field) => field.required && field.default === null && accessColumnNames().has(field.name) && !read.has(field.name)).map((field) => field.name);
|
|
322
|
+
}
|
|
323
|
+
/** What may be wrong with the rules of one table, by the shapes the backend docs warn about. */
|
|
324
|
+
export function accessFindings(input) {
|
|
325
|
+
const readableParts = (parts) => parts.map((item) => ({ ...item, expr: readable(item.expr, input.related) }));
|
|
326
|
+
const partsOf = (privilege) => readableParts(partsFor(input.policies, privilege));
|
|
327
|
+
const all = readableParts(input.policies.map(part));
|
|
328
|
+
const rlsOff = input.rls_enabled === false;
|
|
329
|
+
const literals = all.flatMap((item) => (clausesOf(item.expr) ?? []).flat());
|
|
330
|
+
// The membership nodes: no peer ownership, and no limit such as the read-only block.
|
|
331
|
+
const hasMember = literals.some((literal) => memberOf(literal, ['app', 'row']) && !checkOf(literal).limit);
|
|
332
|
+
const rowTeam = literals.some((literal) => memberOf(literal, ['row']));
|
|
333
|
+
// On a table of teams, only team membership fences an owner: an app member who left the team is still in the app.
|
|
334
|
+
const fence = (literal) => memberOf(literal, rowTeam ? ['row'] : ['app', 'row']);
|
|
335
|
+
const readParts = partsOf('select');
|
|
336
|
+
const roles = rolesOf(input, all);
|
|
337
|
+
const rules = {
|
|
338
|
+
'owner-outlives-membership': (parts, operation) => hasMember &&
|
|
339
|
+
roles.some((role) => {
|
|
340
|
+
const mine = parts.filter((item) => appliesTo(item, role));
|
|
341
|
+
const read = readParts.filter((item) => appliesTo(item, role));
|
|
342
|
+
const open = permissiveClauses(mine).some((clause) => clause.some(bareOwner) && !clause.some(fence));
|
|
343
|
+
const readFenced = operation !== 'read' && (permissiveClauses(read).every((clause) => clause.some(fence)) || fencedBy(read, fence));
|
|
344
|
+
return open && !fencedBy(mine, fence) && !readFenced;
|
|
345
|
+
}),
|
|
346
|
+
'app-membership-on-team-rows': (parts) => rowTeam &&
|
|
347
|
+
groups(parts, roles).some((mine) => {
|
|
348
|
+
const clauses = permissiveClauses(mine);
|
|
349
|
+
const app = clauses.some((clause) => clause.some((literal) => memberOf(literal, ['app']) && !checkOf(literal).owner));
|
|
350
|
+
return app && clauses.some((clause) => clause.some(bareOwner) && !clause.some((literal) => memberOf(literal, ['app', 'row'])));
|
|
351
|
+
}),
|
|
352
|
+
// No rule at all lets anyone do it: the database refuses it to every role.
|
|
353
|
+
'granted-without-policy': (parts, operation) => Boolean(input.grants) && !rlsOff && !parts.some(lets) && rolesOf(input, parts).some((role) => granted(input, role, PRIVILEGE[operation])),
|
|
354
|
+
// A rule lets some role do it, but none names this granted role: a member of a role a rule names passes.
|
|
355
|
+
'granted-role-without-policy': (parts, operation) => Boolean(input.grants) &&
|
|
356
|
+
!rlsOff &&
|
|
357
|
+
parts.some(lets) &&
|
|
358
|
+
rolesOf(input, parts).some((role) => granted(input, role, PRIVILEGE[operation]) && !parts.some((item) => appliesTo(item, role) && lets(item))),
|
|
359
|
+
'policies-without-row-security': (parts) => rlsOff && parts.length > 0,
|
|
360
|
+
'anyone-can-change': (parts, operation) => operation !== 'read' &&
|
|
361
|
+
!rlsOff &&
|
|
362
|
+
roles.some((role) => {
|
|
363
|
+
const mine = parts.filter((item) => appliesTo(item, role));
|
|
364
|
+
const read = readParts.filter((item) => appliesTo(item, role));
|
|
365
|
+
return mine.some(lets) && granted(input, role, 'select') && !limitsPeople(mine) && !limitsPeople(read);
|
|
366
|
+
}),
|
|
367
|
+
'limit-blocks-outside-grant': (parts) => groups(parts, roles).some((mine) => fencedBy(mine, (literal) => memberOf(literal, ['app', 'row', 'peer'])) &&
|
|
368
|
+
permissiveClauses(mine).some((clause) => !clause.some((literal) => memberOf(literal, ['app', 'row', 'peer'])) && clause.some(letsOutsidersIn))),
|
|
369
|
+
// A part the words may not state, in a permissive rule, in a clause with no part that names people: it can let anyone in.
|
|
370
|
+
// Under AND beside such a part it can only narrow; a restrictive rule can only narrow.
|
|
371
|
+
'raw-part-may-widen': (parts) => parts.some((item) => lets(item) && (clausesOf(item.expr) ?? []).some((clause) => !clause.some(never) && clause.some(unreadable) && !clause.some(namesKnownPeople))),
|
|
372
|
+
'write-without-read': (parts, operation) => operation !== 'read' &&
|
|
373
|
+
!rlsOff &&
|
|
374
|
+
roles.some((role) => {
|
|
375
|
+
const mine = parts.filter((item) => appliesTo(item, role));
|
|
376
|
+
if (!mine.some(lets) || !granted(input, role, PRIVILEGE[operation]))
|
|
377
|
+
return false;
|
|
378
|
+
if (!granted(input, role, 'select'))
|
|
379
|
+
return true;
|
|
380
|
+
return !limitsPeople(mine) && limitsPeople(readParts.filter((item) => appliesTo(item, role)));
|
|
381
|
+
})
|
|
382
|
+
};
|
|
383
|
+
const findings = Object.entries(rules).flatMap(([id, fires]) => {
|
|
384
|
+
const operations = OPERATIONS.filter((operation) => fires(partsOf(PRIVILEGE[operation]), operation));
|
|
385
|
+
return operations.length ? [{ id, ...FINDINGS[id], operations }] : [];
|
|
386
|
+
});
|
|
387
|
+
const fields = readableFields(input.fields);
|
|
388
|
+
const stranded = strandedColumns({ ...input, fields });
|
|
389
|
+
if (stranded.length)
|
|
390
|
+
findings.push({ id: 'required-column-not-set', ...FINDINGS['required-column-not-set'], operations: ['create'], columns: stranded });
|
|
391
|
+
const gaps = fields ? columnGaps(fields, input.policies).filter(({ gaps: kinds }) => kinds.includes('type') || kinds.includes('default')) : [];
|
|
392
|
+
if (gaps.length) {
|
|
393
|
+
findings.push({
|
|
394
|
+
id: 'reused-column-differs',
|
|
395
|
+
...FINDINGS['reused-column-differs'],
|
|
396
|
+
operations: gaps.some(({ gaps: kinds }) => kinds.includes('type')) ? OPERATIONS : ['create'],
|
|
397
|
+
columns: gaps.map(({ column }) => column),
|
|
398
|
+
details: gaps.map(gapWords)
|
|
399
|
+
});
|
|
400
|
+
}
|
|
401
|
+
return findings;
|
|
402
|
+
}
|
|
403
|
+
const CURRENT_USER = { function: 'current_user_id', schema: 'jwt_public' };
|
|
404
|
+
// The default DataDirectOwner gives an owner column: the id of the person who makes the row.
|
|
405
|
+
const isCurrentUser = (value) => typeof value === 'object' && value !== null && 'function' in value && value.function === CURRENT_USER.function && value.schema === CURRENT_USER.schema;
|
|
406
|
+
function defaultWords(value) {
|
|
407
|
+
if (value === null || value === 'unknown')
|
|
408
|
+
return String(value);
|
|
409
|
+
if ('generated' in value)
|
|
410
|
+
return 'a value PostgreSQL computes';
|
|
411
|
+
if (isCurrentUser(value))
|
|
412
|
+
return 'the id of the person who makes the row';
|
|
413
|
+
if (value.function !== undefined)
|
|
414
|
+
return `${value.schema ? `${value.schema}.` : ''}${value.function}()`;
|
|
415
|
+
return `the value ${JSON.stringify(value.value)}`;
|
|
416
|
+
}
|
|
417
|
+
function gapWords({ column, node, expected, actual, gaps }) {
|
|
418
|
+
if (gaps.includes('type'))
|
|
419
|
+
return `${column} is ${actual.type}, and ${node} makes it ${expected.type}, so the rule cannot work on it.`;
|
|
420
|
+
if (actual.default === null)
|
|
421
|
+
return `${column} has no default, and ${node} gives it ${defaultWords(expected.default)}, so a create must send it itself.`;
|
|
422
|
+
if (expected.default === null && isCurrentUser(actual.default)) {
|
|
423
|
+
return `${column} defaults to the id of the person who makes the row, and ${node} gives it no default, so a row made without it names its maker.`;
|
|
424
|
+
}
|
|
425
|
+
if (expected.default === null)
|
|
426
|
+
return `${column} defaults to ${defaultWords(actual.default)}, and ${node} gives it no default, so a create that leaves it out gets that value.`;
|
|
427
|
+
return `${column} defaults to ${defaultWords(actual.default)}, and ${node} gives it ${defaultWords(expected.default)}.`;
|
|
428
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { ACCESS_CHECKS, type AccessCheck, type AccessFinding, accessFindings, type ColumnGap, columnGaps, type ColumnRead, columnsReadByPolicies, type TableAccessInput, type TableField } from './findings';
|
|
2
|
+
export { type AccessOperation, BUILT_IN_SCOPES, DERIVED_KEYS, type MembershipScope, policyDifference, type ResolvedPolicy, resolveEntityTypes, type RuleProblem, type TablePolicy, tablePolicyFromBlueprint, type TablePrivilege } from './policies';
|
|
3
|
+
export { ADDED_FIELDS, type AddedColumn, type AddedDefault, type AddedField, type ColumnDefault, type DataNodeNeed, dataNodesForPolicies, defaultFills, fieldsAddedByNodes, type NeededDataNode, ROW_ID, TABLE_NEEDS, type TableKind } from './provisioning';
|
|
4
|
+
export { checkRule, POLICY_ROLES } from './rule-check';
|
|
5
|
+
export { explainPolicy, POLICY_WORDS, type PolicyWordsTemplate } from './words';
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { ACCESS_CHECKS, accessFindings, columnGaps, columnsReadByPolicies } from './findings';
|
|
2
|
+
export { BUILT_IN_SCOPES, DERIVED_KEYS, policyDifference, resolveEntityTypes, tablePolicyFromBlueprint } from './policies';
|
|
3
|
+
export { ADDED_FIELDS, dataNodesForPolicies, defaultFills, fieldsAddedByNodes, ROW_ID, TABLE_NEEDS } from './provisioning';
|
|
4
|
+
export { checkRule, POLICY_ROLES } from './rule-check';
|
|
5
|
+
export { explainPolicy, POLICY_WORDS } from './words';
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { type BlueprintPolicy, type NodeTypeDefinition } from 'node-type-registry';
|
|
2
|
+
export type TablePrivilege = 'select' | 'insert' | 'update' | 'delete';
|
|
3
|
+
export type AccessOperation = 'read' | 'create' | 'update' | 'delete';
|
|
4
|
+
/** One policy of a table, as the backend stores it; a read gives it to a caller through `asWritten`. */
|
|
5
|
+
export interface TablePolicy {
|
|
6
|
+
/** An Authz node name. */
|
|
7
|
+
type: string;
|
|
8
|
+
privileges: TablePrivilege[];
|
|
9
|
+
/** False for a restrictive policy. */
|
|
10
|
+
permissive: boolean;
|
|
11
|
+
/** Default 'authenticated'; 'public' applies to every role. */
|
|
12
|
+
role?: string;
|
|
13
|
+
/** As stored: it holds keys the backend resolved too. */
|
|
14
|
+
data: Record<string, unknown>;
|
|
15
|
+
/** UPDATE only: the rule a changed row must pass, when it differs from the policy's own. */
|
|
16
|
+
with_check?: {
|
|
17
|
+
type: string;
|
|
18
|
+
data?: Record<string, unknown>;
|
|
19
|
+
} | null;
|
|
20
|
+
}
|
|
21
|
+
declare const RESOLVED: unique symbol;
|
|
22
|
+
/**
|
|
23
|
+
* A rule in the form the backend stores it: no `entity_type`, only the `membership_type` it stands for. Only
|
|
24
|
+
* `resolveEntityTypes` makes one, so a caller's rules cannot be compared with stored ones before they are resolved.
|
|
25
|
+
*/
|
|
26
|
+
export type ResolvedPolicy = TablePolicy & {
|
|
27
|
+
readonly [RESOLVED]: true;
|
|
28
|
+
};
|
|
29
|
+
export declare const OPERATIONS: AccessOperation[];
|
|
30
|
+
export declare const PRIVILEGE: Record<AccessOperation, TablePrivilege>;
|
|
31
|
+
export declare const PRIVILEGES: TablePrivilege[];
|
|
32
|
+
export declare const SIGNED_IN = "authenticated";
|
|
33
|
+
/** PostgreSQL's PUBLIC: a grant or a policy for it applies to every role. */
|
|
34
|
+
export declare const PUBLIC = "public";
|
|
35
|
+
export declare const AUTHZ_NODES: Map<string, NodeTypeDefinition>;
|
|
36
|
+
/**
|
|
37
|
+
* Keys the backend writes from other keys when it stores a rule. They carry no meaning of their own, so two rules that
|
|
38
|
+
* differ only in them mean the same. The member list (sprt_*) comes from the team kind, and only when a rule gives one;
|
|
39
|
+
* without it the backend keeps a given member list, which then decides who passes. An app rule's membership_type is
|
|
40
|
+
* derived only when it is the app's own (1): another one names the member list the rule reads.
|
|
41
|
+
*/
|
|
42
|
+
export declare const DERIVED_KEYS: Record<string, string[]>;
|
|
43
|
+
export declare const APP_MEMBERSHIP_TYPE = 1;
|
|
44
|
+
/**
|
|
45
|
+
* The keys of this rule's data the backend derived; a member list counts as derived only beside a team kind, an app
|
|
46
|
+
* rule's membership_type only when it is the app's, and a table's schema and name only beside the table's id.
|
|
47
|
+
*/
|
|
48
|
+
export declare function derivedKeys(type: string, data: Record<string, unknown>): Set<string>;
|
|
49
|
+
export declare function isObject(value: unknown): value is Record<string, unknown>;
|
|
50
|
+
export declare function sorted(value: unknown): unknown;
|
|
51
|
+
export declare function unique<T>(items: T[], keyOf: (item: T) => string): T[];
|
|
52
|
+
/** Each parameter's registry default. */
|
|
53
|
+
export declare function defaultsOf(type: string): Record<string, unknown>;
|
|
54
|
+
/** A blueprint policy as a table policy, with the defaults the provisioner applies. */
|
|
55
|
+
export declare function tablePolicyFromBlueprint(policy: BlueprintPolicy): TablePolicy;
|
|
56
|
+
/** The policy a rule stands for: a composite whose data is exactly one Authz node is that node. */
|
|
57
|
+
export declare function effectivePolicy(type: string, data: Record<string, unknown>): {
|
|
58
|
+
type: string;
|
|
59
|
+
data: Record<string, unknown>;
|
|
60
|
+
};
|
|
61
|
+
/** The two team kinds every database has. */
|
|
62
|
+
export declare const BUILT_IN_SCOPES: readonly MembershipScope[];
|
|
63
|
+
/** A team kind of the database: the scope an `entity_type` names (an entity type's prefix), and its membership type. */
|
|
64
|
+
export interface MembershipScope {
|
|
65
|
+
scope: string;
|
|
66
|
+
membership_type: number;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Registry defaults filled in, and the keys the backend derives or strips left out: a plan and the stored policy it
|
|
70
|
+
* becomes have the same normal form.
|
|
71
|
+
*/
|
|
72
|
+
export declare function normalData(type: string, given: Record<string, unknown>): Record<string, unknown>;
|
|
73
|
+
export interface Leaf {
|
|
74
|
+
kind: 'leaf';
|
|
75
|
+
type: string;
|
|
76
|
+
/** Normal form. */
|
|
77
|
+
data: Record<string, unknown>;
|
|
78
|
+
}
|
|
79
|
+
/** A policy's expression: one node, or a composite's and/or/not tree. `raw` is a part no Authz node describes. */
|
|
80
|
+
export type Expr = Leaf | {
|
|
81
|
+
kind: 'and' | 'or';
|
|
82
|
+
args: Expr[];
|
|
83
|
+
} | {
|
|
84
|
+
kind: 'not';
|
|
85
|
+
arg: Expr;
|
|
86
|
+
} | {
|
|
87
|
+
kind: 'raw';
|
|
88
|
+
};
|
|
89
|
+
export declare function exprOf(type: string, data: Record<string, unknown>): Expr;
|
|
90
|
+
export declare function treeOf(value: unknown): Expr;
|
|
91
|
+
/** An object that is an Authz expression: one Authz node, or one of the combinators. */
|
|
92
|
+
export declare const isExpression: (value: unknown) => boolean;
|
|
93
|
+
export interface Part {
|
|
94
|
+
policy: TablePolicy;
|
|
95
|
+
role: string;
|
|
96
|
+
/** Equal for two policies with the same meaning. */
|
|
97
|
+
key: string;
|
|
98
|
+
expr: Expr;
|
|
99
|
+
/** The rule a changed row must pass, when the policy has its own (UPDATE). */
|
|
100
|
+
check_expr?: Expr;
|
|
101
|
+
}
|
|
102
|
+
export declare function part(policy: TablePolicy): Part;
|
|
103
|
+
export declare const covers: (policy: TablePolicy, privilege: TablePrivilege) => boolean;
|
|
104
|
+
export declare function partsFor(policies: TablePolicy[], privilege: TablePrivilege): Part[];
|
|
105
|
+
export declare const appliesTo: (item: Part, role: string) => boolean;
|
|
106
|
+
export declare const lets: (item: Part) => boolean;
|
|
107
|
+
/**
|
|
108
|
+
* What differs between two rule sets of one table, by meaning: order, duplicates, registry defaults and the keys the
|
|
109
|
+
* backend derives do not count, and a `with_check` equal to its policy is no check. Each policy returned is the given
|
|
110
|
+
* one, with only the privileges that differ. The role counts as it is (`public` also lets the signed-out in), and a
|
|
111
|
+
* stored raw part counts by its exact data (key order aside). The planned rules are a caller's, resolved by
|
|
112
|
+
* `resolveEntityTypes`; the stored ones are the backend's, which never hold an `entity_type`.
|
|
113
|
+
*/
|
|
114
|
+
export declare function policyDifference(planned: ResolvedPolicy[], stored: TablePolicy[]): {
|
|
115
|
+
missing: TablePolicy[];
|
|
116
|
+
unplanned: TablePolicy[];
|
|
117
|
+
};
|
|
118
|
+
/** A problem with one rule a caller wrote: where, what kind, and what to do. */
|
|
119
|
+
export interface RuleProblem {
|
|
120
|
+
/** A key, or a path into a composite such as `OR.0.AuthzEntityMembership.membership_type`; '' for the whole data. */
|
|
121
|
+
path: string;
|
|
122
|
+
code: string;
|
|
123
|
+
/** What is wrong, why, and what to do. */
|
|
124
|
+
message: string;
|
|
125
|
+
/** For a misspelling: the name it probably meant. */
|
|
126
|
+
meant?: string;
|
|
127
|
+
}
|
|
128
|
+
export declare const quoted: (keys: string[]) => string[];
|
|
129
|
+
export declare const join: (path: string, key: string | number) => string;
|
|
130
|
+
/**
|
|
131
|
+
* A caller's rules in the form the backend stores them: every `entity_type`, in every node at any depth (and in a
|
|
132
|
+
* `with_check`), becomes its `membership_type`, from the database's team kinds (`scopes`: an entity type's prefix and
|
|
133
|
+
* its `out_membership_type`; `app` and `org` are always there). An `entity_type` the database does not have is a
|
|
134
|
+
* problem, with the index of its rule.
|
|
135
|
+
*/
|
|
136
|
+
export declare function resolveEntityTypes(policies: TablePolicy[], scopes: MembershipScope[]): {
|
|
137
|
+
policies: ResolvedPolicy[];
|
|
138
|
+
problems: Array<RuleProblem & {
|
|
139
|
+
policy: number;
|
|
140
|
+
}>;
|
|
141
|
+
};
|
|
142
|
+
export {};
|