From a4a633bae7f86949bbd889f04d112a098f5aa87b Mon Sep 17 00:00:00 2001 From: Nianze Wu Date: Thu, 19 Nov 2026 23:15:38 +0800 Subject: [PATCH] fix(windows): bound shortcut string offsets to structure data (#10416) * fix(windows): reject shortcut paths into LinkInfo headers * test(windows): cover shortcut suffix header offsets * fix(windows): bound nested shortcut string offsets * fix(windows): compare only complete shortcut targets --------- Co-authored-by: Cursor Agent --- docs/guides/native/windows-shortcut.md | 9 +- .../windows/WindowsShortcutParser.test.ts | 311 ++++++++++++++++++ src/native/windows/WindowsShortcutParser.ts | 158 +++++++-- 3 files changed, 451 insertions(+), 27 deletions(-) diff --git a/docs/guides/native/windows-shortcut.md b/docs/guides/native/windows-shortcut.md index d7a402e01f..dd4fbd94d3 100644 --- a/docs/guides/native/windows-shortcut.md +++ b/docs/guides/native/windows-shortcut.md @@ -84,6 +84,10 @@ extension. Raw fields and artifact identity are preserved. The code page in a ConsoleFE block describes the console and does not establish the original system ANSI encoding for these paths. +An unreadable declared `LinkInfo` target path leaves the target extension +unknown and prevents a mismatch claim, unless another complete target matches +the icon. + A missing optional section is observed absence of that structure. It is not proof the shortcut never carried that data in another container. @@ -96,8 +100,9 @@ LinkInfo, and ExtraData are limited to 64 KiB by default (1 MiB maximum). Malformed `HeaderSize` or `LinkCLSID`, and exhausted budgets, fail without partial success. Truncation after a valid header, ExtraData size overruns, and odd Unicode lengths return partial coverage with `coverage.failing_offset`. -A LinkInfo offset that points outside its structure is reported at the -offset field itself, so every issue offset lies inside the file. +A LinkInfo, VolumeID, or network-name string offset that points inside its +structure header or outside its structure is reported at the offset field itself, +so every issue offset lies inside the file. Coverage is never `complete` when issues are present. The selected path must be a regular file. Symbolic links are rejected. diff --git a/src/native/windows/WindowsShortcutParser.test.ts b/src/native/windows/WindowsShortcutParser.test.ts index 360ac0d72b..61ae91a648 100644 --- a/src/native/windows/WindowsShortcutParser.test.ts +++ b/src/native/windows/WindowsShortcutParser.test.ts @@ -257,6 +257,194 @@ it.each([ }, ); +it("keeps icon comparison unknown when the Unicode network target points into its header", async () => { + const bytes = windowsShortcutBytes({ + idList: null, + linkInfo: { + netName: "\\\\server\\other", + unicode: true, + networkUnicode: true, + }, + iconLocation: "\\\\server\\target", + extraData: [], + }); + const network = 76 + bytes.readUInt32LE(76 + 20); + bytes.writeUInt32LE(4, network + 20); + const report = await parse(bytes); + const icon = report.observations.find( + ({ kind }) => kind === "icon_location_differs_from_target", + ); + expect(report.coverage.status).toBe("partial"); + expect(icon).toMatchObject({ value: null }); + expect( + report.observations.find(({ kind }) => kind === "target_extension"), + ).toMatchObject({ extension: null }); +}); + +it.each([ + { unicode: false, independent: false, expected: null }, + { unicode: true, independent: false, expected: null }, + { unicode: false, independent: true, expected: false }, + { unicode: true, independent: true, expected: false }, +] as const)( + "does not match a bare base when its suffix is unreadable (Unicode $unicode, independent $independent)", + async ({ unicode, independent, expected }) => { + const bytes = windowsShortcutBytes({ + idList: null, + linkInfo: { + localBasePath: "C:\\Apps", + commonPathSuffix: "run.exe", + unicode, + }, + iconLocation: "C:\\Apps", + ...(independent ? { relativePath: "C:\\Apps" } : {}), + extraData: [], + }); + bytes.writeUInt32LE(4, 76 + 24); + if (unicode) bytes.writeUInt32LE(4, 76 + 32); + const report = await parse(bytes); + expect(report.coverage.status).toBe("partial"); + expect( + report.observations.find( + ({ kind }) => kind === "icon_location_differs_from_target", + ), + ).toMatchObject({ value: expected }); + }, +); + +it("retains an independent network match when the local target is unreadable", async () => { + const bytes = windowsShortcutBytes({ + idList: null, + linkInfo: { + localBasePath: "C:\\Apps", + netName: "\\\\server\\share", + unicode: true, + networkUnicode: true, + }, + iconLocation: "\\\\server\\share", + extraData: [], + }); + bytes.writeUInt32LE(4, 76 + 28); + const report = await parse(bytes); + expect(report.coverage.status).toBe("partial"); + expect( + report.observations.find( + ({ kind }) => kind === "icon_location_differs_from_target", + ), + ).toMatchObject({ value: false }); +}); + +it("does not match an ANSI fallback when the preferred Unicode target is unreadable", async () => { + const bytes = windowsShortcutBytes({ + idList: null, + linkInfo: { localBasePath: "C:\\Apps\\same.exe", unicode: true }, + iconLocation: "C:\\Apps\\same.exe", + extraData: [], + }); + bytes.writeUInt32LE(4, 76 + 28); + const report = await parse(bytes); + expect(report.coverage.status).toBe("partial"); + expect( + report.observations.find( + ({ kind }) => kind === "icon_location_differs_from_target", + ), + ).toMatchObject({ value: null }); +}); + +it("does not match an ANSI network fallback when its Unicode target is unreadable", async () => { + const bytes = windowsShortcutBytes({ + idList: null, + linkInfo: { + netName: "\\\\server\\share", + unicode: true, + networkUnicode: true, + }, + iconLocation: "\\\\server\\share", + extraData: [], + }); + const network = 76 + bytes.readUInt32LE(76 + 20); + bytes.writeUInt32LE(4, network + 20); + const report = await parse(bytes); + expect(report.coverage.status).toBe("partial"); + expect( + report.observations.find( + ({ kind }) => kind === "icon_location_differs_from_target", + ), + ).toMatchObject({ value: null }); +}); + +it("retains a known target match alongside an unreadable network target", async () => { + const bytes = windowsShortcutBytes({ + idList: null, + linkInfo: { + netName: "\\\\server\\other", + unicode: true, + networkUnicode: true, + }, + relativePath: "C:\\Apps\\same.exe", + iconLocation: "C:\\Apps\\same.exe", + extraData: [], + }); + const network = 76 + bytes.readUInt32LE(76 + 20); + bytes.writeUInt32LE(4, network + 20); + const report = await parse(bytes); + expect( + report.observations.find( + ({ kind }) => kind === "icon_location_differs_from_target", + ), + ).toMatchObject({ value: false }); +}); + +it("keeps a valid Unicode local target when only its ANSI offset is malformed", async () => { + const bytes = windowsShortcutBytes({ + idList: null, + linkInfo: { localBasePath: "C:\\Apps\\safe.exe", unicode: true }, + iconLocation: "C:\\Apps\\other.exe", + extraData: [], + }); + bytes.writeUInt32LE(4, 76 + 16); + const report = await parse(bytes); + expect(report.coverage.status).toBe("partial"); + expect(report.link_info?.local_base_path_unicode).toBe("C:\\Apps\\safe.exe"); + expect( + report.observations.find( + ({ kind }) => kind === "icon_location_differs_from_target", + ), + ).toMatchObject({ value: true }); + expect( + report.observations.find(({ kind }) => kind === "target_extension"), + ).toMatchObject({ extension: ".exe" }); +}); + +it.each([28, 32] as const)( + "keeps icon comparison unknown for an unreadable LinkInfo path field at %s", + async (field) => { + const bytes = windowsShortcutBytes({ + idList: null, + linkInfo: { localBasePath: "C:\\Apps\\safe.exe", unicode: true }, + iconLocation: "C:\\Apps\\other.exe", + extraData: [], + }); + const valid = await parse(bytes); + expect( + valid.observations.find( + ({ kind }) => kind === "icon_location_differs_from_target", + ), + ).toMatchObject({ value: true }); + bytes.writeUInt32LE(4, 76 + field); + const report = await parse(bytes); + expect(report.coverage.status).toBe("partial"); + expect( + report.observations.find( + ({ kind }) => kind === "icon_location_differs_from_target", + ), + ).toMatchObject({ value: null }); + expect( + report.observations.find(({ kind }) => kind === "target_extension"), + ).toMatchObject({ extension: null }); + }, +); + it("reads ANSI StringData when IsUnicode is unset", async () => { const report = await parse(ansiShortcut()); expect(report.string_data.name?.encoding).toBe("ansi"); @@ -632,6 +820,129 @@ it("reports a VolumeID that selects a Unicode label it cannot hold", async () => ); }); +it.each([ + { field: 16, key: "local_base_path" }, + { field: 24, key: "common_path_suffix" }, + { field: 28, key: "local_base_path_unicode" }, + { field: 32, key: "common_path_suffix_unicode" }, +] as const)( + "reports LinkInfo strings that point into their header ($key)", + async ({ field, key }) => { + const bytes = windowsShortcutBytes({ + idList: null, + linkInfo: { localBasePath: "C:\\Apps\\safe.exe", commonPathSuffix: "" }, + extraData: [], + }); + const control = await parse(bytes); + expect(control.coverage.status).toBe("complete"); + bytes.writeUInt32LE(4, 76 + field); + const report = await parse(bytes); + expect(report.coverage).toMatchObject({ + status: "partial", + failing_offset: 76 + field, + }); + expect(report.link_info?.[key]).toBeNull(); + expect(report.issues).toContainEqual( + expect.objectContaining({ + code: "malformed_structure", + offset: 76 + field, + }), + ); + expect(report.extra_data.terminal_offset).toBe( + control.extra_data.terminal_offset, + ); + }, +); + +it.each([ + { + name: "ANSI volume label", + kind: "volume", + unicode: false, + field: 12, + key: "label", + }, + { + name: "Unicode volume label", + kind: "volume", + unicode: true, + field: 16, + key: "label_unicode", + }, + { + name: "ANSI network name", + kind: "network", + unicode: false, + field: 8, + key: "net_name", + }, + { + name: "ANSI network device", + kind: "network", + unicode: false, + field: 12, + key: "device_name", + }, + { + name: "Unicode network name", + kind: "network", + unicode: true, + field: 20, + key: "net_name_unicode", + }, + { + name: "Unicode network device", + kind: "network", + unicode: true, + field: 24, + key: "device_name_unicode", + }, +] as const)( + "rejects $name offsets into their structure header", + async ({ kind, unicode, field, key }) => { + const linkInfo = + kind === "volume" + ? { localBasePath: "C:\\Apps\\safe.exe", label: "DATA", unicode } + : { + netName: "\\\\server\\share", + deviceName: "Z:", + unicode, + networkUnicode: unicode, + }; + const bytes = windowsShortcutBytes({ + idList: null, + linkInfo, + extraData: [], + }); + const control = await parse(bytes); + expect(control.coverage.status).toBe("complete"); + const linkOffset = 76; + const nested = + linkOffset + + bytes.readUInt32LE(linkOffset + (kind === "volume" ? 12 : 20)); + bytes.writeUInt32LE(4, nested + field); + const report = await parse(bytes); + expect(report.coverage).toMatchObject({ + status: "partial", + failing_offset: nested + field, + }); + const block = + kind === "volume" + ? report.link_info?.volume + : report.link_info?.common_network_relative_link; + expect(block).toMatchObject({ [key]: null }); + expect(report.issues).toContainEqual( + expect.objectContaining({ + code: "malformed_structure", + offset: nested + field, + }), + ); + expect(report.extra_data.terminal_offset).toBe( + control.extra_data.terminal_offset, + ); + }, +); + it("keeps readable dates for FILETIME values before 1970", async () => { // 1950-01-01T00:00:00Z in 100 ns intervals since 1601. const report = await parse( diff --git a/src/native/windows/WindowsShortcutParser.ts b/src/native/windows/WindowsShortcutParser.ts index 61a99a594d..abe05e9ad9 100644 --- a/src/native/windows/WindowsShortcutParser.ts +++ b/src/native/windows/WindowsShortcutParser.ts @@ -320,6 +320,12 @@ type RecordedPath = { readonly extension: string | null; }; +type LinkInfoPathUncertainty = { + local: boolean; + network: boolean; + suffix: boolean; +}; + const isAscii = (text: string): boolean => [...text].every((char) => char <= "\u007f"); @@ -388,7 +394,10 @@ const iconDiffersFromTarget = ( /** LinkInfo local and network base paths, each joined with CommonPathSuffix. */ const linkInfoTargets = ( info: NonNullable, + uncertainty: LinkInfoPathUncertainty, ): RecordedPath[] => { + // Neither joined LinkInfo path can be proven complete without its suffix. + if (uncertainty.suffix) return []; const suffixText = info.common_path_suffix_unicode ?? info.common_path_suffix; const suffix = suffixText === null || suffixText.length === 0 @@ -414,12 +423,16 @@ const linkInfoTargets = ( }; const values: RecordedPath[] = []; const local = info.local_base_path_unicode ?? info.local_base_path; - if (local !== null && (suffix !== null || local.length > 0)) + if ( + !uncertainty.local && + local !== null && + (suffix !== null || local.length > 0) + ) values.push( withSuffix(recorded(local, info.local_base_path_unicode === null)), ); const net = info.common_network_relative_link; - if (net !== null) { + if (!uncertainty.network && net !== null) { const share = net.net_name_unicode ?? net.net_name; if (share !== null) values.push(withSuffix(recorded(share, net.net_name_unicode === null))); @@ -427,14 +440,19 @@ const linkInfoTargets = ( return values; }; -const targetPaths = (report: { - readonly link_info: WindowsShortcut["link_info"]; - readonly string_data: WindowsShortcut["string_data"]; - readonly id_list: WindowsShortcut["id_list"]; - readonly extra_data: WindowsShortcut["extra_data"]; -}): RecordedPath[] => { +const targetPaths = ( + report: { + readonly link_info: WindowsShortcut["link_info"]; + readonly string_data: WindowsShortcut["string_data"]; + readonly id_list: WindowsShortcut["id_list"]; + readonly extra_data: WindowsShortcut["extra_data"]; + }, + uncertainty: LinkInfoPathUncertainty, +): RecordedPath[] => { const values: RecordedPath[] = - report.link_info === null ? [] : linkInfoTargets(report.link_info); + report.link_info === null + ? [] + : linkInfoTargets(report.link_info, uncertainty); const relative = report.string_data.relative_path; if (relative !== null) values.push(recorded(relative.value, relative.encoding === "ansi")); @@ -465,6 +483,7 @@ const observationsOf = ( WindowsShortcut, "header" | "link_info" | "string_data" | "id_list" | "extra_data" >, + uncertainty: LinkInfoPathUncertainty, ): WindowsShortcut["observations"] => { const argumentsDeclared = (report.header.link_flags.raw & 0x00000020) !== 0; const argumentsField = report.string_data.arguments; @@ -475,7 +494,13 @@ const observationsOf = ( : argumentsDeclared ? null : ""; - const targets = targetPaths(report); + const targets = targetPaths(report, uncertainty); + const targetPathUnknown = + uncertainty.local || uncertainty.network || uncertainty.suffix; + const iconDifference = iconDiffersFromTarget( + report.string_data.icon_location, + targets, + ); return [ { kind: "arguments_present", @@ -490,7 +515,7 @@ const observationsOf = ( { kind: "target_extension", evidence_kind: "derived", - extension: targets[0]?.extension ?? null, + extension: targetPathUnknown ? null : (targets[0]?.extension ?? null), }, { kind: "show_command_minimized", @@ -500,7 +525,8 @@ const observationsOf = ( { kind: "icon_location_differs_from_target", evidence_kind: "derived", - value: iconDiffersFromTarget(report.string_data.icon_location, targets), + value: + targetPathUnknown && iconDifference === true ? null : iconDifference, }, ]; }; @@ -566,8 +592,19 @@ type Walk = { stopped: boolean; stringBytes: number; maxStringBytes: number; + pathUncertainty: LinkInfoPathUncertainty; }; +/** A declared preferred path that could not be decoded cannot be treated as absent. */ +const selectedPathUnreadable = ( + ansiOffset: number, + ansi: string | null, + unicodeOffset: number, + unicode: string | null, +): boolean => + (unicodeOffset !== 0 && unicode === null) || + (unicode === null && ansiOffset !== 0 && ansi === null); + const note = ( walk: Walk, code: WindowsShortcutIssue["code"], @@ -602,14 +639,15 @@ const readAnsiAt = ( offset: number, field: number, walk: Walk, + minimum = 0, ): string | null => { if (offset === 0) return null; - if (offset >= view.length) { + if (offset < minimum || offset >= view.length) { note( walk, "malformed_structure", base + field, - "ANSI string offset is outside LinkInfo", + "ANSI string offset is outside the data area", ); return null; } @@ -634,14 +672,15 @@ const readUtf16At = ( offset: number, field: number, walk: Walk, + minimum = 0, ): string | null => { if (offset === 0) return null; - if (offset >= view.length) { + if (offset < minimum || offset >= view.length) { note( walk, "malformed_structure", base + field, - "Unicode string offset is outside LinkInfo", + "Unicode string offset is outside the data area", ); return null; } @@ -704,10 +743,17 @@ const parseVolume = ( serial: volume.readUInt32LE(8).toString(16).toUpperCase().padStart(8, "0"), label: hasUnicodeLabel ? null - : readAnsiAt(volume, base + offset, labelOffset, 12, walk), + : readAnsiAt(volume, base + offset, labelOffset, 12, walk, 16), label_unicode: hasUnicodeLabel && size >= 20 - ? readUtf16At(volume, base + offset, volume.readUInt32LE(16), 16, walk) + ? readUtf16At( + volume, + base + offset, + volume.readUInt32LE(16), + 16, + walk, + 20, + ) : null, }; }; @@ -761,25 +807,53 @@ const parseNetwork = ( const netNameOffsetUnicode = unicode ? block.readUInt32LE(20) : 0; const deviceNameOffsetUnicode = unicode && validDevice ? block.readUInt32LE(24) : 0; - return { - net_name: readAnsiAt(block, base + offset, netNameOffset, 8, walk), + // If the optional header is truncated, keep readable ANSI data after the + // fixed header while the existing truncation issue marks coverage partial. + const dataOffset = unicode ? 0x1c : 0x14; + const result = { + net_name: readAnsiAt( + block, + base + offset, + netNameOffset, + 8, + walk, + dataOffset, + ), net_name_unicode: readUtf16At( block, base + offset, netNameOffsetUnicode, 20, walk, + dataOffset, + ), + device_name: readAnsiAt( + block, + base + offset, + deviceNameOffset, + 12, + walk, + dataOffset, ), - device_name: readAnsiAt(block, base + offset, deviceNameOffset, 12, walk), device_name_unicode: readUtf16At( block, base + offset, deviceNameOffsetUnicode, 24, walk, + dataOffset, ), network_provider_type: validNetType ? block.readUInt32LE(16) : null, }; + walk.pathUncertainty.network ||= + (declaresUnicode && !unicode) || + selectedPathUnreadable( + netNameOffset, + result.net_name, + netNameOffsetUnicode, + result.net_name_unicode, + ); + return result; }; const parseLinkInfo = ( @@ -832,25 +906,58 @@ const parseLinkInfo = ( : null, local_base_path: (flags & 0x1) !== 0 - ? readAnsiAt(view, walk.offset, localOffset, 16, walk) + ? readAnsiAt(view, walk.offset, localOffset, 16, walk, headerSize) : null, local_base_path_unicode: (flags & 0x1) !== 0 - ? readUtf16At(view, walk.offset, localUnicodeOffset, 28, walk) + ? readUtf16At( + view, + walk.offset, + localUnicodeOffset, + 28, + walk, + headerSize, + ) : null, common_network_relative_link: (flags & 0x2) !== 0 ? parseNetwork(view, walk.offset, networkOffset, 20, walk) : null, - common_path_suffix: readAnsiAt(view, walk.offset, suffixOffset, 24, walk), + common_path_suffix: readAnsiAt( + view, + walk.offset, + suffixOffset, + 24, + walk, + headerSize, + ), common_path_suffix_unicode: readUtf16At( view, walk.offset, suffixUnicodeOffset, 32, walk, + headerSize, ), }; + walk.pathUncertainty.local ||= + (flags & 0x1) !== 0 && + selectedPathUnreadable( + localOffset, + info.local_base_path, + localUnicodeOffset, + info.local_base_path_unicode, + ); + walk.pathUncertainty.suffix ||= selectedPathUnreadable( + suffixOffset, + info.common_path_suffix, + suffixUnicodeOffset, + info.common_path_suffix_unicode, + ); + walk.pathUncertainty.network ||= + (flags & 0x2) !== 0 && + networkOffset !== 0 && + info.common_network_relative_link === null; walk.offset += size; return info; }; @@ -1234,6 +1341,7 @@ export const parseWindowsShortcut = async ( stopped: false, stringBytes: 0, maxStringBytes: input.max_string_bytes, + pathUncertainty: { local: false, network: false, suffix: false }, }; const idList = (flags & 0x00000001) !== 0 @@ -1307,7 +1415,7 @@ export const parseWindowsShortcut = async ( const partial = walk.issues.length > 0; return { ...reportBase, - observations: observationsOf(reportBase), + observations: observationsOf(reportBase, walk.pathUncertainty), coverage: { status: partial ? "partial" : "complete", reason: partial