Skip to content

fix: security - #355

Merged
RambokDev merged 1 commit into
mainfrom
fix/security
Jul 4, 2026
Merged

fix: security#355
RambokDev merged 1 commit into
mainfrom
fix/security

Conversation

@RambokDev

@RambokDev RambokDev commented Jul 4, 2026

Copy link
Copy Markdown
Collaborator

Summary by CodeRabbit

  • Bug Fixes
    • Tightened upload finalization checks to reject invalid or unsafe file paths.
    • Added safeguards for backup-storage-backed uploads, returning an error when required identifiers or matching records are missing.
    • Improved handling of path traversal and out-of-scope file destinations during upload processing.
    • Standardized error responses for failed upload hook requests.
    • Blocked direct access to the upload hook endpoint at the web server level, returning a not found response.

@coderabbitai

coderabbitai Bot commented Jul 4, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The TUS hooks route now validates the X-File-Path header against a regex allowlist, enforces backup storage authorization via database lookups, checks for path traversal before file operations, and improves error handling. Nginx is updated to block all external requests to the hooks endpoint with a 404.

Changes

TUS Hook Security Hardening

Layer / File(s) Summary
File path and backup storage validation
app/api/tus/hooks/route.ts
Adds FILE_PATH_RE regex and Drizzle imports; validates X-File-Path, requires X-Generated-Id when X-Backup-Storage-Id is present, queries the database to verify ownership (403 on mismatch), logs a warning for legacy requests, and rejects path traversal via path.resolve/path.relative checks before renaming files.
Error handling and network-level lockdown
app/api/tus/hooks/route.ts, docker/nginx/nginx.conf
Standardizes the catch block to log and return a 500 response; adds an Nginx location = /api/tus/hooks block that denies all direct traffic and returns 404.

Estimated code review effort: 4 (Complex) | ~40 minutes

Sequence Diagram(s)

sequenceDiagram
  participant TusServer
  participant HooksRoute
  participant Database
  participant Filesystem

  TusServer->>HooksRoute: POST success event (X-File-Path, X-Backup-Storage-Id)
  HooksRoute->>HooksRoute: validate X-File-Path against FILE_PATH_RE
  alt X-Backup-Storage-Id present
    HooksRoute->>Database: verify db + backup storage linkage
    Database-->>HooksRoute: matching records or none
    HooksRoute->>HooksRoute: return 403 if mismatch/missing
  else missing header
    HooksRoute->>HooksRoute: log legacy agent warning
  end
  HooksRoute->>HooksRoute: resolve uploadDir, detect path traversal
  HooksRoute->>Filesystem: rename temp file, clean .info/metadata
  HooksRoute-->>TusServer: 200 / 400 / 403 / 500 response
Loading

Poem

A rabbit checks each path with care,
No sneaky traversal slipping there! 🐇
Backup storage proved and true,
Nginx slams the door on you 🚪
Hop along, the hooks are safe and sound!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 inconclusive)

Check name Status Explanation Resolution
Title check ❓ Inconclusive The title is related to the security-focused changes, but it is too generic to convey the main update. Use a more specific title such as "Harden TUS upload hook validation and block direct hook access".
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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.
✨ 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/security

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.

app/api/tus/hooks/route.ts

Oops! Something went wrong! :(

ESLint: 9.39.4

TypeError: Converting circular structure to JSON
--> starting at object with constructor 'Object'
| property 'configs' -> object with constructor 'Object'
| property 'flat' -> object with constructor 'Object'
| ...
| property 'plugins' -> object with constructor 'Object'
--- property 'react' closes the circle
Referenced from: /.eslintrc.json
at JSON.stringify ()
at /node_modules/.pnpm/@eslint+eslintrc@3.3.5/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:2255:45
at Array.map ()
at ConfigValidator.formatErrors (/node_modules/.pnpm/@eslint+eslintrc@3.3.5/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:2246:23)
at ConfigValidator.validateConfigSchema (/node_modules/.pnpm/@eslint+eslintrc@3.3.5/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:2277:84)
at ConfigArrayFactory._normalizeConfigData (/node_modules/.pnpm/@eslint+eslintrc@3.3.5/node_modules/@eslint/

... [truncated 446 characters] ...

c/dist/eslintrc.cjs:3261:25)
at ConfigArrayFactory._normalizeObjectConfigDataBody (/node_modules/.pnpm/@eslint+eslintrc@3.3.5/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:3199:25)
(node:2) ESLintRCWarning: You are using an eslintrc configuration file, which is deprecated and support will be removed in v10.0.0. Please migrate to an eslint.config.js file. See https://eslint.org/docs/latest/use/configure/migration-guide for details. An eslintrc configuration file is used because you have the ESLINT_USE_FLAT_CONFIG environment variable set to false. If you want to use an eslint.config.js file, remove the environment variable. If you want to find the location of the eslintrc configuration file, use the --debug flag.
(Use node --trace-warnings ... to show where the warning was created)


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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@app/api/tus/hooks/route.ts`:
- Around line 43-44: Remove the leftover raw console.log debug statements in the
tus hook route and use the existing structured logger instead. Locate the
backup/generated ID prints in the route handler around the backupStorageId and
generatedId handling, and either delete them entirely or convert them to
log.debug on the existing log child logger so they are not emitted
unconditionally to stdout.
- Around line 61-71: The TUS hook gate in route.ts needs two fixes: the
backupStorage lookup currently only matches by id/database, so it should also
require the row to be in pending status before allowing the request. Also
validate the X-Backup-Storage-Id and X-Generated-Id values before calling
db.query.backupStorage.findFirst, so malformed UUIDs are rejected cleanly
instead of surfacing as 500s. Use the existing variables backupStorageId,
generatedId, and the TUS hook handler logic to place the validation and status
check together before the database query.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 60bc51c7-bf07-45b0-ac50-e10d39f00deb

📥 Commits

Reviewing files that changed from the base of the PR and between a3d0a6e and 237f9b8.

📒 Files selected for processing (2)
  • app/api/tus/hooks/route.ts
  • docker/nginx/nginx.conf
📜 Review details
⏰ Context from checks skipped due to timeout. (1)
  • GitHub Check: build-image
🔇 Additional comments (5)
app/api/tus/hooks/route.ts (4)

4-13: LGTM!


34-37: LGTM!


76-86: LGTM!


126-129: LGTM!

docker/nginx/nginx.conf (1)

33-36: 🔒 Security & Privacy

deny all is redundant here return 404 already blocks the request; the exact-match location is sufficient for the canonical Next.js route path.

			> Likely an incorrect or invalid review comment.

Comment thread app/api/tus/hooks/route.ts
Comment thread app/api/tus/hooks/route.ts
@RambokDev
RambokDev merged commit 8dcb406 into main Jul 4, 2026
6 checks passed
@RambokDev
RambokDev deleted the fix/security branch July 4, 2026 10:24
@coderabbitai coderabbitai Bot mentioned this pull request Jul 21, 2026
Merged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant