Skip to content

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

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

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

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)

1 participant