@sasjs/api 0.43.0 → 0.44.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.
@@ -6,6 +6,7 @@ const deploymentFile_js_1 = require("../drive/deploymentFile.js");
6
6
  const service_js_1 = require("../appstream/service.js");
7
7
  const web_js_1 = require("../appstream/web.js");
8
8
  const bundle_js_1 = require("../appstream/bundle.js");
9
+ const routeAccess_js_1 = require("../identity/routeAccess.js");
9
10
  const types_js_1 = require("../types.js");
10
11
  const deploy_js_1 = require("../drive/deploy.js");
11
12
  /** The defaults sasjs/server's Client schema applies to an omitted lifetime. */
@@ -27,16 +28,22 @@ const RUN_ROUTES = [
27
28
  'POST /SASjsApi/code/trigger'
28
29
  ];
29
30
  /**
30
- * The run routes a permission rule addresses.
31
+ * The operations a permission rule addresses.
31
32
  *
32
- * The triggers are not in the inventory - the server's rules do not reach them -
33
- * so they are gated on the caller alone, exactly as they are there.
33
+ * The inventory less the two entries that name a whole surface rather than an
34
+ * operation: `/SASjsApi` and `/AppStream` are prefixes a rule can cover
35
+ * everything beneath with, not routes a request is dispatched to. This is the
36
+ * server's `isAuthorizingRoute` - the same inventory, minus the routes every
37
+ * caller sees - so a request meets a rule check on exactly the calls it does
38
+ * there: the run routes, the drive routes, and the app stream's own paths.
39
+ *
40
+ * The triggers are absent because the rules do not reach them, and the record,
41
+ * identity, info and admin routes are gated separately.
34
42
  */
