Skip to content

fix(web): serve the index fallback without recursion and handle file errors - #4125

Open
asaah262 wants to merge 2 commits into
opencloud-eu:mainfrom
asaah262:fix/868-web-asset-index-fallback
Open

asaah262 wants to merge 2 commits into
opencloud-eu:mainfrom
asaah262:fix/868-web-asset-index-fallback

Conversation

@asaah262

@asaah262 asaah262 commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

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/..., gets index.html. This change fixes how that fallback and file errors are handled:

  • No recursion in the index fallback. The fallback called the whole handler again for /index.html. If index.html was a directory, it called itself until the process ran out of file descriptors, and only then answered 404. It now opens index.html once.
  • Paths that can't be a file get 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 no index.html either, the answer is a 404, with a debug log entry.
  • Server-side errors are now a 500. An error when opening or reading a file that the server should be able to serve (e.g. missing permissions, an I/O error), a failed Stat, and an error when parsing index.html answered with index.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, no Expires), so a passing error doesn't stick in browsers and proxies, and with a generic error text.
  • Errors are logged. Each server-side error is logged at error level with the request id, the requested path (path) and the file that failed (filename). FileServer gets the logger of the web service for this.

Nothing changes for files that can be read: the same content, Content-Type and cache headers as before, and web app routes, directories and .. paths still get index.html.

Related Issue

  • None

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 with index.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?

  • Test environment: macOS, Go tests.
  • Go tests in services/web/pkg/assets/server_test.go:
    • TestFileServerIndexIsDirectory: index.html is 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, Stat and read errors on an asset, on index.html and on the index fallback, plus names that are too long or invalid. Checks the status, the body, the cache headers (set like Static does before), and the log entries with their level, path, filename and 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, not index.html (skipped when run as root).
  • go test -race ./services/web/... passes. go vet reports nothing new compared with main. gofmt and git diff --check are clean. Builds for Linux, macOS and Windows.
  • Not run locally: golangci-lint and the acceptance tests. No acceptance test covers the asset file server.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Technical debt
  • Tests only (no source changes)

Checklist:

  • Code changes
  • Unit tests added
  • Acceptance tests added
  • Documentation added

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.

…rrors

Assisted-by: Claude Code:claude-opus-5-5
@codacy-production

codacy-production Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Not up to standards ⛔

🔴 Issues 1 high

Alerts:
⚠ 1 issue (≤ 0 issues of at least minor severity)

Results:
1 new issue

Category Results
Security 1 high (1 false positive)

View in Codacy

🟢 Metrics 42 complexity

Metric Results
Complexity 42

View in Codacy

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 pbleser-oc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread services/web/pkg/assets/server.go Outdated
// 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)))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe put the cleaned path into a variable of its own, so that the log messages below can use it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Comment on lines +43 to +45
if err := withBase(buf, file, "/"); err != nil {
http.NotFound(w, r)
return

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +48 to +50
if _, err := buf.ReadFrom(file); err != nil {
http.NotFound(w, r)
return

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread services/web/pkg/assets/server.go Outdated
// 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 {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 Open error is logged as an error and, going beyond your suggestion, answered with a 500 (no-store) instead of the index. Otherwise a .js file the server can't read would still be answered with index.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()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
@asaah262

Copy link
Copy Markdown
Contributor Author

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.

@asaah262
asaah262 requested a review from pbleser-oc November 25, 2026 17:32
@asaah262

Copy link
Copy Markdown
Contributor Author

Codacy reports filepath-clean-misuse on server.go:34 (path.Clean(path.Join("/", r.URL.Path))). It's a false positive: the path is joined with / before path.Clean, so every .. is resolved against the root and the path can't leave the asset directory. TestFileServerOnDisk covers this with /../outside.txt and /%2e%2e/outside.txt. The expression itself is unchanged from main; it's only reported now because the cleaned path goes into a variable of its own for the log messages. Codacy doesn't block the merge, so I'm leaving it as it is.

@pbleser-oc pbleser-oc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants