Opened 5 years ago

Closed 5 years ago

#30599 closed Cleanup/optimization (fixed)

Cloaking PermissionErrors raised in ManifestFilesMixin.read_manifest().

Reported by: Matt Layman Owned by: zeynel
Component: contrib.staticfiles Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

While using the ManifestStaticFilesStorage, I encountered the ValueError shown below.

  <trim>
  File "/<some venv>/site-packages/django/contrib/staticfiles/storage.py", line 134, in _url
    hashed_name = hashed_name_func(*args)
  File "/<some venv>/site-packages/django/contrib/staticfiles/storage.py", line 422, in stored_name
    raise ValueError("Missing staticfiles manifest entry for '%s'" % clean_name)
ValueError: Missing staticfiles manifest entry for 'images/twitter.png'

Further debugging showed that staticfile_storage.hashed_files was empty. This was odd to me because staticfiles.json existed on my system in its proper location in STATIC_ROOT. Additionally, the 'images/twitter.png' existed as a key in the staticfiles.json so I was very confused.

I did more debugging of the manifest loading process and hit the root problem. Reading the manifest file raised a PermissionError because the user account running the Django app did not have proper permission to read from where the file was stored on the filesystem. Unfortunately, https://github.com/django/django/blob/master/django/contrib/staticfiles/storage.py#L385 catches any OSError and proceeds by returning no content.

This was in a Vagrant virtual machine so I switched to the root user to bypass any permission errors and the manifest loading worked. I confirmed that I was able to load a page without any issue.

I'd like to suggest that ManifestFilesMixin.read_manifest either a) not catch OSError at all to let the system fail so a developer can fix it, b) have more granular error handling of different OSError exception subclasses, or c) do some kind of logging to hint to the app developer that there was a problem.

I'm making this suggestion because catching this error and proceeding left the app in an unrecoverable state. The ValueError listed at the beginning of the issue was actually a side effect of the deeper underlying PermissionError problem from attempting to read the manifest file.

If there is any more info you need from me, please let me know. Thanks!

Change History (5)

comment:1 by Mariusz Felisiak, 5 years ago

Component: Template systemcontrib.staticfiles
Summary: ManifestFilesMixin read_manifest swallows OSError that prevents template renderingCloaking PermissionErrors raised in ManifestFilesMixin.read_manifest().
Triage Stage: UnreviewedAccepted
Type: UncategorizedCleanup/optimization
Version: 2.2master

Thanks for the report. I agree that we should catch more specific exception (probably FileNotFoundError) or re-raise PermissionError.

comment:2 by zeynel, 5 years ago

Owner: changed from nobody to zeynel
Status: newassigned

comment:3 by Mariusz Felisiak, 5 years ago

Has patch: set
Needs tests: set

comment:4 by Mariusz Felisiak, 5 years ago

Needs tests: unset
Triage Stage: AcceptedReady for checkin

comment:5 by Mariusz Felisiak <felisiak.mariusz@…>, 5 years ago

Resolution: fixed
Status: assignedclosed

In 955b382:

Fixed #30599 -- Prevented ManifestFilesMixin.read_manifest() from silencing errors other than FileNotFoundError.

Note: See TracTickets for help on using tickets.
Back to Top