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).
| Comment | File | Size | Author |
|---|---|---|---|
| #13 | 3083214-13.patch | 8.9 KB | jrockowitz |
| #12 | 3083214-12.patch | 8.9 KB | jrockowitz |
| #9 | webform.webform.uri_as_link.yml | 4.98 KB | t_d_d |
| #6 | Screen Shot 2019-09-24 at 8.46.16 AM.png | 114.04 KB | jrockowitz |
| #4 | 3083214-4.patch | 6.2 KB | jrockowitz |
Comments
Comment #2
jrockowitz commentedThe 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.
Comment #4
jrockowitz commentedThis patch fixes the broken tests and has test coverage for the truncation.
Comment #5
t_d_d commented#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.
Comment #6
jrockowitz commentedI 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.
Comment #7
t_d_d commentedComment #8
jrockowitz commentedThe 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?
Comment #9
t_d_d commentedAttached exported configuration of minimal webform replicating link issue (using suggested configuration above) in email and also on results page.
Comment #10
jrockowitz commentedUsing
'#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.Comment #11
t_d_d commentedOk, 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?
Comment #12
jrockowitz commentedAttached now patch adds support for
[webform_submission:values:file:basename]Comment #13
jrockowitz commentedPatch sets max length to 250 to account for renamed files (i.e. *_01.txt)
Comment #14
t_d_d commentedFantastic. 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).
Comment #15
jrockowitz commentedComment #16
jrockowitz commented