DumbDrop icon indicating copy to clipboard operation
DumbDrop copied to clipboard

fix: repair failing test baseline (path validation, 404 vs 403, test isolation)

Open greirson opened this issue 5 months ago • 3 comments

Summary

On main, npm test was not green: 65 tests, 47 pass, 18 fail, plus latent parallel-run flakiness. Two of the failures turned out to be real production bugs, not test noise. This PR fixes the bugs, corrects the outdated test expectations, and makes the suite deterministic.

Result: npm test is 67/67 green and deterministic (verified across repeated parallel runs, with and without a local .env); npm run lint is clean.

Production bugs fixed

  1. Symlinked upload directory rejected valid uploads (src/utils/fileUtils.js) isPathWithinUploadDir resolved the upload dir with fs.realpathSync but resolved a non-existent candidate path with only path.resolve. When the upload dir is reached through a symlink (macOS /var -> /private/var, and notably Docker bind mounts), the two sides disagreed and path.relative produced a .. prefix, so valid in-bounds paths were wrongly rejected. Added realpathAllowingMissing, which resolves symlinks in the deepest existing ancestor and re-appends the missing remainder, falling back gracefully on ENOTDIR/EACCES/ELOOP instead of throwing.

  2. Missing in-bounds files returned 403 instead of 404 (src/routes/files.js) The info/download/delete/rename routes used the path check as an existence gate (requireExists=true) and mapped its false to 403 "Access denied" plus a misleading "path traversal attack" log. A simply-missing file therefore returned 403 (and a false security alert) instead of 404. Switched these to requireExists=false so missing in-bounds paths fall through to the fs call and return 404. Path traversal is still rejected with 403 (verified).

