Stop checking file extensions

Changelog-entry: Stop checking file extensions
Change-type: patch
This commit is contained in:
Alexis Svinartchouk 2020-05-27 17:27:09 +02:00
parent 4e08cf3879
commit 4752fa6dd2
8 changed files with 0 additions and 582 deletions

View File

@ -266,17 +266,6 @@ export class SourceSelector extends React.Component<
hasMBR: boolean;
},
) {
if (!supportedFormats.isSupportedImage(image.path)) {
const invalidImageError = errors.createUserError({
title: 'Invalid image',
description: messages.error.invalidImage(image.path),
});
osDialog.showError(invalidImageError);
analytics.logEvent('Invalid image', image);
return;
}
try {
let message = null;
let title = null;
@ -321,16 +310,6 @@ export class SourceSelector extends React.Component<
} catch (error) {
analytics.logException(error);
}
if (!supportedFormats.isSupportedImage(imagePath)) {
const invalidImageError = errors.createUserError({
title: 'Invalid image',
description: messages.error.invalidImage(imagePath),
});
osDialog.showError(invalidImageError);
analytics.logEvent('Invalid image', { path: imagePath });
return;
}
let source;
if (SourceType === sourceDestination.File) {

View File

@ -21,8 +21,6 @@ import { v4 as uuidV4 } from 'uuid';
import * as constraints from '../../../shared/drive-constraints';
import * as errors from '../../../shared/errors';
import * as fileExtensions from '../../../shared/file-extensions';
import * as supportedFormats from '../../../shared/supported-formats';
import * as utils from '../../../shared/utils';
import * as settings from './settings';
@ -402,51 +400,6 @@ function storeReducer(
});
}
if (!_.isString(action.data.extension)) {
throw errors.createError({
title: `Invalid image extension: ${action.data.extension}`,
});
}
const extension = _.toLower(action.data.extension);
if (!_.includes(supportedFormats.getAllExtensions(), extension)) {
throw errors.createError({
title: `Invalid image extension: ${action.data.extension}`,
});
}
let lastImageExtension = fileExtensions.getLastFileExtension(
action.data.path,
);
lastImageExtension = _.isString(lastImageExtension)
? _.toLower(lastImageExtension)
: lastImageExtension;
if (lastImageExtension !== extension) {
if (!_.isString(action.data.archiveExtension)) {
throw errors.createError({
title: 'Missing image archive extension',
});
}
const archiveExtension = _.toLower(action.data.archiveExtension);
if (
!_.includes(supportedFormats.getAllExtensions(), archiveExtension)
) {
throw errors.createError({
title: `Invalid image archive extension: ${action.data.archiveExtension}`,
});
}
if (lastImageExtension !== archiveExtension) {
throw errors.createError({
title: `Image archive extension mismatch: ${action.data.archiveExtension} and ${lastImageExtension}`,
});
}
}
const MINIMUM_IMAGE_SIZE = 0;
if (action.data.size !== undefined) {

View File

@ -1,59 +0,0 @@
/*
* Copyright 2017 balena.io
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import * as _ from 'lodash';
import { lookup } from 'mime-types';
/**
* @summary Get the extensions of a file
*
* @example
* const extensions = fileExtensions.getFileExtensions('path/to/foo.img.gz');
* console.log(extensions);
* > [ 'img', 'gz' ]
*/
export function getFileExtensions(filePath: string): string[] {
return _.chain(filePath).split('.').tail().map(_.toLower).value();
}
/**
* @summary Get the last file extension
*
* @example
* const extension = fileExtensions.getLastFileExtension('path/to/foo.img.gz');
* console.log(extension);
* > 'gz'
*/
export function getLastFileExtension(filePath: string): string | null {
return _.last(getFileExtensions(filePath)) || null;
}
/**
* @summary Get the penultimate file extension
*
* @example
* const extension = fileExtensions.getPenultimateFileExtension('path/to/foo.img.gz');
* console.log(extension);
* > 'img'
*/
export function getPenultimateFileExtension(filePath: string): string | null {
const extensions = getFileExtensions(filePath);
if (extensions.length >= 2) {
const ext = extensions[extensions.length - 2];
return lookup(ext) ? ext : null;
}
return null;
}

View File

@ -143,10 +143,6 @@ export const error = {
].join(' ');
},
invalidImage: (imagePath: string) => {
return `${imagePath} is not a supported image type.`;
},
openImage: (imageBasename: string, errorMessage: string) => {
return [
`Something went wrong while opening ${imageBasename}\n\n`,

View File

@ -15,15 +15,9 @@
*/
import * as sdk from 'etcher-sdk';
import * as _ from 'lodash';
import * as mime from 'mime-types';
import * as path from 'path';
import {
getLastFileExtension,
getPenultimateFileExtension,
} from './file-extensions';
export function getCompressedExtensions(): string[] {
const result = [];
for (const [
@ -57,34 +51,6 @@ export function getAllExtensions(): string[] {
];
}
export function isSupportedImage(imagePath: string): boolean {
const lastExtension = getLastFileExtension(imagePath);
const penultimateExtension = getPenultimateFileExtension(imagePath);
if (
_.some([
_.includes(getNonCompressedExtensions(), lastExtension),
_.includes(getArchiveExtensions(), lastExtension),
])
) {
return true;
}
if (
_.every([
_.includes(getCompressedExtensions(), lastExtension),
_.includes(getNonCompressedExtensions(), penultimateExtension),
])
) {
return true;
}
return (
_.isNil(penultimateExtension) &&
_.includes(getCompressedExtensions(), lastExtension)
);
}
export function looksLikeWindowsImage(imagePath: string): boolean {
const regex = /windows|win7|win8|win10|winxp/i;
return regex.test(path.basename(imagePath));

View File

@ -549,97 +549,6 @@ describe('Model: selectionState', function () {
}).to.throw('Invalid image path: 123');
});
it('should throw if no extension', function () {
expect(function () {
selectionState.selectImage({
path: 'foo.img',
size: 999999999,
isSizeEstimated: false,
});
}).to.throw('Missing image fields: extension');
});
it('should throw if extension is not a string', function () {
expect(function () {
selectionState.selectImage({
path: 'foo.img',
extension: 1,
size: 999999999,
isSizeEstimated: false,
});
}).to.throw('Invalid image extension: 1');
});
it("should throw if the extension doesn't match the path and there is no archive extension", function () {
expect(function () {
selectionState.selectImage({
path: 'foo.img',
extension: 'iso',
size: 999999999,
isSizeEstimated: false,
});
}).to.throw('Missing image archive extension');
});
it("should throw if the extension doesn't match the path and the archive extension is not a string", function () {
expect(function () {
selectionState.selectImage({
path: 'foo.img',
extension: 'iso',
archiveExtension: 1,
size: 999999999,
isSizeEstimated: false,
});
}).to.throw('Missing image archive extension');
});
it("should throw if the archive extension doesn't match the last path extension in a compressed image", function () {
expect(function () {
selectionState.selectImage({
path: 'foo.img.xz',
extension: 'img',
archiveExtension: 'gz',
size: 999999999,
isSizeEstimated: false,
});
}).to.throw('Image archive extension mismatch: gz and xz');
});
it('should throw if the extension is not recognised in an uncompressed image', function () {
expect(function () {
selectionState.selectImage({
path: 'foo.ifg',
extension: 'ifg',
size: 999999999,
isSizeEstimated: false,
});
}).to.throw('Invalid image extension: ifg');
});
it('should throw if the extension is not recognised in a compressed image', function () {
expect(function () {
selectionState.selectImage({
path: 'foo.ifg.gz',
extension: 'ifg',
archiveExtension: 'gz',
size: 999999999,
isSizeEstimated: false,
});
}).to.throw('Invalid image extension: ifg');
});
it('should throw if the archive extension is not recognised', function () {
expect(function () {
selectionState.selectImage({
path: 'foo.img.ifg',
extension: 'img',
archiveExtension: 'ifg',
size: 999999999,
isSizeEstimated: false,
});
}).to.throw('Invalid image archive extension: ifg');
});
it('should throw if the original size is not a number', function () {
expect(function () {
selectionState.selectImage({

View File

@ -1,147 +0,0 @@
/*
* Copyright 2017 balena.io
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { expect } from 'chai';
import * as _ from 'lodash';
import * as fileExtensions from '../../lib/shared/file-extensions';
describe('Shared: fileExtensions', function () {
describe('.getFileExtensions()', function () {
_.forEach(
[
// No extension
{
file: 'path/to/filename',
extensions: [],
},
// Type: 'archive'
{
file: 'path/to/filename.zip',
extensions: ['zip'],
},
{
file: 'path/to/filename.etch',
extensions: ['etch'],
},
// Type: 'compressed'
{
file: 'path/to/filename.img.gz',
extensions: ['img', 'gz'],
},
{
file: 'path/to/filename.img.bz2',
extensions: ['img', 'bz2'],
},
{
file: 'path/to/filename.img.xz',
extensions: ['img', 'xz'],
},
{
file: 'path/to/filename.img.xz.gz',
extensions: ['img', 'xz', 'gz'],
},
// Type: 'image'
{
file: 'path/to/filename.img',
extensions: ['img'],
},
{
file: 'path/to/filename.iso',
extensions: ['iso'],
},
{
file: 'path/to/filename.dsk',
extensions: ['dsk'],
},
{
file: 'path/to/filename.hddimg',
extensions: ['hddimg'],
},
{
file: 'path/to/filename.raw',
extensions: ['raw'],
},
{
file: 'path/to/filename.dmg',
extensions: ['dmg'],
},
],
(testCase) => {
it(`should return ${testCase.extensions} for ${testCase.file}`, function () {
expect(fileExtensions.getFileExtensions(testCase.file)).to.deep.equal(
testCase.extensions,
);
});
},
);
it('should always return lowercase extensions', function () {
const filePath = 'foo.IMG.gZ';
expect(fileExtensions.getFileExtensions(filePath)).to.deep.equal([
'img',
'gz',
]);
});
});
describe('.getLastFileExtension()', function () {
it('should return undefined if the file path has no extension', function () {
expect(fileExtensions.getLastFileExtension('foo')).to.equal(null);
});
it('should return the extension if there is only one extension', function () {
expect(fileExtensions.getLastFileExtension('foo.img')).to.equal('img');
});
it('should return the last extension if there are two extensions', function () {
expect(fileExtensions.getLastFileExtension('foo.img.gz')).to.equal('gz');
});
it('should return the last extension if there are three extensions', function () {
expect(fileExtensions.getLastFileExtension('foo.bar.img.gz')).to.equal(
'gz',
);
});
});
describe('.getPenultimateFileExtension()', function () {
it('should return undefined in the file path has no extension', function () {
expect(fileExtensions.getPenultimateFileExtension('foo')).to.equal(null);
});
it('should return undefined if there is only one extension', function () {
expect(fileExtensions.getPenultimateFileExtension('foo.img')).to.equal(
null,
);
});
it('should return the penultimate extension if there are two extensions', function () {
expect(fileExtensions.getPenultimateFileExtension('foo.img.gz')).to.equal(
'img',
);
});
it('should return the penultimate extension if there are three extensions', function () {
expect(
fileExtensions.getPenultimateFileExtension('foo.bar.img.gz'),
).to.equal('img');
});
});
});

View File

@ -67,185 +67,6 @@ describe('Shared: SupportedFormats', function () {
});
});
describe('.isSupportedImage()', function () {
_.forEach(
[
// Type: 'archive'
'path/to/filename.zip',
'path/to/filename.etch',
// Type: 'compressed'
'path/to/filename.img.gz',
'path/to/filename.img.bz2',
'path/to/filename.img.xz',
// Type: 'image'
'path/to/filename.img',
'path/to/filename.iso',
'path/to/filename.dsk',
'path/to/filename.hddimg',
'path/to/filename.raw',
'path/to/filename.dmg',
'path/to/filename.sdcard',
'path/to/filename.wic',
],
(filename) => {
it(`should return true for ${filename}`, function () {
const isSupported = supportedFormats.isSupportedImage(filename);
expect(isSupported).to.be.true;
});
},
);
it('should return false if the file has no extension', function () {
const isSupported = supportedFormats.isSupportedImage('/path/to/foo');
expect(isSupported).to.be.false;
});
it('should return false if the extension is not included in .getAllExtensions()', function () {
const isSupported = supportedFormats.isSupportedImage('/path/to/foo.jpg');
expect(isSupported).to.be.false;
});
it('should return true if the extension is included in .getAllExtensions()', function () {
const nonCompressedExtension = _.first(
supportedFormats.getNonCompressedExtensions(),
);
const imagePath = `/path/to/foo.${nonCompressedExtension}`;
const isSupported = supportedFormats.isSupportedImage(imagePath);
expect(isSupported).to.be.true;
});
it('should ignore casing when determining extension validity', function () {
const nonCompressedExtension = _.first(
supportedFormats.getNonCompressedExtensions(),
);
const imagePath = `/path/to/foo.${_.toUpper(nonCompressedExtension)}`;
const isSupported = supportedFormats.isSupportedImage(imagePath);
expect(isSupported).to.be.true;
});
it('should not consider an extension before a non compressed extension', function () {
const nonCompressedExtension = _.first(
supportedFormats.getNonCompressedExtensions(),
);
const imagePath = `/path/to/foo.1234.${nonCompressedExtension}`;
const isSupported = supportedFormats.isSupportedImage(imagePath);
expect(isSupported).to.be.true;
});
it('should return true if the extension is supported and the file name includes dots', function () {
const nonCompressedExtension = _.first(
supportedFormats.getNonCompressedExtensions(),
);
const imagePath = `/path/to/foo.1.2.3-bar.${nonCompressedExtension}`;
const isSupported = supportedFormats.isSupportedImage(imagePath);
expect(isSupported).to.be.true;
});
it('should return true if the extension is only a supported archive extension', function () {
const archiveExtension = _.first(supportedFormats.getArchiveExtensions());
const imagePath = `/path/to/foo.${archiveExtension}`;
const isSupported = supportedFormats.isSupportedImage(imagePath);
expect(isSupported).to.be.true;
});
it('should return true if the extension is a supported one plus a supported compressed extensions', function () {
const nonCompressedExtension = _.first(
supportedFormats.getNonCompressedExtensions(),
);
const compressedExtension = _.first(
supportedFormats.getCompressedExtensions(),
);
const imagePath = `/path/to/foo.${nonCompressedExtension}.${compressedExtension}`;
const isSupported = supportedFormats.isSupportedImage(imagePath);
expect(isSupported).to.be.true;
});
it('should return false if the extension is an unsupported one plus a supported compressed extensions', function () {
const compressedExtension = _.first(
supportedFormats.getCompressedExtensions(),
);
const imagePath = `/path/to/foo.jpg.${compressedExtension}`;
const isSupported = supportedFormats.isSupportedImage(imagePath);
expect(isSupported).to.be.false;
});
it('should return false if the file has no extension', function () {
const isSupported = supportedFormats.isSupportedImage('/path/to/foo');
expect(isSupported).to.be.false;
});
it('should return false if the extension is not included in .getAllExtensions()', function () {
const isSupported = supportedFormats.isSupportedImage('/path/to/foo.jpg');
expect(isSupported).to.be.false;
});
it('should return true if the extension is included in .getAllExtensions()', function () {
const nonCompressedExtension = _.first(
supportedFormats.getNonCompressedExtensions(),
);
const imagePath = `/path/to/foo.${nonCompressedExtension}`;
const isSupported = supportedFormats.isSupportedImage(imagePath);
expect(isSupported).to.be.true;
});
it('should ignore casing when determining extension validity', function () {
const nonCompressedExtension = _.first(
supportedFormats.getNonCompressedExtensions(),
);
const imagePath = `/path/to/foo.${_.toUpper(nonCompressedExtension)}`;
const isSupported = supportedFormats.isSupportedImage(imagePath);
expect(isSupported).to.be.true;
});
it('should not consider an extension before a non compressed extension', function () {
const nonCompressedExtension = _.first(
supportedFormats.getNonCompressedExtensions(),
);
const imagePath = `/path/to/foo.1234.${nonCompressedExtension}`;
const isSupported = supportedFormats.isSupportedImage(imagePath);
expect(isSupported).to.be.true;
});
it('should return true if the extension is supported and the file name includes dots', function () {
const nonCompressedExtension = _.first(
supportedFormats.getNonCompressedExtensions(),
);
const imagePath = `/path/to/foo.1.2.3-bar.${nonCompressedExtension}`;
const isSupported = supportedFormats.isSupportedImage(imagePath);
expect(isSupported).to.be.true;
});
it('should return true if the extension is only a supported archive extension', function () {
const archiveExtension = _.first(supportedFormats.getArchiveExtensions());
const imagePath = `/path/to/foo.${archiveExtension}`;
const isSupported = supportedFormats.isSupportedImage(imagePath);
expect(isSupported).to.be.true;
});
it('should return true if the extension is a supported one plus a supported compressed extensions', function () {
const nonCompressedExtension = _.first(
supportedFormats.getNonCompressedExtensions(),
);
const compressedExtension = _.first(
supportedFormats.getCompressedExtensions(),
);
const imagePath = `/path/to/foo.${nonCompressedExtension}.${compressedExtension}`;
const isSupported = supportedFormats.isSupportedImage(imagePath);
expect(isSupported).to.be.true;
});
it('should return false if the extension is an unsupported one plus a supported compressed extensions', function () {
const compressedExtension = _.first(
supportedFormats.getCompressedExtensions(),
);
const imagePath = `/path/to/foo.jpg.${compressedExtension}`;
const isSupported = supportedFormats.isSupportedImage(imagePath);
expect(isSupported).to.be.false;
});
});
describe('.looksLikeWindowsImage()', function () {
_.each(
[