Repository navigation
Conversation
…rrors Assisted-by: Claude Code:claude-opus-5-5
Not up to standards ⛔🔴 Issues
|
| Category | Results |
|---|---|
| Security | 1 high (1 false positive) |
🟢 Metrics 42 complexity
Metric Results Complexity 42
NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.
pbleser-oc
left a comment
There was a problem hiding this comment.
Thanks Sara, getting rid of the recursion and the panics is a good change, and the tests are thorough, especially the one on disk with the .. paths.
My main point is about the errors: they all turn into a 404 now, without a log entry, and a read error even keeps the cache headers. Details inline.
The commit message is pretty terse: "handle file errors" alone doesn't tell me much. When someone looks up this change later, it would help to have in the commit body what was wrong before (the fallback calling the whole handler again until it ran out of file descriptors, the nil pointer panic after a failed Stat, the ignored read and parse errors) and how the change fixes it. The PR description already has most of it, so it could go into the commit message more or less as it is.
| // therefore, we need to check if the file exists and stop the recursion if it doesn't | ||
| file, err := f.fsys.Open(r.URL.Path) | ||
| if err != nil { | ||
| file, info, ok := f.open(path.Clean(path.Join("/", r.URL.Path))) |
There was a problem hiding this comment.
Maybe put the cleaned path into a variable of its own, so that the log messages below can use it.
There was a problem hiding this comment.
Done: name holds the cleaned path (server.go:34), and every log entry uses it as path. The error entries also name the file that failed as filename, which is /index.html for a web app route.
| // not every fs contains an index.html: without one, the path is not found. | ||
| file, info, ok = f.open("/index.html") | ||
| if !ok { | ||
| http.NotFound(w, r) |
There was a problem hiding this comment.
A debug log message here would help to understand what happened: which path couldn't be opened, and that /index.html couldn't be opened either.
There was a problem hiding this comment.
Done: when neither the path nor /index.html is a file, there's a debug entry with the requested path and the request id (server.go:43-45).
| if err := withBase(buf, file, "/"); err != nil { | ||
| http.NotFound(w, r) | ||
| return |
There was a problem hiding this comment.
Read and parse errors only turn into a 404 here, without a log entry, so an admin can't tell a broken disk or wrong file permissions from a missing file. fileServer has no logger yet, but Web.Static has p.logger, which could be passed on to FileServer. The same goes for the read error below.
There was a problem hiding this comment.
Done: FileServer takes the logger as its first argument, and Web.Static passes p.logger (service.go:214). Read and parse errors are logged at error level with the request id, path and filename.
| if _, err := buf.ReadFrom(file); err != nil { | ||
| http.NotFound(w, r) | ||
| return |
There was a problem hiding this comment.
A read error answers with a 404 that keeps the cache headers Static set before (Cache-Control: max-age=<ttl> and Expires), and the test "read error on an asset" even expects max-age=60. A passing I/O error (e.g. on a network file system) would then be cached by browsers and proxies for the whole TTL, as if the file didn't exist.
A read error is a problem on the server side, not a missing file: how about a 500, with the caching taken away (w.Header().Del("Expires") and Cache-Control: no-store)? The same for an error of withBase above.
There was a problem hiding this comment.
Done as you suggested: read and parse errors answer with a 500, Expires removed and Cache-Control: no-store (serverError, server.go:112-121). The body is the generic status text; the details are only in the log. The tests set max-age and Expires like Static does and check that they're gone.
| // open returns the file with the given name if it exists and isn't a directory. | ||
| func (f *fileServer) open(name string) (http.File, fs.FileInfo, bool) { | ||
| file, err := f.fsys.Open(name) | ||
| if err != nil { |
There was a problem hiding this comment.
This treats every error as "not found", but Open can also fail for other reasons, e.g. missing permissions or an I/O error, and http.FS doesn't log anything on its own. Could we tell them apart, e.g. with errors.Is(err, fs.ErrNotExist): a missing file falls back to the index as now, while any other error is logged as an error?
There was a problem hiding this comment.
Done, with two additions:
- A path that can't be a file falls back to the index without a log entry. Besides
fs.ErrNotExist, that includes a name that is too long (ENAMETOOLONG) or contains a NUL byte (EINVAL,fs.ErrInvalid): the OS rejects a path segment of more than 255 bytes before it looks for the file, so a web app link to a folder with a long name (about 85 CJK characters are enough) would otherwise get a 500. - Any other
Openerror is logged as an error and, going beyond your suggestion, answered with a 500 (no-store) instead of the index. Otherwise a.jsfile the server can't read would still be answered withindex.html, which the browser then tries to run as JavaScript.
TestFileServerOnDisk covers the long name and the NUL byte on a real disk, TestFileServerOnDiskUnreadable a file without read permission.
|
|
||
| info, err := file.Stat() | ||
| if err != nil || info.IsDir() { | ||
| _ = file.Close() |
There was a problem hiding this comment.
Same here: a Stat error is never logged. It's fine to fall back to the index for a directory, but a failed Stat should at least leave a log entry.
There was a problem hiding this comment.
Done: a failed Stat is logged as an error and, like the other server-side errors, answered with a 500 (no-store). A directory still falls back to the index, without a log entry.
…s with 500 Assisted-by: Claude Code:claude-opus-5-5
|
Thanks for the review! Everything is addressed in ac365de, see the replies inline. I've also updated the PR description. As agreed in chat, the commit messages stay short and the details are in the description. |
|
Codacy reports |
pbleser-oc
left a comment
There was a problem hiding this comment.
Thanks Sara, that addresses everything, and both additions make sense: no 500 for names that can't be files, and a 500 instead of index.html for an unreadable .js file. I also tried paths below an existing file (/index.html/x, /js/app.js/x) on disk, and they fall back to the index as they should.
One nitpick inline, optional.
The CI didn't run for ac365de (there is no pipeline for it at all, probably a hiccup of the CI at that time). Could you push again, e.g. an empty commit, so that it runs before the merge?
| return errors.Is(err, fs.ErrNotExist) || | ||
| errors.Is(err, fs.ErrInvalid) || | ||
| errors.Is(err, syscall.ENAMETOOLONG) || | ||
| errors.Is(err, syscall.EINVAL) // e.g. a NUL byte in the name |
There was a problem hiding this comment.
Nitpick: EINVAL covers more than a NUL byte. An open can also return it for other reasons (e.g. on some network or FUSE filesystems), and those would now fall back to the index without a log entry. Probably rare enough to accept; the comment could say so, or the NUL byte could be checked on the name itself (strings.ContainsRune(name, 0)) before opening.
Description
The web service serves its assets (
core,apps,themes) through its own file server. A path that is no file, e.g. a route of the web app such as/files/spaces/..., getsindex.html. This change fixes how that fallback and file errors are handled:/index.html. Ifindex.htmlwas a directory, it called itself until the process ran out of file descriptors, and only then answered 404. It now opensindex.htmlonce.index.html, as before. That is a path that doesn't exist, a directory, and a name the filesystem can't have: one that is too long (a path segment of more than 255 bytes) or contains a NUL byte. If there is noindex.htmleither, the answer is a 404, with a debug log entry.Stat, and an error when parsingindex.htmlanswered withindex.html, with a cut-off body and status 200, with a 404, or ended in a nil pointer panic. They now answer with a 500 without caching (Cache-Control: no-store, noExpires), so a passing error doesn't stick in browsers and proxies, and with a generic error text.path) and the file that failed (filename).FileServergets the logger of the web service for this.Nothing changes for files that can be read: the same content,
Content-Typeand cache headers as before, and web app routes, directories and..paths still getindex.html.Related Issue
Motivation and Context
A configuration mistake in an asset directory (a directory named
index.html) could make every request for an unknown path exhaust the file descriptors of the process. A file the server can't read was answered withindex.html(e.g. instead of JavaScript) or a cached 404, and a failing disk could crash the handler or let browsers and proxies cache cut-off JavaScript, all without a log entry that would let an admin find the cause.How Has This Been Tested?
services/web/pkg/assets/server_test.go:TestFileServerIndexIsDirectory:index.htmlis a directory. Without the fix, the handler opens files until the test filesystem refuses (102 opens); with it, 404 after at most 3 opens.TestFileServerFaultyFiles:Open,Statand read errors on an asset, onindex.htmland on the index fallback, plus names that are too long or invalid. Checks the status, the body, the cache headers (set likeStaticdoes before), and the log entries with their level,path,filenameand request id.TestFileServerOnDisk: a directory on disk, layered like in the web service: a regular file, a web app route, a directory,/../and/%2e%2e/, a path without a leading slash, a folder name of 300 bytes, and a NUL byte.TestFileServerOnDiskUnreadable: a file without read permission on disk gets a 500, notindex.html(skipped when run as root).go test -race ./services/web/...passes.go vetreports nothing new compared withmain.gofmtandgit diff --checkare clean. Builds for Linux, macOS and Windows.golangci-lintand the acceptance tests. No acceptance test covers the asset file server.Types of changes
Checklist:
AI assistance: Claude Code (claude-opus-5-5) wrote the first draft of the code, the tests and this description; I reviewed and adjusted them.