From 0c0ee187950d1d7afe27bc70d6e0ef6cea5df421 Mon Sep 17 00:00:00 2001 From: EnixCoda Date: Wed, 11 Nov 2020 17:42:42 +0800 Subject: [PATCH] fix: handle path with unsafe trunks --- src/platforms/GitHub/index.ts | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/src/platforms/GitHub/index.ts b/src/platforms/GitHub/index.ts index 435f52c..a9eda0b 100644 --- a/src/platforms/GitHub/index.ts +++ b/src/platforms/GitHub/index.ts @@ -68,7 +68,15 @@ function getUrlForRedirect( type = 'blob', path = '', ) { - return `https://${window.location.host}/${userName}/${repoName}/${type}/${branchName}/${path}` + // Modern browsers have great support for handling unsafe URL, + // It may be possible to sanitize path with + // `path => path.includes('#') ? path.replace(/#/g, '%23') : '...' + return `https://${ + window.location.host + }/${userName}/${repoName}/${type}/${branchName}/${path + .split('/') + .map(encodeURIComponent) + .join('/')}` } export function isEnterprise() { @@ -142,18 +150,15 @@ export const GitHub: Platform = { const creator = await createPullFileResolver(userName, repoName, pullId) - const nodes: TreeNode[] = treeData.map(item => { - const id = creator(item.filename) - return { - path: item.filename || '', - type: 'blob', - name: item.filename?.replace(/^.*\//, '') || '', - url: `https://${window.location.host}/${metaData.userName}/${ - metaData.repoName - }/pull/${pullId}/files${window.location.search}#${id || ''}`, - sha: item.sha, - } - }) + const nodes: TreeNode[] = treeData.map(item => ({ + path: item.filename || '', + type: 'blob', + name: item.filename?.replace(/^.*\//, '') || '', + url: `https://${window.location.host}/${metaData.userName}/${ + metaData.repoName + }/pull/${pullId}/files${window.location.search}#${creator(item.filename) || ''}`, + sha: item.sha, + })) const root = processTree(nodes) return { root }