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 for readonly headers lambda@edge #433

Merged
merged 2 commits into from
Jun 9, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/flat-coats-train.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"open-next": patch
---

Fix for readonly headers lambda@edge
22 changes: 19 additions & 3 deletions packages/open-next/src/converters/aws-cloudfront.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,19 @@ const CloudFrontBlacklistedHeaders = [
"x-real-ip",
];

// Read-only headers, see: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/edge-function-restrictions-all.html#function-restrictions-read-only-headers
// We should only remove these headers when directly responding in lambda@edge, not for the external middleware
const cloudfrontReadOnlyHeaders = [
"accept-encoding",
"content-length",
"if-modified-since",
"if-none-match",
"if-range",
"if-unmodified-since",
"transfer-encoding",
"via",
];

function normalizeCloudFrontRequestEventHeaders(
rawHeaders: CloudFrontHeaders,
): Record<string, string> {
Expand Down Expand Up @@ -97,14 +110,17 @@ type MiddlewareEvent = {

function convertToCloudfrontHeaders(
headers: Record<string, OutgoingHttpHeader>,
directResponse?: boolean,
) {
const cloudfrontHeaders: CloudFrontHeaders = {};
Object.entries(headers)
.filter(
([key]) =>
!CloudFrontBlacklistedHeaders.some((header) =>
typeof header === "string" ? header === key : header.test(key),
),
) &&
// Only remove read-only headers when directly responding in lambda@edge
(directResponse ? !cloudfrontReadOnlyHeaders.includes(key) : true),
)
.forEach(([key, value]) => {
if (key === "set-cookie") {
Expand Down Expand Up @@ -146,7 +162,7 @@ async function convertToCloudFrontRequestResult(
const cloudfrontResult = {
status: externalResult.statusCode.toString(),
statusDescription: "OK",
headers: convertToCloudfrontHeaders(externalResult.headers),
headers: convertToCloudfrontHeaders(externalResult.headers, true),
bodyEncoding: externalResult.isBase64Encoded
? ("base64" as const)
: ("text" as const),
Expand Down Expand Up @@ -195,7 +211,7 @@ async function convertToCloudFrontRequestResult(
const response: CloudFrontRequestResult = {
status: result.statusCode.toString(),
statusDescription: "OK",
headers: convertToCloudfrontHeaders(responseHeaders),
headers: convertToCloudfrontHeaders(responseHeaders, true),
bodyEncoding: result.isBase64Encoded ? "base64" : "text",
body: result.body,
};
Expand Down
Loading