From 26917581d63ba53345ef70fe02cdc14486b2888a Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Sun, 22 Nov 2026 03:17:40 -0500 Subject: [PATCH] fix(audit): preserve non-ASCII whitespace in Go build paths (#13340) --- .../ReleaseExposureService.test.ts | 242 ------------------ src/domain/releaseExposureGoPaths.test.ts | 150 +++++++++++ src/domain/releaseExposureGoPaths.ts | 9 +- 3 files changed, 155 insertions(+), 246 deletions(-) create mode 100644 src/domain/releaseExposureGoPaths.test.ts diff --git a/src/application/releaseExposure/ReleaseExposureService.test.ts b/src/application/releaseExposure/ReleaseExposureService.test.ts index 3aa660aaaa..4b937c1e4d 100644 --- a/src/application/releaseExposure/ReleaseExposureService.test.ts +++ b/src/application/releaseExposure/ReleaseExposureService.test.ts @@ -2134,53 +2134,6 @@ describe("ReleaseExposureService cleanup", () => { }); }); -describe("ReleaseExposureService Go Windows paths", () => { - it("reports UNC roots retained in Go flags", async () => { - const module = (value: string) => ({ - path: "github.com/acme/app", - main: null, - dependencies: [], - settings: [{ key: "CGO_CFLAGS", value }], - unparsed_lines: [], - unparsed_line_bytes_base64: [], - complete: true, - }); - const report = await goReport( - module( - String.raw`-O2 -I\\buildhost\sdk\include "-isystem \\buildhost\my sdk\include"`, - ), - ); - const check = report.checks.find((item) => item.id === "go.build_paths"); - expect(check?.status).toBe("exposed"); - expect(check?.observation.samples).toEqual([ - String.raw`\\buildhost\sdk\include`, - String.raw`\\buildhost\my sdk\include`, - ]); - expect( - await goStatus(module(String.raw`-DNAME=\\ -DHOST=\\buildhost`)), - ).toBe("not_observed"); - }); - - it.each([ - [String.raw`-I..\sdk\include`, String.raw`..\sdk\include`], - [String.raw`-O2 -I.\sdk`, String.raw`.\sdk`], - [String.raw`"-isystem ..\my sdk\include"`, String.raw`..\my sdk\include`], - ])("reports the backslash-relative path in %s", async (value, path) => { - const report = await goReport({ - path: "github.com/acme/app", - main: null, - dependencies: [], - settings: [{ key: "CGO_CFLAGS", value }], - unparsed_lines: [], - unparsed_line_bytes_base64: [], - complete: true, - }); - const check = report.checks.find((item) => item.id === "go.build_paths"); - expect(check?.status).toBe("exposed"); - expect(check?.observation.samples).toEqual([path]); - }); -}); - const goModule = (settings: { key: string; value: string }[]) => ({ path: "github.com/acme/app", main: null, @@ -2191,80 +2144,6 @@ const goModule = (settings: { key: string; value: string }[]) => ({ complete: true, }); -describe("ReleaseExposureService Go linker path options", () => { - it.each([ - ["-tmpdir=private-obj -s -w", "private-obj"], - ["-tmpdir private-obj", "private-obj"], - ["-capturehostobjs private-objs", "private-objs"], - ["-cpuprofile=link.prof", "link.prof"], - ["-importcfg private.cfg", "private.cfg"], - ["-o out/app", "out/app"], - ["-r private-lib", "private-lib"], - ["-r -private", "-private"], - ["-L -private", "-private"], - ["-r -L", "-L"], - ["-r= -L private-sdk", "private-sdk"], - ["'-L=/tmp/private '", "/tmp/private "], - ["-libgcc -private.a", "-private.a"], - ["-s -w -libgcc=private-toolchain/libgcc.a", "private-toolchain/libgcc.a"], - ["-libgcc private-toolchain/libgcc.a", "private-toolchain/libgcc.a"], - ])("keeps the relative linker path in %s", async (value, path) => { - const report = await goReport(goModule([{ key: "-ldflags", value }])); - const check = report.checks.find((item) => item.id === "go.build_paths"); - expect(check?.status).toBe("exposed"); - expect(check?.observation.samples).toEqual([path]); - }); - - it.each([ - "-extld clang -extar ar -libgcc none", - "-libgcc=none", - "-buildmode pie -linkmode external -installsuffix race", - ])("does not read the program or mode in %s as a path", async (value) => { - const report = await goReport(goModule([{ key: "-ldflags", value }])); - const check = report.checks.find((item) => item.id === "go.build_paths"); - expect(check?.status).toBe("not_observed"); - }); -}); - -describe("ReleaseExposureService compiler path separators", () => { - it.each(["/tmp/private,sdk", "/tmp/private=sdk", "/tmp/private "])( - "preserves a literal compiler argument %s", - async (path) => { - const report = await goReport( - goModule([{ key: "CGO_CFLAGS", value: `'-I${path}'` }]), - ); - const check = report.checks.find((item) => item.id === "go.build_paths"); - expect(check?.observation.samples).toEqual([path]); - }, - ); -}); - -describe("ReleaseExposureService compiler profile paths", () => { - it.each([ - ["CGO_CFLAGS", "-O2 -fprofile-use=internal/profiles", "internal/profiles"], - ["CGO_CFLAGS", "'-fprofile-dir=private profiles'", "private profiles"], - ["-ldflags", "-extldflags=-fprofile-generate=private/gcov", "private/gcov"], - ])("reports the named directory in %s %s", async (key, value, path) => { - const report = await goReport(goModule([{ key, value }])); - const check = report.checks.find((item) => item.id === "go.build_paths"); - expect(check?.status).toBe("exposed"); - expect(check?.observation.samples).toEqual([path]); - }); - - it("finds no profile path for options without directory operands", async () => { - const report = await goReport( - goModule([ - { - key: "CGO_CFLAGS", - value: "-fprofile-use -fprofile-generate -O2", - }, - ]), - ); - const check = report.checks.find((item) => item.id === "go.build_paths"); - expect(check?.status).toBe("not_observed"); - }); -}); - describe("ReleaseExposureService Go linker -r search list", () => { it.each([["-r=$ORIGIN/../lib -s -w"], ["-r ${ORIGIN}/lib:$ORIGIN"]])( "does not read the ELF loader-expanded entries in %s as build paths", @@ -3110,37 +2989,6 @@ const goReport = async ( return releaseExposureReportSchema.parse(result.value.normalized_result); }; -describe("ReleaseExposureService Go tool path options", () => { - const buildPaths = async (key: string, value: string) => { - const report = await goReport(goModule([{ key, value }])); - return report.checks.find((item) => item.id === "go.build_paths"); - }; - - it.each([ - ["-gcflags", "-I private-sdk"], - ["-gcflags", "-I=private-sdk"], - ["-gcflags", "--I private-sdk"], - ["-gcflags", "all=-N -l -I private-sdk"], - ["-gcflags", "-importcfg private-sdk"], - ["-gcflags", "-trimpath private-sdk"], - ["-asmflags", "-I private-sdk"], - ["-asmflags", "all=-trimpath=private-sdk"], - ])("finds the relative path in %s %s", async (key, value) => { - const check = await buildPaths(key, value); - expect(check?.status).toBe("exposed"); - expect(check?.observation.samples).toEqual(["private-sdk"]); - }); - - it.each([ - ["-gcflags", "all=-N -l"], - ["-gcflags", "-p main -lang go1.26 -m"], - ["-gcflags", "-X main.path=private-sdk"], - ["-asmflags", "-D GOAMD64_v1 -p main"], - ])("finds no path in %s %s", async (key, value) => { - expect((await buildPaths(key, value))?.status).toBe("not_observed"); - }); -}); - describe("ReleaseExposureService undecoded metadata", () => { const statuses = async (undecodedContent: boolean) => { const { service, goBinaries, rustBinaries } = owners( @@ -3203,96 +3051,6 @@ describe("ReleaseExposureService unnamed Electron hosts", () => { }); }); -describe("ReleaseExposureService forwarded linker path operands", () => { - it.each([ - ["-Wl,-T,/tmp/private=sdk/link.ld", "/tmp/private=sdk/link.ld"], - [ - "-Wl,--version-script=/tmp/private=sdk/exports", - "/tmp/private=sdk/exports", - ], - [ - "-Xlinker -T -Xlinker /tmp/private,sdk/link.ld", - "/tmp/private,sdk/link.ld", - ], - ["-Xlinker -L -Xlinker /tmp/private=sdk", "/tmp/private=sdk"], - ["'-Wl,-L/tmp/private '", "/tmp/private "], - ["-Xlinker -L -Xlinker '/tmp/private '", "/tmp/private "], - ])("preserves the complete forwarded path in %s", async (value, path) => { - const report = await goReport(goModule([{ key: "CGO_LDFLAGS", value }])); - const check = report.checks.find((item) => item.id === "go.build_paths"); - expect(check?.observation.samples).toEqual([path]); - }); -}); - -describe("ReleaseExposureService compiler operand ownership", () => { - it.each([ - ["-I -Wl,-rpath,$ORIGIN", ["-Wl,-rpath,$ORIGIN"]], - ["-I -Wl,-L,/tmp/private", ["-Wl,-L,/tmp/private"]], - ["-I -Xlinker", ["-Xlinker"]], - [ - "-I -Wl,-rpath,$ORIGIN -Wl,-L,other-sdk", - ["-Wl,-rpath,$ORIGIN", "other-sdk"], - ], - ])( - "keeps compiler operands out of forwarding in %s", - async (value, paths) => { - const report = await goReport(goModule([{ key: "CGO_CFLAGS", value }])); - expect( - report.checks.find((item) => item.id === "go.build_paths")?.observation - .samples, - ).toEqual(paths); - }, - ); -}); - -describe("ReleaseExposureService linker operand ownership", () => { - it.each([ - ["-Wl,-T,-rpath,private-sdk", ["-rpath"]], - ["-Wl,-L,-private-sdk", ["-private-sdk"]], - ["-Wl,-rpath=-private-sdk", ["-private-sdk"]], - ["-Xlinker -rpath -Xlinker '/tmp/private '", ["/tmp/private "]], - ])("consumes each linker operand once in %s", async (value, paths) => { - const report = await goReport(goModule([{ key: "CGO_LDFLAGS", value }])); - expect( - report.checks.find((check) => check.id === "go.build_paths")?.observation - .samples, - ).toEqual(paths); - }); -}); - -describe("ReleaseExposureService Go tool operand ownership", () => { - it.each([ - ["-L -X main.name=private", "-X"], - ["-L -extldflags=-Lprivate", "-extldflags=-Lprivate"], - ["all=-L -X main.name=private", "-X"], - ])( - "consumes Go path operands before interpreting options in %s", - async (value, path) => { - const report = await goReport(goModule([{ key: "-ldflags", value }])); - expect( - report.checks.find((item) => item.id === "go.build_paths")?.observation - .samples, - ).toEqual([path]); - }, - ); -}); - -describe("ReleaseExposureService parsed field trailing characters", () => { - it.each([ - ["CGO_LDFLAGS", "'-Wl,@private '", "private "], - ["CGO_CFLAGS", "'-DROOT=/tmp/private '", "/tmp/private "], - ])( - "preserves quoted trailing characters in %s=%s", - async (key, value, path) => { - const report = await goReport(goModule([{ key, value }])); - expect( - report.checks.find((item) => item.id === "go.build_paths")?.observation - .samples, - ).toEqual([path]); - }, - ); -}); - it("attempts later release-exposure owners after a synchronous close rejection", async () => { const { service, desktop, goBinaries, rustBinaries } = owners(scan()); const failed = new Error("desktop close failed"); diff --git a/src/domain/releaseExposureGoPaths.test.ts b/src/domain/releaseExposureGoPaths.test.ts new file mode 100644 index 0000000000..19706ac29f --- /dev/null +++ b/src/domain/releaseExposureGoPaths.test.ts @@ -0,0 +1,150 @@ +import { describe, expect, it } from "vitest"; + +import { goBuildPaths } from "./releaseExposureGoPaths.js"; + +const extract = (key: string, value: string) => + goBuildPaths( + { + path: "example.test/app", + main: null, + dependencies: [], + settings: [{ key, value }], + unparsed_lines: [], + unparsed_line_bytes_base64: [], + complete: true, + }, + "elf", + ); + +describe("Go tool path flags", () => { + it.each([ + ["-ldflags", "-tmpdir=private-obj -s -w", "private-obj"], + ["-ldflags", "-tmpdir private-obj", "private-obj"], + ["-ldflags", "-capturehostobjs private-objs", "private-objs"], + ["-ldflags", "-cpuprofile=link.prof", "link.prof"], + ["-ldflags", "-importcfg private.cfg", "private.cfg"], + ["-ldflags", "-o out/app", "out/app"], + ["-ldflags", "-r private-lib", "private-lib"], + ["-ldflags", "-r -private", "-private"], + ["-ldflags", "-L -private", "-private"], + ["-ldflags", "-r -L", "-L"], + ["-ldflags", "-r= -L private-sdk", "private-sdk"], + ["-ldflags", "'-L=/tmp/private '", "/tmp/private "], + ["-ldflags", "-L build\u00a0objects", "build\u00a0objects"], + ["-ldflags", "-libgcc -private.a", "-private.a"], + [ + "-ldflags", + "-s -w -libgcc=private-toolchain/libgcc.a", + "private-toolchain/libgcc.a", + ], + [ + "-ldflags", + "-libgcc private-toolchain/libgcc.a", + "private-toolchain/libgcc.a", + ], + ["-ldflags", "-L -X main.name=private", "-X"], + ["-ldflags", "-L -extldflags=-Lprivate", "-extldflags=-Lprivate"], + ["-ldflags", "all=-L -X main.name=private", "-X"], + ["-gcflags", "-I private-sdk", "private-sdk"], + ["-gcflags", "-I=private-sdk", "private-sdk"], + ["-gcflags", "--I private-sdk", "private-sdk"], + ["-gcflags", "all=-N -l -I private-sdk", "private-sdk"], + ["-gcflags", "-importcfg private-sdk", "private-sdk"], + ["-gcflags", "-trimpath private-sdk", "private-sdk"], + ["-asmflags", "-I private-sdk", "private-sdk"], + ["-asmflags", "all=-trimpath=private-sdk", "private-sdk"], + ])("keeps the complete operand in %s=%s", (key, value, path) => { + expect(extract(key, value)).toEqual({ + paths: [path], + ambiguousLinkerR: false, + }); + }); + + it.each([ + ["-ldflags", "-extld clang -extar ar -libgcc none"], + ["-ldflags", "-libgcc=none"], + ["-ldflags", "-buildmode pie -linkmode external -installsuffix race"], + ["-ldflags", "-X main.value=prefix\u00a0/suffix"], + ["-gcflags", "all=-N -l"], + ["-gcflags", "-p main -lang go1.26 -m"], + ["-gcflags", "-X main.path=private-sdk"], + ["-asmflags", "-D GOAMD64_v1 -p main"], + ])("skips non-path operands in %s=%s", (key, value) => { + expect(extract(key, value)).toEqual({ paths: [], ambiguousLinkerR: false }); + }); +}); + +describe("C compiler and forwarded linker paths in Go metadata", () => { + it.each([ + ["CGO_CFLAGS", "'-I/tmp/private,sdk'", ["/tmp/private,sdk"]], + ["CGO_CFLAGS", "'-I/tmp/private=sdk'", ["/tmp/private=sdk"]], + ["CGO_CFLAGS", "'-I/tmp/private '", ["/tmp/private "]], + ["CGO_CFLAGS", "-O2 -I\u00a0headers", ["\u00a0headers"]], + [ + "CGO_CFLAGS", + "-O2 -fprofile-use=internal/profiles", + ["internal/profiles"], + ], + ["CGO_CFLAGS", "'-fprofile-dir=private profiles'", ["private profiles"]], + [ + "-ldflags", + "-extldflags=-fprofile-generate=private/gcov", + ["private/gcov"], + ], + ["CGO_CFLAGS", "-fprofile-use -fprofile-generate -O2", []], + [ + "CGO_CFLAGS", + '-O2 -I\\\\buildhost\\sdk\\include "-isystem \\\\buildhost\\my sdk\\include"', + ["\\\\buildhost\\sdk\\include", "\\\\buildhost\\my sdk\\include"], + ], + ["CGO_CFLAGS", "-DNAME=\\\\ -DHOST=\\\\buildhost", []], + ["CGO_CFLAGS", "-I..\\sdk\\include", ["..\\sdk\\include"]], + ["CGO_CFLAGS", "-O2 -I.\\sdk", [".\\sdk"]], + ["CGO_CFLAGS", '"-isystem ..\\my sdk\\include"', ["..\\my sdk\\include"]], + [ + "CGO_LDFLAGS", + "-Wl,-T,/tmp/private=sdk/link.ld", + ["/tmp/private=sdk/link.ld"], + ], + [ + "CGO_LDFLAGS", + "-Wl,--version-script=/tmp/private=sdk/exports", + ["/tmp/private=sdk/exports"], + ], + [ + "CGO_LDFLAGS", + "-Xlinker -T -Xlinker /tmp/private,sdk/link.ld", + ["/tmp/private,sdk/link.ld"], + ], + [ + "CGO_LDFLAGS", + "-Xlinker -L -Xlinker /tmp/private=sdk", + ["/tmp/private=sdk"], + ], + ["CGO_LDFLAGS", "'-Wl,-L/tmp/private '", ["/tmp/private "]], + ["CGO_LDFLAGS", "-Xlinker -L -Xlinker '/tmp/private '", ["/tmp/private "]], + ["CGO_CFLAGS", "-I -Wl,-rpath,$ORIGIN", ["-Wl,-rpath,$ORIGIN"]], + ["CGO_CFLAGS", "-I -Wl,-L,/tmp/private", ["-Wl,-L,/tmp/private"]], + ["CGO_CFLAGS", "-I -Xlinker", ["-Xlinker"]], + [ + "CGO_CFLAGS", + "-I -Wl,-rpath,$ORIGIN -Wl,-L,other-sdk", + ["-Wl,-rpath,$ORIGIN", "other-sdk"], + ], + ["CGO_LDFLAGS", "-Wl,-T,-rpath,private-sdk", ["-rpath"]], + ["CGO_LDFLAGS", "-Wl,-L,-private-sdk", ["-private-sdk"]], + ["CGO_LDFLAGS", "-Wl,-rpath=-private-sdk", ["-private-sdk"]], + [ + "CGO_LDFLAGS", + "-Xlinker -rpath -Xlinker '/tmp/private '", + ["/tmp/private "], + ], + ["CGO_LDFLAGS", "'-Wl,@private '", ["private "]], + ["CGO_CFLAGS", "'-DROOT=/tmp/private '", ["/tmp/private "]], + ] as const)( + "interprets the operating tool's path in %s=%s", + (key, value, paths) => { + expect(extract(key, value)).toEqual({ paths, ambiguousLinkerR: false }); + }, + ); +}); diff --git a/src/domain/releaseExposureGoPaths.ts b/src/domain/releaseExposureGoPaths.ts index 55ee3c876f..8056c10461 100644 --- a/src/domain/releaseExposureGoPaths.ts +++ b/src/domain/releaseExposureGoPaths.ts @@ -26,12 +26,13 @@ const goProfilePath = (value: string): string | null => /** * One field as Go's `quoted.Split` reads a flag value: a field that begins with * `"` or `'` runs to the matching quote, which it drops; any other field runs to - * whitespace and keeps every quote in it as a literal character. + * ASCII space, tab, CR, or LF and keeps every quote in it as a literal + * character. Other whitespace can be part of a filesystem name. */ -const GO_FLAG_FIELD = /"([^"]*)"|'([^']*)'|[^\s"'][^\s]*/gu; +const GO_FLAG_FIELD = /"([^"]*)"|'([^']*)'|[^ \t\r\n"'][^ \t\r\n]*/gu; /** A path after a flag prefix, whitespace, `=`, or `,`, as in `-isystem/opt/x`. */ const GO_FLAG_PATH = - /(?:^|[\s=,])(?:-(?:idirafter|imacros|include|iprefix|iquote|isysroot|isystem|[BFILT]))?((?:\.{1,2}[\\/]|\/|[A-Za-z]:[\\/]|\\{2}[^\\\s=,]+\\)[^=,]+)/gu; + /(?:^|[ \t\r\n=,])(?:-(?:idirafter|imacros|include|iprefix|iquote|isysroot|isystem|[BFILT]))?((?:\.{1,2}[\\/]|\/|[A-Za-z]:[\\/]|\\{2}[^\\ \t\r\n=,]+\\)[^=,]+)/gu; /** * Settings read by a C compiler, where the flags below take a directory or file. @@ -44,7 +45,7 @@ const GO_FLAG_PATH = */ const GO_COMPILER_SETTING = /^(?:CGO_[A-Z]+FLAGS|-gccgoflags)$/u; const GO_COMPILER_PATH_FLAG = - /^(?:-(?:idirafter|imacros|include|iprefix|iquote|isysroot|isystem|[BFLT]|I(?!-$))=?\s*|--sysroot(?:=|\s+|$)|-fprofile-(?:dir|generate|use)=)(.*)$/su; + /^(?:-(?:idirafter|imacros|include|iprefix|iquote|isysroot|isystem|[BFLT]|I(?!-$))=?[ \t\r\n]*|--sysroot(?:=|[ \t\r\n]+|$)|-fprofile-(?:dir|generate|use)=)(.*)$/su; /** * Linker options a C compiler forwards through `-Wl,a,b` or `-Xlinker a`. The * linker reads its own letters: `-L`, `-T` and `-I` (the ELF interpreter) take