@mastra/server-adapters-test-suite 0.3.0-alpha.1 → 0.3.0-alpha.2

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/CHANGELOG.md CHANGED
@@ -1,5 +1,16 @@
1
1
  # @mastra/server-adapters-test-suite
2
2
 
3
+ ## 0.3.0-alpha.2
4
+
5
+ ### Patch Changes
6
+
7
+ - Added conformance tests for custom route auth: auth and FGA checks must follow the route that handles the request, including its path params, for overlapping routes, `HEAD` requests, letter case and percent-encoded paths. ([#29325](https://github.com/mastra-ai/mastra/pull/29325))
8
+
9
+ - Updated dependencies [[`ce86e53`](https://github.com/mastra-ai/mastra/commit/ce86e5327a0e835e26792e238dacc2c15a777bbf), [`ce76df9`](https://github.com/mastra-ai/mastra/commit/ce76df95d5d942940de7d5453fcbd38b7df5ea83), [`bfda77d`](https://github.com/mastra-ai/mastra/commit/bfda77de9773c383f23101cde8f4ee12af8bff19), [`66c620c`](https://github.com/mastra-ai/mastra/commit/66c620c086cd4a5d12347a832057290a19cb9db2), [`964e1ba`](https://github.com/mastra-ai/mastra/commit/964e1baa24a5a383c86b9f3aa639d488e8742b13), [`311d434`](https://github.com/mastra-ai/mastra/commit/311d4342cc61e9f5a621625e28e483a892fda6bc), [`9730315`](https://github.com/mastra-ai/mastra/commit/9730315a2fddf51580599030916d43b17e1ca553), [`bf9212b`](https://github.com/mastra-ai/mastra/commit/bf9212b308c1d418cac64d54aa380dac324db1bd), [`92f9771`](https://github.com/mastra-ai/mastra/commit/92f9771a0fefaf6e23541ac702850da63bfe4ef9)]:
10
+ - @mastra/core@1.83.0-alpha.5
11
+ - @mastra/server@1.83.0-alpha.5
12
+ - @mastra/mcp@2.9.0-alpha.2
13
+
3
14
  ## 0.3.0-alpha.1
4
15
 
5
16
  ### Minor Changes
@@ -1 +1 @@
1
- {"version":3,"file":"custom-route-fga-test-suite.d.ts","sourceRoot":"","sources":["../src/custom-route-fga-test-suite.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAKtC;;GAEG;AACH,MAAM,WAAW,6BAA6B;IAC5C,8BAA8B;IAC9B,SAAS,CAAC,EAAE,MAAM,CAAC;IAEnB;;;OAGG;IACH,WAAW,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,OAAO,CAAC;QACvC,OAAO,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,WAAW,KAAK,OAAO,CAAC,QAAQ,CAAC,CAAC;QACjE,KAAK,CAAC,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;KAC7B,CAAC,CAAC;CACJ;AAED;;;;GAIG;AACH,wBAAgB,6BAA6B,CAAC,MAAM,EAAE,6BAA6B,QA2ElF"}
1
+ {"version":3,"file":"custom-route-fga-test-suite.d.ts","sourceRoot":"","sources":["../src/custom-route-fga-test-suite.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAKtC;;GAEG;AACH,MAAM,WAAW,6BAA6B;IAC5C,8BAA8B;IAC9B,SAAS,CAAC,EAAE,MAAM,CAAC;IAEnB;;;OAGG;IACH,WAAW,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,OAAO,CAAC;QACvC,OAAO,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,WAAW,KAAK,OAAO,CAAC,QAAQ,CAAC,CAAC;QACjE,KAAK,CAAC,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;KAC7B,CAAC,CAAC;CACJ;AAED;;;;GAIG;AACH,wBAAgB,6BAA6B,CAAC,MAAM,EAAE,6BAA6B,QAoOlF"}
package/dist/index.js CHANGED
@@ -1783,14 +1783,20 @@ function createRouteAdapterTestSuite(config) {
1783
1783
  let context;
1784
1784
  let app;
1785
1785
  let setup;
1786
+ let netlifyProviders;
1786
1787
  beforeEach(async () => {
1787
1788
  if (createTestContext) {
1788
1789
  const result = createTestContext();
1789
1790
  context = result instanceof Promise ? await result : result;
1790
1791
  } else context = await createDefaultTestContext();
1792
+ const netlify = context.mastra.listGateways()?.netlify;
1793
+ netlifyProviders = netlify ? vi.spyOn(netlify, "fetchProviders").mockResolvedValue({}) : void 0;
1791
1794
  setup = await setupAdapter(context);
1792
1795
  app = setup.app;
1793
1796
  });
1797
+ afterEach(() => {
1798
+ netlifyProviders?.mockRestore();
1799
+ });
1794
1800
  if (config.supportsPostQueryRequestContext) it("merges query requestContext into POST route handlers", async () => {
1795
1801
  await setup.adapter.registerRoute(app, {
1796
1802
  method: "POST",
@@ -6357,6 +6363,148 @@ function createCustomRouteFGATestSuite(config) {
6357
6363
  expect(response.status).toBe(403);
6358
6364
  expect(checkedIds(check)).toEqual(["locked"]);
6359
6365
  });
6366
+ it("authenticates overlapping routes the same way they are dispatched", async () => {
6367
+ const mastra = new Mastra({
6368
+ logger: false,
6369
+ server: { apiRoutes: [registerApiRoute("/admin/:id", {
6370
+ method: "GET",
6371
+ handler: async (c) => c.json({ route: "admin" })
6372
+ }), registerApiRoute("/admin/public", {
6373
+ method: "GET",
6374
+ requiresAuth: false,
6375
+ handler: async (c) => c.json({ route: "public" })
6376
+ })] }
6377
+ });
6378
+ const originalGetServer = mastra.getServer.bind(mastra);
6379
+ mastra.getServer = () => ({
6380
+ ...originalGetServer(),
6381
+ auth: {
6382
+ authenticateToken: async (token) => token === "valid-token" ? { id: "user-1" } : null,
6383
+ authorize: async () => true
6384
+ }
6385
+ });
6386
+ const server = await startServer(mastra);
6387
+ close = server.close;
6388
+ expect((await server.request("/admin/public")).status).toBe(401);
6389
+ const authed = await server.request("/admin/public", { headers: { authorization: "Bearer valid-token" } });
6390
+ expect(authed.status).toBe(200);
6391
+ expect(await authed.json()).toEqual({ route: "admin" });
6392
+ });
6393
+ const startWithAuth = async (apiRoutes) => {
6394
+ const mastra = new Mastra({
6395
+ logger: false,
6396
+ server: { apiRoutes }
6397
+ });
6398
+ const originalGetServer = mastra.getServer.bind(mastra);
6399
+ mastra.getServer = () => ({
6400
+ ...originalGetServer(),
6401
+ auth: {
6402
+ authenticateToken: async (token) => token === "valid-token" ? { id: "user-1" } : null,
6403
+ authorize: async () => true
6404
+ }
6405
+ });
6406
+ const server = await startServer(mastra);
6407
+ close = server.close;
6408
+ return server;
6409
+ };
6410
+ const authed = { headers: { authorization: "Bearer valid-token" } };
6411
+ it("does not run a protected route shadowed by an earlier public one without auth", async () => {
6412
+ const protectedHandler = vi.fn(async (c) => c.json({ route: "protected" }));
6413
+ const response = await (await startWithAuth([registerApiRoute("/admin/:id", {
6414
+ method: "GET",
6415
+ requiresAuth: false,
6416
+ handler: async (c) => c.json({ route: "param" })
6417
+ }), registerApiRoute("/admin/public", {
6418
+ method: "GET",
6419
+ handler: protectedHandler
6420
+ })])).request("/admin/public");
6421
+ expect(response.status).toBe(200);
6422
+ expect(await response.json()).toEqual({ route: "param" });
6423
+ expect(protectedHandler).not.toHaveBeenCalled();
6424
+ });
6425
+ it("requires auth for HEAD requests to a protected GET route", async () => {
6426
+ const protectedHandler = vi.fn(async (c) => c.json({ secret: true }));
6427
+ const server = await startWithAuth([registerApiRoute("/secret", {
6428
+ method: "GET",
6429
+ handler: protectedHandler
6430
+ })]);
6431
+ expect((await server.request("/secret", { method: "HEAD" })).status).toBe(401);
6432
+ expect(protectedHandler).not.toHaveBeenCalled();
6433
+ expect((await server.request("/secret", {
6434
+ method: "HEAD",
6435
+ ...authed
6436
+ })).status).toBe(200);
6437
+ });
6438
+ it("matches routes case-sensitively when deciding auth", async () => {
6439
+ const protectedHandler = vi.fn(async (c) => c.json({ route: "protected" }));
6440
+ const server = await startWithAuth([registerApiRoute("/Admin", {
6441
+ method: "GET",
6442
+ requiresAuth: false,
6443
+ handler: async (c) => c.json({ route: "public" })
6444
+ }), registerApiRoute("/admin", {
6445
+ method: "GET",
6446
+ handler: protectedHandler
6447
+ })]);
6448
+ expect((await server.request("/admin")).status).toBe(401);
6449
+ expect(protectedHandler).not.toHaveBeenCalled();
6450
+ const publicResponse = await server.request("/Admin");
6451
+ expect(publicResponse.status).toBe(200);
6452
+ expect(await publicResponse.json()).toEqual({ route: "public" });
6453
+ });
6454
+ it("checks auth for percent-encoded paths against the route they decode to", async () => {
6455
+ const protectedHandler = vi.fn(async (c) => c.json({ route: "admin" }));
6456
+ const server = await startWithAuth([registerApiRoute("/admin", {
6457
+ method: "GET",
6458
+ handler: protectedHandler
6459
+ }), registerApiRoute("/:page", {
6460
+ method: "GET",
6461
+ requiresAuth: false,
6462
+ handler: async (c) => c.json({ route: "page" })
6463
+ })]);
6464
+ const response = await server.request("/%61dmin");
6465
+ expect(protectedHandler).not.toHaveBeenCalled();
6466
+ expect(response.status).not.toBe(200);
6467
+ expect((await server.request("/about")).status).toBe(200);
6468
+ });
6469
+ it("checks FGA with the params of the route that handles an overlapping path", async () => {
6470
+ const check = vi.fn(async (_user, { resource }) => resource.id === "open");
6471
+ const fga = {
6472
+ check,
6473
+ require: vi.fn(),
6474
+ filterAccessible: vi.fn()
6475
+ };
6476
+ const mastra = new Mastra({
6477
+ logger: false,
6478
+ server: {
6479
+ fga,
6480
+ apiRoutes: [registerApiRoute("/docs/:docId", {
6481
+ method: "GET",
6482
+ fga: {
6483
+ resourceType: "doc",
6484
+ resourceIdParam: "docId",
6485
+ permission: "docs:read"
6486
+ },
6487
+ handler: async (c) => c.json({ docId: c.req.param("docId") })
6488
+ }), registerApiRoute("/docs/open", {
6489
+ method: "GET",
6490
+ handler: async (c) => c.json({ route: "static" })
6491
+ })]
6492
+ }
6493
+ });
6494
+ const originalGetServer = mastra.getServer.bind(mastra);
6495
+ mastra.getServer = () => ({
6496
+ ...originalGetServer(),
6497
+ auth: {
6498
+ authenticateToken: async (token) => token === "valid-token" ? { id: "user-1" } : null,
6499
+ authorize: async () => true
6500
+ }
6501
+ });
6502
+ const server = await startServer(mastra);
6503
+ close = server.close;
6504
+ const response = await server.request("/docs/open", { headers: { authorization: "Bearer valid-token" } });
6505
+ expect(response.status).toBe(200);
6506
+ expect(checkedIds(check)).toEqual(["open"]);
6507
+ });
6360
6508
  });
6361
6509
  }
6362
6510
  // src/knowledge-routes-test-suite.ts