From c5754810c0b2cda1442e9a7593a707ba431b0179 Mon Sep 17 00:00:00 2001 From: Claas Augner Date: Fri, 12 May 2023 12:50:13 +0200 Subject: [PATCH] fix(cloud-function): normalize encoded leading slash --- cloud-function/src/middlewares/redirect-leading-slash.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/cloud-function/src/middlewares/redirect-leading-slash.ts b/cloud-function/src/middlewares/redirect-leading-slash.ts index acd50cae75fa..910c0ba8377b 100644 --- a/cloud-function/src/middlewares/redirect-leading-slash.ts +++ b/cloud-function/src/middlewares/redirect-leading-slash.ts @@ -20,9 +20,14 @@ export async function redirectLeadingSlash( next: NextFunction ) { const pathname = req.url; - if (pathname.startsWith("//")) { - return redirect(res, pathname.replace(/^\/+/g, "/")); + const normalizedPathname = normalizeLeadingSlash(pathname); + if (pathname !== normalizedPathname) { + return redirect(res, normalizedPathname); } next(); } + +function normalizeLeadingSlash(pathname: string): string { + return pathname.replace(/^(\/|%2f)+/i, "/"); +}