Skip to content

Commit 473949d

Browse files
authored
fix: remove base64 validation due to validation inefficiency (#1543)
1 parent ee73a88 commit 473949d

File tree

2 files changed

+6
-38
lines changed

2 files changed

+6
-38
lines changed

src/ParseFile.js

+3-29
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,6 @@ export type FileSource =
4242
type: string,
4343
};
4444

45-
const base64Regex = new RegExp(
46-
'([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/][AQgw]==)|([0-9a-zA-Z+/]{2}[AEIMQUYcgkosw048]=)|([0-9a-zA-Z+/]{4}))',
47-
'i'
48-
);
49-
50-
const dataUriRegex = new RegExp(
51-
`^data:([a-zA-Z]+\\/[-a-zA-Z0-9+.]+(;[a-z-]+=[a-zA-Z0-9+.-]+)?)?(;base64)?,(${base64Regex.source})*$`,
52-
'i'
53-
);
54-
5545
function b64Digit(number: number): string {
5646
if (number < 26) {
5747
return String.fromCharCode(65 + number);
@@ -145,29 +135,13 @@ class ParseFile {
145135
type: specifiedType,
146136
};
147137
} else if (data && typeof data.base64 === 'string') {
148-
// Check if data URI or base64 string is valid
149-
const validationRegex = new RegExp(base64Regex.source + '|' + dataUriRegex.source, 'i');
150-
if (!validationRegex.test(data.base64)) {
151-
throw new Error(
152-
'Cannot create a Parse.File without valid data URIs or base64 encoded data.'
153-
);
154-
}
155-
156138
const base64 = data.base64.split(',').slice(-1)[0];
157-
let type =
158-
specifiedType || data.base64.split(';').slice(0, 1)[0].split(':').slice(1, 2)[0] || '';
159-
160-
// https://tools.ietf.org/html/rfc2397
161-
// If <mediatype> is omitted, it defaults to text/plain;charset=US-ASCII.
162-
// As a shorthand, "text/plain" can be omitted but the charset parameter supplied.
163-
if (dataUriRegex.test(data.base64)) {
164-
type = type || 'text/plain';
165-
}
166-
139+
const dataType =
140+
specifiedType || data.base64.split(';').slice(0, 1)[0].split(':').slice(1, 2)[0] || 'text/plain';
167141
this._source = {
168142
format: 'base64',
169143
base64,
170-
type,
144+
type: dataType,
171145
};
172146
} else {
173147
throw new TypeError('Cannot create a Parse.File with that data.');

src/__tests__/ParseFile-test.js

+3-9
Original file line numberDiff line numberDiff line change
@@ -63,19 +63,19 @@ describe('ParseFile', () => {
6363
it('can create files with base64 encoding (no padding)', () => {
6464
const file = new ParseFile('parse.txt', { base64: 'YWJj' });
6565
expect(file._source.base64).toBe('YWJj');
66-
expect(file._source.type).toBe('');
66+
expect(file._source.type).toBe('text/plain');
6767
});
6868

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

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

8181
it('can set the default type to be text/plain when using base64', () => {
@@ -164,12 +164,6 @@ describe('ParseFile', () => {
164164
expect(function () {
165165
new ParseFile('parse.txt', 'string');
166166
}).toThrow('Cannot create a Parse.File with that data.');
167-
168-
expect(function () {
169-
new ParseFile('parse.txt', {
170-
base64: 'abc',
171-
});
172-
}).toThrow('Cannot create a Parse.File without valid data URIs or base64 encoded data.');
173167
});
174168

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

0 commit comments

Comments
 (0)