-
Notifications
You must be signed in to change notification settings - Fork 887
feat: disable directory listings for static files #12229
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
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
38357c3
feat: disable directory listings for static files
Emyrk 19032b9
make test parallel
Emyrk 8378673
add comment
Emyrk b723edf
return 200 for / on unit tests
Emyrk 7c124a1
swagger expectations
Emyrk f76b6bf
Swagger default to 404
Emyrk ea442be
better disabled message
Emyrk 80ed8b0
fix lint
Emyrk ccf0e2c
Linting
Emyrk 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
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 |
---|---|---|
|
@@ -102,7 +102,8 @@ func New(opts *Options) *Handler { | |
// Set ETag header to the SHA1 hash of the file contents. | ||
name := filePath(r.URL.Path) | ||
if name == "" || name == "/" { | ||
// Serve the directory listing. | ||
// Serve the directory listing. This intentionally allows directory listings to | ||
// be served. This file system should not contain anything sensitive. | ||
http.FileServer(opts.BinFS).ServeHTTP(rw, r) | ||
return | ||
} | ||
|
@@ -129,7 +130,15 @@ func New(opts *Options) *Handler { | |
// If-Match and If-None-Match headers on the request properly. | ||
http.FileServer(opts.BinFS).ServeHTTP(rw, r) | ||
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. Binary directory |
||
}))) | ||
mux.Handle("/", http.FileServer(http.FS(opts.SiteFS))) | ||
mux.Handle("/", http.FileServer( | ||
http.FS( | ||
// OnlyFiles is a wrapper around the file system that prevents directory | ||
// listings. Directory listings are not required for the site file system, so we | ||
// exclude it as a security measure. In practice, this file system comes from our | ||
// open source code base, but this is considered a best practice for serving | ||
// static files. | ||
OnlyFiles(opts.SiteFS))), | ||
) | ||
|
||
buildInfo := codersdk.BuildInfoResponse{ | ||
ExternalURL: buildinfo.ExternalURL(), | ||
|
@@ -873,3 +882,35 @@ func applicationNameOrDefault(cfg codersdk.AppearanceConfig) string { | |
} | ||
return "Coder" | ||
} | ||
|
||
// OnlyFiles returns a new fs.FS that only contains files. If a directory is | ||
// requested, os.ErrNotExist is returned. This prevents directory listings from | ||
// being served. | ||
func OnlyFiles(files fs.FS) fs.FS { | ||
return justFilesSystem{FS: files} | ||
} | ||
|
||
type justFilesSystem struct { | ||
FS fs.FS | ||
} | ||
|
||
func (jfs justFilesSystem) Open(name string) (fs.File, error) { | ||
f, err := jfs.FS.Open(name) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
stat, err := f.Stat() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Returning a 404 here does prevent the http.FileServer from serving | ||
// index.* files automatically. Coder handles this above as all index pages | ||
// are considered template files. So we never relied on this behavior. | ||
if stat.IsDir() { | ||
return nil, os.ErrNotExist | ||
} | ||
|
||
return f, nil | ||
} |
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 |
---|---|---|
|
@@ -4,12 +4,19 @@ | |
package site | ||
|
||
import ( | ||
"embed" | ||
"io/fs" | ||
"testing/fstest" | ||
"time" | ||
) | ||
|
||
var slim embed.FS | ||
|
||
func FS() fs.FS { | ||
return slim | ||
// This is required to contain an index.html file for unit tests. | ||
// Our unit tests frequently just hit `/` and expect to get a 200. | ||
// So a valid index.html file should be expected to be served. | ||
return fstest.MapFS{ | ||
"index.html": &fstest.MapFile{ | ||
Data: []byte("Slim build of Coder, does not contain the frontend static files."), | ||
ModTime: time.Now(), | ||
}, | ||
} | ||
} | ||
Comment on lines
12
to
22
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. @kylecarbs I had to add this so unit tests work. When they query |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It feels beneficial to keep this for listing what binaries are supported. Thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like it!