From f361ee1222f6fcffd08add5d268ab878b85f64fe Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Thu, 19 Nov 2026 11:36:16 +0800 Subject: [PATCH] fix(kuna): reject lossy text in retained project exports (#12171) --- src/kuna/KunaDocumentError.ts | 1 + src/kuna/KunaProjectFiles.ts | 37 ++++++++++--- src/kuna/KunaProjectReader.ts | 45 +++++++++++++-- tests/boundary/providers/kuna/fakeKuna.ts | 12 +++- .../providers/kuna/kunaProjectFiles.test.ts | 45 ++++++++++++++- .../kuna/kunaProjectTextUtf8.test.ts | 55 +++++++++++++++++++ 6 files changed, 179 insertions(+), 16 deletions(-) create mode 100644 tests/boundary/providers/kuna/kunaProjectTextUtf8.test.ts diff --git a/src/kuna/KunaDocumentError.ts b/src/kuna/KunaDocumentError.ts index 96b02ff679..9a6a2d9d86 100644 --- a/src/kuna/KunaDocumentError.ts +++ b/src/kuna/KunaDocumentError.ts @@ -32,6 +32,7 @@ const KUNA_DOCUMENT_CODES = [ "xref_function_hex", "xref_target", "span_short", + "invalid_utf8", ] as const; export type KunaDocumentCode = (typeof KUNA_DOCUMENT_CODES)[number]; diff --git a/src/kuna/KunaProjectFiles.ts b/src/kuna/KunaProjectFiles.ts index 2af9dbb771..9c18ed37fd 100644 --- a/src/kuna/KunaProjectFiles.ts +++ b/src/kuna/KunaProjectFiles.ts @@ -1,6 +1,8 @@ import { z } from "zod"; import { err, ok, type Result } from "../domain/result.js"; +import { decodeStrictUtf8 } from "../domain/strictUtf8.js"; +import { KunaDocumentError } from "./KunaDocumentError.js"; /** * One function block in a `kuna decompile-project` `.c` file. The span covers @@ -50,6 +52,19 @@ interface KeptLine { readonly text: string; } +/** Decode only the selected source bytes and retain their starting offset. */ +export const decodeKunaProjectText = ( + bytes: Uint8Array, + offset: number, +): Result => { + try { + return ok(decodeStrictUtf8(bytes, "preserve")); + } catch (cause: unknown) { + if (!(cause instanceof TypeError)) throw cause; + return err(new KunaDocumentError("invalid_utf8", { byte_offset: offset })); + } +}; + /** * Stream a file's lines, keeping the text of only the lines whose first * `headBytes` bytes `keep` accepts. Every other line is skipped without being @@ -72,6 +87,13 @@ const scanLines = async ( let line: Buffer[] | undefined = []; let lineBytes = 0; let decided = false; + const recordLine = (bytes: Buffer): string | undefined => { + const decoded = decodeKunaProjectText(bytes, lineStart); + if (!decoded.ok) + return `the structural line at byte ${String(lineStart)} is not valid UTF-8`; + lines.push({ offset: lineStart, text: decoded.value }); + return undefined; + }; for await (const chunk of chunks) { let position = 0; while (position < chunk.length) { @@ -90,11 +112,10 @@ const scanLines = async ( return err(`the line at byte ${String(lineStart)} is too long`); } if (newline === -1) break; - if (line !== undefined) - lines.push({ - offset: lineStart, - text: Buffer.concat(line).toString("utf8"), - }); + if (line !== undefined) { + const failure = recordLine(Buffer.concat(line)); + if (failure !== undefined) return err(failure); + } position = newline + 1; lineStart = offset + position; line = []; @@ -105,8 +126,10 @@ const scanLines = async ( } if (line !== undefined && lineBytes > 0) { const head = Buffer.concat(line); - if (decided || keep(head.subarray(0, headBytes))) - lines.push({ offset: lineStart, text: head.toString("utf8") }); + if (decided || keep(head.subarray(0, headBytes))) { + const failure = recordLine(head); + if (failure !== undefined) return err(failure); + } } return ok({ lines, bytes: offset }); }; diff --git a/src/kuna/KunaProjectReader.ts b/src/kuna/KunaProjectReader.ts index cbea74384d..980ceac77b 100644 --- a/src/kuna/KunaProjectReader.ts +++ b/src/kuna/KunaProjectReader.ts @@ -14,6 +14,7 @@ import { AnalysisError } from "../domain/analysisErrorBase.js"; import { analysisErrorWithCleanupDependencies } from "../domain/analysisErrorCleanup.js"; import { AnalysisCancelledError, + AnalysisOutputError, AnalysisResourceConstraintError, cancelledBy, carriedReason, @@ -33,6 +34,7 @@ import { import { AbortedWait, waitForAbortable } from "../process/AbortablePromise.js"; import type { KunaProject } from "./KunaProjectCache.js"; import { + decodeKunaProjectText, scanKunaAssembly, type KunaAssemblyIndex, type KunaProjectFunction, @@ -79,6 +81,29 @@ interface KunaSpanOwnership { readonly finishSpan: (finish: () => Promise) => Promise; } +/** Project a refused text span without treating it as a provider-wide failure. */ +const invalidSpanText = ( + cause: unknown, + operation: string, + path: string, + offset: number, + length: number, +): AnalysisOutputError | undefined => + cause instanceof KunaDocumentError && cause.code === "invalid_utf8" + ? new AnalysisOutputError( + operation, + "Kuna's cached project text is not valid UTF-8", + { + cause, + diagnostics: { + file: path, + byte_offset: offset, + byte_length: length, + }, + }, + ) + : undefined; + const readSpan = async ( operation: string, path: string, @@ -142,7 +167,7 @@ const readSpan = async ( if (!sameRegularFileState(expected, await owner.handle.stat())) throw new RegularFileChangedError(path); await assertKunaFileState(path, expected); - read = ok(buffer.toString("utf8")); + read = decodeKunaProjectText(buffer, offset); } catch (cause: unknown) { read = err(cause); } @@ -178,9 +203,16 @@ const readSpan = async ( if (cause.operationCause instanceof RegularFileChangedError) throw cause.operationCause; return err( - new ProviderAdapterError(KUNA_PROVIDER_ID, operation, { - cause: cause.operationCause, - }), + invalidSpanText( + cause.operationCause, + operation, + path, + offset, + length, + ) ?? + new ProviderAdapterError(KUNA_PROVIDER_ID, operation, { + cause: cause.operationCause, + }), ); } if (cause instanceof RegularFileCleanupFailure) @@ -273,10 +305,11 @@ const spanCloseFailure = ( failure.operationFailure === undefined && primaryCause instanceof AnalysisError ? primaryCause - : new ProviderAdapterError(KUNA_PROVIDER_ID, operation, { + : (invalidSpanText(primaryCause, operation, path, offset, length) ?? + new ProviderAdapterError(KUNA_PROVIDER_ID, operation, { cause: primaryCause, userMessage: errorMessage(primaryCause), - }); + })); return analysisErrorWithCleanupDependencies(primary, marker, operation); }; diff --git a/tests/boundary/providers/kuna/fakeKuna.ts b/tests/boundary/providers/kuna/fakeKuna.ts index e38bca8537..c25b118eb0 100644 --- a/tests/boundary/providers/kuna/fakeKuna.ts +++ b/tests/boundary/providers/kuna/fakeKuna.ts @@ -53,6 +53,8 @@ export interface FakeKunaBehavior { readonly asmPadding?: number; /** Extra C comment bytes inside each exported body, for buffered-read capacity. */ readonly cPadding?: number; + /** Exact bytes inside the first exported C body, for text-admission tests. */ + readonly cBodyComment?: readonly number[]; readonly decompile?: "ok" | "hang"; readonly standaloneFunctions?: readonly FakeKunaFunction[]; /** Hold successful work until releaseFakeKunaCommand acknowledges the test phase. */ @@ -212,7 +214,15 @@ if (args[0] === "--version") { ? "// Function: " + fn.name + " @ " + fn.addr + "\nint " + fn.name + "(void)\n{\n return " + index + ";\n" + (behavior.cPadding ? " /*" + "x".repeat(behavior.cPadding) + "*/\n" : "") + "}\n\n" : "// Function: " + fn.name + " @ " + fn.addr + " (error: " + fn.error + ")\n\n"; }); - fs.writeFileSync(path.join(output, stem + ".c"), c); + if (behavior.cBodyComment === undefined) fs.writeFileSync(path.join(output, stem + ".c"), c); + else { + const comment = c.indexOf(" return 0;"); + if (comment === -1) throw new Error("fixture requires a successful first C body"); + fs.writeFileSync(path.join(output, stem + ".c"), Buffer.concat([ + Buffer.from(c.slice(0, comment) + " /*"), Buffer.from(behavior.cBodyComment), + Buffer.from("*/\n" + c.slice(comment)), + ])); + } fs.writeFileSync(path.join(output, stem + ".h"), "#pragma once\n"); let asm = "; kuna decompile-project — " + stem + "\n\n; --- code section 0x1000..0x2000 ---\n\n"; functions.forEach((fn) => { asm += fn.name + ": ; " + fn.addr + "\n " + fn.addr.slice(2).padStart(8, "0") + ": c3 RET\n\n"; }); diff --git a/tests/boundary/providers/kuna/kunaProjectFiles.test.ts b/tests/boundary/providers/kuna/kunaProjectFiles.test.ts index 9c173b9acd..cda02354bf 100644 --- a/tests/boundary/providers/kuna/kunaProjectFiles.test.ts +++ b/tests/boundary/providers/kuna/kunaProjectFiles.test.ts @@ -25,8 +25,11 @@ const SOURCE = [ ].join("\n"); /** Feed the text in fixed-size pieces so headers straddle chunk boundaries. */ -async function* chunked(text: string, size: number): AsyncGenerator { - const bytes = Buffer.from(text); +async function* chunked( + text: string | Buffer, + size: number, +): AsyncGenerator { + const bytes = typeof text === "string" ? Buffer.from(text) : text; for (let offset = 0; offset < bytes.length; offset += size) yield bytes.subarray(offset, offset + size); } @@ -205,3 +208,41 @@ it("indexes code listings and data labels in Kuna's .asm", async () => { await scanKunaAssembly(chunked("a: ; 0x10\n\nb: ; 0x10\n", 64)), ).toEqual({ ok: false, error: "the .asm repeats code address 0x10" }); }); + +it.each([ + { + scan: (bytes: Buffer) => + scanKunaProjectSource(chunked(bytes, 1), { decompiled: 1, failed: 0 }), + prefix: '#include "sample.h"\n\n', + head: "// Function: ", + tail: " @ 0x1000\n", + }, + { + scan: (bytes: Buffer) => scanKunaAssembly(chunked(bytes, 1)), + prefix: "; code\n", + head: "", + tail: ": ; 0x1000", + }, +])( + "validates complete structural lines before indexing them", + async ({ scan, prefix, head, tail }) => { + const bytesWithName = (name: Buffer) => + Buffer.concat([Buffer.from(prefix + head), name, Buffer.from(tail)]); + const invalid = await scan(bytesWithName(Buffer.from([0xff]))); + expect(invalid).toEqual({ + ok: false, + error: expect.stringContaining( + `the structural line at byte ${Buffer.byteLength(prefix)} is not valid UTF-8`, + ), + }); + const valid = await scan(bytesWithName(Buffer.from("�Δ"))); + const functions = valid.ok + ? "code" in valid.value + ? [...valid.value.code.values()] + : valid.value + : []; + expect(functions).toEqual([ + expect.objectContaining({ name: "�Δ", address: "0x1000" }), + ]); + }, +); diff --git a/tests/boundary/providers/kuna/kunaProjectTextUtf8.test.ts b/tests/boundary/providers/kuna/kunaProjectTextUtf8.test.ts new file mode 100644 index 0000000000..f5d31a7403 --- /dev/null +++ b/tests/boundary/providers/kuna/kunaProjectTextUtf8.test.ts @@ -0,0 +1,55 @@ +import { expect, it } from "vitest"; + +import { projectAnalysisError } from "../../../../src/domain/analysisErrorProjection.js"; +import { run, session } from "./kunaSessionFixture.js"; + +it.skipIf(process.platform === "win32").each([ + { bytes: [0xff], valid: false }, + { bytes: [...Buffer.from("�Δ")], valid: true }, +])( + "keeps cached function text faithful to its source bytes ($valid)", + async ({ bytes, valid }) => { + const { open } = await session("rea-kuna-project-text-", { + cBodyComment: bytes, + }); + const client = open(); + + // Assembly requires the published project. The next query therefore reads + // the verified on-disk .c span instead of a cold subprocess response. + const assembly = await run(client, "procedure_assembly", { + procedure: "main", + }); + expect(assembly.rawResult).toMatchObject({ + source: { kind: "project-export" }, + }); + const result = await client.execute("procedure_pseudo_code", { + procedure: "main", + }); + if (valid) { + expect(result).toMatchObject({ + ok: true, + value: { + result: expect.stringContaining("/*�Δ*/"), + rawResult: { source: { kind: "project-export" } }, + }, + }); + } else { + expect(result).toMatchObject({ + ok: false, + error: { + cause: { code: "invalid_utf8", kind: "malformed" }, + diagnostics: { + file: expect.stringMatching(/program\.c$/u), + byte_offset: Buffer.byteLength('#include "program.h"\n\n'), + byte_length: expect.any(Number), + }, + }, + }); + } + // An unreadable C body does not invalidate independently verified assembly. + const again = await run(client, "procedure_assembly", { + procedure: "main", + }); + expect(again.result).toEqual(assembly.result); + }, +);