Skip to content

Commit

Permalink
fix(file_server): don't crash on "%" pathname (denoland#3953)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhmushan committed Feb 11, 2020
1 parent b67f20b commit 9201949
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
12 changes: 9 additions & 3 deletions std/http/file_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,9 +301,15 @@ function html(strings: TemplateStringsArray, ...values: unknown[]): string {
listenAndServe(
addr,
async (req): Promise<void> => {
const normalizedUrl = posix.normalize(req.url);
const decodedUrl = decodeURIComponent(normalizedUrl);
const fsPath = posix.join(target, decodedUrl);
let normalizedUrl = posix.normalize(req.url);
try {
normalizedUrl = decodeURIComponent(normalizedUrl);
} catch (e) {
if (!(e instanceof URIError)) {
throw e;
}
}
const fsPath = posix.join(target, normalizedUrl);

let response: Response;
try {
Expand Down
11 changes: 7 additions & 4 deletions std/http/file_server_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,15 @@ test(async function serveFallback(): Promise<void> {
}
});

test(async function serveFallback(): Promise<void> {
test(async function serveWithUnorthodoxFilename(): Promise<void> {
await startFileServer();
try {
const res = await fetch(
"http://localhost:4500/http/testdata/test%20file.txt"
);
let res = await fetch("http://localhost:4500/http/testdata/%");
assert(res.headers.has("access-control-allow-origin"));
assert(res.headers.has("access-control-allow-headers"));
assertEquals(res.status, 200);

res = await fetch("http://localhost:4500/http/testdata/test%20file.txt");
assert(res.headers.has("access-control-allow-origin"));
assert(res.headers.has("access-control-allow-headers"));
assertEquals(res.status, 200);
Expand Down
Empty file added std/http/testdata/%
Empty file.

0 comments on commit 9201949

Please sign in to comment.