Skip to content

Commit bc90b2e

Browse files
committed
fix(fetch): path traversal attack
1 parent 79d1083 commit bc90b2e

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

src/fetch/__tests__/fetchUtils.test.ts

+23-9
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,28 @@ describe('request', () => {
3434
})
3535
})
3636

37-
describe('createRequest', async () => {
38-
const api = createRequest(BASE_URL)
39-
expect(await api('/a')).toEqual([
40-
`${BASE_URL}/a`,
41-
{
42-
headers: {
43-
'Content-Type': 'application/json',
37+
describe('createRequest', () => {
38+
it('works', async () => {
39+
const api = createRequest(BASE_URL)
40+
expect(await api('/a')).toEqual([
41+
new URL(`${BASE_URL}/a`),
42+
{
43+
headers: {
44+
'Content-Type': 'application/json',
45+
},
46+
},
47+
])
48+
})
49+
50+
it('path traversal attack', async () => {
51+
const api = createRequest(BASE_URL)
52+
expect(await api('/../a')).toEqual([
53+
new URL(`${BASE_URL}/a`),
54+
{
55+
headers: {
56+
'Content-Type': 'application/json',
57+
},
4458
},
45-
},
46-
])
59+
])
60+
})
4761
})

src/fetch/fetch.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export function createRequest(baseUrl: string) {
3737
) {
3838
const newInput =
3939
typeof input === 'string' && input.startsWith('/')
40-
? `${baseUrl}${input}`
40+
? new URL(input, baseUrl)
4141
: input
4242
return request(newInput, options)
4343
}

0 commit comments

Comments
 (0)