Skip to content

Creating a Parse.File with base64 string fails #1510

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
4 tasks done
Fouadktb opened this issue Jul 14, 2022 · 10 comments · Fixed by #1517
Closed
4 tasks done

Creating a Parse.File with base64 string fails #1510

Fouadktb opened this issue Jul 14, 2022 · 10 comments · Fixed by #1517
Labels
type:bug Impaired feature or lacking behavior that is likely assumed

Comments

@Fouadktb
Copy link

Fouadktb commented Jul 14, 2022

New Issue Checklist

Issue Description

When Calling new Parse.File with the versions 3.4.1 upwards the process takes a few mins then actually throws an error saying
"Cannot create a Parse.File without valid data URIs or base64 encoded data."

Steps to reproduce

Calling Parse.file with the file blob will reproduce the issue

Actual Outcome

an error gets thrown saying: "Cannot create a Parse.File without valid data URIs or base64 encoded data."

Expected Outcome

getting a Parse File back

Environment

not a server issue but =>

Server

  • Parse Server version: n/a
  • Operating system: n/a
  • Local or remote host (AWS, Azure, Google Cloud, Heroku, Digital Ocean, etc): n/a

Database

  • System (MongoDB or Postgres): n/a
  • Database version: n/a
  • Local or remote host (MongoDB Atlas, mLab, AWS, Azure, Google Cloud, etc): n/a

Client

  • Parse JS SDK version: 3.4.3

Logs

Screenshot 2022-07-14 at 15 29 26

@parse-github-assistant
Copy link

parse-github-assistant bot commented Jul 14, 2022

Thanks for opening this issue!

  • 🚀 You can help us to fix this issue faster by opening a pull request with a failing test. See our Contribution Guide for how to make a pull request, or read our New Contributor's Guide if this is your first time contributing.

@mtrezza
Copy link
Member

mtrezza commented Jul 15, 2022

Could you submit a PR with a failing test to reproduce the issue?

@mtrezza mtrezza added the type:bug Impaired feature or lacking behavior that is likely assumed label Jul 15, 2022
@dblythy
Copy link
Member

dblythy commented Jul 26, 2022

It seems this is the issue that is causing the issues here. Trying to save a Parse File with a base64 string of SGVsbG8gV29ybGQh (Hello World!) fails the regex.

Failing test can be as simple as:

it('can create basic files', async () => {
    const buffer =  Buffer.from('Hello World!');
    const file = new ParseFile('hello.txt', {base64: buffer.toString('base64')}, 'text/plain');  // fails here
    expect(file._source.base64).toBe('SGVsbG8gV29ybGQh');
    expect(file._source.type).toBe('text/plain');
})

The problematic regex is:

/([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))|^data:([a-zA-Z]+\/[-a-zA-Z0-9+.]+(;[a-z-]+=[a-zA-Z0-9+.-]+)?)?(;base64)?,(([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=)))*$/i

@mtrezza
Copy link
Member

mtrezza commented Jul 26, 2022

Did you find out what the problem is with the regex?

@justinlettau
Copy link
Contributor

justinlettau commented Jul 27, 2022

@mtrezza Here is a PR with failing unit test: #1517

I believe the issue is related to the required padding characters = in the regex. I have an example here to demonstrate.

@mtrezza mtrezza linked a pull request Jul 28, 2022 that will close this issue
3 tasks
@mtrezza
Copy link
Member

mtrezza commented Jul 28, 2022

It seems you are right, but the regex in your example does not seem correct, because we'd still strictly expect padding when encoding a 16 bit group. The issue with the current regex seems to be that it doesn't expect to encode a string that is exactly a multiple of 3:

([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))

The fixed regex that accounts for that would be:

([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=)|([0-9a-zA-Z+/]{4}))

If we want to be even stricter, we can use the following, because padding assumes a defined set of prefixes:

([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/][AQgw]==)|([0-9a-zA-Z+/]{2}[AEIMQUYcgkosw048]=)|([0-9a-zA-Z+/]{4}))

I think we should also make sure that we have tests which encode a string:

  • without padding (abc = YWJj)
  • with 1 padding (ab -> YWI=)
  • with 2 padding (a -> YQ==)

If you could add the regex fix to your PR and add the tests, we should be able to close this issue.

@justinlettau
Copy link
Contributor

@mtrezza Regex updated and unit tests added here, thanks.

@mtrezza
Copy link
Member

mtrezza commented Sep 27, 2022

@DennisVanAcker1 Please make sure you are using the Parse JS SDK version which contains the fix, see #1543. If the issue still exists despite you using the alpha version, please open a new issue.

@DennisVanAcker1
Copy link

Will do! thanks again, should have looked into this abit more before posting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:bug Impaired feature or lacking behavior that is likely assumed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants