Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(signature-v4): getCanonicalHeaders ignores undefined header values #3789

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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