Skip to content

Commit

Permalink
Merge 4d0b903 into 7d393ed
Browse files Browse the repository at this point in the history
  • Loading branch information
dphang committed Mar 4, 2021
2 parents 7d393ed + 4d0b903 commit 11278dd
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/libs/lambda-at-edge/src/api-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { addHeadersToResponse } from "./headers/addHeaders";
import { getUnauthenticatedResponse } from "./auth/authenticator";
import lambdaAtEdgeCompat from "@sls-next/next-aws-cloudfront";
import { removeLocalePrefixFromUri } from "./routing/locale-utils";
import { removeBlacklistedHeaders } from "./headers/removeBlacklistedHeaders";

const basePath = RoutesManifestJson.basePath;

Expand Down Expand Up @@ -169,5 +170,9 @@ export const handler = async (
// Add custom headers before returning response
addHeadersToResponse(request.uri, response, routesManifest);

if (response.headers) {
removeBlacklistedHeaders(response.headers);
}

return response;
};
6 changes: 6 additions & 0 deletions packages/libs/lambda-at-edge/src/default-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import {
isLocalePrefixedUri,
removeLocalePrefixFromUri
} from "./routing/locale-utils";
import { removeBlacklistedHeaders } from "./headers/removeBlacklistedHeaders";

const basePath = RoutesManifestJson.basePath;

Expand Down Expand Up @@ -248,6 +249,11 @@ export const handler = async (
);
}

// Remove blacklisted headers
if (response.headers) {
removeBlacklistedHeaders(response.headers);
}

const tHandlerEnd = now();

log("handler execution time", tHandlerBegin, tHandlerEnd);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Blacklisted or read-only headers in CloudFront
import { CloudFrontHeaders } from "aws-lambda";

const blacklistedHeaders = [
"connection",
"expect",
"keep-alive",
"proxy-authenticate",
"proxy-authorization",
"proxy-connection",
"trailer",
"upgrade",
"x-accel-buffering",
"x-accel-charset",
"x-accel-limit-rate",
"x-accel-redirect",
"x-cache",
"x-forwarded-proto",
"x-real-ip"
];

const blacklistedHeaderPrefixes = ["x-amz-cf-", "x-amzn-", "x-edge-"];

export function isBlacklistedHeader(name: string): boolean {
const lowerCaseName = name.toLowerCase();

for (const prefix of blacklistedHeaderPrefixes) {
if (lowerCaseName.startsWith(prefix)) {
return true;
}
}

return blacklistedHeaders.includes(lowerCaseName);
}

export function removeBlacklistedHeaders(headers: CloudFrontHeaders): void {
for (const header in headers) {
if (isBlacklistedHeader(header)) {
delete headers[header];
}
}
}
5 changes: 5 additions & 0 deletions packages/libs/lambda-at-edge/src/image-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
getDomainRedirectPath
} from "./routing/redirector";
import { getUnauthenticatedResponse } from "./auth/authenticator";
import { removeBlacklistedHeaders } from "./headers/removeBlacklistedHeaders";

const basePath = RoutesManifestJson.basePath;

Expand Down Expand Up @@ -102,6 +103,10 @@ export const handler = async (

addHeadersToResponse(request.uri, response, routesManifest);

if (response.headers) {
removeBlacklistedHeaders(response.headers);
}

return response;
} else {
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { handler } from "../../src/api-handler";
import { CloudFrontResponseResult } from "next-aws-cloudfront/node_modules/@types/aws-lambda";
import { runRedirectTestWithHandler } from "../utils/runRedirectTest";
import { CloudFrontResultResponse } from "aws-lambda";
import { isBlacklistedHeader } from "../../src/headers/removeBlacklistedHeaders";

jest.mock("node-fetch", () => require("fetch-mock-jest").sandbox());

Expand Down Expand Up @@ -231,6 +232,11 @@ describe("API lambda handler", () => {
value: expectedHeaders[header]
});
}

// Verify no blacklisted headers are present
for (const header in response.headers) {
expect(isBlacklistedHeader(header)).toBe(false);
}
}
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
CloudFrontOrigin
} from "aws-lambda";
import { runRedirectTestWithHandler } from "../utils/runRedirectTest";
import { isBlacklistedHeader } from "../../src/headers/removeBlacklistedHeaders";

jest.mock("node-fetch", () => require("fetch-mock-jest").sandbox());

Expand Down Expand Up @@ -363,6 +364,11 @@ describe("Lambda@Edge", () => {

expect(decodedBody).toEqual(expectedPage);
expect(cfResponse.status).toEqual(200);

// Verify no blacklisted headers are present
for (const header in response.headers) {
expect(isBlacklistedHeader(header)).toBe(false);
}
}
);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module.exports = {
default: (req, res) => {
res.setHeader("connection", "keep-alive"); // AWS Blacklisted header will be removed
res.end("pages/api/getCustomers");
}
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module.exports = {
render: (req, res) => {
res.setHeader("connection", "keep-alive"); // AWS Blacklisted header will be removed
res.end("pages/customers/index.js");
},
renderReqToHTML: (req, res) => {
Expand Down

0 comments on commit 11278dd

Please sign in to comment.