Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: only set extend notation on non-ascii filename #558

Merged
merged 2 commits into from
Dec 5, 2024
Merged

Conversation

fengmk2
Copy link
Member

@fengmk2 fengmk2 commented Dec 5, 2024

Summary by CodeRabbit

  • New Features

    • Enhanced filename handling to include UTF-8 encoding for non-ASCII filenames in content disposition.
  • Bug Fixes

    • Improved logic for constructing content disposition strings based on filename character set.

@fengmk2 fengmk2 requested a review from killagu December 5, 2024 06:40
Copy link

coderabbitai bot commented Dec 5, 2024

Walkthrough

The changes introduce a new regular expression constant, NON_ASCII_RE, to the FormData class for identifying non-ASCII characters in filenames. The method _getContentDisposition has been updated to modify the construction of the contentDisposition string. It now conditionally includes a UTF-8 encoded version of the filename only if the filename contains non-ASCII characters, thereby altering the control flow related to filename handling.

Changes

File Change Summary
src/FormData.ts Added NON_ASCII_RE regex constant; modified _getContentDisposition to conditionally include UTF-8 encoded filenames based on ASCII check.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant FormData
    participant FileSystem

    User->>FormData: Upload File
    FormData->>FileSystem: Check filename
    alt Non-ASCII filename
        FormData->>FormData: Append UTF-8 encoded version
    end
    FormData->>FileSystem: Construct contentDisposition
    FileSystem-->>User: Return contentDisposition
Loading

🐰 "In the land of code, where changes bloom,
A regex was added to lighten the gloom.
Non-ASCII filenames now get their due,
With UTF-8 magic, all shiny and new!
Hooray for the updates, let’s hop with delight,
For clearer content, our future is bright!" 🐇


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Member

@killagu killagu left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link

pkg-pr-new bot commented Dec 5, 2024

Open in Stackblitz

npm i https://pkg.pr.new/node-modules/urllib@558

commit: 06b4252

Copy link

codecov bot commented Dec 5, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 94.62%. Comparing base (e9f4258) to head (06b4252).
Report is 2 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #558      +/-   ##
==========================================
+ Coverage   94.60%   94.62%   +0.01%     
==========================================
  Files          11       11              
  Lines        1223     1227       +4     
  Branches      297      298       +1     
==========================================
+ Hits         1157     1161       +4     
  Misses         62       62              
  Partials        4        4              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (1)
src/FormData.ts (1)

4-5: Consider simplifying the regex pattern.

The regex pattern correctly identifies non-ASCII characters, but can be improved:

  1. The case-insensitive flag i is unnecessary for character ranges
  2. Consider using a more semantic variable name like NON_ASCII_RE
-const ASCII_RE = /[^\x00-\x7F]/i;
+const NON_ASCII_RE = /[^\x00-\x7F]/;
🧰 Tools
🪛 Biome (1.9.4)

[error] 5-5: Unexpected control character in a regular expression.

Control characters are unusual and potentially incorrect inputs, so they are disallowed.

(lint/suspicious/noControlCharactersInRegex)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between b98b502 and 1588c90.

📒 Files selected for processing (1)
  • src/FormData.ts (2 hunks)
🧰 Additional context used
🪛 Biome (1.9.4)
src/FormData.ts

[error] 5-5: Unexpected control character in a regular expression.

Control characters are unusual and potentially incorrect inputs, so they are disallowed.

(lint/suspicious/noControlCharactersInRegex)

🔇 Additional comments (1)
src/FormData.ts (1)

30-33: LGTM! Implementation follows RFC 6266 correctly.

The changes properly implement conditional extended filename notation:

  1. Always includes the base filename parameter
  2. Only adds UTF-8 extended notation for non-ASCII filenames
  3. Follows RFC 6266 section 4.1 format

Let's verify the handling of various filename types:

✅ Verification successful

Content-Disposition implementation verified with test coverage

The test files show comprehensive coverage of filename handling including:

  • ASCII filenames (e.g., 'foo.txt', 'index.js')
  • Non-ASCII filenames with emojis (e.g., '😄foo😭.txt')
  • Multiple file uploads in various formats
  • Different file types and extensions

