From 1a89383a79230a7488fc74f757d401d1d992cdb5 Mon Sep 17 00:00:00 2001 From: ShuaixinHuang <1836656514@qq.com> Date: Thu, 19 Nov 2026 19:34:36 +0800 Subject: [PATCH] fix(reference): refuse undecodable Git directory pointers (#12209) --- src/application/ReferenceSourceVcsAdapter.ts | 3 +- .../referenceSourceGitPaths.test.ts | 48 +++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/application/ReferenceSourceVcsAdapter.ts b/src/application/ReferenceSourceVcsAdapter.ts index bc236807df..392c517eba 100644 --- a/src/application/ReferenceSourceVcsAdapter.ts +++ b/src/application/ReferenceSourceVcsAdapter.ts @@ -1,3 +1,4 @@ +import { decodeStrictUtf8 } from "../domain/strictUtf8.js"; import { isAborted } from "../domain/analysisErrorCore.js"; import fs from "node:fs"; import { join } from "node:path"; @@ -142,7 +143,7 @@ const resolveSourceVcs = async ( } }; const readMetadataText = async (path: string): Promise => - (await readMetadataBytes(path)).toString("utf8"); + decodeStrictUtf8(await readMetadataBytes(path), "preserve"); const throwCleanupFailures = (primary: unknown): never => { const observations = cleanupFailures.flatMap((failure) => failure.cleanup === undefined ? [] : [failure.cleanup], diff --git a/tests/boundary/filesystem/referenceSourceGitPaths.test.ts b/tests/boundary/filesystem/referenceSourceGitPaths.test.ts index 0a72396024..5ecef6ab28 100644 --- a/tests/boundary/filesystem/referenceSourceGitPaths.test.ts +++ b/tests/boundary/filesystem/referenceSourceGitPaths.test.ts @@ -52,3 +52,51 @@ it.skipIf(process.platform === "win32").each(["gitdir", "commondir"] as const)( }); }, ); + +it.each(["gitdir", "commondir"] as const)( + "keeps provenance unknown when a %s pointer is not valid UTF-8", + async (pointer) => { + const root = await createTestTempDirectory("rea-reference-git-encoding-"); + const source = join(root, "source"); + const metadata = join(root, "metadata-�"); + await mkdir(source); + await mkdir(metadata); + const head = "1".repeat(40); + await writeFile( + join(metadata, "HEAD"), + pointer === "gitdir" ? `${head}\n` : "ref: refs/heads/main\n", + ); + await mkdir(join(metadata, "refs", "heads"), { recursive: true }); + await writeFile(join(metadata, "refs", "heads", "main"), `${head}\n`); + const path = + pointer === "gitdir" + ? join(source, ".git") + : join(source, ".git", "commondir"); + if (pointer === "commondir") { + await mkdir(join(source, ".git")); + await writeFile(join(source, ".git", "HEAD"), "ref: refs/heads/main\n"); + } + const prefix = `${pointer === "gitdir" ? "gitdir: " : ""}${join(root, "metadata-")}`; + const read = () => + withArtifactResourceScope((resources) => + readReferenceSourceVcs(source, resources), + ); + await writeFile(path, `${prefix}�\n`); + expect(await read()).toMatchObject({ + ok: true, + value: { kind: "git", head }, + }); + await writeFile( + path, + Buffer.concat([ + Buffer.from(prefix), + Buffer.from([0xff]), + Buffer.from("\n"), + ]), + ); + expect(await read()).toMatchObject({ + ok: true, + value: { kind: "unknown", head: null }, + }); + }, +);