Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 24 additions & 12 deletions packages/http-message-sig/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -329,6 +330,7 @@ function snapshotRequest(
if (isNativeRequest(message)) {
return Object.freeze({
kind: "request",
native: true,
method: message.method,
targetUri: message.url,
fields: fieldsFromHeaders(message.headers),
Expand All @@ -344,6 +346,7 @@ function snapshotRequest(
}
return Object.freeze({
kind: "request",
native: false,
method: message.method,
targetUri: message.targetUri,
requestTarget: message.requestTarget,
Expand Down Expand Up @@ -448,37 +451,46 @@ function requestTarget(
name: string
): {
readonly url: URL;
readonly targetUri: string;
readonly path: string;
readonly query: string;
} {
const url = requestUrl(snapshot, name);
if (url.hash !== "") {
return fail(
SignatureErrorCode.InvalidComponent,
`${name} target URI cannot contain a fragment`
);
let { targetUri } = snapshot;
// URL.hash is empty for a bare "#".
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),
});
}

Expand All @@ -505,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":
Expand Down
21 changes: 21 additions & 0 deletions packages/http-message-sig/test/core.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,27 @@ describe("createSignature", () => {
expect(base).toContain('"@request-target": /a/../b/%7e?q=%2f');
});

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("rejects a bare fragment in a descriptor target URI", async () => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💬 SUGGESTION · Code Quality

Add a descriptor test for a non-empty fragment (and for @target-uri)

The only descriptor rejection test uses a bare # with @path. Add a case with https://example.com/path#frag, and one that requests @target-uri. Together they would show that the new includes("#") check rejects non-empty fragments too, and that every derived component goes through it.

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(
createSignature(new Request("https://example.com/path"), {
Expand Down