fix: sanitize location

This commit is contained in:
EnixCoda 2022-08-19 02:44:57 +08:00
parent 1237fc44bc
commit 86775c6c1f
6 changed files with 27 additions and 8 deletions

View file

@ -4,6 +4,7 @@ import * as React from 'react'
import { parseURLSearch, run } from 'utils/general'
import { useLoadedContext } from 'utils/hooks/useLoadedContext'
import { useStateIO } from 'utils/hooks/useStateIO'
import { sanitizedLocation } from 'utils/URLHelper'
import { SideBarStateContext } from './SideBarState'
/**
@ -55,7 +56,7 @@ async function getAccessTokenWithCode(code: string) {
window.history.replaceState(
{},
'removed search param',
window.location.pathname.replace(window.location.search, '?' + search.toString()),
sanitizedLocation.pathname.replace(window.location.search, '?' + search.toString()),
)
return accessToken
}

View file

@ -1,9 +1,9 @@
import { raiseError } from 'analytics'
import { sanitizedLocation } from 'utils/URLHelper'
export function parse(): Partial<Pick<MetaData, 'userName' | 'repoName' | 'type'>> & {
path: string[]
} {
const { pathname } = window.location
const [
,
// ignore content before the first '/'
@ -11,7 +11,7 @@ export function parse(): Partial<Pick<MetaData, 'userName' | 'repoName' | 'type'
repoName,
type,
...path // should be [...branchName.split('/'), ...filePath.split('/')]
] = unescape(decodeURIComponent(pathname)).split('/')
] = unescape(decodeURIComponent(sanitizedLocation.pathname)).split('/')
return {
userName,
repoName,

View file

@ -1,3 +1,4 @@
import { sanitizedLocation } from 'utils/URLHelper'
import * as API from './API'
import { getPRDiffTotalStat, getPullRequestFilesCount, isInPullFilesPage } from './DOMHelper'
import { processTree } from './index'
@ -50,7 +51,7 @@ export async function getPullRequestTreeData(
}
}
const url = new URL(window.location.href)
const url = new URL(sanitizedLocation.href)
url.pathname = `/${userName}/${repoName}/pull/${pullId}/files`
const commentsMap = getCommentsMap(commentData)
const nodes: TreeNode[] = treeData.map(

View file

@ -1,7 +1,7 @@
import { raiseError } from 'analytics'
import { sanitizedLocation } from 'utils/URLHelper'
export function parse(): Partial<MetaData> & { path: string[] } {
const { pathname } = window.location
const [
,
// ignore content before the first '/'
@ -9,7 +9,7 @@ export function parse(): Partial<MetaData> & { path: string[] } {
repoName,
type,
...path // should be [...branchName.split('/'), ...filePath.split('/')]
] = unescape(decodeURIComponent(pathname)).split('/')
] = unescape(decodeURIComponent(sanitizedLocation.pathname)).split('/')
return {
userName,
repoName,

View file

@ -1,7 +1,7 @@
import { raiseError } from 'analytics'
import { sanitizedLocation } from 'utils/URLHelper'
export function parse(): Partial<MetaData> & { path: string[] } {
const { pathname } = window.location
const [
,
// ignore content before the first '/'
@ -9,7 +9,7 @@ export function parse(): Partial<MetaData> & { path: string[] } {
repoName,
type,
...path // should be [...branchName.split('/'), ...filePath.split('/')]
] = unescape(decodeURIComponent(pathname)).split('/')
] = unescape(decodeURIComponent(sanitizedLocation.pathname)).split('/')
return {
userName,
repoName,

17
src/utils/URLHelper.ts Normal file
View file

@ -0,0 +1,17 @@
export const sanitizedLocation = {
get href() {
const { href, pathname } = window.location
const hasDupSlashes = pathname.includes('//')
const needSanitize = hasDupSlashes
if (!needSanitize) return href
const url = new URL(href)
url.pathname = sanitizedLocation.pathname
return url.href
},
get pathname() {
const { pathname } = window.location
// remove duplicated slashes
return pathname.replace(/^\/\/+/g, '/')
},
}