Repository: parse-community/Parse-SDK-JS
Pull request #1517 (2022-07-27 19:05:21)

fix: creating a Parse.File with base64 string fails for some encodings
### 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. -->

Related issue: #1510

### Approach

Adding a failing unit test for reproducing base64 regex issue.

### 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/1517?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=parse-community) Report
> Merging [#1517](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/1517?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=parse-community) (f36f353) into [alpha](https://codecov.io/gh/parse-community/Parse-SDK-JS/commit/31cf433eddc990f3ea8f563a358678c739cb6421?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=parse-community) (31cf433) will **not change** coverage.
> The diff coverage is `n/a`.

> :exclamation: Current head f36f353 differs from pull request most recent head ddd8b6c. Consider uploading reports for the commit ddd8b6c to get more accurate results

```diff
@@           Coverage Diff           @@
##            alpha    #1517   +/-   ##
=======================================
  Coverage   99.94%   99.94%           
=======================================
  Files          61       61           
  Lines        5955     5955           
  Branches     1358     1358           
=======================================
  Hits         5952     5952           
  Misses          3        3           
```


| [Impacted Files](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/1517?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/1517/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% <ø> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/1517?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=parse-community).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=parse-community)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/1517?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=parse-community). Last update [fc79281...ddd8b6c](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/1517?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=parse-community). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=parse-community).

mtrezza: off-topic: @dblythy it's interesting that the CI passes here

Patch:
diff --git a/src/ParseFile.js b/src/ParseFile.js
index 0cc4e7d..4498456 100644
--- a/src/ParseFile.js
+++ b/src/ParseFile.js
@@ -43,7 +43,7 @@ export type FileSource =
     };
 
 const base64Regex = new RegExp(
-  '([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))',
+  '([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/][AQgw]==)|([0-9a-zA-Z+/]{2}[AEIMQUYcgkosw048]=)|([0-9a-zA-Z+/]{4}))',
   'i'
 );
 


Test patch:
diff --git a/src/__tests__/ParseFile-test.js b/src/__tests__/ParseFile-test.js
index fb045db..31bfb7c 100644
--- a/src/__tests__/ParseFile-test.js
+++ b/src/__tests__/ParseFile-test.js
@@ -60,7 +60,19 @@ describe('ParseFile', () => {
     process.env.PARSE_BUILD = 'node';
   });
 
-  it('can create files with base64 encoding', () => {
+  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('');
+  });
+
+  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('');
+  });
+
+  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('');