Skip to content

Commit

Permalink
fix(http): do not serve dot files when showDotfiles=false (#6180)
Browse files Browse the repository at this point in the history
  • Loading branch information
kt3k authored Nov 12, 2024
1 parent 69858cf commit 9607eb8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
5 changes: 5 additions & 0 deletions http/file_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,11 @@ async function createServeDirResponse(
normalizedPath = normalizedPath.slice(0, -1);
}

// Exclude dotfiles if showDotfiles is false
if (!showDotfiles && /\/\./.test(normalizedPath)) {
return createStandardResponse(STATUS_CODE.NotFound);
}

const fsPath = join(target, normalizedPath);
const fileInfo = await Deno.stat(fsPath);

Expand Down
19 changes: 17 additions & 2 deletions http/file_server_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,21 @@ Deno.test("serveDir() doesn't show directory listings", async () => {
assertEquals(res.status, 404);
});

Deno.test("serveDir() doesn't show dotfiles", async () => {
Deno.test("serveDir() shows dotfiles when showDotfiles=true", async () => {
const req1 = new Request("http://localhost/");
const res1 = await serveDir(req1, serveDirOptions);
const page1 = await res1.text();

assert(page1.includes(".dotfile"));

const req2 = new Request("http://localhost/.dotfile");
const res2 = await serveDir(req2, serveDirOptions);
const body = await res2.text();

assertEquals(body, "dotfile");
});

Deno.test("serveDir() doesn't show dotfiles when showDotfiles=false", async () => {
const req1 = new Request("http://localhost/");
const res1 = await serveDir(req1, {
...serveDirOptions,
Expand All @@ -462,7 +476,8 @@ Deno.test("serveDir() doesn't show dotfiles", async () => {
});
const body = await res2.text();

assertEquals(body, "dotfile");
assertEquals(res2.status, 404);
assertEquals(body, "Not Found");
});

Deno.test("serveDir() shows .. if it makes sense", async () => {
Expand Down

0 comments on commit 9607eb8

Please sign in to comment.