-
Notifications
You must be signed in to change notification settings - Fork 750
fix: make static folders with dot such as .well-known allowed
#2929
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
Merged
marvinhagemeister
merged 1 commit into
freshframework:main
from
csvn:fix/handle-static-dot-folder
May 13, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| import { expect } from "@std/expect"; | ||
| import * as path from "@std/path"; | ||
| import { ProdBuildCache, type StaticFile } from "./build_cache.ts"; | ||
| import type { ResolvedFreshConfig } from "./mod.ts"; | ||
|
|
||
| async function getContent(readResult: Promise<StaticFile | null>) { | ||
| const res = await readResult; | ||
| if (res === null) return null; | ||
| if (res.readable instanceof Uint8Array) throw new Error("not implemented"); | ||
| return new Response(res.readable).text(); | ||
| } | ||
|
|
||
| Deno.test({ | ||
| name: "ProdBuildCache - should error if reading outside of staticDir", | ||
| fn: async () => { | ||
| const tmp = await Deno.makeTempDir(); | ||
| const config: ResolvedFreshConfig = { | ||
| root: tmp, | ||
| mode: "production", | ||
| basePath: "/", | ||
| staticDir: path.join(tmp, "static"), | ||
| build: { | ||
| outDir: path.join(tmp, "dist"), | ||
| }, | ||
| }; | ||
| await Deno.mkdir(path.join(tmp, "static", ".well-known"), { | ||
| recursive: true, | ||
| }); | ||
| await Deno.mkdir(path.join(tmp, "dist", "static"), { | ||
| recursive: true, | ||
| }); | ||
| await Promise.all([ | ||
| Deno.writeTextFile( | ||
| path.join(tmp, "dist", "secret-styles.css"), | ||
| "SECRET!", | ||
| ), | ||
| Deno.writeTextFile(path.join(tmp, "SECRETS.txt"), "SECRET!"), | ||
| Deno.writeTextFile(path.join(tmp, "dist", "static", "styles.css"), "OK"), | ||
| Deno.writeTextFile( | ||
| path.join(tmp, "static", ".well-known", "foo.txt"), | ||
| "OK", | ||
| ), | ||
| ]); | ||
| const buildCache = new ProdBuildCache( | ||
| config, | ||
| new Map(), | ||
| new Map([ | ||
| ["../secret-styles.css", { generated: true, hash: "SECRET!" }], | ||
| ["../SECRETS.txt", { generated: false, hash: "SECRET!" }], | ||
| ["./../secret-styles.css", { generated: true, hash: "SECRET!" }], | ||
| ["./../SECRETS.txt", { generated: false, hash: "SECRET!" }], | ||
| ["styles.css", { generated: true, hash: "OK" }], | ||
| [".well-known/foo.txt", { generated: false, hash: "OK" }], | ||
| ["./styles.css", { generated: true, hash: "OK" }], | ||
| ["./.well-known/foo.txt", { generated: false, hash: "OK" }], | ||
| ]), | ||
| true, | ||
| ); | ||
|
|
||
| const secret1 = getContent(buildCache.readFile("../styles.css")); | ||
| const secret2 = getContent(buildCache.readFile("../SECRETS.txt")); | ||
| const secret3 = getContent(buildCache.readFile("./../styles.css")); | ||
| const secret4 = getContent(buildCache.readFile("./../SECRETS.txt")); | ||
| const public1 = getContent(buildCache.readFile("styles.css")); | ||
| const public2 = getContent(buildCache.readFile(".well-known/foo.txt")); | ||
| const public3 = getContent(buildCache.readFile("./styles.css")); | ||
| const public4 = getContent(buildCache.readFile("./.well-known/foo.txt")); | ||
|
|
||
| await expect(secret1).resolves.toBe(null); | ||
| await expect(secret2).resolves.toBe(null); | ||
| await expect(secret3).resolves.toBe(null); | ||
| await expect(secret4).resolves.toBe(null); | ||
| await expect(public1).resolves.toBe("OK"); | ||
| await expect(public2).resolves.toBe("OK"); | ||
| await expect(public3).resolves.toBe("OK"); | ||
| await expect(public4).resolves.toBe("OK"); | ||
| }, | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import { expect } from "@std/expect"; | ||
| import * as path from "@std/path"; | ||
| import { MemoryBuildCache } from "./dev_build_cache.ts"; | ||
| import { FreshFileTransformer } from "./file_transformer.ts"; | ||
| import { createFakeFs } from "../test_utils.ts"; | ||
| import type { ResolvedFreshConfig } from "../mod.ts"; | ||
|
|
||
| Deno.test({ | ||
| name: "MemoryBuildCache - should error if reading outside of staticDir", | ||
| fn: async () => { | ||
| const tmp = await Deno.makeTempDir(); | ||
| const config: ResolvedFreshConfig = { | ||
| root: tmp, | ||
| mode: "development", | ||
| basePath: "/", | ||
| staticDir: path.join(tmp, "static"), | ||
| build: { | ||
| outDir: path.join(tmp, "dist"), | ||
| }, | ||
| }; | ||
| const fileTransformer = new FreshFileTransformer(createFakeFs({})); | ||
| const buildCache = new MemoryBuildCache( | ||
| config, | ||
| "testing", | ||
| fileTransformer, | ||
| "latest", | ||
| ); | ||
|
|
||
| const thrown = buildCache.readFile("../SECRETS.txt"); | ||
| const thrown2 = buildCache.readFile("./../../SECRETS.txt"); | ||
| const noThrown = buildCache.readFile("styles.css"); | ||
| const noThrown2 = buildCache.readFile(".well-known/foo.txt"); | ||
| const noThrown3 = buildCache.readFile("./styles.css"); | ||
| const noThrown4 = buildCache.readFile("./.well-known/foo.txt"); | ||
| await buildCache.flush(); | ||
|
|
||
| const err = "Processed file resolved outside of static dir"; | ||
| await expect(thrown).rejects.toThrow(err); | ||
| await expect(thrown2).rejects.toThrow(err); | ||
| await expect(noThrown).resolves.toBe(null); | ||
| await expect(noThrown2).resolves.toBe(null); | ||
| await expect(noThrown3).resolves.toBe(null); | ||
| await expect(noThrown4).resolves.toBe(null); | ||
| }, | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,7 +10,7 @@ import { trace, tracer } from "../otel.ts"; | |
| * Fresh middleware to enable file-system based routing. | ||
| * ```ts | ||
| * // Enable Fresh static file serving | ||
| * app.use(freshStaticFles()); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oops, good catch! |
||
| * app.use(staticFiles()); | ||
| * ``` | ||
| */ | ||
| export function staticFiles<T>(): MiddlewareFn<T> { | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.