Repository: parse-community/Parse-SDK-JS
Pull request #1543 (2022-09-11 04:45:47)

fix: remove base64 validation due to validation inefficiency
### New Pull Request Checklist
<!--
    Check every following box [x] before submitting your PR.
    Click the "Preview" tab for better readability.
    Thanks for contributing to Parse Platform!
-->

- [x] I am not disclosing a [vulnerability](https://github.com/parse-community/parse-server/blob/master/SECURITY.md).
- [x] I am creating this PR in reference to an [issue](https://github.com/parse-community/Parse-SDK-JS/issues?q=is%3Aissue).

### Issue Description
<!-- Add a brief description of the issue this PR solves. -->

RegExp validation of base64 can take a long time to resolve

Related issue: #1532

### Approach
<!-- Add a description of the approach in this PR. -->

Removes client side base64 validation.

### TODOs before merging
<!--
    Add TODOs that need to be completed before merging this PR.
    Delete TODOs that do not apply to this PR.
-->

- [x] Add tests

parse-github-assistant[bot]: 
### Thanks for opening this pull request!

- 🎉 We are excited about your hands-on contribution!

<!-- parse-issue-bot-meta-tag-id {"suggestPr":false,"thanksFeature":false,"thanksPr":true} -->


codecov[bot]: # [Codecov](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/1543?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=parse-community) Report
> Merging [#1543](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/1543?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=parse-community) (2d93190) into [alpha](https://codecov.io/gh/parse-community/Parse-SDK-JS/commit/89fd5ec6a8e210de3946434c6c88d6de87b6635c?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=parse-community) (89fd5ec) will **decrease** coverage by `0.00%`.
> The diff coverage is `100.00%`.

```diff
@@            Coverage Diff             @@
##            alpha    #1543      +/-   ##
==========================================
- Coverage   99.94%   99.94%   -0.01%     
==========================================
  Files          61       61              
  Lines        5961     5954       -7     
  Branches     1362     1359       -3     
==========================================
- Hits         5958     5951       -7     
  Misses          3        3              
```


| [Impacted Files](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/1543?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=parse-community) | Coverage Δ | |
|---|---|---|
| [src/ParseFile.js](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/1543/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=parse-community#diff-c3JjL1BhcnNlRmlsZS5qcw==) | `100.00% <100.00%> (ø)` | |

Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=parse-community). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=parse-community)

mtrezza: @FransGH, @samateja Removing the regex validation would address both of your issues, correct?
FransGH: @mtrezza I've worked around the issue on the client side but yes, removing the regex would restore the original functionality and resolve the issue.

Patch:
diff --git a/src/ParseFile.js b/src/ParseFile.js
index 4498456..197d8c0 100644
--- a/src/ParseFile.js
+++ b/src/ParseFile.js
@@ -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);
@@ -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.');


Test patch:
diff --git a/src/__tests__/ParseFile-test.js b/src/__tests__/ParseFile-test.js
index 31bfb7c..caab362 100644
--- a/src/__tests__/ParseFile-test.js
+++ b/src/__tests__/ParseFile-test.js
@@ -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', () => {
@@ -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', () => {