Test corrections

  • auth.test.js configured the PIN via process.env.PIN, but the app reads DUMBDROP_PIN, and it was set inside before() after config is loaded and frozen, so the PIN was never actually configured. Fixed, and corrected the empty-PIN expectation to 401 (the route's actual response for an invalid PIN).
  • Corrected outdated expectations for hyphen-to-underscore filename sanitization and the batch-ID format contract.

Flakiness fix

All suites shared the real ./local_uploads directory and ran in parallel, with some suites deleting the whole directory in teardown. Each suite now isolates its UPLOAD_DIR to a unique per-process temp dir (parallel-safe), which also stops tests from polluting the dev upload directory. A pre-clean in before() guards against pid reuse.

Verification

  • npm test: 67/67 green, deterministic across parallel runs (with and without .env).
  • npm run lint: clean (also fixed 2 pre-existing no-unused-vars errors in the touched file).
  • Confirmed in a running browser/app: upload/list/download/delete work; missing in-bounds files now return 404; path traversal still returns 403.

Not in scope (noted for follow-up)

  • The download route performs fs.access/createReadStream on the unresolved path (a pre-existing TOCTOU window, defense-in-depth only); left for a separate hardening pass.

High-level PR Summary

This PR fixes two production bugs and makes the test suite reliable and deterministic. The first bug caused symlinked upload directories (common in Docker bind mounts and macOS /var paths) to incorrectly reject valid uploads due to inconsistent symlink resolution. The second bug caused missing but in-bounds files to return 403 "Access denied" instead of the correct 404 status. Additionally, the test suite had incorrect expectations (PIN configuration, filename sanitization, batch ID format) and suffered from parallel-run flakiness due to shared upload directories. All test suites now use isolated per-process temp directories, and the suite is now 67/67 green and deterministic.

⏱️ Estimated Review Time: 30-90 minutes

💡 Review Order Suggestion
Order File Path
1 test/auth.test.js
2 test/files.test.js
3 test/security.test.js
4 test/upload.test.js
5 src/utils/fileUtils.js
6 src/routes/files.js
7 test/path-validation.test.js

Need help? Join our Discord

Summary by CodeRabbit

  • Bug Fixes
    • Improved handling of missing files in upload directory—requests for non-existent files now return appropriate "not found" responses instead of access errors.
    • Enhanced symlink resolution for better compatibility across different systems.
    • Filenames with hyphens are now automatically converted to underscores for consistency.

greirson avatar Jun 13 '26 03:06 greirson

The preview deployment for DumbWareio/DumbDrop-main is ready. 🟢

Open Preview | Open Build Logs | Open Application Logs

Last updated at: 2026-06-13 03:56:21 CET

control-dw[bot] avatar Jun 13 '26 03:06 control-dw[bot]

The preview deployment for DumbWareio/DumbDrop-demo is ready. 🟢

Open Preview | Open Build Logs | Open Application Logs

Last updated at: 2026-06-13 03:56:22 CET

control-dw[bot] avatar Jun 13 '26 03:06 control-dw[bot]

Review Change Stack

Walkthrough

The PR refactors path traversal defense by introducing symlink-aware resolution for non-existent targets, updating the traversal guard to validate only path inclusion (not existence), and switching four route endpoints to this "guard-only" mode. Test suites are refactored to use isolated temp directories for parallel safety with updated assertions.

Changes

Path Traversal and Route Behavior Refactoring

Layer / File(s) Summary
Symlink-aware path resolution for missing targets
src/utils/fileUtils.js
New realpathAllowingMissing(targetPath) helper resolves the deepest existing ancestor via fs.realpathSync and re-appends unresolved path segments, enabling consistent symlink interpretation when the full path does not exist (e.g., macOS /var → /private/var, Docker bind-mount symlinks).
Updated isPathWithinUploadDir for traversal-only mode
src/utils/fileUtils.js
isPathWithinUploadDir now uses realpathAllowingMissing when requireExists=false, replacing lexical path.resolve normalization to ensure symlink-consistent comparison with the realpath'd upload directory.
Four endpoints switch to traversal-only guard mode
src/routes/files.js
GET /info/*, GET /download/*, DELETE /*, and PUT /rename/* now call isPathWithinUploadDir(..., false), making the traversal check guard-only; missing-file handling defers to downstream fs.stat/fs.access calls instead of the traversal guard.
Test suite infrastructure: isolated temp directories
test/auth.test.js, test/files.test.js, test/security.test.js, test/upload.test.js
All five test suites set process.env.UPLOAD_DIR to unique pid-scoped temp directories before app import for parallel safety; before hooks delete stale contents, after hooks perform single recursive cleanup instead of manual file iteration.
Test assertion updates for new behavior
test/auth.test.js, test/files.test.js, test/path-validation.test.js, test/security.test.js, test/upload.test.js
Auth empty-PIN expects HTTP 401; files rename test verifies filename sanitization and asserts missing in-bounds source returns 404 not 403; path-validation adds ENOTDIR edge case coverage; security/upload tests correct filenames and batch-ID format to match sanitizer behavior.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

  • DumbWareio/DumbDrop#73: Both PRs modify isPathWithinUploadDir(filePath, uploadDir, requireExists) semantics for non-existent targets and update route endpoint validation/tests accordingly.
  • DumbWareio/DumbDrop#70: Both PRs adjust path-traversal protection in src/routes/files.js via isPathWithinUploadDir behavior changes.

Poem

🛡️ Symlinks danced, paths once missed,
Now realpathAllowingMissing turns mist to grist.
Four guards stand watch, then step aside—
Let filesystems finish what traversal denied.
Tests run safe in temp-dirs clean,
The safest dance that ever was seen! 🎭

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately and specifically summarizes the main fixes: path validation bugs, 404 vs 403 corrections, and test isolation improvements.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • [ ] Create stacked PR
  • [ ] Commit on current branch
🧪 Generate unit tests (beta)
  • [ ] Create PR with unit tests
  • [ ] Commit unit tests in branch fix/test-baseline

[!WARNING] There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

ESLint install failed due to a network error.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

coderabbitai[bot] avatar Jun 13 '26 03:06 coderabbitai[bot]