Skip to content

Commit 245b3bd

Browse files
authoredFeb 17, 2021
fix: handle querystring parameters in path (#276)
1 parent ec49cdf commit 245b3bd

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed
 

‎src/options.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ export async function processOptions(
129129
options.serverRoot = options.path[0];
130130
if (s.isFile()) {
131131
const pathParts = options.path[0].split(path.sep);
132-
options.path = [path.sep + pathParts[pathParts.length - 1]];
132+
options.path = [path.join('.', pathParts[pathParts.length - 1])];
133133
options.serverRoot =
134134
pathParts.slice(0, pathParts.length - 1).join(path.sep) || '.';
135135
} else {

‎src/server.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as fs from 'fs';
44
import {promisify} from 'util';
55
import * as marked from 'marked';
66
import * as mime from 'mime';
7+
import {URL} from 'url';
78
import escape = require('escape-html');
89
import enableDestroy = require('server-destroy');
910

@@ -44,9 +45,10 @@ async function handleRequest(
4445
root: string,
4546
options: WebServerOptions
4647
) {
47-
const pathParts = req.url?.split('/') || [];
48+
const url = new URL(req.url || '/', `http://localhost:${options.port}`);
49+
const pathParts = url.pathname.split('/').filter(x => !!x);
4850
const originalPath = path.join(root, ...pathParts);
49-
if (req.url?.endsWith('/')) {
51+
if (url.pathname.endsWith('/')) {
5052
pathParts.push('index.html');
5153
}
5254
const localPath = path.join(root, ...pathParts);

‎test/test.server.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ describe('server', () => {
4646
it('should protect against path escape attacks', async () => {
4747
const url = `${rootUrl}/../../etc/passwd`;
4848
const res = await request({url, validateStatus: () => true});
49-
assert.strictEqual(res.status, 500);
49+
assert.strictEqual(res.status, 404);
5050
});
5151

5252
it('should return a 404 for missing paths', async () => {
@@ -61,4 +61,18 @@ describe('server', () => {
6161
assert.strictEqual(res.status, 200);
6262
assert.strictEqual(res.data, contents);
6363
});
64+
65+
it('should ignore query strings', async () => {
66+
const url = `${rootUrl}/index.html?a=b`;
67+
const res = await request({url});
68+
assert.strictEqual(res.status, 200);
69+
assert.strictEqual(res.data, contents);
70+
});
71+
72+
it('should ignore query strings in a directory', async () => {
73+
const url = `${rootUrl}/?a=b`;
74+
const res = await request({url});
75+
assert.strictEqual(res.status, 200);
76+
assert.strictEqual(res.data, contents);
77+
});
6478
});

0 commit comments

Comments
 (0)
Please sign in to comment.