From d117cc945500bce261cfa6bd501cd19fcb3103f0 Mon Sep 17 00:00:00 2001 From: Jesse Wierzbinski Date: Thu, 16 May 2024 23:05:58 -1000 Subject: [PATCH] fix: :bug: Fix bug setting all requests to have an empty body --- federation/cryptography/index.test.ts | 15 +++++++++++++++ federation/cryptography/index.ts | 4 ++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/federation/cryptography/index.test.ts b/federation/cryptography/index.test.ts index 3bf5fbc..e9056c7 100644 --- a/federation/cryptography/index.test.ts +++ b/federation/cryptography/index.test.ts @@ -180,5 +180,20 @@ describe("SignatureConstructor", () => { ); expect(parts[3].split("=")[1]).toBeString(); }); + + test("should correctly sign a Request", async () => { + const url = new URL("https://example.com"); + const request = new Request(url.toString(), { + method: "GET", + body: body, + }); + const newRequest = await ctor.sign(request); + + headers = newRequest.headers; + expect(headers.get("Signature")).toBeDefined(); + expect(headers.get("Date")).toBeDefined(); + + expect(await newRequest.text()).toBe(body); + }); }); }); diff --git a/federation/cryptography/index.ts b/federation/cryptography/index.ts index 11f9095..7a8d4d7 100644 --- a/federation/cryptography/index.ts +++ b/federation/cryptography/index.ts @@ -266,6 +266,8 @@ export class SignatureConstructor { headers: Headers = new Headers(), ): Promise { if (requestOrMethod instanceof Request) { + const request = requestOrMethod.clone(); + const headers = await this.sign( requestOrMethod.method as HttpVerb, new URL(requestOrMethod.url), @@ -273,8 +275,6 @@ export class SignatureConstructor { requestOrMethod.headers, ); - const request = requestOrMethod.clone(); - request.headers.set("Date", headers.get("Date") ?? ""); request.headers.set("Signature", headers.get("Signature") ?? "");