Skip to content

Commit

Permalink
fix(file_server): don't crash on "%" pathname (denoland/deno#3953)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhmushan authored and denobot committed Jan 31, 2021
1 parent a0967c8 commit f380776
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
12 changes: 9 additions & 3 deletions 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 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 http/testdata/%
Empty file.

0 comments on commit f380776

Please sign in to comment.