Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: correctly decode path #149

Merged
merged 2 commits into from
Apr 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/sirv/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ export default function (dir, opts={}) {
extns.push(...extensions); // [...br, ...gz, orig, ...exts]

if (pathname.indexOf('%') !== -1) {
try { pathname = decodeURIComponent(pathname) }
try { pathname = decodeURI(pathname) }
catch (err) { /* malform uri */ }
}

Expand Down
32 changes: 30 additions & 2 deletions tests/sirv.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ encode('should work when the request path contains encoded characters :: prod',
}
});

encode(`should work when the request path contains space encoded :: dev`, async () => {
encode('should work when the request path contains space encoded :: dev', async () => {
let server = utils.http({ dev: true });

try {
Expand All @@ -136,7 +136,7 @@ encode(`should work when the request path contains space encoded :: dev`, async
}
});

encode(`should work when the request path contains space encoded :: prod`, async () => {
encode('should work when the request path contains space encoded :: prod', async () => {
let server = utils.http({ dev: false });

try {
Expand All @@ -149,6 +149,34 @@ encode(`should work when the request path contains space encoded :: prod`, async
}
});

encode('should not treat "/foo%2Fbar.txt" the same as "/foo.bar.txt" path :: dev', async () => {
let server = utils.http({ dev: true });

try {
let res1 = await server.send('GET', '/about/index.htm');
assert.is(res1.statusCode, 200);

let res2 = await server.send('GET', '/about%2Findex.htm').catch(r => r);
assert.is(res2.statusCode, 404);
} finally {
server.close();
}
});

encode('should not treat "/foo%2Fbar.txt" the same as "/foo.bar.txt" path :: prod', async () => {
let server = utils.http({ dev: false });

try {
let res1 = await server.send('GET', '/about/index.htm');
assert.is(res1.statusCode, 200);

let res2 = await server.send('GET', '/about%2Findex.htm').catch(r => r);
assert.is(res2.statusCode, 404);
} finally {
server.close();
}
});

encode.run();

// ---
Expand Down