From 16a1fc17c6f23f000de00a6f6b055cebfe2afa3a Mon Sep 17 00:00:00 2001 From: highmachs Date: Mon, 30 Nov 2026 20:26:03 +0530 Subject: [PATCH] fix(artifacts): normalize Godot PCK member paths for extraction (#8810) * fix(artifacts): normalize Godot PCK member paths for extraction * fix(artifacts): consume combined prefix segments in Godot PCK paths * fix(artifacts): narrow root path check to complete slash-separated segments --------- Co-authored-by: Vincent Koc --- src/artifacts/GodotPckArtifactReader.test.ts | 122 ++++++++++++++++++- src/artifacts/GodotPckArtifactReader.ts | 30 +++-- 2 files changed, 144 insertions(+), 8 deletions(-) diff --git a/src/artifacts/GodotPckArtifactReader.test.ts b/src/artifacts/GodotPckArtifactReader.test.ts index 99f27d747f1..63a5e134558 100644 --- a/src/artifacts/GodotPckArtifactReader.test.ts +++ b/src/artifacts/GodotPckArtifactReader.test.ts @@ -1,3 +1,5 @@ +import { ArtifactResourceScope } from "../resources/index.js"; +import { extractArtifact } from "./extraction/ArtifactExtraction.js"; import { buildGodotPck, buildGodotPck1, @@ -7,7 +9,13 @@ import { pckU64, type GodotPckMember, } from "../../tests/fixtures/godotPck.js"; -import { appendFile, mkdir, writeFile } from "node:fs/promises"; +import { + appendFile, + mkdir, + writeFile, + readFile, + lstat, +} from "node:fs/promises"; import { join } from "node:path"; import { text } from "node:stream/consumers"; import { once } from "node:events"; @@ -714,3 +722,115 @@ it.each([ }); }); }); + +describe("Godot PCK resource path normalization", () => { + const variedMembers: readonly GodotPckMember[] = [ + { path: "res:///scenes/main.gd", data: "extends Node\n" }, + { path: "res://./config.cfg", data: "[section]\nkey=1\n" }, + { path: "res://.//settings.cfg", data: "[settings]\nready=true\n" }, + { path: "res:///.//scripts/player.gd", data: "class_name Player\n" }, + { path: "res://...", data: "ellipsis file\n" }, + { path: "plain/resource.dat", data: "bytes" }, + ]; + const expectedPaths = [ + "scenes/main.gd", + "config.cfg", + "settings.cfg", + "scripts/player.gd", + "...", + "plain/resource.dat", + ]; + const pack = (version: 1 | 2 | 3 | 4, entries: readonly GodotPckMember[]) => + version === 1 ? buildGodotPck1(entries) : buildGodotPck(version, entries); + + it.each([1, 2, 3, 4] as const)( + "preserves raw paths while normalizing format %i resource paths", + async (version) => { + await withReader(pack(version, variedMembers), async (reader) => { + const entries = await list(reader); + expect(entries.map((entry) => entry.sourcePath)).toEqual( + variedMembers.map((entry) => entry.path), + ); + expect(entries.map((entry) => entry.path)).toEqual(expectedPaths); + expect( + entries.map((entry) => + entry.formatMetadata?.kind === "godot-pck-entry" + ? entry.formatMetadata.source_path + : null, + ), + ).toEqual(variedMembers.map((entry) => entry.path)); + }); + }, + ); + + it.each(["res://", "res:///", "res:////", "res://.", "res://.//"])( + "refuses the root-only resource path %s", + async (path) => { + await withReader( + buildGodotPck(4, [{ path, data: "" }]), + async (reader) => { + await expect(list(reader)).rejects.toMatchObject({ + reason: "malformed", + }); + }, + ); + }, + ); + + it("extracts normalized resource paths, including a literal ellipsis filename", async () => { + const inputPath = await writePck(pack(4, variedMembers)); + const outputRoot = join( + await createTestTempDirectory("rea-godot-normalized-"), + "extracted", + ); + const resourceScope = new ArtifactResourceScope(); + try { + const extracted = await extractArtifact({ + inputPath, + inputFormat: "godot-pck", + outputRoot, + environment: {}, + integrityPolicy: "fail", + resourceScope, + }); + expect( + extracted.artifacts.map((entry) => entry.relative_path).sort(), + ).toEqual([...expectedPaths].sort()); + for (let i = 0; i < variedMembers.length; i++) + expect( + await readFile(join(outputRoot, expectedPaths[i]!), "utf8"), + ).toBe(variedMembers[i]!.data); + } finally { + await resourceScope.close(); + } + }); + + it.each(["/etc/config", "C:/config", "../config", "res://../config"])( + "refuses unsafe path %s without publishing extraction", + async (path) => { + const inputPath = await writePck(pack(4, [{ path, data: "payload" }])); + const outputRoot = join( + await createTestTempDirectory("rea-godot-unsafe-"), + "extracted", + ); + const resourceScope = new ArtifactResourceScope(); + try { + await expect( + extractArtifact({ + inputPath, + inputFormat: "godot-pck", + outputRoot, + environment: {}, + integrityPolicy: "fail", + resourceScope, + }), + ).rejects.toMatchObject({ reason: "path" }); + await expect(lstat(outputRoot)).rejects.toMatchObject({ + code: "ENOENT", + }); + } finally { + await resourceScope.close(); + } + }, + ); +}); diff --git a/src/artifacts/GodotPckArtifactReader.ts b/src/artifacts/GodotPckArtifactReader.ts index e2da200f4bc..5f4522f01b2 100644 --- a/src/artifacts/GodotPckArtifactReader.ts +++ b/src/artifacts/GodotPckArtifactReader.ts @@ -188,7 +188,7 @@ export class GodotPckArtifactReader extends PositionalArtifactReader { "unavailable", `Godot unknown member flags are unsupported: 0x${flags.toString(16)}`, ); - const sourcePath = pckMemberPath(rawPath); + const { sourcePath, path } = pckMemberPath(rawPath); // A format 2–4 removal references a path in an overlay, with no member bytes. if ((flags & PACK_FILE_REMOVAL) !== 0) return undefined; if (header.format_version === 1 && offset === 0n) @@ -216,7 +216,7 @@ export class GodotPckArtifactReader extends PositionalArtifactReader { if (size > BigInt(header.pack_end - start)) throw formatFailure(`member exceeds the pack: ${sourcePath}`); return { - path: sourcePath.startsWith("res://") ? sourcePath.slice(6) : sourcePath, + path, sourcePath, kind: "file" as const, declaredSize: Number(size), @@ -331,21 +331,37 @@ const readGodotPckHeader = async ( }; }; -const pckMemberPath = (raw: Buffer): string => { +const pckMemberPath = (raw: Buffer): { sourcePath: string; path: string } => { let end = raw.length; while (end > 0 && raw[end - 1] === 0) end--; - let path: string; + let sourcePath: string; try { - path = UTF8.decode(raw.subarray(0, end)); + sourcePath = UTF8.decode(raw.subarray(0, end)); } catch (cause: unknown) { throw malformedArtifact("godot-pck", "Godot PCK path is not valid UTF-8", { cause, field: "path", }); } - if (path.length === 0 || path === "res://" || path.includes("\0")) + if (sourcePath.length === 0 || sourcePath.includes("\0")) throw formatFailure("empty or NUL-containing member path"); - return path; + let path = sourcePath; + if (sourcePath.startsWith("res://")) { + let start = 6; + // Only the resource scheme admits redundant leading separators and ./. + while (start < sourcePath.length) { + if (sourcePath[start] === "/") start++; + else if ( + sourcePath[start] === "." && + (start + 1 === sourcePath.length || sourcePath[start + 1] === "/") + ) + start++; + else break; + } + path = sourcePath.slice(start); + if (path.length === 0) throw formatFailure("empty resource member path"); + } + return { sourcePath, path }; }; /** One bounded read window; no whole-pack buffer or per-member source reopening. */