From 1e3ae5d44553ceab3ae94391b2d41e7dd7630822 Mon Sep 17 00:00:00 2001 From: Brandon Bayer Date: Tue, 18 Jan 2022 12:11:23 -0500 Subject: [PATCH] fix passport adapter to work behind cloudfront (#3132) (patch) --- nextjs/packages/next/stdlib-server/middleware.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/nextjs/packages/next/stdlib-server/middleware.ts b/nextjs/packages/next/stdlib-server/middleware.ts index 4a27aea166..5f9047bb36 100644 --- a/nextjs/packages/next/stdlib-server/middleware.ts +++ b/nextjs/packages/next/stdlib-server/middleware.ts @@ -149,10 +149,14 @@ function getProtocol(req: MiddlewareRequest) { if (req.connection.encrypted) { return 'https' } + + if (!req.headers) return 'http' + const forwardedProto = - req.headers && - ((req.headers['forwarded'] as string)?.match(/(?<=proto=).+/g)?.[0] || - (req.headers['x-forwarded-proto'] as string)) + (req.headers['forwarded'] as string)?.match(/(?<=proto=).+/g)?.[0] || + (req.headers['x-forwarded-proto'] as string) || + (req.headers['CloudFront-Forwarded-Proto'] as string) + if (forwardedProto) { return forwardedProto.split(/\s*,\s*/)[0] }