35
- const GRANTED_RUN_ROUTES = [
36
- 'GET /SASjsApi/stp/execute',
37
- 'POST /SASjsApi/stp/execute',
38
- 'POST /SASjsApi/code/execute'
39
- ];
43
+ const GRANTED_ROUTES = routeAccess_js_1.DEFAULT_GRANTABLE_ROUTES.filter((path) => !routeAccess_js_1.TOP_LEVEL_ROUTES.includes(path));
44
+ // The OpenAPI route table lowercases filetree; use the inventory's spelling
45
+ // when checking rules, so dispatch and myAuthorizedRoutes agree.
46
+ const grantPath = (path) => GRANTED_ROUTES.find((candidate) => candidate.toLowerCase() === path.toLowerCase());
40
47
  /**
41
48
  * The host's own reports, which the server mounts `verifyAdmin` in front of.
42
49
  *
@@ -142,7 +149,15 @@ const dispatch = async (services, routeId, request = {}) => {
142
149
  // permission rules address. Asked of the record service, so the answer a
143
150
  // request meets is the one `myAuthorizedRoutes` reported for the same caller.
144
151
  const adminScoped = ADMIN_ROUTES.includes(routeId) && Boolean(services.admin);
145
- const grantScoped = Boolean(records) && GRANTED_RUN_ROUTES.includes(routeId);
152
+ const grantScoped = grantPath(route.path);
153
+ const appDetailScoped = routeId === 'GET /SASjsApi/app/{id}';
154
+ // A host with identity is enforcing access and must not silently skip a
155
+ // permission gate when its record service is absent. A drive-only host has
156
+ // no identity model at all and retains its existing desktop-style behavior.
157
+ if ((grantScoped || appDetailScoped) &&
158
+ (identity || request.subject || request.token) &&
159
+ !records)
160
+ throw new types_js_1.ApiError(501, `This host has no record service, so it cannot answer ${routeId}.`);
146
161
  // The administrator routes need the record service to answer the question the
147
162
  // host's middleware answers on a server. A host that supplies the reports
148
163
  // without one is told which capability it is missing, rather than serving its
@@ -167,19 +182,41 @@ const dispatch = async (services, routeId, request = {}) => {
167
182
  }
168
183
  return subject;
169
184
  };
185
+ // Detail uses authentication but is not an ordinary grantable API path:
186
+ // its own app-specific decision is made after lookup and conceals denials.
187
+ if (appDetailScoped &&
188
+ (identity || records || request.subject || request.token) &&
189
+ !(await caller())) {
190
+ if (!identity)
191
+ throw new types_js_1.ApiError(501, `This host has no identity service, so it cannot answer ${routeId}.`);
192
+ throw new types_js_1.ApiError(401, 'Unauthorized');
193
+ }
194
+ if (appDetailScoped && records && !(await records.mayAct(await caller())))
195
+ throw new types_js_1.ApiError(401, 'Unauthorized');
170
196
  if ((identityScoped && records) ||
171
197
  (runtimeScoped && services.runtime) ||
172
198
  adminScoped ||
173
- grantScoped) {
199
+ (grantScoped && records)) {
174
200
  const callerSubject = await caller();
201
+ let publicGrant = false;
175
202
  if (!callerSubject) {
176
- // A host may serve records without a sign-in flow - it resolves the caller
177
- // itself and passes `subject`. Without one it cannot authenticate at all,
178
- // and a 401 would send every request round the login loop for ever, so it
179
- // is told which capability it is missing instead.
180
- if (!identity)
181
- throw new types_js_1.ApiError(501, `This host has no identity service, so it cannot answer ${routeId}.`);
182
- throw new types_js_1.ApiError(401, 'Unauthorized');
203
+ // An exact public-group grant also authenticates a tokenless caller on
204
+ // the server. Do not treat a parent grant as public authentication.
205
+ publicGrant = Boolean(grantScoped &&
206
+ records &&
207
+ !identityScoped &&
208
+ !runtimeScoped &&
209
+ !adminScoped &&
210
+ (await records.mayUseRoute(undefined, grantScoped)));
211
+ if (!publicGrant) {
212
+ // A host may serve records without a sign-in flow - it resolves the caller
213
+ // itself and passes `subject`. Without one it cannot authenticate at all,
214
+ // and a 401 would send every request round the login loop for ever, so it
215
+ // is told which capability it is missing instead.
216
+ if (!identity)
217
+ throw new types_js_1.ApiError(501, `This host has no identity service, so it cannot answer ${routeId}.`);
218
+ throw new types_js_1.ApiError(401, 'Unauthorized');
219
+ }
183
220
  }
184
221
  // `verifyAdmin`, in the wording a caller sees on a server - except for a
185
222
  // caller the server refuses before that middleware runs, which meets the
@@ -193,7 +230,10 @@ const dispatch = async (services, routeId, request = {}) => {
193
230
  }
194
231
  // `authorize`: the rules this host's interface reads decide the call, not
195
232
  // only the menu entry beside it
196
- if (grantScoped && !(await records.mayUseRoute(callerSubject, route.path)))
233
+ if (grantScoped &&
234
+ records &&
235
+ !publicGrant &&
236
+ !(await records.mayUseRoute(callerSubject, grantScoped)))
197
237
  throw new types_js_1.ApiError(401, 'Unauthorized');
198
238
  }
199
239
  validate(route, request);
@@ -507,6 +547,23 @@ const dispatch = async (services, routeId, request = {}) => {
507
547
  throw appNotRegistered();
508
548
  throw err;
509
549
  });
550
+ if (records) {
551
+ const subject = await caller();
552
+ if (!(await records.mayAdminister(subject))) {
553
+ // listPermissions exposes the caller's user/group rules of all types.
554
+ // Only routes select the id or name and decide /AppStream access.
555
+ const rules = (await records.listPermissions(subject)).filter((rule) => rule.type === 'Route');
556
+ const paths = [`/AppStream/${entry.id}`, `/AppStream/${entry.name}`];
557
+ const decided = paths.find((path) => rules.some((rule) => rule.path === path)) ??
558
+ paths[0];
559
+ const routeRules = {
560
+ user: rules.filter((rule) => rule.user === subject.userId),
561
+ group: rules.filter((rule) => rule.group !== undefined)
562
+ };
563
+ if (!(0, routeAccess_js_1.isRouteGranted)(routeRules, decided, '/AppStream'))
564
+ throw appNotRegistered();
565
+ }
566
+ }
510
567
  // the entry's own folder, whichever shape it was published in: `streamWebFolder`
511
568
  // is the field that separates them - an entry carrying it is served from
512
569
  // `<appLoc>/services/<streamWebFolder>`, every other entry from `apps/<name>` -
@@ -354,6 +354,16 @@ const createRecordService = (store, options = {}) => {
354
354
  return Boolean(await readerOf(store, caller));
355
355
  },
356
356
  async mayUseRoute(caller, path) {
357
+ if (!caller) {
358
+ const rules = await store.permissions();
359
+ if (!rules.some((rule) => rule.group !== undefined))
360
+ return false;
361
+ const publicGroup = (await store.groups()).find((group) => group.name === PUBLIC_GROUP);
362
+ return Boolean(publicGroup &&
363
+ rules.some((rule) => rule.group === publicGroup.uid &&
364
+ rule.path === path &&
365
+ rule.setting === 'Grant'));
366
+ }
357
367
  const reader = await readerOf(store, caller);
358
368
  if (!reader)
359
369
  return false;
@@ -558,6 +558,10 @@ exports.GENERATED_ROUTES = [
558
558
  status: 400,
559
559
  description: 'Invalid Format'
560
560
  },
561
+ {
562
+ status: 401,
563
+ description: 'Unauthorized'
564
+ },
561
565
  {
562
566
  status: 404,
563
567
  description: 'No app is published with that id'
@@ -624,6 +628,10 @@ exports.GENERATED_ROUTES = [
624
628
  status: 400,
625
629
  description: 'Invalid Format'
626
630
  },
631
+ {
632
+ status: 401,
633
+ description: 'Unauthorized'
634
+ },
627
635
  {
628
636
  status: 413,
629
637
  description: 'File tree exceeds the deploy limit'
@@ -694,6 +702,10 @@ exports.GENERATED_ROUTES = [
694
702
  status: 400,
695
703
  description: 'Invalid Format'
696
704
  },
705
+ {
706
+ status: 401,
707
+ description: 'Unauthorized'
708
+ },
697
709
  {
698
710
  status: 413,
699
711
  description: 'File tree exceeds the deploy limit'
@@ -728,6 +740,10 @@ exports.GENERATED_ROUTES = [
728
740
  status: 400,
729
741
  description: "Can't delete file outside drive"
730
742
  },
743
+ {
744
+ status: 401,
745
+ description: 'Unauthorized'
746
+ },
731
747
  {
732
748
  status: 404,
733
749
  description: "File doesn't exist"
@@ -768,6 +784,10 @@ exports.GENERATED_ROUTES = [
768
784
  status: 400,
769
785
  description: 'A path outside the drive, a refused extension, or a request naming neither or both'
770
786
  },
787
+ {
788
+ status: 401,
789
+ description: 'Unauthorized'
790
+ },
771
791
  {
772
792
  status: 404,
773
793
  description: "File doesn't exist"
@@ -810,6 +830,10 @@ exports.GENERATED_ROUTES = [
810
830
  status: 400,
811
831
  description: "Can't modify file outside drive"
812
832
  },
833
+ {
834
+ status: 401,
835
+ description: 'Unauthorized'
836
+ },
813
837
  {
814
838
  status: 404,
815
839
  description: "File doesn't exist"
@@ -852,6 +876,10 @@ exports.GENERATED_ROUTES = [
852
876
  status: 400,
853
877
  description: "Can't put file outside drive"
854
878
  },
879
+ {
880
+ status: 401,
881
+ description: 'Unauthorized'
882
+ },
855
883
  {
856
884
  status: 409,
857
885
  description: 'File already exists'
@@ -877,6 +905,10 @@ exports.GENERATED_ROUTES = [
877
905
  {
878
906
  status: 200,
879
907
  description: 'Ok'
908
+ },
909
+ {
910
+ status: 401,
911
+ description: 'Unauthorized'
880
912
  }
881
913
  ]
882
914
  },
@@ -904,6 +936,10 @@ exports.GENERATED_ROUTES = [
904
936
  status: 400,
905
937
  description: "Can't delete folder outside drive"
906
938
  },
939
+ {
940
+ status: 401,
941
+ description: 'Unauthorized'
942
+ },
907
943
  {
908
944
  status: 404,
909
945
  description: "Folder doesn't exist"
@@ -934,6 +970,10 @@ exports.GENERATED_ROUTES = [
934
970
  status: 400,
935
971
  description: "Can't get folder outside drive"
936
972
  },
973
+ {
974
+ status: 401,
975
+ description: 'Unauthorized'
976
+ },
937
977
  {
938
978
  status: 404,
939
979
  description: "Folder doesn't exist"
@@ -964,6 +1004,10 @@ exports.GENERATED_ROUTES = [
964
1004
  status: 400,
965
1005
  description: "Can't put folder outside drive"
966
1006
  },
1007
+ {
1008
+ status: 401,
1009
+ description: 'Unauthorized'
1010
+ },
967
1011
  {
968
1012
  status: 409,
969
1013
  description: 'Folder already exists. The body is that message as plain text.'
@@ -1000,6 +1044,10 @@ exports.GENERATED_ROUTES = [
1000
1044
  status: 400,
1001
1045
  description: "Can't rename outside drive"
1002
1046
  },
1047
+ {
1048
+ status: 401,
1049
+ description: 'Unauthorized'
1050
+ },
1003
1051
  {
1004
1052
  status: 404,
1005
1053
  description: 'No file/folder found for provided path'
@@ -272,9 +272,8 @@ exports.OVERLAY = {
272
272
  // sasjs/cli#1550, not this package's.
273
273
  'GET /SASjsApi/app/{id}': {
274
274
  implemented: true,
275
- // the caller's token is the host's middleware, and a failure that is not the
276
- // index refusing the id - a drive read that broke - is the host's to answer
277
- hostResponses: [401, 500],
275
+ // a drive read that broke is the host's to answer
276
+ hostResponses: [500],
278
277
  // the shape a client reads: the entry as registered, the file it is served from,
279
278
  // and whether that file is there
280
279
  responses: {
@@ -2,11 +2,9 @@
2
2
  * The App Stream detail route's conformance corpus.
3
3
  *
4
4
  * `GET /SASjsApi/app/{id}` describes an app by its id: the entry as registered, the
5
- * file it is served from, and whether that file is there. Two answers are the
6
- * package's - the `200` and the `404` - and both have a case here, which is what
7
- * `spec/corpus-parity.spec.ts` enforces in both directions. The `401` is the host's
8
- * (the route is mounted behind its authenticator) and is marked as such in the
9
- * overlay.
5
+ * file it is served from, and whether that file is there. The `200`, `401`, and
6
+ * `404` answers have cases here, which `spec/corpus-parity.spec.ts` enforces in
7
+ * both directions. The host also authenticates at the HTTP boundary.
10
8
  *
11
9
  * The cases name the same app the rest of the corpus starts from (`CORPUS_APP`), so
12
10
  * a host running this corpus against a live server sees the same id the package
@@ -1 +1 @@
1
- {"version":3,"file":"app.d.ts","sourceRoot":"","sources":["../../../src/corpus/app.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AACH,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAA;AAyB5C,eAAO,MAAM,UAAU,EAAE,UAAU,EAyElC,CAAA"}
1
+ {"version":3,"file":"app.d.ts","sourceRoot":"","sources":["../../../src/corpus/app.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAA;AAyB5C,eAAO,MAAM,UAAU,EAAE,UAAU,EA2FlC,CAAA"}
@@ -18,6 +18,13 @@ const FRONTEND_ENTRY_FILE = `/apps/${FRONTEND_APP.name}/index.html`;
18
18
  /** Where the app's streamed entry file sits inside its folder. */
