From 3602fd3a96cbd8b9f8bd84e28ef0bb1e1dffdb07 Mon Sep 17 00:00:00 2001 From: Kyosuke Konishi Date: Thu, 19 Nov 2026 16:25:39 +0900 Subject: [PATCH 1/2] fix(http-message-sig): exclude the fragment from the target URI --- packages/http-message-sig/src/core.ts | 11 +++++++-- packages/http-message-sig/test/core.spec.ts | 25 +++++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/packages/http-message-sig/src/core.ts b/packages/http-message-sig/src/core.ts index 862487d..439bd61 100644 --- a/packages/http-message-sig/src/core.ts +++ b/packages/http-message-sig/src/core.ts @@ -323,6 +323,12 @@ function isNativeResponse(message: SignatureMessage): message is Response { return typeof Response !== "undefined" && message instanceof Response; } +// RFC 9110 Section 7.1: the target URI excludes the fragment. +function withoutFragment(uri: string): string { + const fragment = uri.indexOf("#"); + return fragment === -1 ? uri : uri.slice(0, fragment); +} + function snapshotRequest( message: Request | RequestDescriptor ): RequestSnapshot { @@ -330,7 +336,7 @@ function snapshotRequest( return Object.freeze({ kind: "request", method: message.method, - targetUri: message.url, + targetUri: withoutFragment(message.url), fields: fieldsFromHeaders(message.headers), trailers: Object.freeze([]), }); @@ -452,7 +458,8 @@ function requestTarget( readonly query: string; } { const url = requestUrl(snapshot, name); - if (url.hash !== "") { + // URL.hash is empty for a bare "#". + if (snapshot.targetUri.includes("#")) { return fail( SignatureErrorCode.InvalidComponent, `${name} target URI cannot contain a fragment` diff --git a/packages/http-message-sig/test/core.spec.ts b/packages/http-message-sig/test/core.spec.ts index 0148227..d86c172 100644 --- a/packages/http-message-sig/test/core.spec.ts +++ b/packages/http-message-sig/test/core.spec.ts @@ -308,6 +308,31 @@ describe("createSignature", () => { expect(base).toContain('"@request-target": /a/../b/%7e?q=%2f'); }); + it.each(["https://example.com/path?q#", "https://example.com/path?q#frag"])( + "derives Request components without the fragment of %s", + async (url) => { + const components = ["@target-uri", "@path", "@query"]; + const expected = await signed( + new Request("https://example.com/path?q"), + components + ); + expect((await signed(new Request(url), components)).base).toEqual( + expected.base + ); + } + ); + + it.each(["https://example.com/path?q#", "https://example.com/path?q#frag"])( + "rejects a descriptor target URI with the fragment of %s", + async (targetUri) => { + await expect( + signed({ kind: "request", method: "GET", targetUri, fields: [] }, [ + "@path", + ]) + ).rejects.toMatchObject({ code: SignatureErrorCode.InvalidComponent }); + } + ); + it("rejects @request-target when Fetch cannot provide it", async () => { await expect( createSignature(new Request("https://example.com/path"), { From 5b029d1c4482aebe9c42b73c218845b15011eefe Mon Sep 17 00:00:00 2001 From: Thibault Meunier Date: Wed, 25 Nov 2026 09:42:27 +0100 Subject: [PATCH 2/2] refactor(http-message-sig): reuse parsed URL to strip fragments --- packages/http-message-sig/src/core.ts | 43 ++++++++++++--------- packages/http-message-sig/test/core.spec.ts | 42 +++++++++----------- 2 files changed, 43 insertions(+), 42 deletions(-) diff --git a/packages/http-message-sig/src/core.ts b/packages/http-message-sig/src/core.ts index 439bd61..1f9d38d 100644 --- a/packages/http-message-sig/src/core.ts +++ b/packages/http-message-sig/src/core.ts @@ -48,6 +48,7 @@ const unsupportedParameters = new Set(["sf", "bs", "tr"]); interface RequestSnapshot { readonly kind: "request"; + readonly native: boolean; readonly method: string; readonly targetUri: string; readonly requestTarget?: string; @@ -323,20 +324,15 @@ function isNativeResponse(message: SignatureMessage): message is Response { return typeof Response !== "undefined" && message instanceof Response; } -// RFC 9110 Section 7.1: the target URI excludes the fragment. -function withoutFragment(uri: string): string { - const fragment = uri.indexOf("#"); - return fragment === -1 ? uri : uri.slice(0, fragment); -} - function snapshotRequest( message: Request | RequestDescriptor ): RequestSnapshot { if (isNativeRequest(message)) { return Object.freeze({ kind: "request", + native: true, method: message.method, - targetUri: withoutFragment(message.url), + targetUri: message.url, fields: fieldsFromHeaders(message.headers), trailers: Object.freeze([]), }); @@ -350,6 +346,7 @@ function snapshotRequest( } return Object.freeze({ kind: "request", + native: false, method: message.method, targetUri: message.targetUri, requestTarget: message.requestTarget, @@ -454,38 +451,46 @@ function requestTarget( name: string ): { readonly url: URL; + readonly targetUri: string; readonly path: string; readonly query: string; } { const url = requestUrl(snapshot, name); + let { targetUri } = snapshot; // URL.hash is empty for a bare "#". - if (snapshot.targetUri.includes("#")) { - return fail( - SignatureErrorCode.InvalidComponent, - `${name} target URI cannot contain a fragment` - ); + if (targetUri.includes("#")) { + if (!snapshot.native) { + return fail( + SignatureErrorCode.InvalidComponent, + `${name} target URI cannot contain a fragment` + ); + } + // Fetch does not send the fragment (RFC 9110, Section 7.1). + url.hash = ""; + targetUri = url.href; } - const schemeEnd = snapshot.targetUri.indexOf("://"); + const schemeEnd = targetUri.indexOf("://"); if (schemeEnd === -1) { return fail( SignatureErrorCode.InvalidComponent, `${name} requires an HTTP target URI` ); } - const pathStart = snapshot.targetUri.indexOf("/", schemeEnd + 3); - const queryStart = snapshot.targetUri.indexOf("?", schemeEnd + 3); + const pathStart = targetUri.indexOf("/", schemeEnd + 3); + const queryStart = targetUri.indexOf("?", schemeEnd + 3); const start = pathStart !== -1 && (queryStart === -1 || pathStart < queryStart) ? pathStart : queryStart; - const pathEnd = queryStart === -1 ? snapshot.targetUri.length : queryStart; + const pathEnd = queryStart === -1 ? targetUri.length : queryStart; return Object.freeze({ url, + targetUri, path: start === -1 || start === queryStart ? "/" - : snapshot.targetUri.slice(start, pathEnd), - query: queryStart === -1 ? "?" : snapshot.targetUri.slice(queryStart), + : targetUri.slice(start, pathEnd), + query: queryStart === -1 ? "?" : targetUri.slice(queryStart), }); } @@ -512,7 +517,7 @@ function extractDerived( const target = requestTarget(snapshot, name); switch (name) { case "@target-uri": - return snapshot.targetUri; + return target.targetUri; case "@authority": return target.url.host; case "@scheme": diff --git a/packages/http-message-sig/test/core.spec.ts b/packages/http-message-sig/test/core.spec.ts index d86c172..30390c8 100644 --- a/packages/http-message-sig/test/core.spec.ts +++ b/packages/http-message-sig/test/core.spec.ts @@ -308,30 +308,26 @@ describe("createSignature", () => { expect(base).toContain('"@request-target": /a/../b/%7e?q=%2f'); }); - it.each(["https://example.com/path?q#", "https://example.com/path?q#frag"])( - "derives Request components without the fragment of %s", - async (url) => { - const components = ["@target-uri", "@path", "@query"]; - const expected = await signed( - new Request("https://example.com/path?q"), - components - ); - expect((await signed(new Request(url), components)).base).toEqual( - expected.base - ); - } - ); + it.each([ + ["?", "#"], + ["?q=/part%23", "#frag"], + ])("drops the Request fragment after %s", async (query, fragment) => { + const targetUri = `https://example.com/path${query}`; + const { base } = await signed(new Request(targetUri + fragment), [ + "@target-uri", + "@path", + "@query", + ]); + expect(new TextDecoder().decode(base)).toContain( + `"@target-uri": ${targetUri}\n"@path": /path\n"@query": ${query}\n` + ); + }); - it.each(["https://example.com/path?q#", "https://example.com/path?q#frag"])( - "rejects a descriptor target URI with the fragment of %s", - async (targetUri) => { - await expect( - signed({ kind: "request", method: "GET", targetUri, fields: [] }, [ - "@path", - ]) - ).rejects.toMatchObject({ code: SignatureErrorCode.InvalidComponent }); - } - ); + it("rejects a bare fragment in a descriptor target URI", async () => { + await expect( + signed({ ...request, targetUri: "https://example.com/path#" }, ["@path"]) + ).rejects.toMatchObject({ code: SignatureErrorCode.InvalidComponent }); + }); it("rejects @request-target when Fetch cannot provide it", async () => { await expect(