Updated: Comment #N

Problem/Motivation

While reviewing #1815504: file_save_upload() should not alter the file URI string before validation, I found that file_save_upload() uses $file->filename and $file->uri in a watchdog message, which lead to erroneous watchdog messages:

 72  12/Dec 14:09  file      notice    Upload error. Could not move uploaded file Object to destination Object.                                                                                                                     

(this is generated when drupal_move_uploaded_file() fails - which I manually forced to fail for this exercise).

Proposed resolution

Use the appropriate methods $file->getFilename() and $file->getFileUri(), respectively.

Remaining tasks

write patch and look for other occurences of $file->filename and $file->uri throughout core.

Beta phase evaluation

Reference: https://www.drupal.org/core/beta-changes
Issue category Bug
Unfrozen changes Unfrozen because it only changes how file properties are accessed, and fixes bugs in watchdog messages.
Prioritized changes The main goal of this issue is fixing a bug in watchdog notices.
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

scor’s picture

Issue summary: View changes
scor’s picture

Status: Active » Needs review
FileSize
1.11 KB

I also fixed another instance that Peter had found in #1815504: file_save_upload() should not alter the file URI string before validation.

With this patch, the watchdog message reads:

 73  12/Dec 14:19  file      notice    Upload error. Could not move uploaded file 00B6899D.php.txt to destination public://field/image/00B6899D.php_11.txt.                                                                         
scor’s picture

Grepping through core, with

  • git g "file->filename"
  • git g "file->uri"

I find lots of results. Surely there must be some legitimate use cases for using the property directly, but can someone familiar with D8 file entities explain where to draw the line?

jhedstrom’s picture

Status: Needs review » Needs work
Issue tags: +Needs reroll

I find lots of results. Surely there must be some legitimate use cases for using the property directly, but can someone familiar with D8 file entities explain where to draw the line?

I'd say all of them should use the method if available. I'm surprised those properties aren't protected.

areke’s picture

FileSize
1.12 KB

Rerolled patch.

areke’s picture

Status: Needs work » Needs review
jhedstrom’s picture

Issue summary: View changes
Status: Needs review » Reviewed & tested by the community

This looks good. I added the beta phase evaluation to the summary.

I think we can tackle remaining occurrences of the direct property usage later (since this change fixes a bug, while the others aren't urgent).

penyaskito’s picture

Issue tags: -Needs reroll

Removing Needs reroll tag for now. Probably this should have tests, but I will wait for others to chime in.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs tests

If we are fixing a bug we should have a test.

ChristianAdamski’s picture

This patch does two things:

1.) It adds a new test that deliberately fails moving a file upload by targeting a non-writable directory, triggering and failing move_uploaded_file() and therefor causing the aforementioned log message to be written. It then checks for that log message being present.
NOTICE: without the patches above, this will fail. That's the purpose :)

2.) This patch additionally introduces a check in the tested form, if the const SIMPLETEST_COLLECT_ERRORS should be set, to suppress the unavoidable PHP WARNINGs by move_uploaded_file() failing.

Berdir’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests

The warnings trick is a bit nasty, but I have no better idea right now.

Can you upload a combined patch with the test and fix as well, so we can see that the test is now green?

ChristianAdamski’s picture

The last submitted patch, 10: drupal-file_upload_test_addition-2155245-10.patch, failed testing.

tstoeckler’s picture

Status: Needs review » Reviewed & tested by the community

Yes, I don't much like it either, but by wrapping it in the state check it's at least fairly self contained. And I looked around for quite a while and there really isn't any cleaner way of doing this.

There's a lot of room for the error handling in simpletest but in the scope of this issue I really cannot find anything to complain about this patch.

tstoeckler’s picture

Oh and thanks a lot for figuring this out @ChristianAdamski, quite a though one! :-)

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed 0c1276b and pushed to 8.0.x. Thanks!

Thanks for adding beta evaluation to the issue summary.

  • alexpott committed 0c1276b on 8.0.x
    Issue #2155245 by ChristianAdamski, scor, areke: Use proper methods...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

sudhanshug queued 5: 2155245-5.patch for re-testing.