From 6377a9ccaab92569d732c911b92bdc9a4d50c414 Mon Sep 17 00:00:00 2001 From: William Oldham Date: Tue, 19 Dec 2023 17:41:56 +0000 Subject: [PATCH] Handle invalid CORS origin --- src/index.ts | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/index.ts b/src/index.ts index ff9bddd..3c7e922 100644 --- a/src/index.ts +++ b/src/index.ts @@ -30,11 +30,16 @@ app.use('*', (context, next) => { return cors({ origin: (origin) => { - const hostname = new URL(origin).hostname; - if (allowedCorsHosts.includes(hostname)) { - return origin; + try { + const hostname = new URL(origin).hostname; + if (allowedCorsHosts.includes(hostname)) { + return origin; + } + return ''; + } catch (_) { + // If the Origin URL is not valid, return empty allowed origin + return ''; } - return ''; }, })(context, next); });