grav-plugin-form icon indicating copy to clipboard operation
grav-plugin-form copied to clipboard

[FIX] Critical Bug - Form save: File with extension not allowed

Open maofree opened this issue 7 months ago • 7 comments

Hi Today I've seen that the form has a big problem after to send a message I get this error

Form save: File with extension not allowed: contactform-06/01/2024 13:24:36.txt

1

why should the txt file be wrong? It worked fine before, I think it's due to the latest plugin updates, which added this error

How is it possible to fix it? all sites with grav have this problem. I use the latest versions

the email is sent correctly


I've seen that the problem is due from this condition inside Utils::checkFilename of Utils.php at line 989

|| strtr($filename, "\t\v\n\r\0\/", '_______') !== $filename

I think the problem is in the $filename variable because it is like so

contactform-06/01/2024 15:07:23.txt

but the real filename is so 2024 15:07:23.txt, because contactform-06 is a folder and 01 is another folder and the condition check the presence of slashes


it is not possible to use this line

$filename = $prefix . $this->udate($format, $raw_format) . $postfix . $ext;

with this condition

if (!Utils::checkFilename($filename)) { throw new RuntimeException(sprintf('Form save: File with extension not allowed: %s', $filename)); }

the filename includes folders with slashes in its name, so you should pass to that condition only the real filename or use a different condition


1

I suggest this solution

` $filename_array = $filename ? explode('/', $filename) : [];

            // Handle bad filenames.
            if (!Utils::checkFilename($filename_array[count($filename_array) -1])) {
                throw new RuntimeException(sprintf('Form save: File with extension not allowed: %s', $filename));
            }`

thanks

maofree avatar Jan 12 '24 08:01 maofree

Is there anyone who handles this topic? This would solve a problem in sending emails, I think it's important

maofree avatar Jan 22 '24 15:01 maofree

Sorry only me, and i've been sick.

It sounds like you are trying to have a custom path in the filename, and then you are trying to work-around the security checks that are there to prevent adding custom paths to filenames. The reason this is there now is due to a vulnerability that allowed arbitrary paths to be passed in via a user editable form.

So that said, it seems the error is correct, but the message is wrong, it's really the filename is not allowed, due to the path structure in it, not merely the extension.

In short the filename should not contain paths.

A better solution is to have a new path option that is added to the front of the filename, but is in some format that can be auto-generated, perhaps using PHP date tokens, So not completely arbitrary, something that can be validated.

In the meantime, i suggest just using the date in the filename rather than prefixing folders.

rhukster avatar Jan 22 '24 15:01 rhukster

Hi I'm sorry, yes I also got sick 23 weeks ago, now there are a lot of people with the flu

I don't think I did any customization, just something in the theme files for the form graphics. the problem arose due to an update made in the "form" plugin, in fact I saw that this check was added

1 2 3 4

do a check and you will see that in form/form.php the variable $filename also contains the paths to the file

with the version v7.2.1 this condition is not present and all is ok

as mentioned, the condition gives an error because the variable also contains the paths to the filename I don't think I've made any changes to the file path, because I'm not interested in this type of change

thanks

maofree avatar Jan 22 '24 17:01 maofree

What about your form definition in the form.md page file?

rhukster avatar Jan 22 '24 17:01 rhukster

form.it.md

maofree avatar Jan 22 '24 17:01 maofree

So in this part:

dateformat: 'd/m/Y H:i:s'

change it to:

dateformat: 'd-m-Y_H:i:s'

or someting similar

rhukster avatar Jan 22 '24 17:01 rhukster

yes with this modification it doesn't give me errors

maofree avatar Jan 22 '24 18:01 maofree