From 3f095e0b1410bb2d5f8916e6250f76a5796934c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bajto=C5=A1?= Date: Wed, 17 Jan 2024 16:16:36 +0100 Subject: [PATCH] refactor: simplify handling of '//' in req.url MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Miroslav Bajtoš --- lib/handler.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/handler.js b/lib/handler.js index 50dd462..90c2ac9 100644 --- a/lib/handler.js +++ b/lib/handler.js @@ -32,9 +32,8 @@ export const createHandler = ({ * @param {import('pg').Pool} pgPool */ const handler = async (req, res, pgPool) => { - // Fix the edge case: new URL('//foo', 'http://127.0.0.1') produces href "http://foo/" - const reqUrl = req.url.replace(/^\/+/, '/') - const { pathname, searchParams } = new URL(reqUrl, 'http://127.0.0.1') + // Caveat! `new URL('//foo', 'http://127.0.0.1')` would produce "http://foo/" - not what we want! + const { pathname, searchParams } = new URL(`http://127.0.0.1${req.url}`) const segs = pathname.split('/').filter(Boolean) if (req.method === 'GET' && segs[0] === 'retrieval-success-rate' && segs.length === 1) { await getStatsWithFilterAndCaching(