From 14aca264eab2da4ffa22e0ecf8b61a8059b8de6b Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Thu, 19 Nov 2026 08:33:05 -0500 Subject: [PATCH] fix(windows): report out-of-spec shortcut StringData counts (#12352) --- docs/guides/native/windows-shortcut.md | 6 +- scripts/verify/baselines/raw-buffer-read.json | 2 +- src/native/windows/WindowsShortcutParser.ts | 89 +++++++++---------- .../filesystem/windowsShortcut.test.ts | 51 +++++++++++ 4 files changed, 101 insertions(+), 47 deletions(-) diff --git a/docs/guides/native/windows-shortcut.md b/docs/guides/native/windows-shortcut.md index 0343b62576..d7a402e01f 100644 --- a/docs/guides/native/windows-shortcut.md +++ b/docs/guides/native/windows-shortcut.md @@ -46,7 +46,11 @@ original system ANSI code page is unobserved. `string_data` reports name, relative path, working directory, command-line arguments, and icon location according to `LinkFlags`, each with encoding, `CountCharacters`, byte length, and the decoded value. `IsUnicode` selects -UTF-16LE; otherwise the Latin1 projection is used. +UTF-16LE, with a Latin1 projection for ANSI. A stored count above 260 makes +coverage partial for name, relative path, working directory, or icon location. +The report retains readable values and following fields. Command-line arguments +are exempt from that format limit and count toward the retained string budget. +[MS-SHLLINK StringData](https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-shllink/17b69472-0f34-4bcf-b290-eccdb8de224b) `extra_data` enumerates every block: signature, size, and offset. `EnvironmentVariableDataBlock` and `IconEnvironmentDataBlock` expose ANSI and diff --git a/scripts/verify/baselines/raw-buffer-read.json b/scripts/verify/baselines/raw-buffer-read.json index 0ae759b2f1..18fce61f3c 100644 --- a/scripts/verify/baselines/raw-buffer-read.json +++ b/scripts/verify/baselines/raw-buffer-read.json @@ -77,7 +77,7 @@ "src/native/pwndbg/CoreFileMappingAuthority.ts": 1, "src/native/pwntools/PwntoolsRecordedCrashProvider.ts": 1, "src/native/pwntools/RecordedCrashSourceBindings.ts": 6, - "src/native/windows/WindowsShortcutParser.ts": 49, + "src/native/windows/WindowsShortcutParser.ts": 48, "src/pcap/PcapDnsWire.ts": 5, "src/v8/V8CodeCacheParser.ts": 1 } diff --git a/src/native/windows/WindowsShortcutParser.ts b/src/native/windows/WindowsShortcutParser.ts index c8647f0e11..61a99a594d 100644 --- a/src/native/windows/WindowsShortcutParser.ts +++ b/src/native/windows/WindowsShortcutParser.ts @@ -11,6 +11,10 @@ import type { WindowsShortcutUuidV1, } from "../../domain/native/windowsShortcut.js"; import { WindowsShortcutResourceFailure } from "./WindowsShortcut.errors.js"; +import { + BinaryFormatFailure, + BinaryReader, +} from "../../domain/binaryContainer.js"; import { malformedInput } from "../../domain/parseFailure.js"; import { byteToHex } from "../../kernel/byteToHex.js"; import { sha256Hex } from "../../kernel/sha256Hex.js"; @@ -23,6 +27,8 @@ const FILETIME_UNIX_EPOCH = 116444736000000000n; const UUID_UNIX_EPOCH = 0x01b21dd213814000n; const ENTRIES_PER_YIELD = 32; const VOLUME_UNICODE_LABEL_SELECTOR = 0x14; +// MS-SHLLINK ยง2.4 exempts COMMAND_LINE_ARGUMENTS from this format limit. +const MAX_STRING_DATA_CHARACTERS = 260; /** ECMAScript Date range: 8.64e15 ms either side of the Unix epoch. */ const MAX_DATE_MS = 8_640_000_000_000_000n; @@ -850,60 +856,52 @@ const parseLinkInfo = ( }; const parseStringField = ( - bytes: Buffer, + reader: BinaryReader, walk: Walk, unicode: boolean, label: string, ): WindowsShortcutStringField | null => { - if (!fits(bytes, walk.offset, 2)) { + const offset = walk.offset; + const payloadOffset = offset + 2; + let count: number | undefined; + try { + count = reader.u16(offset); + const payloadBytes = count * (unicode ? 2 : 1); + const payload = reader.range(payloadOffset, payloadBytes, label); + if ( + label !== "COMMAND_LINE_ARGUMENTS" && + count > MAX_STRING_DATA_CHARACTERS + ) + note( + walk, + "malformed_structure", + offset, + `${label} declares ${String(count)} characters, above the ${String(MAX_STRING_DATA_CHARACTERS)}-character StringData limit`, + ); + charge(walk, payloadBytes); + walk.offset = payloadOffset + payloadBytes; + return { + encoding: unicode ? "unicode" : "ansi", + count_characters: count, + byte_length: payloadBytes, + value: payload.toString(unicode ? "utf16le" : "latin1"), + location: { offset, bytes: 2 + payloadBytes }, + }; + } catch (cause: unknown) { + if (!(cause instanceof BinaryFormatFailure)) throw cause; + const oddUnicodePayload = + count !== undefined && + unicode && + (reader.bytes.length - payloadOffset) % 2 === 1; note( walk, - "truncated", - walk.offset, - `${label} CountCharacters is truncated`, + oddUnicodePayload ? "odd_unicode_length" : "truncated", + offset, + `${label} ${count === undefined ? "CountCharacters is" : "string bytes are"} truncated`, true, ); return null; } - const count = bytes.readUInt16LE(walk.offset); - const unit = unicode ? 2 : 1; - const payloadBytes = count * unit; - if (unicode && payloadBytes % 2 === 1) { - note( - walk, - "odd_unicode_length", - walk.offset, - `${label} Unicode payload is an odd length`, - true, - ); - return null; - } - if (!fits(bytes, walk.offset + 2, payloadBytes)) { - const remaining = bytes.length - (walk.offset + 2); - note( - walk, - remaining % 2 === 1 && unicode ? "odd_unicode_length" : "truncated", - walk.offset, - `${label} string bytes are truncated`, - true, - ); - return null; - } - const payload = bytes.subarray( - walk.offset + 2, - walk.offset + 2 + payloadBytes, - ); - charge(walk, payloadBytes); - const value = payload.toString(unicode ? "utf16le" : "latin1"); - const field: WindowsShortcutStringField = { - encoding: unicode ? "unicode" : "ansi", - count_characters: count, - byte_length: payloadBytes, - value, - location: { offset: walk.offset, bytes: 2 + payloadBytes }, - }; - walk.offset += 2 + payloadBytes; - return field; }; const environmentTargets = ( @@ -1262,9 +1260,10 @@ export const parseWindowsShortcut = async ( [0x00000040, "icon_location", "ICON_LOCATION"], ]; if (!walk.stopped) { + const reader = new BinaryReader(bytes, true, "windows-shortcut"); for (const [bit, key, label] of stringFlags) { if ((flags & bit) === 0) continue; - const field = parseStringField(bytes, walk, unicode, label); + const field = parseStringField(reader, walk, unicode, label); stringData[key] = field; if (field === null) break; } diff --git a/tests/boundary/filesystem/windowsShortcut.test.ts b/tests/boundary/filesystem/windowsShortcut.test.ts index f264e04d17..03b2c4795a 100644 --- a/tests/boundary/filesystem/windowsShortcut.test.ts +++ b/tests/boundary/filesystem/windowsShortcut.test.ts @@ -203,6 +203,57 @@ describe("Windows shortcut stable filesystem boundary", () => { }); }); +it.each([false, true])( + "reports an out-of-spec StringData count but retains following fields (Unicode: %s)", + async (unicode) => { + const root = await createTestTempDirectory("rea-shortcut-string-data-"); + const arguments_ = "a".repeat(400); + const service = createWindowsShortcutService(); + for (const count of [260, 261]) { + const path = join(root, `${count}.lnk`); + const name = "n".repeat(count); + const bytes = windowsShortcutBytes({ + unicode, + idList: null, + linkInfo: null, + extraData: [], + name, + arguments: arguments_, + iconLocation: "fixture.ico", + }); + await writeFile(path, bytes); + const result = await service.inspect({ path }); + if (!result.ok) throw result.error; + expect(result.value.normalized_result).toMatchObject({ + coverage: { + status: count === 260 ? "complete" : "partial", + failing_offset: count === 260 ? null : 76, + }, + issues: + count === 260 + ? [] + : [ + { + code: "malformed_structure", + offset: 76, + message: expect.stringContaining("NAME_STRING"), + }, + ], + string_data: { + name: { + value: name, + count_characters: count, + byte_length: count * (unicode ? 2 : 1), + }, + arguments: { value: arguments_, count_characters: 400 }, + icon_location: { value: "fixture.ico" }, + }, + extra_data: { terminal_offset: bytes.length - 4 }, + }); + } + }, +); + describe("Windows shortcut parent directory links", () => { it.skipIf(process.platform === "win32")( "rejects a parent directory link before reading through it",