The implementation correctly handles both ASCII and non-ASCII filenames as evidenced by the passing tests, particularly in test/options.files.test.ts which verifies the content disposition headers through multipart form submissions.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Find test files that verify filename handling

# Look for related test files
fd -e test.ts -e spec.ts -e test.js -e spec.js | xargs rg -l "filename|FormData|Content-Disposition" 

# Look for actual usage patterns
rg -A 2 "new FormData\(\)" 

Length of output: 515


Script:

#!/bin/bash
# Let's examine the test files for content disposition and filename handling tests

# Check test files for content disposition tests
rg -B 2 -A 4 "Content-Disposition|filename" test/options.files.test.ts test/options.data.test.ts

# Look for FormData usage in tests with context
rg -B 2 -A 4 "new FormData\(\)" test/options.files.test.ts test/options.data.test.ts

# Search for actual filename test cases to verify different scenarios
rg -B 1 -A 1 "['\"](.*(?:\.txt|\.png|\.jpg))['\"]" test/options.files.test.ts

Length of output: 15718

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (1)
src/FormData.ts (1)

4-5: LGTM with minor suggestions.

The regex pattern correctly identifies non-ASCII characters. Consider these optional improvements:

  1. Remove the unnecessary case-insensitive flag 'i' since character ranges are not affected by case
  2. Add a brief comment explaining the ASCII range check
-const NON_ASCII_RE = /[^\x00-\x7F]/i;
+// Matches any character outside the ASCII range (0-127)
+const NON_ASCII_RE = /[^\x00-\x7F]/;
🧰 Tools
🪛 Biome (1.9.4)

[error] 5-5: Unexpected control character in a regular expression.

Control characters are unusual and potentially incorrect inputs, so they are disallowed.

(lint/suspicious/noControlCharactersInRegex)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 1588c90 and 06b4252.

📒 Files selected for processing (1)
  • src/FormData.ts (2 hunks)
🧰 Additional context used
🪛 Biome (1.9.4)
src/FormData.ts

[error] 5-5: Unexpected control character in a regular expression.

Control characters are unusual and potentially incorrect inputs, so they are disallowed.

(lint/suspicious/noControlCharactersInRegex)

🔇 Additional comments (1)
src/FormData.ts (1)

30-33: Implementation correctly follows RFC 6266 specification.

The changes properly implement the Content-Disposition header format by:

  1. Always including the standard filename parameter
  2. Only adding the extended UTF-8 notation when necessary (non-ASCII characters present)

This fixes potential issues with unnecessary encoding while maintaining compatibility.

Let's verify the behavior with different filename types:

✅ Verification successful

The test results show comprehensive coverage of filename handling, including:

  1. ASCII filenames (e.g., 'index.js', 'package.json')
  2. Non-ASCII filenames (e.g., '😄foo😭.txt')
  3. Various file types and encodings

The implementation correctly handles both scenarios as evidenced by passing tests, particularly in test/options.files.test.ts where non-ASCII filenames are properly processed.

Content-Disposition header implementation verified as correct

The changes properly implement RFC 6266 by:

  1. Always including the base filename parameter
  2. Adding UTF-8 encoded version only for non-ASCII filenames
  3. Using proper encoding via encodeURIComponent

The test suite confirms proper handling of both ASCII and non-ASCII filenames across different scenarios.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Create test cases to verify the content disposition logic
echo "Testing content disposition handling..."

# Find and analyze all filename usages in tests
rg -l "filename" --type ts --type js | while read -r file; do
  echo "=== $file ==="
  # Show test cases and their context
  rg -B2 -A2 "filename.*[^a-zA-Z0-9]" "$file"
done

Length of output: 15000

@killagu killagu merged commit 0cd9b06 into master Dec 5, 2024
24 checks passed
@killagu killagu deleted the ascii-filename branch December 5, 2024 06:45
fengmk2 pushed a commit that referenced this pull request Dec 5, 2024
[skip ci]

## [4.6.3](v4.6.2...v4.6.3) (2024-12-05)

### Bug Fixes

* only set extend notation on non-ascii filename ([#558](#558)) ([0cd9b06](0cd9b06))
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.

2 participants