Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(http): do not serve dot files when showDotfiles=false #6180

Merged
merged 1 commit into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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