Skip to content

Commit 78690d3

Browse files
authored
fix(signature-v4): getCanonicalHeaders ignores undefined header values (#3789)
1 parent b43e0b5 commit 78690d3

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

packages/signature-v4/src/getCanonicalHeaders.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { HttpRequest } from "@aws-sdk/protocol-http";
2+
import { HeaderBag } from "@aws-sdk/types";
23

34
import { ALWAYS_UNSIGNABLE_HEADERS } from "./constants";
45
import { getCanonicalHeaders } from "./getCanonicalHeaders";
@@ -49,6 +50,24 @@ describe("getCanonicalHeaders", () => {
4950
});
5051
});
5152

53+
it("should ignore headers with undefined values", () => {
54+
const headers: HeaderBag = {
55+
"x-amz-user-agent": "aws-sdk-js-v3",
56+
host: "foo.us-east-1.amazonaws.com",
57+
};
58+
59+
(headers.foo as any) = undefined;
60+
const request = new HttpRequest({
61+
method: "POST",
62+
protocol: "https:",
63+
path: "/",
64+
headers,
65+
hostname: "foo.us-east-1.amazonaws.com",
66+
});
67+
68+
expect(getCanonicalHeaders(request)).toEqual(headers);
69+
});
70+
5271
it("should allow specifying custom unsignable headers", () => {
5372
const request = new HttpRequest({
5473
method: "POST",

packages/signature-v4/src/getCanonicalHeaders.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ export const getCanonicalHeaders = (
1212
): HeaderBag => {
1313
const canonical: HeaderBag = {};
1414
for (const headerName of Object.keys(headers).sort()) {
15+
if (!headers[headerName]) {
16+
continue;
17+
}
18+
1519
const canonicalHeaderName = headerName.toLowerCase();
1620
if (
1721
canonicalHeaderName in ALWAYS_UNSIGNABLE_HEADERS ||

0 commit comments

Comments
 (0)