Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix: re-encode Next.js decoded slug segments in REST route handler#15799

Closed
andershermansen wants to merge 2 commits intopayloadcms:mainfrom
andershermansen:issue-15798-filename-with-hash
Closed

fix: re-encode Next.js decoded slug segments in REST route handler#15799
andershermansen wants to merge 2 commits intopayloadcms:mainfrom
andershermansen:issue-15798-filename-with-hash

Conversation

@andershermansen
Copy link
Contributor

What?

Fixes file downloads returning 404 for filenames containing # when served through the REST API.

Why?

Next.js catch-all [...slug] params decodes URL-encoded segments before passing them to route handlers. So a request for /api/media/file/document%20%23123.pdf arrives with slug segments ['media', 'file', 'document #123.pdf'].

These decoded segments are joined and passed to path-to-regexp for endpoint matching. The :filename pattern uses [^\/#\?]+? which excludes # and ? — so the match fails and returns 404.

How?

Re-encode each slug segment with encodeURIComponent before joining:

handleEndpoints already uses match(endpoint.path, { decode: decodeURIComponent }), so params are decoded back to their original values after matching

Also adds a test that creates a file with # in the filename and verifies it can be served via REST.

Fixes #15798

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes REST file downloads returning 404 when filenames contain # by ensuring Next.js-decoded catch-all slug segments are re-encoded before route matching.

Changes:

  • Re-encode each [...slug] segment with encodeURIComponent before joining into the REST handler path
  • Add an integration test covering uploads/downloads where the stored filename contains #

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
test/uploads/int.spec.ts Adds an integration test asserting files with # in the filename can be fetched via the REST file endpoint
packages/next/src/routes/rest/index.ts Re-encodes decoded slug segments so path-to-regexp endpoint matching works with #/? in filenames

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@andershermansen
Copy link
Contributor Author

Superseeded by #15823, I think that is a better way.

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.

REST API returns 404 for filenames containing # (hash character)

2 participants