Skip to content
This repository was archived by the owner on Feb 27, 2026. It is now read-only.

fix(server): Remove RFC 2616 separator chars from Content-Disposition filename#291

Merged
danielalves96 merged 2 commits intonextfrom
copilot/fix-s3-file-download-error
Oct 20, 2025
Merged

fix(server): Remove RFC 2616 separator chars from Content-Disposition filename#291
danielalves96 merged 2 commits intonextfrom
copilot/fix-s3-file-download-error

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Oct 9, 2025

Problem

File downloads and previews were failing on S3-compatible storage providers (particularly Backblaze B2) when filenames contained special characters like parentheses, brackets, or commas.

For example, a file named privacypack (2).png would result in this error from Backblaze B2:

<Error>
<Code>InvalidRequest</Code>
<Message>invalid b2-content-disposition: expected a token character at 75, but got '('</Message>
</Error>

Root Cause

The encodeFilenameForHeader function in both s3-storage.provider.ts and filesystem/controller.ts was generating Content-Disposition headers that violated RFC 2616 HTTP token specifications.

The function was creating ASCII-safe filenames by filtering characters to the range 32-126, which includes many characters that are not valid in HTTP tokens according to RFC 2616:

  • Parentheses: ( )
  • Brackets: [ ]
  • Commas: ,
  • Semicolons: ;
  • At signs: @
  • And other separator characters

According to RFC 2616, tokens can only contain:

  • Alphanumeric characters: A-Z, a-z, 0-9
  • Special characters: !, #, $, %, &, ', *, +, -, ., ^, _, `, |, ~

Solution

Added a new isTokenChar() helper method that properly validates characters according to RFC 2616 token rules. This method filters out all separator characters (()<>@,;:\"/[]?={} \t) from the ASCII-safe portion of the Content-Disposition header.

The fix ensures:

  • The ASCII-safe filename parameter only contains valid RFC 2616 token characters
  • The full filename with special characters is preserved in the filename*=UTF-8''... parameter
  • Compatibility with all S3-compatible providers (AWS S3, Backblaze B2, MinIO, DigitalOcean Spaces, etc.)

Example

Before:

attachment; filename="privacypack (2).png"; filename*=UTF-8''privacypack%20(2).png

❌ Rejected by S3 providers because ( and ) are not valid token characters

After:

attachment; filename="privacypack2.png"; filename*=UTF-8''privacypack%20(2).png

✅ Accepted by all S3 providers

  • ASCII-safe portion has separators removed: privacypack2.png
  • UTF-8 encoded portion preserves original: privacypack%20(2).png

Testing

Verified with multiple test cases:

  • privacypack (2).pngprivacypack2.png
  • document[1].pdfdocument1.pdf
  • file@email.com.txtfileemail.com.txt
  • file,with,commas.txtfilewithcommas.txt
  • my-file.zipmy-file.zip (no change needed)

Impact

  • ✅ Fixes S3 download/preview errors for files with special characters
  • ✅ Maintains compatibility with all S3-compatible providers
  • ✅ Preserves original filenames in the UTF-8 encoded portion
  • ✅ No breaking changes to existing functionality
  • ✅ Applies to both S3 storage and filesystem storage modes

Fixes #288

Original prompt

This section details on the original issue you should resolve

<issue_title>[S3] File download and preview fails if filenames contain special chars</issue_title>
<issue_description>filename: privacypack (2).png

Error from S3 (Backblaze B2):

<Error>
<Code>InvalidRequest</Code>
<Message>invalid b2-content-disposition: expected a token character at 75, but got '('</Message>
</Error>
```</issue_description>

## Comments on the Issue (you are @copilot in this section)

<comments>
</comments>

Fixes #262


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

… filename

Filter out HTTP token separator characters (including parentheses, brackets, commas, etc.) from the ASCII-safe portion of the Content-Disposition header to prevent S3 providers like Backblaze B2 from rejecting the header. The full filename with special characters is still preserved in the filename*=UTF-8 parameter.

Co-authored-by: danielalves96 <62755605+danielalves96@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix file download and preview issues for special characters fix(server): Remove RFC 2616 separator chars from Content-Disposition filename Oct 9, 2025
Copilot AI requested a review from danielalves96 October 9, 2025 17:12
@danielalves96 danielalves96 marked this pull request as ready for review October 20, 2025 13:49
@danielalves96 danielalves96 merged commit ab5ea15 into next Oct 20, 2025
@danielalves96 danielalves96 deleted the copilot/fix-s3-file-download-error branch October 21, 2025 17:11
anthony0030 pushed a commit to anthony0030/Palmr that referenced this pull request Jan 13, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants