fix(missions): only accept Arma 3 Launcher preset exports as mod lists
Root cause of the upload failures: the launcher writes an <?xml version="1.0"?> 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).
This commit is contained in:
parent
27bf97946e
commit
cc9d3b8073
5 changed files with 192 additions and 17 deletions
|
|
@ -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<T = undefined> {
|
||||
|
|
@ -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"
|
||||
|
|
|
|||
|
|
@ -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
|
||||
// `<?xml version="1.0"?>` 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"],
|
||||
},
|
||||
};
|
||||
|
|
|
|||
|
|
@ -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({
|
|||
/>
|
||||
<Input
|
||||
type="file"
|
||||
accept=".html,.htm,.txt,text/html,text/plain"
|
||||
accept=".html,.htm,text/html"
|
||||
disabled={modlistUploading}
|
||||
onChange={(e) => {
|
||||
const file = e.target.files?.[0];
|
||||
|
|
@ -550,8 +561,8 @@ export function CreateSideOpDialog({
|
|||
}}
|
||||
/>
|
||||
<p className="text-xs text-muted-foreground">
|
||||
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.
|
||||
</p>
|
||||
</div>
|
||||
|
||||
|
|
|
|||
64
src/lib/modlistPreset.ts
Normal file
64
src/lib/modlistPreset.ts
Normal file
|
|
@ -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 `<meta name="arma:Type"
|
||||
* content="preset">`, an optional `<meta name="arma:PresetName" ...>`, and one
|
||||
* `<tr data-type="ModContainer">` 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 = /<meta\b[^>]*>/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 (!/<html\b/i.test(html)) return { ok: false, reason: "notPreset" };
|
||||
|
||||
let armaType: string | null = null;
|
||||
let presetName: string | null = null;
|
||||
for (const tag of html.match(META_TAG_RE) ?? []) {
|
||||
const name = attr(tag, NAME_ATTR_RE)?.toLowerCase();
|
||||
if (name === "arma:type") armaType = attr(tag, CONTENT_ATTR_RE)?.toLowerCase() ?? null;
|
||||
else if (name === "arma:presetname") presetName = attr(tag, CONTENT_ATTR_RE) || null;
|
||||
}
|
||||
if (armaType !== "preset") return { ok: false, reason: "notPreset" };
|
||||
|
||||
const modCount = (html.match(MOD_ROW_RE) ?? []).length;
|
||||
if (modCount === 0) return { ok: false, reason: "empty" };
|
||||
|
||||
return { ok: true, presetName, modCount };
|
||||
}
|
||||
|
||||
/** Shared user-facing copy for each failure reason (client + server). */
|
||||
export function describeArmaPresetFailure(reason: ArmaPresetFailureReason): string {
|
||||
switch (reason) {
|
||||
case "notPreset":
|
||||
return "That file is not an Arma 3 Launcher mod list export. In the launcher, open the MODS tab, use the menu and choose 'Export preset to HTML', then upload that .html file.";
|
||||
case "empty":
|
||||
return "That Arma 3 Launcher preset does not contain any mods. Select your mods in the launcher, export again, and re-upload.";
|
||||
}
|
||||
}
|
||||
93
tests/int/modlist-preset.int.spec.ts
Normal file
93
tests/int/modlist-preset.int.spec.ts
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
import { describe, expect, it } from "vitest";
|
||||
import { describeArmaPresetFailure, parseArmaPreset } from "@/lib/modlistPreset";
|
||||
|
||||
/** Trimmed fixture mirroring the structure of a real Arma 3 Launcher export. */
|
||||
function presetHtml(modRows: string, presetName = "The World We Knew"): string {
|
||||
return `<?xml version="1.0" encoding="utf-8"?>
|
||||
<html>
|
||||
<head>
|
||||
<meta name="arma:Type" content="preset" />
|
||||
<meta name="arma:PresetName" content="${presetName}" />
|
||||
<meta name="generator" content="Arma 3 Launcher - https://www.arma3.com" />
|
||||
<title>Arma 3</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Arma 3 - Preset <strong>${presetName}</strong></h1>
|
||||
<div class="mod-list">
|
||||
<table>
|
||||
${modRows}
|
||||
</table>
|
||||
</div>
|
||||
<div class="dlc-list">
|
||||
<table></table>
|
||||
</div>
|
||||
<div class="footer">
|
||||
<span>Created by Arma 3 Launcher by Bohemia Interactive.</span>
|
||||
</div>
|
||||
</body>
|
||||
</html>`;
|
||||
}
|
||||
|
||||
const MOD_ROW = ` <tr data-type="ModContainer">
|
||||
<td data-type="DisplayName">ACE3</td>
|
||||
<td><span class="from-steam">Steam</span></td>
|
||||
<td><a href="https://steamcommunity.com/sharedfiles/filedetails/?id=463939057" data-type="Link">link</a></td>
|
||||
</tr>`;
|
||||
|
||||
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('<meta name="arma:Type" content="preset" />', "<META NAME='arma:Type' CONTENT='preset'>")
|
||||
.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(
|
||||
/<meta name="arma:PresetName"[^>]*>/,
|
||||
"",
|
||||
);
|
||||
expect(parseArmaPreset(html)).toEqual({ ok: true, presetName: null, modCount: 1 });
|
||||
});
|
||||
|
||||
it("rejects an unrelated HTML page", () => {
|
||||
const result = parseArmaPreset(
|
||||
"<html><body><h1>My mod list</h1><ul><li>ACE3</li></ul></body></html>",
|
||||
);
|
||||
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);
|
||||
});
|
||||
});
|
||||
Loading…
Reference in a new issue