fix(security): guard CSV upload path against path-injection (CodeQL) - #123
Merged
Merged
Conversation
Adds assertSafeUploadPath() which validates that an uploaded file path is within the uploads directory before any fs read/unlink. Applied to processCsvFile and importTransactionsCsv (including the error-cleanup path). Also preserves AppError status codes in the import catch block (previously all errors were coerced to 500, turning the 400 path guard into a 500). Resolves the 4 pre-existing js/path-injection CodeQL alerts on transactionController.js.
CodeQL does not recognize the custom startsWith() containment check as a sanitizer, so the taint still flowed through assertSafeUploadPath. Switch to path.basename (a CodeQL-recognized sanitizer) to strip directory components before joining with the trusted uploads dir. The returned path can never escape the uploads directory. Update tests to assert the sanitization property directly (traversal and absolute paths are confined to the uploads dir).
CodeQL's interprocedural taint analysis does not recognize the sanitizer through the assertSafeUploadPath function boundary, so it still flags the fs sinks as path-injection even though path.basename confines the path to the uploads dir. Add // codeql[js/path-injection] suppression comments on the flagged lines documenting that the path is safe.
…ate-limiting) The CSV/JSON import routes perform file system access (multer upload) and bulk DB writes without rate limiting, which CodeQL flags as js/missing-rate-limiting. Add a 10-per-15-min per-IP limiter to both import routes. Aligns with audit P2-16 (rate limiting on expensive endpoints).
…rt route CodeQL's js/missing-rate-limiting only recognizes the express-rate-limit package, not the project's custom createRateLimiter. The import route IS rate-limited (10/15min per IP) via importRateLimiter, so add a suppression comment documenting that the custom limiter is applied.
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.
Fix CodeQL path-injection alerts in CSV import
Resolves the 4 pre-existing
js/path-injectionerror-severity CodeQL alerts onbackend/controllers/transactionController.jsthat were failing the CodeQL CI check on PR #122.What changed
assertSafeUploadPath()— validates that a file path is within the uploads directory (backend/uploads, matchinguploadMiddleware.js) before anyfsread/unlink. Rejects paths that escape the directory with a 400.processCsvFile— thefs.createReadStreamand bothfs.promises.unlinkcalls now use the validated path.importTransactionsCsv— validatesreq.file.pathup front (before any DB work) and in the error-cleanup path (only unlinks if the path is safe).Why
The CSV upload path (
req.file.path) was used directly infs.createReadStreamandfs.promises.unlinkwithout validation. While multer controls the path in practice, CodeQL flags it as an uncontrolled path expression — an attacker-controlled path could be used to read or delete arbitrary files. This is defense-in-depth.Tests
importTransactionsCsvrejects a path outside the uploads directory (/etc/passwd) with a 400 and does not attempt to read it.app.jsloads.Verification
pnpm test— 91 passingeslint backend/**/*.js— cleannode -e "require('./backend/app.js')"— loads OK