Repository navigation
fix(server): stop serving siblings that share the cwd prefix - #2104
Merged
johanneswuerbach merged 1 commit intoOct 26, 2026
Merged
johanneswuerbach merged 1 commit into
johanneswuerbach merged 1 commit into
Conversation
With `unsafe_file_serving` off, testem's dev server should only serve files inside `cwd` (or behind a configured route). serveStaticFile() in lib/server/index.js checked that with `filePath.indexOf(path.resolve(config.cwd())) !== -1`, a substring test. Any resolved path that merely contains the cwd string passed it. With `cwd` set to `/tmp/app`, a request for `/%2E%2E%2Fapp-secret%2Fsecret.txt` resolves to `/tmp/app-secret/secret.txt` and was served with a 200. Check containment with path.relative() instead. The result climbs out of cwd when it is `..` or starts with `..` plus the separator, and stays absolute when the target is on another Windows drive. Files inside cwd, including names that begin with two dots such as `..foo.js`, routed paths and `unsafe_file_serving: true` behave as before. Fixes #2016 Co-Authored-By: Claude Opus 5.5 <[email protected]>
This was referenced Oct 26, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #2016.
With
unsafe_file_servingoff, the dev server should only serve files insidecwdor behind a configured route.serveStaticFile()checked that with a substring test:Any resolved path that merely contains the cwd string passes. With
cwdset to/tmp/app, a request for/%2E%2E%2Fapp-secret%2Fsecret.txtresolves to/tmp/app-secret/secret.txtand is served with a 200.This PR checks containment with
path.relative()instead, which is the check suggested in the issue. A request is rejected when the relative path is.., starts with..plus the separator, or is absolute (another drive on Windows). These behave as before: files insidecwd(including names that start with two dots, such as..foo.js),cwditself, routed paths andunsafe_file_serving: true. Symlinks are still not resolved, as before.Tests
Adds a describe block next to
unsafe directory handlingintests/server_tests.js. It createsapp/andapp-secret/in a temp directory and starts the server withcwdset toapp. It then checks thathello.jsis served (200) and that%2E%2E%2Fapp-secret%2Fsecret.txtis rejected (403). The encoded request uses the same technique as the existingrejects a request for unsafe contenttest.I could not run the mocha suite or eslint because npm dependencies could not be installed in my environment. Instead, I ran the real
Server#serveStaticFileandConfigon Node 22, with stand-ins for express, socket.io, compression, js-yaml and execa. A smallnode:httpadapter passedserveStaticFilethe same decoded path that the/*fileroute produces, and I sent the requests with Node'sfetch:hello.js(cwd =<tmp>/app)%2E%2E%2Fapp-secret%2Fsecret.txt(cwd =<tmp>/app)secret%2E%2E%2Fpublic%2F.eslintrc.js(cwd =tests)I also called
serveStaticFiledirectly. These cases are still served:..foo.jsinside cwd, cwd itself, a relative or trailing-slashcwd,/ascwd, routed paths andunsafe_file_serving: true. An absolute path elsewhere that contains the cwd string now gets 403 (it was served before). I also evaluated the new check withpath.win32for a forward-slashcwd(#1286), another drive and a drive root.🤖 Generated with Claude Code