@burdenoff/be-sdk 2026.1017.1 → 2026.1021.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 +25 -0
- package/dist/chunk-WVOW3CJR.js +545 -0
- package/dist/chunk-WVOW3CJR.js.map +1 -0
- package/dist/index-qwG_TaUg.d.ts +155 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/validation/index.d.ts +2 -1
- package/dist/validation/index.js +1 -1
- package/package.json +1 -1
- package/dist/chunk-AHEXIONU.js +0 -165
- package/dist/chunk-AHEXIONU.js.map +0 -1
- package/dist/index-B2jIInhp.d.ts +0 -76
package/README.md
CHANGED
|
@@ -135,6 +135,31 @@ await sdk.translation.upsertProductTranslations({
|
|
|
135
135
|
- **payments**: Payment processing
|
|
136
136
|
- **bootstrap**: Server initialization
|
|
137
137
|
- **platform**: Scale primitives (see below)
|
|
138
|
+
- **validation**: User-field validators and the `@rbac` coverage guard (see below)
|
|
139
|
+
|
|
140
|
+
### RBAC coverage guard (`@burdenoff/be-sdk/validation`)
|
|
141
|
+
|
|
142
|
+
`assertRbacCoverage(pathOrDocuments, options)` parses a svc's SDL (a directory of
|
|
143
|
+
`.gql`/`.graphql` files, or a single file such as `.hive-schema.graphql`) and fails when a
|
|
144
|
+
Query/Mutation/Subscription field has no `@rbac`, has one the gateway would deny as invalid
|
|
145
|
+
(missing `action`/`resource`/`scopeType`, unknown `scopeType`, `entity` without `scopeArg`),
|
|
146
|
+
or has a `resourceIdArg`/`scopeArg` that does not resolve to one `ID`/`String` within two
|
|
147
|
+
segments. Pass `selfScopedFields` to pin the reviewed `scopeType: "self"` surface. Call it
|
|
148
|
+
from one unit test per svc:
|
|
149
|
+
|
|
150
|
+
```typescript
|
|
151
|
+
import { join } from 'node:path';
|
|
152
|
+
import { assertRbacCoverage } from '@burdenoff/be-sdk/validation';
|
|
153
|
+
|
|
154
|
+
it('gates every root field with a well-formed @rbac', () => {
|
|
155
|
+
assertRbacCoverage(join(__dirname, '../modules'), {
|
|
156
|
+
minRootFields: 200, // a wrong path must not pass
|
|
157
|
+
selfScopedFields: { mySettings: 'type C: resolver filters by ctx.actorId' },
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
`analyzeRbacCoverage` returns the same report without throwing.
|
|
138
163
|
|
|
139
164
|
### Platform Primitives (`@burdenoff/be-sdk/platform`)
|
|
140
165
|
|
|
@@ -0,0 +1,545 @@
|
|
|
1
|
+
import { __export, __name } from './chunk-XE4OXN2W.js';
|
|
2
|
+
import { readFileSync, statSync, readdirSync } from 'fs';
|
|
3
|
+
import { join, extname } from 'path';
|
|
4
|
+
import { parse, Kind } from 'graphql';
|
|
5
|
+
|
|
6
|
+
// src/validation/index.ts
|
|
7
|
+
var validation_exports = {};
|
|
8
|
+
__export(validation_exports, {
|
|
9
|
+
DEFAULT_MAX_RBAC_ARG_PATH_SEGMENTS: () => DEFAULT_MAX_RBAC_ARG_PATH_SEGMENTS,
|
|
10
|
+
DEFAULT_RBAC_ROOT_TYPES: () => DEFAULT_RBAC_ROOT_TYPES,
|
|
11
|
+
NAME_MAX_LENGTH: () => NAME_MAX_LENGTH,
|
|
12
|
+
NAME_MIN_LENGTH: () => NAME_MIN_LENGTH,
|
|
13
|
+
RBAC_GATED_SCOPE_TYPES: () => RBAC_GATED_SCOPE_TYPES,
|
|
14
|
+
RBAC_SCOPE_TYPES: () => RBAC_SCOPE_TYPES,
|
|
15
|
+
RbacCoverageError: () => RbacCoverageError,
|
|
16
|
+
USERNAME_MAX_LENGTH: () => USERNAME_MAX_LENGTH,
|
|
17
|
+
USERNAME_MIN_LENGTH: () => USERNAME_MIN_LENGTH,
|
|
18
|
+
analyzeRbacCoverage: () => analyzeRbacCoverage,
|
|
19
|
+
assertRbacCoverage: () => assertRbacCoverage,
|
|
20
|
+
lengthInCodePoints: () => lengthInCodePoints,
|
|
21
|
+
loadRbacSchemaDocuments: () => loadRbacSchemaDocuments,
|
|
22
|
+
rbacArgPathProblem: () => rbacArgPathProblem,
|
|
23
|
+
readRbacDirectiveArgs: () => readRbacDirectiveArgs,
|
|
24
|
+
validateName: () => validateName,
|
|
25
|
+
validateUserFields: () => validateUserFields,
|
|
26
|
+
validateUsername: () => validateUsername
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
// src/validation/name.ts
|
|
30
|
+
var NAME_MIN_LENGTH = 1;
|
|
31
|
+
var NAME_MAX_LENGTH = 100;
|
|
32
|
+
var NAME_ALLOWED_CHARS = /^[\p{L}\p{M} '.\-\u00B7\u30FB\u200C\u200D]+$/u;
|
|
33
|
+
var TYPOGRAPHIC_APOSTROPHES = /[\u2018\u2019\u02BC]/g;
|
|
34
|
+
var TYPOGRAPHIC_HYPHENS = /[\u2010\u2011]/g;
|
|
35
|
+
function lengthInCodePoints(s) {
|
|
36
|
+
return [
|
|
37
|
+
...s
|
|
38
|
+
].length;
|
|
39
|
+
}
|
|
40
|
+
__name(lengthInCodePoints, "lengthInCodePoints");
|
|
41
|
+
function validateName(name) {
|
|
42
|
+
const trimmed = (name ?? "").normalize("NFC").replace(TYPOGRAPHIC_APOSTROPHES, "'").replace(TYPOGRAPHIC_HYPHENS, "-").trim();
|
|
43
|
+
if (lengthInCodePoints(trimmed) < NAME_MIN_LENGTH) {
|
|
44
|
+
return {
|
|
45
|
+
valid: false,
|
|
46
|
+
code: "required",
|
|
47
|
+
message: "Name is required"
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
if (lengthInCodePoints(trimmed) > NAME_MAX_LENGTH) {
|
|
51
|
+
return {
|
|
52
|
+
valid: false,
|
|
53
|
+
code: "too_long",
|
|
54
|
+
message: `Name must be ${NAME_MAX_LENGTH} characters or fewer`
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
if (!NAME_ALLOWED_CHARS.test(trimmed) || !/\p{L}/u.test(trimmed)) {
|
|
58
|
+
if (!/\p{L}/u.test(trimmed)) {
|
|
59
|
+
return {
|
|
60
|
+
valid: false,
|
|
61
|
+
code: "no_letter",
|
|
62
|
+
message: "Name must contain at least one letter"
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
return {
|
|
66
|
+
valid: false,
|
|
67
|
+
code: "invalid_chars",
|
|
68
|
+
message: "Name can only contain letters, spaces, periods, hyphens, and apostrophes"
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
return {
|
|
72
|
+
valid: true,
|
|
73
|
+
value: trimmed
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
__name(validateName, "validateName");
|
|
77
|
+
|
|
78
|
+
// src/validation/username.ts
|
|
79
|
+
var USERNAME_MIN_LENGTH = 3;
|
|
80
|
+
var USERNAME_MAX_LENGTH = 30;
|
|
81
|
+
var USERNAME_ALLOWED_CHARS = /^[a-zA-Z0-9_-]+$/;
|
|
82
|
+
function validateUsername(username) {
|
|
83
|
+
const errors = [];
|
|
84
|
+
const value = (username ?? "").trim();
|
|
85
|
+
if (lengthInCodePoints(value) === 0) {
|
|
86
|
+
errors.push({
|
|
87
|
+
code: "required",
|
|
88
|
+
message: "Username is required"
|
|
89
|
+
});
|
|
90
|
+
return {
|
|
91
|
+
valid: false,
|
|
92
|
+
errors
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
if (lengthInCodePoints(value) < USERNAME_MIN_LENGTH) {
|
|
96
|
+
errors.push({
|
|
97
|
+
code: "too_short",
|
|
98
|
+
message: `Username must be at least ${USERNAME_MIN_LENGTH} characters`
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
if (lengthInCodePoints(value) > USERNAME_MAX_LENGTH) {
|
|
102
|
+
errors.push({
|
|
103
|
+
code: "too_long",
|
|
104
|
+
message: `Username must be ${USERNAME_MAX_LENGTH} characters or fewer`
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
if (!USERNAME_ALLOWED_CHARS.test(value)) {
|
|
108
|
+
errors.push({
|
|
109
|
+
code: "invalid_chars",
|
|
110
|
+
message: "Username can only contain letters, numbers, underscores, and hyphens"
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
if (lengthInCodePoints(value) >= USERNAME_MIN_LENGTH && lengthInCodePoints(value) <= USERNAME_MAX_LENGTH && !/[a-zA-Z]/.test(value)) {
|
|
114
|
+
errors.push({
|
|
115
|
+
code: "no_letter",
|
|
116
|
+
message: "Username must contain at least one letter"
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
return errors.length === 0 ? {
|
|
120
|
+
valid: true,
|
|
121
|
+
value
|
|
122
|
+
} : {
|
|
123
|
+
valid: false,
|
|
124
|
+
errors
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
__name(validateUsername, "validateUsername");
|
|
128
|
+
|
|
129
|
+
// src/validation/userFields.ts
|
|
130
|
+
function validateUserFields(input, options = {}) {
|
|
131
|
+
const requireAll = options.requireAll ?? true;
|
|
132
|
+
const errors = [];
|
|
133
|
+
const isBlank = /* @__PURE__ */ __name((v) => !v || v.trim().length === 0, "isBlank");
|
|
134
|
+
const firstName = !isBlank(input.firstName) || requireAll ? validateName(input.firstName) : null;
|
|
135
|
+
if (firstName && !firstName.valid) {
|
|
136
|
+
errors.push({
|
|
137
|
+
field: "firstName",
|
|
138
|
+
code: firstName.code,
|
|
139
|
+
message: firstName.message
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
const lastName = !isBlank(input.lastName) || requireAll ? validateName(input.lastName) : null;
|
|
143
|
+
if (lastName && !lastName.valid) {
|
|
144
|
+
errors.push({
|
|
145
|
+
field: "lastName",
|
|
146
|
+
code: lastName.code,
|
|
147
|
+
message: lastName.message
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
const username = !isBlank(input.username) || requireAll ? validateUsername(input.username) : null;
|
|
151
|
+
if (username && !username.valid) {
|
|
152
|
+
for (const e of username.errors) {
|
|
153
|
+
errors.push({
|
|
154
|
+
field: "username",
|
|
155
|
+
code: e.code,
|
|
156
|
+
message: e.message
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
if (errors.length > 0) return {
|
|
161
|
+
valid: false,
|
|
162
|
+
errors
|
|
163
|
+
};
|
|
164
|
+
const valueOr = /* @__PURE__ */ __name((result, fallback) => result ? result.value : (fallback ?? "").trim(), "valueOr");
|
|
165
|
+
return {
|
|
166
|
+
valid: true,
|
|
167
|
+
values: {
|
|
168
|
+
firstName: valueOr(firstName, input.firstName),
|
|
169
|
+
lastName: valueOr(lastName, input.lastName),
|
|
170
|
+
username: valueOr(username, input.username)
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
__name(validateUserFields, "validateUserFields");
|
|
175
|
+
var RBAC_GATED_SCOPE_TYPES = [
|
|
176
|
+
"wspace",
|
|
177
|
+
"global",
|
|
178
|
+
"entity",
|
|
179
|
+
"billing",
|
|
180
|
+
"organization"
|
|
181
|
+
];
|
|
182
|
+
var RBAC_SCOPE_TYPES = [
|
|
183
|
+
...RBAC_GATED_SCOPE_TYPES,
|
|
184
|
+
"self"
|
|
185
|
+
];
|
|
186
|
+
var DEFAULT_RBAC_ROOT_TYPES = [
|
|
187
|
+
"Query",
|
|
188
|
+
"Mutation",
|
|
189
|
+
"Subscription"
|
|
190
|
+
];
|
|
191
|
+
var DEFAULT_MAX_RBAC_ARG_PATH_SEGMENTS = 2;
|
|
192
|
+
var DEFAULT_BILLING_SCOPE_ARG = "billingAccountId";
|
|
193
|
+
var DEFAULT_ID_SCALARS = [
|
|
194
|
+
"ID",
|
|
195
|
+
"String"
|
|
196
|
+
];
|
|
197
|
+
var SCHEMA_EXTENSIONS = /* @__PURE__ */ new Set([
|
|
198
|
+
".gql",
|
|
199
|
+
".graphql"
|
|
200
|
+
]);
|
|
201
|
+
var SKIPPED_DIRECTORIES = /* @__PURE__ */ new Set([
|
|
202
|
+
"node_modules",
|
|
203
|
+
"dist",
|
|
204
|
+
"build",
|
|
205
|
+
"coverage"
|
|
206
|
+
]);
|
|
207
|
+
var PLACEHOLDER_FIELD = /^_empty[A-Za-z0-9]*$/;
|
|
208
|
+
var RbacCoverageError = class extends Error {
|
|
209
|
+
static {
|
|
210
|
+
__name(this, "RbacCoverageError");
|
|
211
|
+
}
|
|
212
|
+
report;
|
|
213
|
+
constructor(report) {
|
|
214
|
+
super(`RBAC coverage check failed (${report.problems.length} problem(s)):
|
|
215
|
+
- ${report.problems.join("\n - ")}`);
|
|
216
|
+
this.name = "RbacCoverageError";
|
|
217
|
+
this.report = report;
|
|
218
|
+
}
|
|
219
|
+
};
|
|
220
|
+
function collectSchemaFiles(path) {
|
|
221
|
+
if (!statSync(path).isDirectory()) return [
|
|
222
|
+
path
|
|
223
|
+
];
|
|
224
|
+
const out = [];
|
|
225
|
+
const entries = readdirSync(path, {
|
|
226
|
+
withFileTypes: true
|
|
227
|
+
}).sort((a, b) => a.name.localeCompare(b.name, "en"));
|
|
228
|
+
for (const entry of entries) {
|
|
229
|
+
const full = join(path, entry.name);
|
|
230
|
+
if (entry.isDirectory()) {
|
|
231
|
+
if (!SKIPPED_DIRECTORIES.has(entry.name) && !entry.name.startsWith(".")) out.push(...collectSchemaFiles(full));
|
|
232
|
+
} else if (SCHEMA_EXTENSIONS.has(extname(entry.name))) {
|
|
233
|
+
out.push(full);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
return out;
|
|
237
|
+
}
|
|
238
|
+
__name(collectSchemaFiles, "collectSchemaFiles");
|
|
239
|
+
function loadRbacSchemaDocuments(paths) {
|
|
240
|
+
const files = (typeof paths === "string" ? [
|
|
241
|
+
paths
|
|
242
|
+
] : paths).flatMap(collectSchemaFiles);
|
|
243
|
+
return files.map((file) => {
|
|
244
|
+
try {
|
|
245
|
+
return {
|
|
246
|
+
file,
|
|
247
|
+
document: parse(readFileSync(file, "utf8"))
|
|
248
|
+
};
|
|
249
|
+
} catch (error) {
|
|
250
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
251
|
+
throw new Error(`Failed to parse GraphQL schema ${file}: ${message}`, {
|
|
252
|
+
cause: error
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
__name(loadRbacSchemaDocuments, "loadRbacSchemaDocuments");
|
|
258
|
+
function isDocumentNode(value) {
|
|
259
|
+
return typeof value === "object" && value !== null && value.kind === Kind.DOCUMENT;
|
|
260
|
+
}
|
|
261
|
+
__name(isDocumentNode, "isDocumentNode");
|
|
262
|
+
function toSchemaDocuments(source) {
|
|
263
|
+
if (typeof source === "string") return loadRbacSchemaDocuments(source);
|
|
264
|
+
if (isDocumentNode(source)) return [
|
|
265
|
+
{
|
|
266
|
+
document: source
|
|
267
|
+
}
|
|
268
|
+
];
|
|
269
|
+
const items = source;
|
|
270
|
+
if (items.every((item) => typeof item === "string")) return loadRbacSchemaDocuments(items);
|
|
271
|
+
return items.map((item) => {
|
|
272
|
+
if (isDocumentNode(item)) return {
|
|
273
|
+
document: item
|
|
274
|
+
};
|
|
275
|
+
if (typeof item === "object" && item !== null && isDocumentNode(item.document)) {
|
|
276
|
+
return item;
|
|
277
|
+
}
|
|
278
|
+
throw new TypeError("RbacCoverageSource arrays must contain only paths, DocumentNodes or RbacSchemaDocuments");
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
__name(toSchemaDocuments, "toSchemaDocuments");
|
|
282
|
+
function rbacDirective(field) {
|
|
283
|
+
return (field.directives ?? []).find((d) => d.name.value === "rbac");
|
|
284
|
+
}
|
|
285
|
+
__name(rbacDirective, "rbacDirective");
|
|
286
|
+
function readRbacDirectiveArgs(field) {
|
|
287
|
+
const directive = rbacDirective(field);
|
|
288
|
+
if (!directive) return null;
|
|
289
|
+
const args = {};
|
|
290
|
+
for (const arg of directive.arguments ?? []) {
|
|
291
|
+
const name = arg.name.value;
|
|
292
|
+
const value = arg.value;
|
|
293
|
+
if (name === "public") {
|
|
294
|
+
if (value.kind === Kind.BOOLEAN) args.public = value.value;
|
|
295
|
+
} else if (name === "actorTypes") {
|
|
296
|
+
if (value.kind === Kind.LIST) {
|
|
297
|
+
args.actorTypes = value.values.flatMap((v) => v.kind === Kind.STRING ? [
|
|
298
|
+
v.value
|
|
299
|
+
] : []);
|
|
300
|
+
} else if (value.kind === Kind.STRING) {
|
|
301
|
+
args.actorTypes = [
|
|
302
|
+
value.value
|
|
303
|
+
];
|
|
304
|
+
}
|
|
305
|
+
} else if (value.kind === Kind.STRING && (name === "action" || name === "resource" || name === "resourceIdArg" || name === "scopeType" || name === "scopeArg")) {
|
|
306
|
+
args[name] = value.value;
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
return args;
|
|
310
|
+
}
|
|
311
|
+
__name(readRbacDirectiveArgs, "readRbacDirectiveArgs");
|
|
312
|
+
function rootTypeNames(docs, configured) {
|
|
313
|
+
if (configured) return new Set(configured);
|
|
314
|
+
const names = new Set(DEFAULT_RBAC_ROOT_TYPES);
|
|
315
|
+
for (const { document } of docs) {
|
|
316
|
+
for (const def of document.definitions) {
|
|
317
|
+
if (def.kind === Kind.SCHEMA_DEFINITION || def.kind === Kind.SCHEMA_EXTENSION) {
|
|
318
|
+
for (const op of def.operationTypes ?? []) names.add(op.type.name.value);
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
return names;
|
|
323
|
+
}
|
|
324
|
+
__name(rootTypeNames, "rootTypeNames");
|
|
325
|
+
function inputFieldsByType(docs) {
|
|
326
|
+
const out = /* @__PURE__ */ new Map();
|
|
327
|
+
for (const { document } of docs) {
|
|
328
|
+
for (const def of document.definitions) {
|
|
329
|
+
if (def.kind === Kind.INPUT_OBJECT_TYPE_DEFINITION || def.kind === Kind.INPUT_OBJECT_TYPE_EXTENSION) {
|
|
330
|
+
const input = def;
|
|
331
|
+
const existing = out.get(input.name.value) ?? [];
|
|
332
|
+
existing.push(...input.fields ?? []);
|
|
333
|
+
out.set(input.name.value, existing);
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
return out;
|
|
338
|
+
}
|
|
339
|
+
__name(inputFieldsByType, "inputFieldsByType");
|
|
340
|
+
function unwrapSingular(type) {
|
|
341
|
+
if (type.kind === Kind.NON_NULL_TYPE) return unwrapSingular(type.type);
|
|
342
|
+
if (type.kind === Kind.LIST_TYPE) return null;
|
|
343
|
+
return type.name.value;
|
|
344
|
+
}
|
|
345
|
+
__name(unwrapSingular, "unwrapSingular");
|
|
346
|
+
function rbacArgPathProblem(field, path, inputs, options = {}) {
|
|
347
|
+
const maxSegments = options.maxSegments ?? DEFAULT_MAX_RBAC_ARG_PATH_SEGMENTS;
|
|
348
|
+
const idScalars = new Set(options.idScalars ?? DEFAULT_ID_SCALARS);
|
|
349
|
+
const segments = path.split(".");
|
|
350
|
+
if (segments.some((s) => s.length === 0)) return `"${path}" has an empty segment`;
|
|
351
|
+
if (segments.length > maxSegments) {
|
|
352
|
+
return `"${path}" has ${segments.length} segments; the gateway reads at most ${maxSegments} (it drops nested object literals)`;
|
|
353
|
+
}
|
|
354
|
+
const [head, ...rest] = segments;
|
|
355
|
+
const arg = (field.arguments ?? []).find((a) => a.name.value === head);
|
|
356
|
+
if (!arg) return `no argument "${head}"`;
|
|
357
|
+
let typeName = unwrapSingular(arg.type);
|
|
358
|
+
let walked = head;
|
|
359
|
+
for (const segment of rest) {
|
|
360
|
+
if (typeName === null) return `"${walked}" is a list`;
|
|
361
|
+
const fields = inputs.get(typeName);
|
|
362
|
+
if (!fields) return `"${walked}" is ${typeName}, not an input object`;
|
|
363
|
+
const next = fields.find((f) => f.name.value === segment);
|
|
364
|
+
if (!next) return `${typeName} has no field "${segment}"`;
|
|
365
|
+
typeName = unwrapSingular(next.type);
|
|
366
|
+
walked = `${walked}.${segment}`;
|
|
367
|
+
}
|
|
368
|
+
if (typeName === null) return `"${walked}" is a list`;
|
|
369
|
+
if (!idScalars.has(typeName)) return `"${walked}" is ${typeName}, not ${[
|
|
370
|
+
...idScalars
|
|
371
|
+
].join("/")}`;
|
|
372
|
+
return null;
|
|
373
|
+
}
|
|
374
|
+
__name(rbacArgPathProblem, "rbacArgPathProblem");
|
|
375
|
+
function malformedReason(rbac) {
|
|
376
|
+
if (rbac.public === true) return null;
|
|
377
|
+
if (rbac.scopeType === "self") return null;
|
|
378
|
+
const hasActionResource = Boolean(rbac.action) && Boolean(rbac.resource);
|
|
379
|
+
if (!hasActionResource && rbac.actorTypes && rbac.actorTypes.length > 0) {
|
|
380
|
+
if (rbac.action || rbac.resource) return "actorTypes gate sets only one of action/resource";
|
|
381
|
+
return null;
|
|
382
|
+
}
|
|
383
|
+
if (!hasActionResource) return "missing action or resource (and not public, self or actorTypes-only)";
|
|
384
|
+
if (!rbac.scopeType) return "missing scopeType";
|
|
385
|
+
if (!RBAC_GATED_SCOPE_TYPES.includes(rbac.scopeType)) {
|
|
386
|
+
return `unknown scopeType "${rbac.scopeType}"`;
|
|
387
|
+
}
|
|
388
|
+
if (rbac.scopeType === "entity" && !rbac.scopeArg) return "entity scope requires scopeArg";
|
|
389
|
+
return null;
|
|
390
|
+
}
|
|
391
|
+
__name(malformedReason, "malformedReason");
|
|
392
|
+
function pinEntries(pin) {
|
|
393
|
+
return Array.isArray(pin) ? [
|
|
394
|
+
...pin
|
|
395
|
+
] : Object.keys(pin);
|
|
396
|
+
}
|
|
397
|
+
__name(pinEntries, "pinEntries");
|
|
398
|
+
function matchesEntry(field, entry) {
|
|
399
|
+
return entry === field.name || entry === field.coordinate;
|
|
400
|
+
}
|
|
401
|
+
__name(matchesEntry, "matchesEntry");
|
|
402
|
+
function sortLines(lines) {
|
|
403
|
+
return lines.sort((a, b) => a.localeCompare(b, "en"));
|
|
404
|
+
}
|
|
405
|
+
__name(sortLines, "sortLines");
|
|
406
|
+
function collectRootFields(docs, roots, exempt) {
|
|
407
|
+
const out = [];
|
|
408
|
+
for (const { document, file } of docs) {
|
|
409
|
+
for (const def of document.definitions) {
|
|
410
|
+
if (def.kind !== Kind.OBJECT_TYPE_DEFINITION && def.kind !== Kind.OBJECT_TYPE_EXTENSION) continue;
|
|
411
|
+
const type = def.name.value;
|
|
412
|
+
if (!roots.has(type)) continue;
|
|
413
|
+
for (const definition of def.fields ?? []) {
|
|
414
|
+
const name = definition.name.value;
|
|
415
|
+
const coordinate = `${type}.${name}`;
|
|
416
|
+
if (PLACEHOLDER_FIELD.test(name) || exempt.has(name) || exempt.has(coordinate)) continue;
|
|
417
|
+
const root = {
|
|
418
|
+
type,
|
|
419
|
+
name,
|
|
420
|
+
coordinate,
|
|
421
|
+
rbac: readRbacDirectiveArgs(definition)
|
|
422
|
+
};
|
|
423
|
+
if (file) root.file = file;
|
|
424
|
+
out.push({
|
|
425
|
+
root,
|
|
426
|
+
definition
|
|
427
|
+
});
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
return out;
|
|
432
|
+
}
|
|
433
|
+
__name(collectRootFields, "collectRootFields");
|
|
434
|
+
function scopeArgPath(rbac) {
|
|
435
|
+
if (rbac.public === true) return void 0;
|
|
436
|
+
if (rbac.scopeType === "entity") return rbac.scopeArg;
|
|
437
|
+
if (rbac.scopeType === "billing") return rbac.scopeArg ?? DEFAULT_BILLING_SCOPE_ARG;
|
|
438
|
+
return void 0;
|
|
439
|
+
}
|
|
440
|
+
__name(scopeArgPath, "scopeArgPath");
|
|
441
|
+
function argPathProblems(definition, rbac, context) {
|
|
442
|
+
const options = {
|
|
443
|
+
maxSegments: context.maxSegments,
|
|
444
|
+
idScalars: [
|
|
445
|
+
...context.idScalars
|
|
446
|
+
]
|
|
447
|
+
};
|
|
448
|
+
const checks = [
|
|
449
|
+
{
|
|
450
|
+
label: "resourceIdArg",
|
|
451
|
+
path: rbac.resourceIdArg
|
|
452
|
+
},
|
|
453
|
+
{
|
|
454
|
+
label: "scopeArg",
|
|
455
|
+
path: scopeArgPath(rbac)
|
|
456
|
+
}
|
|
457
|
+
];
|
|
458
|
+
return checks.flatMap(({ label, path }) => {
|
|
459
|
+
if (path === void 0) return [];
|
|
460
|
+
const problem = rbacArgPathProblem(definition, path, context.inputs, options);
|
|
461
|
+
return problem ? [
|
|
462
|
+
{
|
|
463
|
+
label,
|
|
464
|
+
problem
|
|
465
|
+
}
|
|
466
|
+
] : [];
|
|
467
|
+
});
|
|
468
|
+
}
|
|
469
|
+
__name(argPathProblems, "argPathProblems");
|
|
470
|
+
function selfScopedPinProblems(rootFields, pin) {
|
|
471
|
+
if (!pin) return {
|
|
472
|
+
unreviewed: [],
|
|
473
|
+
stale: []
|
|
474
|
+
};
|
|
475
|
+
const entries = pinEntries(pin);
|
|
476
|
+
const selfScoped = rootFields.filter((f) => f.rbac?.scopeType === "self" && f.rbac.public !== true);
|
|
477
|
+
return {
|
|
478
|
+
unreviewed: selfScoped.filter((field) => !entries.some((entry) => matchesEntry(field, entry))).map((field) => field.coordinate),
|
|
479
|
+
stale: entries.filter((entry) => !selfScoped.some((field) => matchesEntry(field, entry)))
|
|
480
|
+
};
|
|
481
|
+
}
|
|
482
|
+
__name(selfScopedPinProblems, "selfScopedPinProblems");
|
|
483
|
+
function analyzeRbacCoverage(source, options = {}) {
|
|
484
|
+
const docs = toSchemaDocuments(source);
|
|
485
|
+
const collected = collectRootFields(docs, rootTypeNames(docs, options.rootTypes), new Set(options.exemptFields));
|
|
486
|
+
const pathContext = {
|
|
487
|
+
inputs: inputFieldsByType(docs),
|
|
488
|
+
maxSegments: options.maxArgPathSegments ?? DEFAULT_MAX_RBAC_ARG_PATH_SEGMENTS,
|
|
489
|
+
idScalars: new Set(options.idScalars ?? DEFAULT_ID_SCALARS)
|
|
490
|
+
};
|
|
491
|
+
const missing = [];
|
|
492
|
+
const malformed = [];
|
|
493
|
+
const unresolvedArgPaths = [];
|
|
494
|
+
for (const { root, definition } of collected) {
|
|
495
|
+
const { rbac, coordinate } = root;
|
|
496
|
+
if (!rbac) {
|
|
497
|
+
missing.push(coordinate);
|
|
498
|
+
continue;
|
|
499
|
+
}
|
|
500
|
+
const reason = malformedReason(rbac);
|
|
501
|
+
if (reason) malformed.push(`${coordinate}: ${reason}`);
|
|
502
|
+
for (const { label, problem } of argPathProblems(definition, rbac, pathContext)) {
|
|
503
|
+
unresolvedArgPaths.push(`${coordinate}: ${label} ${problem}`);
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
const rootFields = collected.map(({ root }) => root);
|
|
507
|
+
const pins = selfScopedPinProblems(rootFields, options.selfScopedFields);
|
|
508
|
+
const unreviewedSelfScoped = sortLines(pins.unreviewed);
|
|
509
|
+
const staleSelfScoped = sortLines(pins.stale);
|
|
510
|
+
sortLines(missing);
|
|
511
|
+
sortLines(malformed);
|
|
512
|
+
sortLines(unresolvedArgPaths);
|
|
513
|
+
const minRootFields = options.minRootFields ?? 1;
|
|
514
|
+
const problems = [
|
|
515
|
+
...rootFields.length < minRootFields ? [
|
|
516
|
+
`found ${rootFields.length} root field(s), expected at least ${minRootFields} (wrong schema path?)`
|
|
517
|
+
] : [],
|
|
518
|
+
...missing.map((c) => `${c}: missing @rbac directive`),
|
|
519
|
+
...malformed.map((m) => `malformed @rbac on ${m}`),
|
|
520
|
+
...unresolvedArgPaths.map((p) => `unresolvable @rbac path on ${p}`),
|
|
521
|
+
...unreviewedSelfScoped.map((c) => `${c}: scopeType "self" is not in the reviewed selfScopedFields list`),
|
|
522
|
+
...staleSelfScoped.map((e) => `selfScopedFields entry "${e}" matches no self-scoped root field (stale)`)
|
|
523
|
+
];
|
|
524
|
+
return {
|
|
525
|
+
rootFields,
|
|
526
|
+
missing,
|
|
527
|
+
malformed,
|
|
528
|
+
unresolvedArgPaths,
|
|
529
|
+
unreviewedSelfScoped,
|
|
530
|
+
staleSelfScoped,
|
|
531
|
+
problems,
|
|
532
|
+
ok: problems.length === 0
|
|
533
|
+
};
|
|
534
|
+
}
|
|
535
|
+
__name(analyzeRbacCoverage, "analyzeRbacCoverage");
|
|
536
|
+
function assertRbacCoverage(source, options = {}) {
|
|
537
|
+
const report = analyzeRbacCoverage(source, options);
|
|
538
|
+
if (!report.ok) throw new RbacCoverageError(report);
|
|
539
|
+
return report;
|
|
540
|
+
}
|
|
541
|
+
__name(assertRbacCoverage, "assertRbacCoverage");
|
|
542
|
+
|
|
543
|
+
export { DEFAULT_MAX_RBAC_ARG_PATH_SEGMENTS, DEFAULT_RBAC_ROOT_TYPES, NAME_MAX_LENGTH, NAME_MIN_LENGTH, RBAC_GATED_SCOPE_TYPES, RBAC_SCOPE_TYPES, RbacCoverageError, USERNAME_MAX_LENGTH, USERNAME_MIN_LENGTH, analyzeRbacCoverage, assertRbacCoverage, lengthInCodePoints, loadRbacSchemaDocuments, rbacArgPathProblem, readRbacDirectiveArgs, validateName, validateUserFields, validateUsername, validation_exports };
|
|
544
|
+
//# sourceMappingURL=chunk-WVOW3CJR.js.map
|
|
545
|
+
//# sourceMappingURL=chunk-WVOW3CJR.js.map
|