bonjurmrfirst / ng4-files

20 stars 15 forks source link

Not verifying files with extensions in uppercase #16

Open avi-s opened 6 years ago

avi-s commented 6 years ago

If you have added a specific extension in the acceptExtensions field in the config and try to upload a file with the same exact extension but in uppercase, the file in not verified.

(for example png is added to acceptExtensions but the user tries to upload a file with a PNG extension)

even if you try and add that extension in uppercase to the acceptExtensions field it still won't work.

There is a function called transformAcceptExtensions which is a part of a bigger function called newConfigVerifyPipeline under the addConfig function [all in ng4-files.service.ts] which converts all the extensions added in acceptExtensions to lowercase.

private transformAcceptExtensions(config: any): Ng4FilesService {
        if (
            config.acceptExtensions === '*' ||
            config.acceptExtensions.indexOf('*') !== -1 ||
            Array.isArray(config.acceptExtensions) && config.acceptExtensions.length === 0
        ) {
            config.acceptExtensions = '*/*';
        } else {
            config.acceptExtensions = (config.acceptExtensions as string[])
                .map(extension => '.' + extension.toLowerCase()).join(', ');
        }

        return this;
    }

But when a file is added and is verified using the verifyFiles function [ng4-files-utils.service.ts] the file name is not converted to lowercase in order to be matched by the regex built by the acceptable extensions [which all of them have been converted to lowercase as I explained above].

const filesNotMatchExtensions = filesArray.filter((file: File) => {
            const extensionsList = (acceptExtensions as string)
                .split(', ')
                .map(extension => extension.slice(1))
                .join('|');

            const regexp = Ng4FilesUtilsService.getRegExp(extensionsList);

            return !regexp.test(file.name);
        });

So somehow the whole acceptExtensions and verifying files becomes unusable because it will bring up a lot of trouble with the users.

cmehta82 commented 5 years ago

This can be easily solved by :

`

const filesNotMatchExtensions = filesArray.filter((file: File) => { const extensionsList = (acceptExtensions as string) .split(', ') .map(extension => extension.slice(1).toLowerCase()) .join('|');

const regexp = Ng4FilesUtilsService.getRegExp(extensionsList);

let fileName = file.name; if(fileName) { fileName = fileName.toLowerCase(); } return !regexp.test(fileName); });

`