File uploaded via REST can set an on-screen message and trigger X-CSRF token error for next REST requests

Created on 11 August 2020, almost 5 years ago
Updated 28 July 2025, 6 days ago

Problem/Motivation

An on-screen message is set when trying to upload a file that has a short (2-5 chars) name part (Eg: awesome_picture.beer.jpg - the part "beer" is shorter that 5 chars).

"For security reasons, your upload has been renamed to %filename."

When adding an on-screen message, a session is also set and this will block any future POST/PATCH api calls triggering an X-CSRF token error.

"X-CSRF-Token request header is missing"

This is a problem when using other authentication methods (Eg. oauth2). If you use cookie authentication for the rest calls then I think it should work just fine.

Steps to reproduce

When you try to upload a file via REST api (Eg: /file/upload/{entity_type_id}/{bundle}/{field_name} ) that has a short (2-5 chars) middle name - Eg: awesome_picture.beer.jpg, the filename will be changed to "awesome_picture.beer_.jpg".

This happens when calling file_munge_filename() from \Drupal\file\Plugin\rest\resource\FileUploadResource->prepareFilename($filename, array &$validators)

Proposed resolution

file_munge_filename($filename, $extensions, $alerts = TRUE) function takes 3 parameters but when calling it from FileUploadResource class only 2 parameters are sent.

The 3rd parameter is the one telling Drupal not to set an on-screen message. So if we just add the third parameter as false, this should fix the issue.

πŸ› Bug report
Status

Postponed: needs info

Version

11.0 πŸ”₯

Component

file system

Created by

πŸ‡·πŸ‡΄Romania mariusilie

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024