From 7f27f649cca0e928a422aaa6182988087338e435 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=A8=E6=9D=89?= Date: Thu, 12 Dec 2019 13:05:26 +0800 Subject: [PATCH] fix: file_server swallowing permission errors (#3467) --- std/http/file_server.ts | 8 +++----- std/http/file_server_test.ts | 26 ++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/std/http/file_server.ts b/std/http/file_server.ts index 41aac5e451..e3caae882b 100755 --- a/std/http/file_server.ts +++ b/std/http/file_server.ts @@ -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, @@ -142,10 +142,7 @@ async function serveDir( } async function serveFallback(req: ServerRequest, e: Error): Promise { - if ( - e instanceof Deno.DenoError && - (e as Deno.DenoError).kind === ErrorKind.NotFound - ) { + if (e instanceof DenoError && e.kind === ErrorKind.NotFound) { return { status: 404, body: encoder.encode("Not found") @@ -297,6 +294,7 @@ listenAndServe( response = await serveFile(req, fsPath); } } catch (e) { + console.error(e.message); response = await serveFallback(req, e); } finally { if (CORSEnabled) { diff --git a/std/http/file_server_test.ts b/std/http/file_server_test.ts index f725b32a20..85785d923e 100644 --- a/std/http/file_server_test.ts +++ b/std/http/file_server_test.ts @@ -93,3 +93,29 @@ test(async function serveFallback(): Promise { killFileServer(); } }); + +test(async function servePermissionDenied(): Promise { + 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://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(); + } +});