Drupal\file\IconMimeTypes doesn't handle NULL mimetypes

Created on 20 August 2024, 3 months ago
Updated 6 September 2024, 3 months ago

Problem/Motivation

Drupal\file\IconMimeTypes cannot handle NULL value for mimetype well. Errors out pages with file references to file with NULL as the mimetype.

Steps to reproduce

Attempt to view or edit a page with a file reference to a file with NULL as the mimetype, where a variable icon is expected to display.

Instead you'll get a white error page, with watchdog errors, e.g., TypeError: Drupal\file\IconMimeTypes::getIconClass(): Argument #1 ($mimeType) must be of type string, null given ...

Proposed resolution

The problem is that the database table file_managed allows NULL for the filemime column, but the internal methods of Drupal\file\IconMimeTypes require strings and can't handle NULL at all.

The signatures should be altered to allow NULL values, and to handle them gracefully, as did the file_icon_class() function this class replaces.

Remaining tasks

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

๐Ÿ› Bug report
Status

Needs work

Version

11.0 ๐Ÿ”ฅ

Component
File systemย  โ†’

Last updated about 12 hours ago

Created by

๐Ÿ‡บ๐Ÿ‡ธUnited States msypes

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @msypes
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States msypes
  • ๐Ÿ‡ต๐Ÿ‡ฐPakistan asghar

    I created the Patch for it.

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia Sahana _N

    sahana _n โ†’ made their first commit to this issueโ€™s fork.

  • Merge request !9399File-mime-type-null-issue-3469280 โ†’ (Open) created by Sahana _N
  • Status changed to Needs review 3 months ago
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia Sahana _N

    Hi,
    I can reproduce the issue in Drupal 10.3.x, when I attempt to view the page with a file reference to a file with NULL as the mimetype I got this error TypeError: Drupal\file\IconMimeTypes::getIconClass(): Argument #1 ($mimeType) must be of type string, null given, called in /var/www/html/core/modules/file/file.module on line 774 in Drupal\file\IconMimeTypes::getIconClass() (line 21 of core/modules/file/src/IconMimeTypes.php).

    I tested the same scenario by applying the patch provided in comment #3 (update this comment number). The patch is applied cleanly and the error was cleared. So, I created MR with the same code changes.

    I am attaching before & after screenshots for reference.

    This issue is also reproducible in 11.x and was able to fix the issue with the same code changes.

    I'll be happy to create MR for 11.x also if required. Please suggest how we can proceed further.

  • Pipeline finished with Success
    3 months ago
    Total: 616s
    #272526
  • Status changed to Needs work 3 months ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    Fixes should be against 11.x

    Also we should probably do some debugging vs putting a null check. Could be masking a larger issue

  • Merge request !9407File-mime-type-null-issue-3469280 โ†’ (Open) created by Sahana _N
  • Pipeline finished with Success
    3 months ago
    Total: 465s
    #273193
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia Sahana _N

    Hi,
    Created an MR for 11. x.

  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia kim.pepper ๐Ÿ„โ€โ™‚๏ธ๐Ÿ‡ฆ๐Ÿ‡บSydney, Australia

    Despite the db column allowing NULL. our API assumes there will always be a mime type. Why do you have NULL mimetypes?

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States msypes

    @kim.pepper: Are you asking me why we had NULL mimetypes in the database? I don't know. Could be from a less-than-perfect migration, from when we moved from D7. They were generally pretty old records. Did the the API always assume there was a mimetype? If not, why would the db allow it to be NULL? At any rate, the fact that it does allow NULLs should mean that the system can handle them.

Production build 0.71.5 2024