Skip to content

Commit 61066fe

Browse files
authored
Fix polyfill for crypto in the middleware (#418)
* fix crypto polyfill for the middleware * update version * Create brave-games-argue.md
1 parent 65f7a60 commit 61066fe

File tree

4 files changed

+57
-6
lines changed

4 files changed

+57
-6
lines changed

.changeset/brave-games-argue.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"open-next": patch
3+
---
4+
5+
Fix polyfill for crypto in the middleware

packages/open-next/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"access": "public"
44
},
55
"name": "open-next",
6-
"version": "3.0.1",
6+
"version": "3.0.1-middleware-fix",
77
"bin": {
88
"open-next": "./dist/index.js"
99
},

packages/open-next/src/converters/aws-apigw-v2.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,31 @@ import { debug } from "../adapters/logger";
66
import { convertToQuery } from "../core/routing/util";
77
import { removeUndefinedFromQuery } from "./utils";
88

9+
// Not sure which one is reallly needed as this is not documented anywhere but server actions redirect are not working without this, it causes a 500 error from cloudfront itself with a 'x-amzErrortype: InternalFailure' header
10+
const CloudFrontBlacklistedHeaders = [
11+
"connection",
12+
"expect",
13+
"keep-alive",
14+
"proxy-authenticate",
15+
"proxy-authorization",
16+
"proxy-connection",
17+
"trailer",
18+
"upgrade",
19+
"x-accel-buffering",
20+
"x-accel-charset",
21+
"x-accel-limit-rate",
22+
"x-accel-redirect",
23+
/x-amz-cf-(.*)/,
24+
/x-amzn-(.*)/,
25+
/x-edge-(.*)/,
26+
"x-cache",
27+
"x-forwarded-proto",
28+
"x-real-ip",
29+
"set-cookie",
30+
"age",
31+
"via",
32+
];
33+
934
function normalizeAPIGatewayProxyEventV2Body(
1035
event: APIGatewayProxyEventV2,
1136
): Buffer {
@@ -65,7 +90,12 @@ function convertToApiGatewayProxyResultV2(
6590
): APIGatewayProxyResultV2 {
6691
const headers: Record<string, string> = {};
6792
Object.entries(result.headers)
68-
.filter(([key]) => key.toLowerCase() !== "set-cookie")
93+
.filter(
94+
([key]) =>
95+
!CloudFrontBlacklistedHeaders.some((header) =>
96+
typeof header === "string" ? header === key : header.test(key),
97+
),
98+
)
6999
.forEach(([key, value]) => {
70100
if (value === null) {
71101
headers[key] = "";

packages/open-next/src/plugins/edge.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,6 @@ globalThis._ROUTES = ${JSON.stringify(routes)};
9999
100100
import {Buffer} from "node:buffer";
101101
globalThis.Buffer = Buffer;
102-
import crypto from "node:crypto";
103-
if(!globalThis.crypto){
104-
globalThis.crypto = crypto;
105-
}
106102
107103
import {AsyncLocalStorage} from "node:async_hooks";
108104
globalThis.AsyncLocalStorage = AsyncLocalStorage;
@@ -125,6 +121,26 @@ class OverrideRequest extends Request {
125121
}
126122
}
127123
globalThis.Request = OverrideRequest;
124+
125+
// If we're not in cloudflare, we polyfill crypto
126+
// https://github.com/vercel/edge-runtime/blob/main/packages/primitives/src/primitives/crypto.js
127+
import { webcrypto } from 'node:crypto'
128+
if(!globalThis.crypto){
129+
globalThis.crypto = new webcrypto.Crypto()
130+
}
131+
if(!globalThis.CryptoKey){
132+
globalThis.CryptoKey = webcrypto.CryptoKey
133+
}
134+
function SubtleCrypto() {
135+
if (!(this instanceof SubtleCrypto)) return new SubtleCrypto()
136+
throw TypeError('Illegal constructor')
137+
}
138+
if(!globalThis.SubtleCrypto) {
139+
globalThis.SubtleCrypto = SubtleCrypto
140+
}
141+
if(!globalThis.Crypto) {
142+
globalThis.Crypto = webcrypto.Crypto
143+
}
128144
`
129145
}
130146
${wasmFiles

0 commit comments

Comments
 (0)