@sasjs/api 0.41.0 → 0.43.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -23,6 +23,29 @@ const RUN_ROUTES = [
23
23
  'POST /SASjsApi/code/execute',
24
24
  'POST /SASjsApi/code/trigger'
25
25
  ];
26
+ /**
27
+ * The run routes a permission rule addresses.
28
+ *
29
+ * The triggers are not in the inventory - the server's rules do not reach them -
30
+ * so they are gated on the caller alone, exactly as they are there.
31
+ */
32
+ const GRANTED_RUN_ROUTES = [
33
+ 'GET /SASjsApi/stp/execute',
34
+ 'POST /SASjsApi/stp/execute',
35
+ 'POST /SASjsApi/code/execute'
36
+ ];
37
+ /**
38
+ * The host's own reports, which the server mounts `verifyAdmin` in front of.
39
+ *
40
+ * The check belongs to the package for the same reason the record routes' does:
41
+ * the mock is a host too, and a browser build that published the host log to a
42
+ * restricted account would be a leak with a fixture in front of it.
43
+ */
44
+ const ADMIN_ROUTES = [
45
+ 'GET /SASjsApi/admin/diagnostics',
46
+ 'GET /SASjsApi/admin/logs',
47
+ 'GET /SASjsApi/admin/logs/{fileName}'
48
+ ];
26
49
  /** A payload value, when the caller actually stated one. */
27
50
  const given = (value) => typeof value === 'string' && value !== '' ? value : undefined;
