From cc9d3b8073010c23a640bdd1a43d3177327a9ad5 Mon Sep 17 00:00:00 2001 From: Z8MB1E Date: Thu, 8 Oct 2026 13:25:08 -0400 Subject: [PATCH] fix(missions): only accept Arma 3 Launcher preset exports as mod lists Root cause of the upload failures: the launcher writes an prologue, Payload sniffs uploads with fileTypeFromBuffer, and the collection mimeTypes allowlist lacked application/xml, so every genuine launcher export was rejected with 'Invalid MIME type: application/xml'. - Add application/xml to the mod-lists upload mimeTypes (the document is still stored as text/html; verified end to end). - New pure parser src/lib/modlistPreset.ts validates the launcher export structure (arma:Type=preset meta + data-type=ModContainer rows) and is shared by the server action (authoritative) and CreateSideOpDialog (instant client-side feedback). - Plain-text lists are no longer accepted anywhere: dialog accept/hint, collection mimeTypes, and the action now agree on HTML presets only. - Upload name defaults to the preset's own arma:PresetName. - Tests: tests/int/modlist-preset.int.spec.ts (9 cases). --- .../intelligence/missions/actions.ts | 23 +++-- src/collections/server/ModLists.ts | 10 +- .../intelligence/CreateSideOpDialog.tsx | 19 +++- src/lib/modlistPreset.ts | 64 +++++++++++++ tests/int/modlist-preset.int.spec.ts | 93 +++++++++++++++++++ 5 files changed, 192 insertions(+), 17 deletions(-) create mode 100644 src/lib/modlistPreset.ts create mode 100644 tests/int/modlist-preset.int.spec.ts diff --git a/src/app/(frontend)/intelligence/missions/actions.ts b/src/app/(frontend)/intelligence/missions/actions.ts index cfabd22a..47773747 100644 --- a/src/app/(frontend)/intelligence/missions/actions.ts +++ b/src/app/(frontend)/intelligence/missions/actions.ts @@ -23,6 +23,7 @@ import { zonedParts, zonedWeekday, } from "@/lib/intelligence/fridayTime"; +import { describeArmaPresetFailure, parseArmaPreset } from "@/lib/modlistPreset"; import type { Mission, User } from "@/payload-types"; export interface ActionResult { @@ -210,17 +211,16 @@ export async function uploadSideOpModlist( if (file.size > 10 * 1024 * 1024) { return { success: false, error: "Mod list file must be 10 MB or smaller." }; } - // Only HTML mod lists exported from the Arma 3 launcher are supported. Some - // browsers report an empty file.type, so also accept by extension. - const isHtml = file.type === "text/html" || /\.html?$/i.test(file.name); - if (!isHtml) { - return { - success: false, - error: "Mod list must be an HTML file exported from the Arma 3 launcher.", - }; + // Only genuine Arma 3 Launcher preset exports are accepted. Validate the + // file CONTENTS, not the extension or the browser-guessed mimetype: junk + // uploads then fail with actionable guidance instead of a stored useless + // file, and valid exports pass regardless of what file.type reports. + const buffer = Buffer.from(await file.arrayBuffer()); + const parsed = parseArmaPreset(buffer.toString("utf-8")); + if (!parsed.ok) { + return { success: false, error: describeArmaPresetFailure(parsed.reason) }; } - const buffer = Buffer.from(await file.arrayBuffer()); const created = await payload.create({ collection: "mod-lists", overrideAccess: true, @@ -230,7 +230,7 @@ export async function uploadSideOpModlist( }, file: { data: buffer, - mimetype: file.type || "text/html", + mimetype: "text/html", name: file.name, size: file.size, }, @@ -242,8 +242,7 @@ export async function uploadSideOpModlist( if (/invalid: file/i.test(message)) { return { success: false, - error: - "That file type is not accepted. Please upload the .html file exported from the Arma 3 launcher, or a .txt mod list.", + error: describeArmaPresetFailure("notPreset"), }; } return message === "Unauthorized" diff --git a/src/collections/server/ModLists.ts b/src/collections/server/ModLists.ts index b1e7cf8e..0d5e9f2e 100644 --- a/src/collections/server/ModLists.ts +++ b/src/collections/server/ModLists.ts @@ -36,6 +36,14 @@ export const ModLists: CollectionConfig = { // is gitignored. upload: { staticDir: path.resolve(process.cwd(), "media", "mod-lists"), - mimeTypes: ["text/html", "text/plain"], + // Only Arma 3 Launcher preset exports; uploadSideOpModlist validates the + // file contents (arma:Type meta + ModContainer rows) before storing. + // application/xml is required because the launcher writes an + // `` prologue and Payload sniffs uploads with + // fileTypeFromBuffer, which reports those bytes as application/xml — + // without this entry every genuine launcher export fails with + // "Invalid MIME type: application/xml". The document itself is stored + // with the text/html mimetype the action passes. + mimeTypes: ["text/html", "application/xml"], }, }; diff --git a/src/components/frontend/intelligence/CreateSideOpDialog.tsx b/src/components/frontend/intelligence/CreateSideOpDialog.tsx index a9396348..464ee80c 100644 --- a/src/components/frontend/intelligence/CreateSideOpDialog.tsx +++ b/src/components/frontend/intelligence/CreateSideOpDialog.tsx @@ -30,6 +30,7 @@ import { type FridaySideOpInput, } from "@/app/(frontend)/intelligence/missions/actions"; import { uploadUgcImage } from "./create"; +import { describeArmaPresetFailure, parseArmaPreset } from "@/lib/modlistPreset"; import { SideOpNarrativeFields, type SideOpNarrative, @@ -243,9 +244,19 @@ export function CreateSideOpDialog({ async function handleModlistUpload(file: File) { setModlistUploading(true); try { + // Pre-validate with the same parser the server action uses so users get + // instant feedback; the server still re-checks the bytes it receives. + const parsed = parseArmaPreset(await file.text()); + if (!parsed.ok) { + toast.error(describeArmaPresetFailure(parsed.reason)); + return; + } const formData = new FormData(); formData.append("file", file); - formData.append("name", modlistName.trim() || file.name.replace(/\.html?$/i, "")); + formData.append( + "name", + modlistName.trim() || parsed.presetName || file.name.replace(/\.html?$/i, ""), + ); const result = await uploadSideOpModlist(formData); if (!result.success || !result.data) { toast.error(result.error ?? "Failed to upload the mod list."); @@ -541,7 +552,7 @@ export function CreateSideOpDialog({ /> { const file = e.target.files?.[0]; @@ -550,8 +561,8 @@ export function CreateSideOpDialog({ }} />