19
19
  const ENTRY_FILE_PATH = `${CORPUS_APP.appLoc}/services/${CORPUS_APP.streamWebFolder}/index.html`;
20
20
  export const APP_CORPUS = [
21
+ {
22
+ id: 'app-detail-anonymous',
23
+ route: 'GET /SASjsApi/app/{id}',
24
+ description: 'refuses an anonymous caller before looking up an app',
25
+ request: { params: { id: CORPUS_APP.id } },
26
+ expect: { status: 401, error: 'Unauthorized' }
27
+ },
21
28
  {
22
29
  id: 'app-detail-published',
23
30
  route: 'GET /SASjsApi/app/{id}',
@@ -76,6 +83,16 @@ export const APP_CORPUS = [
76
83
  partial: { entryFileExists: false, lastModified: null }
77
84
  }
78
85
  },
86
+ {
87
+ id: 'app-detail-denied',
88
+ route: 'GET /SASjsApi/app/{id}',
89
+ description: 'conceals a registered app from a caller without a stream grant',
90
+ request: {
91
+ subject: { userId: 'user-restricted', clientId: 'web_app' },
92
+ params: { id: CORPUS_APP.id }
93
+ },
94
+ expect: { status: 404, error: 'No App Stream is registered with that id.' }
95
+ },
79
96
  {
80
97
  id: 'app-detail-unknown-id',
81
98
  route: 'GET /SASjsApi/app/{id}',
@@ -12,7 +12,7 @@
12
12
  * records each place the two disagree.
13
13
  */
14
14
  import type { ApiRequest } from '../host/dispatch.js';
15
- import type { IdentityFixture } from '../identity/fixture.js';
15
+ import { type IdentityFixture } from '../identity/fixture.js';
16
16
  import type { AppStreamEntry } from '../appstream/service.js';
17
17
  export interface CorpusCase {
18
18
  /** Stable name, used as the test's title. */
@@ -1 +1 @@
1
- {"version":3,"file":"drive.d.ts","sourceRoot":"","sources":["../../../src/corpus/drive.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AACH,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAA;AAErD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAA;AAC7D,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,yBAAyB,CAAA;AAE7D,MAAM,WAAW,UAAU;IACzB,6CAA6C;IAC7C,EAAE,EAAE,MAAM,CAAA;IACV,kEAAkE;IAClE,KAAK,EAAE,MAAM,CAAA;IACb,WAAW,EAAE,MAAM,CAAA;IACnB,kCAAkC;IAClC,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,UAAU,CAAC,CAAA;IAC1C,2EAA2E;IAC3E,QAAQ,CAAC,EAAE,eAAe,CAAA;IAC1B;;;;;;OAMG;IACH,SAAS,CAAC,EAAE,cAAc,EAAE,CAAA;IAC5B;;;;OAIG;IACH,UAAU,CAAC,EAAE,OAAO,CAAA;IACpB;;;;;;OAMG;IACH,SAAS,CAAC,EAAE;QAAE,QAAQ,EAAE,MAAM,GAAG,UAAU,CAAC;QAAC,WAAW,CAAC,EAAE,MAAM,CAAA;KAAE,CAAA;IACnE,uEAAuE;IACvE,UAAU,CAAC,EAAE,OAAO,CAAA;IACpB;;;;;;;;OAQG;IACH,SAAS,CAAC,EAAE,WAAW,GAAG,SAAS,CAAA;IACnC;;;;;;OAMG;IACH,WAAW,CAAC,EAAE;QACZ,uDAAuD;QACvD,OAAO,CAAC,EAAE,MAAM,EAAE,CAAA;QAClB,0E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