Skip to content

Commit d797244

Browse files
authored
more robust url checking (#422)
1 parent 1923cff commit d797244

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

src/api.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,9 @@ export default class Api {
158158
}
159159
}
160160

161-
function isSafeURL(url: string) {
161+
function isSafeURL(origUrl: string) {
162+
// browsers interpret backslash as slash
163+
const url = origUrl.replace(/\\/g, '/');
162164
if (url.endsWith('/..')) {
163165
return false;
164166
}

src/datasource.test.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,25 @@ test('range gets converted into unix epoch notation', () => {
4242
describe('datasource', () => {
4343
it('should not allow urls that contains ..', async () => {
4444
const ds = new JsonDataSource({ url: 'http://localhost:3000', jsonData: {} } as any);
45-
const badPaths = ['/..', '/..?', '/../../', '/../..?'];
45+
const badPaths = [
46+
'/..',
47+
'\\..',
48+
'/..?',
49+
'\\..?',
50+
'/../../',
51+
'\\../../',
52+
'/..\\../',
53+
'\\..\\../',
54+
'/../..?',
55+
'\\../..?',
56+
];
4657

4758
for (let path of badPaths) {
4859
const response = ds.doRequest({ urlPath: path, method: 'GET' } as any);
4960
await expect(response).rejects.toThrowError('URL path contains unsafe characters');
5061
}
5162

52-
const goodPaths = ['/..thing', '/one..two/', '/thing../'];
63+
const goodPaths = ['/..thing', '\\..thing', '/one..two/', '\\one..two\\', '/thing../', '\\thing..\\'];
5364

5465
for (let path of goodPaths) {
5566
const response = ds.doRequest({ urlPath: path, method: 'GET' } as any);

0 commit comments

Comments
 (0)