When submitting webform with attached file in field "Document file", saving of file fails if resulting filename to save is to long. Webform is submitted without file.

Long filename can be obtained from relatively short filename when "Rename files" with "File name patterns" and "Sanitize file name" settings are enabled.
For example with above settings and pattern "[webform_submission:values:uploaded_file]-[random:hash:md5]" drupal log outputs following error:
The specified file 'public://webform/contest_application_form/_sid_/Статья-Януль А.Н.-Гепатология и гастроэнтерология-перевод на англ.pdf' could not be moved to 'public://webform/contest_application_form/22/d0a1d182d0b0d182d18cd18f-d0afd0bdd183d0bbd18c20d090.d09d.-d093d0b5d0bfd0b0d182d0bed0bbd0bed0b3d0b8d18f20d0b820d0b3d0b0d181d182d180d0bed18dd0bdd182d0b5d180d0bed0bbd0bed0b3d0b8d18f-d0bfd0b5d180d0b5d0b2d0bed0b420d0bdd0b020d0b0d0bdd0b3d0bb.pdf-71880601a14db1e9654e720b28235bbf.pdf'.

PHP error log has following related record:

Uncaught PHP Exception Drupal\Core\Entity\EntityStorageException: "SQLSTATE[22001]: String data, right truncated: 1406 Data too long for column 'uri' at row 1: UPDATE {file_managed} SET uuid=:db_update_placeholder_0, langcode=:db_update_placeholder_1, uid=:db_update_placeholder_2, filename=:db_update_placeholder_3, uri=:db_update_placeholder_4, filemime=:db_update_placeholder_5, filesize=:db_update_placeholder_6, status=:db_update_placeholder_7, created=:db_update_placeholder_8, changed=:db_update_placeholder_9
WHERE fid = :db_condition_placeholder_0; Array
(
    [:db_update_placeholder_0] => badbe998-f249-4ef8-8a7c-0f8d750747eb
    [:db_update_placeholder_1] => cs
    [:db_update_placeholder_2] => 0
    [:db_update_placeholder_3] => d0a2d0b8d182d183d0bbd18cd0bdd18bd0b920d0bbd0b8d181d18220d0b6d183d180d0bdd0b0d0bbd0b0-d093d0b5d0bfd0b0d182d0bed0bbd0bed0b3d0b8d18f20d0b820d0b3d0b0d181d182d180d0bed18dd0bdd182d0b5d180d0bed0bbd0bed0b3d0b8d18f-2018.pdf-a4ff8230225f741f54cbb33c42563399.pdf
    [:db_update_placeholder_4] => public://webform/contest_application_form/23/d0a2d0b8d182d183d0bbd18cd0bdd18bd0b920d0bbd0b8d181d18220d0b6d183d180d0bdd0b0d0bbd0b0-d093d0b5d0bfd0b0d182d0bed0bbd0bed0b3d0b8d18f20d0b820d0b3d0b0d181d182d180d0bed18dd0bdd182d0b5d180d0bed0bbd0bed0b3d0b8d18f-2018.pdf-a4ff8230225f741f54cbb33c42563399.pdf
    [:db_update_placeholder_5] => application/pdf
    [:db_update_placeholder_6] => 589218
    [:db_update_placeholder_7] => 0
    [:db_update_placeholder_8] => 1568835726
    [:db_update_placeholder_9] => 1568835743
    [:db_condition_placeholder_0] => 227
)
" at /www/doc/www.example.com/www/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php line 847

It is obvious that problem is with missing check / automatic trimming of resulting filename if its URI is to long for varchar(255) column.
Problem is amplified by sanitization converting cyrilic (or any other sanitizable characters) to longer string, but i presume that longer uri can be obtained by using tokens only.

Side note: similar problem occurs when trying to attach file with extra long name, only this time it fails already when trying to attach file (sadly it fails without any frontend visible error, only AJAX logs error in console) and thus form submission is prevented (if file field is required).

Comments

t_d_d created an issue. See original summary.

jrockowitz’s picture

Status: Active » Needs review
StatusFileSize
new1.82 KB
new4.51 KB

The attached webform replicates the issue just using a simple string.

Here is more information about the issue.

I don't see any way to reliably validate the file's name and file's uri length since combined they can't exceed 255 characters. The most immediate solution to prevent the data loss is to truncate the file name when the upload file is moved.

Please review the attached patch truncates long file names.

Status: Needs review » Needs work

The last submitted patch, 2: 3083214-2.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

jrockowitz’s picture

Status: Needs work » Needs review
StatusFileSize
new6.2 KB

This patch fixes the broken tests and has test coverage for the truncation.

t_d_d’s picture

#4 patch fixes problem, but might introduced another issue (testing on site affected by original issue using #4 patched webform 8.x-5.4):

I am emailing webform values [webform_submission:values] with file field selected in included email values. Before it outputed only file name as link text inside email, now it uses full URI (stripped of slashes) as file link text.

Edit: also there is same full URI as a file link text in result page for afterpatched submissions.

jrockowitz’s picture

StatusFileSize
new114.04 KB

I think you need to change the file element's submission display format to "Link' and use the  [webform_submission:values:html] token.

This is an unrelated issue and if there is a regression, please create a new ticket.

t_d_d’s picture

Status: Needs review » Needs work
jrockowitz’s picture

The full very long file name is outputted as expected.

Can you please provide an example webform that can be used to replicate your specific issue?

t_d_d’s picture

StatusFileSize
new4.98 KB

Attached exported configuration of minimal webform replicating link issue (using suggested configuration above) in email and also on results page.

jrockowitz’s picture

Using '#file_name': '[webform_submission:values:file]' is causing the temp file uri to be used as the file name and it is working as expected. It just does not look right.

t_d_d’s picture

Ok, thanks for support. I was confused because before patch [webform_submission:values:file] token outputed filename only, not uri.

Any idea how to modify submitted filename (for example to: "original.filename-somecustomstring") if i can not use "[webform_submission:values:file]-somecustomstring" now? Is there another token for just filename?

jrockowitz’s picture

Status: Needs work » Needs review
StatusFileSize
new8.9 KB

Attached now patch adds support for [webform_submission:values:file:basename]

jrockowitz’s picture

StatusFileSize
new8.9 KB

Patch sets max length to 250 to account for renamed files (i.e. *_01.txt)

t_d_d’s picture

Fantastic. Can confirm #13 works for me as expected. Even better than before, because transliteration inside sanitization is now actually transliterating (using new basename token) and not only converting characters to unicode codes (nice side effect).
Thanks a lot!

Side note: Found another possible bug with long names file upload while testing this. I will investigate it further and post another issue for it, since it seems out of scope of this issue (not dependent on tokens, sanitization and happening before submit).

jrockowitz’s picture

Status: Needs review » Reviewed & tested by the community
jrockowitz’s picture

Status: Reviewed & tested by the community » Fixed

  • jrockowitz authored 12f0ec4 on 8.x-5.x
    Issue #3083214 by jrockowitz, t_d_d: failed file saving when submitting...

Status: Fixed » Closed (fixed)

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