Skip to content

Conversation

@obenland
Copy link
Member

Proposed changes:

  • Strip query parameters from URLs before generating attachment filenames
  • Prevents "Filename too long" filesystem errors
  • Fixes issues with Instagram CDN URLs and other services that use long query strings

Other information:

  • Have you written new tests for your changes, if applicable?

Testing instructions:

  1. Process an ActivityPub activity with an attachment URL that has query parameters (e.g., Instagram CDN URL)
  2. Verify the file is saved successfully without errors
  3. Check that the filename is clean without query parameters

Before:

PHP Warning: copy(.../582455906_17930138757114605_3007218277985087381_n.jpgstpdst-jpg_e35_tt6_nc_cat101ccb7-5_nc_sid18de74efgeyJlZmdfdGFnIjoiRkVFRC5iZXN0X2ltYWdlX3VybGdlbi5DMyJ9_nc_ohcne9zSzVebdsQ7kNvwGMeb6Q_nc_ocAdmzsGgwxVX3rDrTgZriddqj37P8s3eVEtAa8tNuy5DnRzJUT7nFkflSQgNVztbdQCIpM6rhfFycR1UGy1LYI2oP_nc_adz-m_nc_cid0_nc_zt23_nc_htscontent.cdninstagram.com_nc_gideIO13gOfATKzYZ-ERRIiugoh00_Afg0LbbH0emgIK49rQawuJMuNFn3fJZCkBf5zfuflpG90Qoe6922C08B): Failed to open stream: Filename too long

After:
File saved successfully as 582455906_17930138757114605_3007218277985087381_n.jpg

Changelog entry

  • Automatically create a changelog entry from the details below.
Changelog Entry Details

Significance

  • Patch
  • Minor
  • Major

Type

  • Added - for new features
  • Changed - for changes in existing functionality
  • Deprecated - for soon-to-be removed features
  • Removed - for now removed features
  • Fixed - for any bug fixes
  • Security - in case of vulnerabilities

Message

Fix "Filename too long" errors when downloading attachments from URLs with query parameters (e.g., Instagram CDN URLs).

Strip query parameters from URLs before generating filenames to prevent
'Filename too long' errors. This is particularly important for Instagram
and other CDN URLs that include long query strings with cache-busting
parameters.

Without this fix, URLs like:
  https://example.com/image.jpg?stp=dst-jpg_e35&nc_cat=101&...
Would generate filenames like:
  image.jpgstp=dst-jpg_e35_nc_cat=101_... (400+ characters)

Now correctly generates:
  image.jpg

Fixes in both save_attachment() and save_file() methods.
Copilot AI review requested due to automatic review settings November 19, 2025 00:40
@obenland obenland self-assigned this Nov 19, 2025
@obenland obenland requested a review from a team November 19, 2025 00:40
Copy link

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

This PR fixes filesystem errors caused by excessively long filenames when downloading attachments from URLs containing query parameters. The fix strips query strings before generating filenames, preventing "Filename too long" errors commonly encountered with Instagram CDN URLs and similar services.

Key Changes:

  • Modified filename generation to strip query parameters using strtok() before extracting the basename
  • Applied the fix to both save_attachment() and save_file() methods

Reviewed Changes

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

File Description
includes/class-attachments.php Strips query parameters from URLs before generating attachment filenames in both save methods
.github/changelog/2499-from-description Adds changelog entry documenting the bug fix

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

Tests verify that:
- save_attachment() strips query parameters from attachment URLs
- save_file() strips query parameters from direct file storage URLs
- Filenames are clean and don't contain ? or query string components

This ensures the fix for 'Filename too long' errors is properly tested.
- Remove unused variable to fix phpcs warning
- Use assertStringEndsWith instead of assertStringContainsString for file extension checks
Changed from strtok() to wp_parse_url() to properly handle URL fragments
(anchors with #) in addition to query parameters. This matches WordPress
core's approach in download_url() and ensures filenames never contain
fragment identifiers.
@obenland obenland requested a review from pfefferle November 19, 2025 14:41
@obenland obenland merged commit c99298e into trunk Nov 19, 2025
23 of 24 checks passed
@obenland obenland deleted the fix/attachment-filename-query-params branch November 19, 2025 16:09
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.

4 participants