From a2a85696b55e8d05dd4703631896f94c4b2fabb5 Mon Sep 17 00:00:00 2001 From: Ananth Venkatesh <46249765+quantum9Innovation@users.noreply.github.com> Date: Thu, 7 Mar 2024 09:48:46 -0800 Subject: [PATCH] fix(security): incorrect url substring sanitization (#50) * fix(security): incorrect url substring sanitization * fix: graceful error handling in git url parsing * refactor: simplify url parsing logic --- src/app/components/DataDisplay.tsx | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/app/components/DataDisplay.tsx b/src/app/components/DataDisplay.tsx index 923c685..5fb7c97 100644 --- a/src/app/components/DataDisplay.tsx +++ b/src/app/components/DataDisplay.tsx @@ -7,9 +7,19 @@ import Link from 'next/link' import { Fragment } from 'react' const getRepo = (link: string) => { - if (link.includes('github.com')) { - const owner = link.split('/')[3] - const name = link.split('/')[4] + let host + + try { + host = new URL(link).host + } catch (error) { + console.error('Invalid URL:', link) + return + } + + if (host === 'github.com') { + const parts = link.split('/') + const owner = parts[3] + const name = parts[4] return `git:${owner}/${name}` } }