From 6138acc14ab02362b942cfb13acca83c0b19c61b Mon Sep 17 00:00:00 2001 From: Pawel Romanski Date: Sun, 8 Dec 2024 17:39:29 +0100 Subject: [PATCH] fix cf header encoding --- src/lib/detect.ts | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/lib/detect.ts b/src/lib/detect.ts index 56a037ec49..c3ce6feedb 100644 --- a/src/lib/detect.ts +++ b/src/lib/detect.ts @@ -67,6 +67,14 @@ function getRegionCode(country: string, region: string) { return region.includes('-') ? region : `${country}-${region}`; } +function safeDecodeCfHeader(s: string | undefined | null): string | undefined | null { + if (s === undefined || s === null) { + return s; + } + + return Buffer.from(s, 'latin1').toString('utf-8'); +} + export async function getLocation(ip: string, req: NextApiRequestCollect) { // Ignore local ips if (await isLocalhost(ip)) { @@ -75,9 +83,9 @@ export async function getLocation(ip: string, req: NextApiRequestCollect) { // Cloudflare headers if (req.headers['cf-ipcountry']) { - const country = safeDecodeURIComponent(req.headers['cf-ipcountry']); - const subdivision1 = safeDecodeURIComponent(req.headers['cf-region-code']); - const city = safeDecodeURIComponent(req.headers['cf-ipcity']); + const country = safeDecodeCfHeader(req.headers['cf-ipcountry']); + const subdivision1 = safeDecodeCfHeader(req.headers['cf-region-code']); + const city = safeDecodeCfHeader(req.headers['cf-ipcity']); return { country,