- Upload the mod list file exported from the Arma 3 launcher (.html) or a .txt mod list, - or pick an existing one. + Upload the .html preset exported from the Arma 3 launcher (MODS tab → Export + preset to HTML), or pick an existing one.

diff --git a/src/lib/modlistPreset.ts b/src/lib/modlistPreset.ts new file mode 100644 index 00000000..d8e3fa66 --- /dev/null +++ b/src/lib/modlistPreset.ts @@ -0,0 +1,64 @@ +/** + * Validation for Arma 3 Launcher preset exports (the only accepted mod list + * format). Pure module (client-safe): the server action treats it as + * authoritative, and CreateSideOpDialog uses it for instant client-side + * feedback before the upload starts. + * + * A launcher export is an HTML document carrying ``, an optional ``, and one + * `` row per mod. We validate those structural + * markers rather than the generator/footer strings, which third-party launcher + * clones rewrite. + */ + +export type ArmaPresetFailureReason = "notPreset" | "empty"; + +export type ArmaPresetSummary = + | { ok: true; presetName: string | null; modCount: number } + | { ok: false; reason: ArmaPresetFailureReason }; + +const META_TAG_RE = /]*>/gi; +const NAME_ATTR_RE = /\bname\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s>]+))/i; +const CONTENT_ATTR_RE = /\bcontent\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s>]+))/i; +const MOD_ROW_RE = /\bdata-type\s*=\s*(?:"ModContainer"|'ModContainer'|ModContainer)/gi; + +function attr(tag: string, attribute: RegExp): string | null { + const match = tag.match(attribute); + if (!match) return null; + return (match[1] ?? match[2] ?? match[3] ?? "").trim(); +} + +/** + * Inspect a candidate file's text. Returns the preset name and mod count when + * the content matches the launcher export format, otherwise a failure reason: + * - `notPreset`: not an HTML Arma 3 preset export at all (wrong file type). + * - `empty`: a valid preset containing zero mod entries. + */ +export function parseArmaPreset(source: string): ArmaPresetSummary { + const html = source.replace(/^\uFEFF/, ""); + if (!/ + + + + + + Arma 3 + + +

Arma 3 - Preset ${presetName}

+
+ +${modRows} +
+
+
+
+
+ + +`; +} + +const MOD_ROW = ` + ACE3 + Steam + link + `; + +describe("parseArmaPreset", () => { + it("accepts a launcher preset export and reports name + mod count", () => { + const result = parseArmaPreset(presetHtml(`${MOD_ROW}\n${MOD_ROW}`)); + expect(result).toEqual({ ok: true, presetName: "The World We Knew", modCount: 2 }); + }); + + it("tolerates a UTF-8 BOM", () => { + const result = parseArmaPreset(`\uFEFF${presetHtml(MOD_ROW)}`); + expect(result.ok).toBe(true); + }); + + it("tolerates single-quoted and uppercase attributes", () => { + const html = presetHtml(MOD_ROW) + .replace('', "") + .replace('data-type="ModContainer"', "DATA-TYPE='ModContainer'"); + const result = parseArmaPreset(html); + expect(result.ok).toBe(true); + }); + + it("reports a null preset name when the meta tag is absent", () => { + const html = presetHtml(MOD_ROW).replace( + /]*>/, + "", + ); + expect(parseArmaPreset(html)).toEqual({ ok: true, presetName: null, modCount: 1 }); + }); + + it("rejects an unrelated HTML page", () => { + const result = parseArmaPreset( + "

My mod list

  • ACE3
", + ); + expect(result).toEqual({ ok: false, reason: "notPreset" }); + }); + + it("rejects a plain-text mod list", () => { + expect(parseArmaPreset("CUP Units\nCUP Vehicles\nACE3\n")).toEqual({ + ok: false, + reason: "notPreset", + }); + }); + + it("rejects launcher exports of other arma:Type values", () => { + const html = presetHtml(MOD_ROW).replace('content="preset"', 'content="save"'); + expect(parseArmaPreset(html)).toEqual({ ok: false, reason: "notPreset" }); + }); + + it("rejects a valid preset with zero mods", () => { + expect(parseArmaPreset(presetHtml(""))).toEqual({ ok: false, reason: "empty" }); + }); +}); + +describe("describeArmaPresetFailure", () => { + it("gives actionable copy for every reason", () => { + expect(describeArmaPresetFailure("notPreset")).toMatch(/Export preset to HTML/i); + expect(describeArmaPresetFailure("empty")).toMatch(/does not contain any mods/i); + }); +});