From a6709e4cf3858197ce50fdc7dd22454f80a8c55c Mon Sep 17 00:00:00 2001 From: Z8MB1E Date: Sat, 31 Oct 2026 15:01:29 -0400 Subject: [PATCH] fix(modlists): allow anonymous link download of preset files The file-serving route (/api/mod-lists/file/) runs the collection read access with isReadingStaticFile: true, but mod-lists required the mod-lists:read permission, so anyone clicking a shared download link got 403 until they signed in. Gate the read access on isReadingStaticFile: the file-serving path serves the preset to anyone with the link, while REST and admin-panel reads keep the mod-lists:read gate. Documented in src/collections/AGENTS.md and covered by an integration test. --- src/collections/AGENTS.md | 2 +- src/collections/server/ModLists.ts | 11 +++- tests/int/modlist-file-access.int.spec.ts | 65 +++++++++++++++++++++++ 3 files changed, 75 insertions(+), 3 deletions(-) create mode 100644 tests/int/modlist-file-access.int.spec.ts diff --git a/src/collections/AGENTS.md b/src/collections/AGENTS.md index e6d13a90..8042f67a 100644 --- a/src/collections/AGENTS.md +++ b/src/collections/AGENTS.md @@ -27,7 +27,7 @@ collections/ game/ 9 files # GameRules (global, currency + staffing flags), GameStructures, # GameVehicles, GameNpcs, GameHardResources, GameEventLogs, # NpcStaffing, LaborClassifications (+ shared staffingFields.ts factory) - server/ 5 files # MissionFiles, ModLists, GameServers, ArmaCommands, ArmaSyncEvents + server/ 5 files # MissionFiles, ModLists (file route `/file/` is public download for anyone with the link; REST/admin reads still need `mod-lists:read`), GameServers, ArmaCommands, ArmaSyncEvents world/ 6 files # Maps (basemap config), MapRoads/MapZones/ResourceNodes (map features), MapObjectives (superuser strategic objectives), NarrativeEvents projects/ 4 files # Projects, Labels, Releases, Sprints (Jira-like) tickets/ 2 files # Tickets (Lexical rich text; `archived` hides from all front-ends, admin panel only), TicketVotes diff --git a/src/collections/server/ModLists.ts b/src/collections/server/ModLists.ts index bb23c714..45332f37 100644 --- a/src/collections/server/ModLists.ts +++ b/src/collections/server/ModLists.ts @@ -1,4 +1,4 @@ -import { CollectionConfig } from "payload"; +import { CollectionConfig, type AccessArgs, type AccessResult } from "payload"; import path from "path"; import { requirePermission } from "@/utils/access-control/hasPermission"; @@ -11,7 +11,14 @@ export const ModLists: CollectionConfig = { create: requirePermission("mod-lists:create"), update: requirePermission("mod-lists:update"), delete: requirePermission("mod-lists:delete"), - read: requirePermission("mod-lists:read"), + // The file-serving route (`/api/mod-lists/file/`) runs `read` with + // `isReadingStaticFile: true`. Allow anyone with the link to download the + // preset without signing in; REST and admin-panel reads still require + // `mod-lists:read`, so the document metadata stays permission-gated. + read: async (args: AccessArgs): Promise => { + if (args.isReadingStaticFile) return true; + return requirePermission("mod-lists:read")(args); + }, }, fields: [ { diff --git a/tests/int/modlist-file-access.int.spec.ts b/tests/int/modlist-file-access.int.spec.ts new file mode 100644 index 00000000..28d29322 --- /dev/null +++ b/tests/int/modlist-file-access.int.spec.ts @@ -0,0 +1,65 @@ +import { getTestPayload } from "../helpers/get-test-payload"; +import { Payload } from "payload"; + +import { afterAll, beforeAll, describe, expect, it } from "vitest"; + +import { ModLists } from "@/collections/server/ModLists"; +import { scopedAdminPageAccess } from "@/utils/access-control/divisionAccess"; + +let payload: Payload; + +const TIMEOUT = 30_000; + +/** + * Replicate the production wrapping exactly: payload.config.ts maps every + * collection through `scopedAdminPageAccess(slug, read)`, and the file-serving + * route (`/api/mod-lists/file/`) invokes `read` with + * `isReadingStaticFile: true`. The vitest environment has no Next.js HTTP + * server, so invoking the access function directly is the standard way to + * unit-test Payload access control (see admin-page-access.int.spec.ts). + */ +const readAccess = scopedAdminPageAccess(ModLists.slug, ModLists.access.read); + +/** + * A synthetic args object: non-admin pathname (REST/file route), no auth cookie. + * `isReadingStaticFile` is a sibling of `req` (Payload builds + * `{ data, isReadingStaticFile, req }` for the file route), not nested in `req`. + */ +const anonReq = (pathname: string, isReadingStaticFile?: boolean) => ({ + isReadingStaticFile, + req: { user: null, payload, pathname }, +}); + +describe("ModLists file access", () => { + beforeAll(async () => { + payload = await getTestPayload(); + }, TIMEOUT); + + afterAll(async () => { + // Nothing persists. + }); + + it( + "lets anyone with the link download the preset without signing in", + async () => { + // The file-serving route runs read with isReadingStaticFile: true. + const result = await readAccess(anonReq("/api/mod-lists/file/x.html", true)); + expect(result).toBe(true); + }, + TIMEOUT, + ); + + it( + "still requires mod-lists:read for anonymous REST and admin-panel reads", + async () => { + // A logged-out REST read of the document metadata is denied. + const api = await readAccess(anonReq("/api/mod-lists/1")); + expect(api).toBe(false); + + // The admin-panel path keeps its permission gate (no user → denied). + const admin = await readAccess(anonReq("/admin/collections/mod-lists", true)); + expect(admin).toBe(false); + }, + TIMEOUT, + ); +});