1
0
Fork 0

fix(modlists): allow anonymous link download of preset files

The file-serving route (/api/mod-lists/file/<name>) 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.
This commit is contained in:
Jason Fraley 2026-10-31 15:01:29 -04:00
parent 948109e362
commit a6709e4cf3
3 changed files with 75 additions and 3 deletions

View file

@ -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/<name>` 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

View file

@ -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/<name>`) 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<AccessResult> => {
if (args.isReadingStaticFile) return true;
return requirePermission("mod-lists:read")(args);
},
},
fields: [
{

View file

@ -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/<name>`) 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,
);
});