diff --git a/src/native/windows/WindowsShortcutParser.ts b/src/native/windows/WindowsShortcutParser.ts index d2c72adb682..bf677ad50e9 100644 --- a/src/native/windows/WindowsShortcutParser.ts +++ b/src/native/windows/WindowsShortcutParser.ts @@ -700,36 +700,57 @@ const linkInfoStringReader = (encoding: "latin1" | "utf16le") => { const readAnsiAt = linkInfoStringReader("latin1"); const readUtf16At = linkInfoStringReader("utf16le"); -const parseVolume = ( +const LINK_INFO_BLOCKS = { + VolumeID: { field: 12, minimum: 16 }, + CommonNetworkRelativeLink: { field: 20, minimum: 20 }, +} as const; + +const linkInfoBlock = ( linkInfo: BinaryReader, - base: number, - offset: number, - field: number, walk: Walk, -): NonNullable["volume"] => { + offset: number, + label: keyof typeof LINK_INFO_BLOCKS, +): BinaryReader | null => { if (offset === 0) return null; - const view = linkInfo.bytes; - if (!fits(view, offset, 16)) { + const { field, minimum } = LINK_INFO_BLOCKS[label]; + const { bytes, little, format } = linkInfo; + const base = walk.offset; + if (!fits(bytes, offset, minimum)) { note( walk, "truncated", - offset < view.length ? base + offset : base + field, - "VolumeID is truncated", + base + (offset < bytes.length ? offset : field), + `${label} is truncated`, ); return null; } const size = linkInfo.u32(offset); - if (size < 16 || !fits(view, offset, size)) { + if (size < minimum || !fits(bytes, offset, size)) { note( walk, "block_size_overrun", base + offset, - "VolumeID size overruns LinkInfo", + `${label} size overruns LinkInfo`, ); return null; } - const volume = view.subarray(offset, offset + size); - const reader = new BinaryReader(volume, linkInfo.little, linkInfo.format); + return new BinaryReader( + bytes.subarray(offset, offset + size), + little, + format, + ); +}; + +const parseVolume = ( + linkInfo: BinaryReader, + offset: number, + walk: Walk, +): NonNullable["volume"] => { + const reader = linkInfoBlock(linkInfo, walk, offset, "VolumeID"); + if (reader === null) return null; + const { bytes: volume } = reader; + const size = volume.length; + const base = walk.offset + offset; const labelOffset = reader.u32(12); // MS-SHLLINK 2.3.1: exactly 0x14 selects VolumeLabelOffsetUnicode, and that // field exists for no other value. The two label encodings never coexist. @@ -738,7 +759,7 @@ const parseVolume = ( note( walk, "truncated", - base + offset + 16, + base + 16, "VolumeID ends before VolumeLabelOffsetUnicode", ); return { @@ -746,46 +767,31 @@ const parseVolume = ( serial: reader.u32(8).toString(16).toUpperCase().padStart(8, "0"), label: hasUnicodeLabel ? null - : readAnsiAt(volume, base + offset, labelOffset, 12, walk, 16), + : readAnsiAt(volume, base, labelOffset, 12, walk, 16), label_unicode: hasUnicodeLabel && size >= 20 - ? readUtf16At(volume, base + offset, reader.u32(16), 16, walk, 20) + ? readUtf16At(volume, base, reader.u32(16), 16, walk, 20) : null, }; }; const parseNetwork = ( linkInfo: BinaryReader, - base: number, offset: number, - field: number, walk: Walk, ): NonNullable< WindowsShortcut["link_info"] >["common_network_relative_link"] => { - if (offset === 0) return null; - const view = linkInfo.bytes; - if (!fits(view, offset, 20)) { - note( - walk, - "truncated", - offset < view.length ? base + offset : base + field, - "CommonNetworkRelativeLink is truncated", - ); - return null; - } - const size = linkInfo.u32(offset); - if (size < 20 || !fits(view, offset, size)) { - note( - walk, - "block_size_overrun", - base + offset, - "CommonNetworkRelativeLink size overruns LinkInfo", - ); - return null; - } - const block = view.subarray(offset, offset + size); - const reader = new BinaryReader(block, linkInfo.little, linkInfo.format); + const reader = linkInfoBlock( + linkInfo, + walk, + offset, + "CommonNetworkRelativeLink", + ); + if (reader === null) return null; + const { bytes: block } = reader; + const size = block.length; + const base = walk.offset + offset; // MS-SHLLINK 2.3.2: DeviceNameOffset (and its Unicode twin) are only // meaningful when ValidDevice is set; otherwise the fields are ignored. const flags = reader.u32(4); @@ -798,7 +804,7 @@ const parseNetwork = ( note( walk, "truncated", - base + offset + 20, + base + 20, "CommonNetworkRelativeLink ends before its declared Unicode offsets", ); const unicode = declaresUnicode && size >= 0x1c; @@ -808,17 +814,10 @@ const parseNetwork = ( // 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: readAnsiAt(block, base, netNameOffset, 8, walk, dataOffset), net_name_unicode: readUtf16At( block, - base + offset, + base, netNameOffsetUnicode, 20, walk, @@ -826,7 +825,7 @@ const parseNetwork = ( ), device_name: readAnsiAt( block, - base + offset, + base, deviceNameOffset, 12, walk, @@ -834,7 +833,7 @@ const parseNetwork = ( ), device_name_unicode: readUtf16At( block, - base + offset, + base, deviceNameOffsetUnicode, 24, walk, @@ -909,9 +908,7 @@ const parseLinkInfo = ( common_network_relative_link_and_path_suffix: (flags & 0x2) !== 0, }, volume: - (flags & 0x1) !== 0 - ? parseVolume(reader, walk.offset, volumeOffset, 12, walk) - : null, + (flags & 0x1) !== 0 ? parseVolume(reader, volumeOffset, walk) : null, local_base_path: (flags & 0x1) !== 0 ? readAnsiAt(view, walk.offset, localOffset, 16, walk, stringOffset) @@ -928,9 +925,7 @@ const parseLinkInfo = ( ) : null, common_network_relative_link: - (flags & 0x2) !== 0 - ? parseNetwork(reader, walk.offset, networkOffset, 20, walk) - : null, + (flags & 0x2) !== 0 ? parseNetwork(reader, networkOffset, walk) : null, common_path_suffix: readAnsiAt( view, walk.offset,