Skip to content

Commit

Permalink
fix(signature-v4): getCanonicalHeaders ignores undefined header values (
Browse files Browse the repository at this point in the history
  • Loading branch information
lew-gordon committed Jul 12, 2022
1 parent b43e0b5 commit 78690d3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
19 changes: 19 additions & 0 deletions packages/signature-v4/src/getCanonicalHeaders.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { HttpRequest } from "@aws-sdk/protocol-http";
import { HeaderBag } from "@aws-sdk/types";

import { ALWAYS_UNSIGNABLE_HEADERS } from "./constants";
import { getCanonicalHeaders } from "./getCanonicalHeaders";
Expand Down Expand Up @@ -49,6 +50,24 @@ describe("getCanonicalHeaders", () => {
});
});

it("should ignore headers with undefined values", () => {
const headers: HeaderBag = {
"x-amz-user-agent": "aws-sdk-js-v3",
host: "foo.us-east-1.amazonaws.com",
};

(headers.foo as any) = undefined;
const request = new HttpRequest({
method: "POST",
protocol: "https:",
path: "/",
headers,
hostname: "foo.us-east-1.amazonaws.com",
});

expect(getCanonicalHeaders(request)).toEqual(headers);
});

it("should allow specifying custom unsignable headers", () => {
const request = new HttpRequest({
method: "POST",
Expand Down
4 changes: 4 additions & 0 deletions packages/signature-v4/src/getCanonicalHeaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ export const getCanonicalHeaders = (
): HeaderBag => {
const canonical: HeaderBag = {};
for (const headerName of Object.keys(headers).sort()) {
if (!headers[headerName]) {
continue;
}

const canonicalHeaderName = headerName.toLowerCase();
if (
canonicalHeaderName in ALWAYS_UNSIGNABLE_HEADERS ||
Expand Down

0 comments on commit 78690d3

Please sign in to comment.