From cb00a4c8debd58871f8ca69ed9270ba6686a1dd2 Mon Sep 17 00:00:00 2001 From: ShuaixinHuang <1836656514@qq.com> Date: Thu, 19 Nov 2026 19:17:23 +0800 Subject: [PATCH] perf(artifacts): decode URL path escapes in one byte buffer (#12241) --- src/domain/artifactPathSyntax.test.ts | 11 +++++++++ src/domain/artifactPathSyntax.ts | 33 +++++++++++++++++---------- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/src/domain/artifactPathSyntax.test.ts b/src/domain/artifactPathSyntax.test.ts index 50eb86c4d4..0f10041634 100644 --- a/src/domain/artifactPathSyntax.test.ts +++ b/src/domain/artifactPathSyntax.test.ts @@ -4,6 +4,7 @@ import { admitsCanonicalPathSyntax, hasScheme, looksExternal, + percentDecodeUrlPath, stripQueryAndFragment, } from "./artifactPathSyntax.js"; @@ -48,3 +49,13 @@ describe("artifact path syntax", () => { expect(admitsCanonicalPathSyntax("./%5capp.js")).toBe(false); }); }); + +it.each([ + ["./路𐐷/caf%C3%a9%20menu.js", "./路𐐷/café menu.js"], + ["./a%20literal%zz%25.js", "./a literal%zz%.js"], + ["./%F0%90%90%B7.js", "./𐐷.js"], + ["./%C3.js", null], + ["./%00.js", null], +])("preserves URL path decoding for %j", (input, expected) => { + expect(percentDecodeUrlPath(input)).toBe(expected); +}); diff --git a/src/domain/artifactPathSyntax.ts b/src/domain/artifactPathSyntax.ts index 7db6b4161d..adc62e8256 100644 --- a/src/domain/artifactPathSyntax.ts +++ b/src/domain/artifactPathSyntax.ts @@ -51,7 +51,11 @@ export const htmlUrlText = (value: string): string => { }; const UTF8 = strictUtf8Decoder("preserve"); -const UTF8_ENCODER = new TextEncoder(); +const hexNibble = (byte: number): number => { + if (byte >= 0x30 && byte <= 0x39) return byte - 0x30; + const lower = byte | 0x20; + return lower >= 0x61 && lower <= 0x66 ? lower - 0x61 + 10 : -1; +}; /** * Percent-decode a URL path as the URL standard does: valid `%XX` sequences @@ -61,20 +65,25 @@ const UTF8_ENCODER = new TextEncoder(); */ export const percentDecodeUrlPath = (value: string): string | null => { if (!/%[0-9a-f]{2}/iu.test(value)) return value; - const bytes: number[] = []; - for (let index = 0; index < value.length;) { - const escape = value.slice(index, index + 3); - if (/^%[0-9a-f]{2}$/iu.test(escape)) { - bytes.push(Number.parseInt(escape.slice(1), 16)); - index += 3; - continue; + // Percent escapes are ASCII, so their markers cannot be confused with a + // UTF-8 code unit. Compact the encoded path into its own byte buffer. + const bytes = Buffer.from(value, "utf8"); + let written = 0; + for (let read = 0; read < bytes.length; read++) { + const byte = bytes[read]!; + if (byte === 0x25 && read + 2 < bytes.length) { + const high = hexNibble(bytes[read + 1]!); + const low = hexNibble(bytes[read + 2]!); + if (high >= 0 && low >= 0) { + bytes[written++] = (high << 4) | low; + read += 2; + continue; + } } - const character = String.fromCodePoint(value.codePointAt(index) ?? 0); - bytes.push(...UTF8_ENCODER.encode(character)); - index += character.length; + bytes[written++] = byte; } try { - const decoded = UTF8.decode(new Uint8Array(bytes)); + const decoded = UTF8.decode(bytes.subarray(0, written)); return decoded.includes("\0") ? null : decoded; } catch (cause: unknown) { // Invalid UTF-8 cannot name an inventoried path.