Skip to content

fix: Add RFC 5987 filename* encoding for special characters#600

Open
abhu85 wants to merge 3 commits into
form-data:masterfrom
abhu85:fix-filename-encoding
Open

fix: Add RFC 5987 filename* encoding for special characters#600
abhu85 wants to merge 3 commits into
form-data:masterfrom
abhu85:fix-filename-encoding

Conversation

@abhu85
Copy link
Copy Markdown

@abhu85 abhu85 commented Feb 23, 2026

Summary

Fixes #572 - Invalid Content-Disposition with FileName* containing parenthesis

This PR adds RFC 5987-compliant filename* encoding for filenames containing special characters. When a filename contains characters that are not in the RFC 5987 attr-char set (parentheses, spaces, quotes, non-ASCII, etc.), the _getContentDisposition method now outputs both:

  • filename="..." - with quotes/backslashes escaped for backwards compatibility
  • filename*=utf-8''... - with proper percent-encoding per RFC 5987

Changes

  • Added isAttrChar() helper to check if a character is safe per RFC 5987
  • Added encodeRfc5987() to percent-encode filenames using UTF-8
  • Added needsRfc5987Encoding() to detect when encoding is needed
  • Modified _getContentDisposition() to:
    • Escape quotes and backslashes in the filename parameter
    • Add filename* parameter when special characters are present

RFC References

Test Cases

Added comprehensive tests for:

  • Filenames with parentheses: test(1).txt
  • Filenames with spaces: file name.txt
  • Simple filenames (no encoding needed): simple.txt
  • Filenames with quotes: test"quotes".txt
  • Filenames with backslashes: test\slash.txt
  • Unicode filenames: 日本語.txt

All 29 existing tests continue to pass.

Per RFC 5987, characters like parentheses, spaces, quotes, and non-ASCII
characters should be percent-encoded in the `filename*` parameter.

This change:
- Adds `filename*=utf-8''...` with proper percent-encoding when the filename
  contains characters outside the RFC 5987 attr-char set
- Properly escapes double quotes and backslashes in the `filename="..."`
  parameter for backwards compatibility
- Maintains simple `filename="..."` format for filenames that don't need encoding

Fixes form-data#572

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@abhu85 abhu85 closed this Mar 3, 2026
@abhu85 abhu85 reopened this Mar 3, 2026
@abhu85 abhu85 closed this Mar 9, 2026
@abhu85 abhu85 reopened this Mar 9, 2026
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.

Invalid Content-Disposition with FileName* containing parenthesis

1 participant