Skip to content

Commit

Permalink
fix: file_server swallowing permission errors (denoland#3467)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhmushan authored and ry committed Dec 12, 2019
1 parent d146d45 commit 7f27f64
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
8 changes: 3 additions & 5 deletions std/http/file_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// TODO Add tests like these:
// https://github.com/indexzero/http-server/blob/master/test/http-server-test.js

const { ErrorKind, cwd, args, stat, readDir, open } = Deno;
const { ErrorKind, DenoError, cwd, args, stat, readDir, open } = Deno;
import { posix } from "../path/mod.ts";
import {
listenAndServe,
Expand Down Expand Up @@ -142,10 +142,7 @@ async function serveDir(
}

async function serveFallback(req: ServerRequest, e: Error): Promise<Response> {
if (
e instanceof Deno.DenoError &&
(e as Deno.DenoError<Deno.ErrorKind.NotFound>).kind === ErrorKind.NotFound
) {
if (e instanceof DenoError && e.kind === ErrorKind.NotFound) {
return {
status: 404,
body: encoder.encode("Not found")
Expand Down Expand Up @@ -297,6 +294,7 @@ listenAndServe(
response = await serveFile(req, fsPath);
}
} catch (e) {
console.error(e.message);
response = await serveFallback(req, e);
} finally {
if (CORSEnabled) {
Expand Down
26 changes: 26 additions & 0 deletions std/http/file_server_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,29 @@ test(async function serveFallback(): Promise<void> {
killFileServer();
}
});

test(async function servePermissionDenied(): Promise<void> {
const deniedServer = Deno.run({
args: [Deno.execPath(), "run", "--allow-net", "http/file_server.ts"],
stdout: "piped",
stderr: "piped"
});
const reader = new TextProtoReader(new BufReader(deniedServer.stdout!));
const errReader = new TextProtoReader(new BufReader(deniedServer.stderr!));
const s = await reader.readLine();
assert(s !== Deno.EOF && s.includes("server listening"));

try {
await fetch("http:https://localhost:4500/");
assertEquals(
await errReader.readLine(),
"run again with the --allow-read flag"
);
} catch (e) {
throw e;
} finally {
deniedServer.close();
deniedServer.stdout!.close();
deniedServer.stderr!.close();
}
});

0 comments on commit 7f27f64

Please sign in to comment.