28
51
  /**
@@ -111,10 +134,42 @@ export const dispatch = async (services, routeId, request = {}) => {
111
134
  // told which capability it is missing rather than sent round a login loop, so
112
135
  // the gate is tested on the service that serves the route.
113
136
  const runtimeScoped = RUN_ROUTES.includes(routeId);
114
- if ((identityScoped && records) || (runtimeScoped && services.runtime)) {
115
- const subject = request.subject ??
116
- (identity ? await identity.authenticate(request.token) : undefined);
117
- if (!subject) {
137
+ // The two checks the server mounts ahead of these routes: `verifyAdmin` in
138
+ // front of the host's own reports, and `authorize` on the run routes the
139
+ // permission rules address. Asked of the record service, so the answer a
140
+ // request meets is the one `myAuthorizedRoutes` reported for the same caller.
141
+ const adminScoped = ADMIN_ROUTES.includes(routeId) && Boolean(services.admin);
142
+ const grantScoped = Boolean(records) && GRANTED_RUN_ROUTES.includes(routeId);
143
+ // The administrator routes need the record service to answer the question the
144
+ // host's middleware answers on a server. A host that supplies the reports
145
+ // without one is told which capability it is missing, rather than serving its
146
+ // own log to every caller - which is what a gate that simply skipped them did.
147
+ if (adminScoped && !records)
148
+ throw new ApiError(501, `This host has no record service, so it cannot answer ${routeId}.`);
149
+ /**
150
+ * The caller, from the request's own subject or the token it carried.
151
+ *
152
+ * Resolved once per request: the token is looked up in the store, and every
153
+ * gate below asks the same question - re-reading it for each would be a
154
+ * second lookup for one answer.
155
+ */
156
+ let subject = request.subject;
157
+ let subjectResolved = Boolean(request.subject);
158
+ const caller = async () => {
159
+ if (!subjectResolved) {
160
+ subject = identity
161
+ ? await identity.authenticate(request.token)
162
+ : undefined;
163
+ subjectResolved = true;
164
+ }
165
+ return subject;
166
+ };
167
+ if ((identityScoped && records) ||
168
+ (runtimeScoped && services.runtime) ||
169
+ adminScoped ||
170
+ grantScoped) {
171
+ const callerSubject = await caller();
172
+ if (!callerSubject) {
118
173
  // A host may serve records without a sign-in flow - it resolves the caller
119
174
  // itself and passes `subject`. Without one it cannot authenticate at all,
120
175
  // and a 401 would send every request round the login loop for ever, so it
@@ -123,6 +178,20 @@ export const dispatch = async (services, routeId, request = {}) => {
123
178
  throw new ApiError(501, `This host has no identity service, so it cannot answer ${routeId}.`);
124
179
  throw new ApiError(401, 'Unauthorized');
125
180
  }
181
+ // `verifyAdmin`, in the wording a caller sees on a server - except for a
182
+ // caller the server refuses before that middleware runs, which meets the
183
+ // token refusal instead
184
+ if (adminScoped && !(await records.mayAdminister(callerSubject))) {
185
+ // a caller the server refuses before this middleware runs meets the token
186
+ // refusal, not the admin wording
187
+ if (!(await records.mayAct(callerSubject)))
188
+ throw new ApiError(401, 'Unauthorized');
189
+ throw new ApiError(401, 'Admin account required');
190
+ }
191
+ // `authorize`: the rules this host's interface reads decide the call, not
192
+ // only the menu entry beside it
193
+ if (grantScoped && !(await records.mayUseRoute(callerSubject, route.path)))
194
+ throw new ApiError(401, 'Unauthorized');
126
195
  }
127
196
  validate(route, request);
128
197
  const query = request.query || {};
@@ -173,12 +242,6 @@ export const dispatch = async (services, routeId, request = {}) => {
173
242
  throw new ApiError(501, `This host has no record service, so it cannot answer ${routeId}.`);
174
243
  return records;
175
244
  };
176
- /** The caller, from the request's own subject or the token it carried. */
177
- const caller = async () => {
178
- if (request.subject)
179
- return request.subject;
180
- return identity?.authenticate(request.token);
181
- };
182
245
  if (routeId.startsWith('POST /SASjsApi/auth/') ||
183
246
  routeId.startsWith('POST /SASLogon/') ||
184
247
  routeId === 'GET /SASLogon/logout' ||
@@ -238,10 +301,11 @@ export const dispatch = async (services, routeId, request = {}) => {
238
301
  }
239
302
  // ------------------------------------------------- a host's own reports
240
303
  //
241
- // The administrator middleware is the host's (sasjs/server mounts
242
- // authenticateAccessToken and verifyAdmin ahead of all of these), which is
243
- // why their 401 is marked host-only in the overlay. The package answers a
244
- // body, and for a log tail the refusals that belong to the name rule.
304
+ // The administrator check is the package's, from the caller the transport
305
+ // resolved: `mayAdminister` is the decision `verifyAdmin` makes on a server,
306
+ // asked here so a browser host that supplies these reports cannot serve them
307
+ // to anyone who asks. What stays the host's is the body, and for a log tail
308
+ // the refusals that belong to the name rule.
245
309
  case 'GET /SASjsApi/admin/diagnostics': {
246
310
  if (!services.admin)
247
311
  throw new ApiError(501, `This host has no admin service, so it cannot answer ${routeId}.`);
@@ -99,6 +99,41 @@ export interface RecordService {
99
99
  listAuthorizedRoutes(caller: Subject | undefined): Promise<AuthorizedRoutesResponse>;
100
100
  /** The routes this caller may use, from the rules that bear on them. */
101
101
  myAuthorizedRoutes(caller: Subject | undefined): Promise<AuthorizedRoutesResponse>;
102
+ /**
103
+ * Whether this caller may administer - the decision `requireAdmin` acts on,
104
+ * asked as a question. A host that gates its own transport reads it here so
105
+ * its answer and the package's cannot disagree.
106
+ *
107
+ * `false` for a caller that is absent or whose account may no longer act, as
108
+ * well as for one that simply is not an administrator. Where the two refusals
109
+ * have to be worded differently - a server refuses an inactive caller before
110
+ * the administrator middleware runs - ask `mayAct` as well.
111
+ */
112
+ mayAdminister(caller: Subject | undefined): Promise<boolean>;
113
+ /**
114
+ * Whether this caller may act at all: a live account behind the subject.
115
+ *
116
+ * A server answers `401 Unauthorized` for a caller whose account was
117
+ * deactivated after the token was issued, and never reaches the administrator
118
+ * check, so a host that gates its own transport needs both answers to word the
119
+ * refusal the way a caller sees it there.
120
+ */
121
+ mayAct(caller: Subject | undefined): Promise<boolean>;
122
+ /**
123
+ * Whether this caller may call `path`, by the same decision
124
+ * `myAuthorizedRoutes` reports: an always-visible route and a public-group
125
+ * grant are open to any caller, and otherwise the rules decide. `path` is the
126
+ * operation as the route table states it (`/SASjsApi/code/execute`).
127
+ *
128
+ * An administrator holds every route, including a path the inventory does not
129
+ * list, because the question is what the rules admit rather than what the
130
+ * rules can address. For anyone else a path outside the inventory answers
131
+ * `false`, exactly as `myAuthorizedRoutes` omits it.
132
+ *
133
+ * `false` for a caller that is absent or deactivated, so a host answers a
134
+ * `401` rather than letting the call through.
135
+ */
136
+ mayUseRoute(caller: Subject | undefined, path: string): Promise<boolean>;
102
137
  listPermissions(caller: Subject | undefined): Promise<PermissionResponse[]>;
103
138
  createPermission(caller: Subject | undefined, body: {
104
139
  path: string;
@@ -1 +1 @@
1
- {"version":3,"file":"records.d.ts","sourceRoot":"","sources":["../../../src/identity/records.ts"],"names":[],"mappings":"AAuBA,OAAO,KAAK,EACV,oBAAoB,EAGpB,aAAa,EAEb,OAAO,EACR,MAAM,YAAY,CAAA;AAEnB,iFAAiF;AACjF,MAAM,WAAW,YAAY;IAC3B,GAAG,EAAE,MAAM,CAAA;IACX,QAAQ,EAAE,MAAM,CAAA;IAChB,WAAW,EAAE,MAAM,CAAA;IACnB,OAAO,EAAE,OAAO,CAAA;CACjB;AAED,MAAM,WAAW,mBAAoB,SAAQ,YAAY;IACvD,QAAQ,EAAE,OAAO,CAAA;I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M,CAAA;KACpB,GACA,OAAO,CAAC,kBAAkB,CAAC,CAAA;IAC9B,gBAAgB,CACd,MAAM,EAAE,OAAO,GAAG,SAAS,EAC3B,GAAG,EAAE,MAAM,EACX,IAAI,EAAE;QAAE,OAAO,EAAE,MAAM,CAAA;KAAE,GACxB,OAAO,CAAC,kBAAkB,CAAC,CAAA;IAC9B,gBAAgB,CAAC,MAAM,EAAE,OAAO,GAAG,SAAS,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAA;IAE3E,WAAW,CAAC,MAAM,EAAE,OAAO,GAAG,SAAS,GAAG,OAAO,CAAC,oBAAoB,EAAE,CAAC,CAAA;IACzE,YAAY,CACV,MAAM,EAAE,OAAO,GAAG,SAAS,EAC3B,IAAI,EAAE,oBAAoB,GACzB,OAAO,CAAC,oBAAoB,CAAC,CAAA;IAEhC,wEAAwE;IACxE,cAAc,CACZ,MAAM,EAAE,OAAO,GAAG,SAAS,EAC3B,IAAI,EAAE;QAAE,eAAe,EAAE,MAAM,CAAC;QAAC,WAAW,EAAE,MAAM,CAAA;KAAE,GACrD,OAAO,CAAC,IAAI,CAAC,CAAA;CACjB;AAiFD,eAAO,MAAM,mBAAmB,GAAI,OAAO,OAAO,KAAG,MAKf,CAAA;AAetC,MAAM,WAAW,oBAAoB;IACnC;;;;OAIG;IACH,eAAe,CAAC,EAAE,MAAM,EAAE,CAAA;CAC3B;AAED,eAAO,MAAM,mBAAmB,GAC9B,OAAO,aAAa,EACpB,UAAS,oBAAyB,KACjC,aAmfF,CAAA"}
@@ -20,6 +20,20 @@ const ALL_USERS_GROUP = 'all-users';
20
20
  /** The group whose membership the API refuses to change. */
21
21
  const PUBLIC_GROUP = 'public';
22
22
  const unauthorized = () => new ApiError(401, 'Unauthorized');
23
+ /**
24
+ * The account behind a caller, while it may still act, or undefined.
25
+ *
26
+ * The reads every administration route makes: the server re-reads the user on
27
+ * every request and refuses a deactivated account, so a token that outlived the
28
+ * account must not administer anything. The gate asks this rather than
29
+ * `requireCaller` when a refusal is the caller's answer rather than an error.
30
+ */
31
+ const readerOf = async (store, caller) => {
32
+ if (!caller)
33
+ return undefined;
34
+ const user = await store.userById(caller.userId);
35
+ return user && user.isActive ? user : undefined;
36
+ };
23
37
  /**
24
38
  * The caller, while the account behind it may still act.
25
39
  *
@@ -28,10 +42,8 @@ const unauthorized = () => new ApiError(401, 'Unauthorized');
28
42
  * token or session that outlived the account must not administer anything.
29
43
  */
30
44
  const requireCaller = async (store, caller) => {
31
- if (!caller)
32
- throw unauthorized();
33
- const user = await store.userById(caller.userId);
34
- if (!user || !user.isActive)
45
+ const user = await readerOf(store, caller);
46
+ if (!user)
35
47
  throw unauthorized();
36
48
  return user;
37
49
  };
@@ -82,6 +94,37 @@ const asGroup = (group) => ({
82
94
  });
83
95
  export const createRecordService = (store, options = {}) => {
84
96
  const grantableRoutes = options.grantableRoutes || DEFAULT_GRANTABLE_ROUTES;
97
+ /**
98
+ * The routes this reader is granted. One pass over the rules per call, and no
99
+ * read of the groups table unless some rule names a group - the transport
100
+ * gate runs on every request, so the cheap path is the common one.
101
+ */
102
+ const grantedRoutesFor = async (reader) => {
103
+ if (reader.isAdmin)
104
+ return new Set(grantableRoutes);
105
+ const rules = await store.permissions();
106
+ const mine = {
107
+ user: rules
108
+ .filter((rule) => rule.user === reader.uid)
109
+ .map((rule) => ({ path: rule.path, setting: rule.setting })),
110
+ group: rules
111
+ .filter((rule) => rule.group !== undefined && reader.groups.includes(rule.group))
112
+ .map((rule) => ({ path: rule.path, setting: rule.setting }))
113
+ };
114
+ // a grant on the reserved public group admits any caller, and it is checked
115
+ // before the rules so no user or group rule can take it away
116
+ const publicGroup = rules.some((rule) => rule.group !== undefined)
117
+ ? (await store.groups()).find((group) => group.name === PUBLIC_GROUP)
118
+ : undefined;
119
+ const publicGrants = publicGroup
120
+ ? rules
121
+ .filter((rule) => rule.group === publicGroup.uid && rule.setting === 'Grant')
122
+ .map((rule) => rule.path)
123
+ : [];
124
+ return new Set(grantableRoutes.filter((path) => ALWAYS_VISIBLE_ROUTES.includes(path) ||
125
+ publicGrants.includes(path) ||
126
+ isRouteGranted(mine, path, topLevelRouteFor(path))));
127
+ };
85
128
  const groupDetails = async (group) => {
86
129
  const members = [];
87
130
  for (const uid of group.users) {
@@ -296,32 +339,30 @@ export const createRecordService = (store, options = {}) => {
296
339
  },
297
340
  async myAuthorizedRoutes(caller) {
298
341
  const reader = await requireCaller(store, caller);
299
- // an administrator holds every route, and the always-visible ones are
300
- // visible whatever the rules say
342
+ const granted = await grantedRoutesFor(reader);
343
+ return { paths: grantableRoutes.filter((path) => granted.has(path)) };
344
+ },
345
+ async mayAdminister(caller) {
346
+ const reader = await readerOf(store, caller);
347
+ return Boolean(reader?.isAdmin);
348
+ },
349
+ async mayAct(caller) {
350
+ return Boolean(await readerOf(store, caller));
351
+ },
352
+ async mayUseRoute(caller, path) {
353
+ const reader = await readerOf(store, caller);
354
+ if (!reader)
355
+ return false;
356
+ // an administrator holds every route, listed or not: the question is
357
+ // whether the rules admit the caller, and the inventory is only what the
358
+ // rules can address - a host may gate a route the inventory has not caught
359
+ // up with, and refusing an administrator there would be the wrong answer
301
360
  if (reader.isAdmin)
302
- return { paths: [...grantableRoutes] };
303
- const rules = await store.permissions();
304
- const mine = {
305
- user: rules
306
- .filter((rule) => rule.user === reader.uid)
307
- .map((rule) => ({ path: rule.path, setting: rule.setting })),
308
- group: rules
309
- .filter((rule) => rule.group !== undefined && reader.groups.includes(rule.group))
310
- .map((rule) => ({ path: rule.path, setting: rule.setting }))
311
- };
312
- // a grant on the reserved public group admits any caller, and it is
313
- // checked before the rules so no user or group rule can take it away
314
- const publicGroup = (await store.groups()).find((group) => group.name === 'public');
315
- const publicGrants = publicGroup
316
- ? rules
317
- .filter((rule) => rule.group === publicGroup.uid && rule.setting === 'Grant')
318
- .map((rule) => rule.path)
319
- : [];
320
- return {
321
- paths: grantableRoutes.filter((path) => ALWAYS_VISIBLE_ROUTES.includes(path) ||
322
- publicGrants.includes(path) ||
323
- isRouteGranted(mine, path, topLevelRouteFor(path)))
324
- };
361
+ return true;
362
+ // otherwise the answer is exactly the one `myAuthorizedRoutes` reports, so
363
+ // a menu entry and the call behind it cannot disagree
364
+ const granted = await grantedRoutesFor(reader);
365
+ return granted.has(path);
325
366
  },
326
367
  async listPermissions(caller) {
327
368
  const reader = await requireCaller(store, caller);
@@ -1 +1 @@
1
- {"version":3,"file":"overlay.d.ts","sourceRoot":"","sources":["../../../src/routes/overlay.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,OAAO,KAAK,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AAGzD,yDAAyD;AACzD,UAAU,aAAa;IACrB,YAAY,CAAC,EAAE,UAAU,EAAE,CAAA;IAC3B;;;;;;;OAOG;IACH,aAAa,CAAC,EAAE,MAAM,EAAE,CAAA;IACxB;;;OAGG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;CACpC;AAED;;;;;;GAMG;AACH,MAAM,MAAM,YAAY,GAAG,aAAa,GACtC,CAAC;IAAE,WAAW,EAAE,IAAI,CAAA;CAAE,GAAG;IAAE,WAAW,EAAE,KAAK,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC,CAAA;AAElE,kFAAkF;AAClF,eAAO,MAAM,gBAAgB,EAAE,MAAM,CAAC,MAAM,EAAE,UAAU,EAAE,CAgBzD,CAAA;AAmBD,eAAO,MAAM,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,YAAY,CA0WhD,CAAA;AAED,wEAAwE;AACxE,eAAO,MAAM,OAAO,EAAE,UAAU,EAA8C,CAAA"}
1
+ {"version":3,"file":"overlay.d.ts","sourceRoot":"","sources":["../../../src/routes/overlay.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,OAAO,KAAK,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AAGzD,yDAAyD;AACzD,UAAU,aAAa;IACrB,YAAY,CAAC,EAAE,UAAU,EAAE,CAAA;IAC3B;;;;;;;OAOG;IACH,aAAa,CAAC,EAAE,MAAM,EAAE,CAAA;IACxB;;;OAGG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;CACpC;AAED;;;;;;GAMG;AACH,MAAM,MAAM,YAAY,GAAG,aAAa,GACtC,CAAC;IAAE,WAAW,EAAE,IAAI,CAAA;CAAE,GAAG;IAAE,WAAW,EAAE,KAAK,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC,CAAA;AAElE,kFAAkF;AAClF,eAAO,MAAM,gBAAgB,EAAE,MAAM,CAAC,MAAM,EAAE,UAAU,EAAE,CAgBzD,CAAA;AAmBD,eAAO,MAAM,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,YAAY,CAyWhD,CAAA;AAED,wEAAwE;AACxE,eAAO,MAAM,OAAO,EAAE,UAAU,EAA8C,CAAA"}
@@ -63,12 +63,11 @@ export const OVERLAY = {
63
63
  responses: { 200: 'OK!' }
64
64
  },
65
65
  // Admin - the host's own process and files, over the reports it supplies.
66
- // The administrator check is the host's mount point (sasjs/server puts
67
- // authenticateAccessToken and verifyAdmin in front of all three), so its 401
68
- // - and the 500 a genuine failure answers - are host-only statuses.
66
+ // The administrator check is the package's, so its 401 is a case like any
67
+ // other; the 500 a genuine failure answers stays the host's.
69
68
  'GET /SASjsApi/admin/diagnostics': {
70
69
  implemented: true,
71
- hostResponses: [401, 500],
70
+ hostResponses: [500],
72
71
  responses: {
73
72
  200: {
74
73
  build: { version: '', gitHash: '', buildDate: null },
@@ -108,14 +107,14 @@ export const OVERLAY = {
108
107
  },
109
108
  'GET /SASjsApi/admin/logs': {
110
109
  implemented: true,
111
- hostResponses: [401, 500],
110
+ hostResponses: [500],
112
111
  responses: { 200: { files: [] } }
113
112
  },
114
113
  'GET /SASjsApi/admin/logs/{fileName}': {
115
114
  implemented: true,
116
115
  // 400 and 404 are the name rule's, and this package answers them before the
117
116
  // host is asked for anything
118
- hostResponses: [401, 500],
117
+ hostResponses: [500],
119
118
  responses: {
120
119
  200: {
121
120
  fileName: '',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sasjs/api",
3
- "version": "0.41.0",
3
+ "version": "0.43.0",
4
4
  "description": "The SASjs Server API, described once - a generated route table, the services behind it, and the conformance corpus both hosts run",
5
5
  "license": "MIT",
6
6
  "author": "SASjs",
@@ -38,6 +38,7 @@
38
38
  "test": "jest --coverage",
39
39
  "coverage:comment": "node scripts/coverage-comment.mjs",
40
40
  "test:watch": "jest --watch",
41
+ "bench:gates": "node tools/bench-gates.cjs",
41
42
  "lint": "prettier --check \"{src,spec,scripts,tools}/**/*.{ts,mjs,cjs,md}\" \"*.{json,md}\"",
42
43
  "lint:fix": "prettier --write \"{src,spec,scripts,tools}/**/*.{ts,mjs,cjs,md}\" \"*.{json,md}\"",
43
44
  "sync:routes": "node scripts/sync-routes.mjs",
@@ -75,6 +76,6 @@
75
76
  "minimatch": "11.0.4"
76
77
  },
77
78
  "dependencies": {
78
- "@sasjs/lint": "^5.5.3"
79
+ "@sasjs/lint": "5.5.7"
79
80
  }
80
81
  }