Skip to content

Commit

Permalink
fix: remove base64 validation due to validation inefficiency (#1543)
Browse files Browse the repository at this point in the history
  • Loading branch information
dblythy authored Sep 12, 2022
1 parent ee73a88 commit 473949d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 38 deletions.
32 changes: 3 additions & 29 deletions src/ParseFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,6 @@ export type FileSource =
type: string,
};

const base64Regex = new RegExp(
'([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/][AQgw]==)|([0-9a-zA-Z+/]{2}[AEIMQUYcgkosw048]=)|([0-9a-zA-Z+/]{4}))',
'i'
);

const dataUriRegex = new RegExp(
`^data:([a-zA-Z]+\\/[-a-zA-Z0-9+.]+(;[a-z-]+=[a-zA-Z0-9+.-]+)?)?(;base64)?,(${base64Regex.source})*$`,
'i'
);

function b64Digit(number: number): string {
if (number < 26) {
return String.fromCharCode(65 + number);
Expand Down Expand Up @@ -145,29 +135,13 @@ class ParseFile {
type: specifiedType,
};
} else if (data && typeof data.base64 === 'string') {
// Check if data URI or base64 string is valid
const validationRegex = new RegExp(base64Regex.source + '|' + dataUriRegex.source, 'i');
if (!validationRegex.test(data.base64)) {
throw new Error(
'Cannot create a Parse.File without valid data URIs or base64 encoded data.'
);
}

const base64 = data.base64.split(',').slice(-1)[0];
let type =
specifiedType || data.base64.split(';').slice(0, 1)[0].split(':').slice(1, 2)[0] || '';

// https://tools.ietf.org/html/rfc2397
// If <mediatype> is omitted, it defaults to text/plain;charset=US-ASCII.
// As a shorthand, "text/plain" can be omitted but the charset parameter supplied.
if (dataUriRegex.test(data.base64)) {
type = type || 'text/plain';
}

const dataType =
specifiedType || data.base64.split(';').slice(0, 1)[0].split(':').slice(1, 2)[0] || 'text/plain';
this._source = {
format: 'base64',
base64,
type,
type: dataType,
};
} else {
throw new TypeError('Cannot create a Parse.File with that data.');
Expand Down
12 changes: 3 additions & 9 deletions src/__tests__/ParseFile-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,19 @@ describe('ParseFile', () => {
it('can create files with base64 encoding (no padding)', () => {
const file = new ParseFile('parse.txt', { base64: 'YWJj' });
expect(file._source.base64).toBe('YWJj');
expect(file._source.type).toBe('');
expect(file._source.type).toBe('text/plain');
});

it('can create files with base64 encoding (1 padding)', () => {
const file = new ParseFile('parse.txt', { base64: 'YWI=' });
expect(file._source.base64).toBe('YWI=');
expect(file._source.type).toBe('');
expect(file._source.type).toBe('text/plain');
});

it('can create files with base64 encoding (2 padding)', () => {
const file = new ParseFile('parse.txt', { base64: 'ParseA==' });
expect(file._source.base64).toBe('ParseA==');
expect(file._source.type).toBe('');
expect(file._source.type).toBe('text/plain');
});

it('can set the default type to be text/plain when using base64', () => {
Expand Down Expand Up @@ -164,12 +164,6 @@ describe('ParseFile', () => {
expect(function () {
new ParseFile('parse.txt', 'string');
}).toThrow('Cannot create a Parse.File with that data.');

expect(function () {
new ParseFile('parse.txt', {
base64: 'abc',
});
}).toThrow('Cannot create a Parse.File without valid data URIs or base64 encoded data.');
});

it('throws with invalid base64', () => {
Expand Down

0 comments on commit 473949d

Please sign in to comment.