Skip to content
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

Playground: Incorrect Handling of Non-Latin1 Characters Causes Encoding Error in Share Functionality #4024

Closed
4 tasks done
orange-guo opened this issue Jan 5, 2024 · 1 comment

Comments

@orange-guo
Copy link
Contributor

Prerequisites

What theme are you using?

core

Version

5.x

Current Behavior

No response

Expected Behavior

No error occurred.

Steps To Reproduce

image

In the playground, paste the following JSON into the JSONSchema editor:

{
  "title": "你好",
  "type": "string"
}

After clicking the Share button, the Chrome console displays the following error:

index-83d04ae3.js:1221 DOMException: Failed to execute 'btoa' on 'Window': The string to be encoded contains characters outside of the Latin1 range.
    at https://rjsf-team.github.io/react-jsonschema-form/assets/index-83d04ae3.js:1221:73205
    at Object.k4e (https://rjsf-team.github.io/react-jsonschema-form/assets/index-83d04ae3.js:37:9903)
    at I4e (https://rjsf-team.github.io/react-jsonschema-form/assets/index-83d04ae3.js:37:10060)
    at R4e (https://rjsf-team.github.io/react-jsonschema-form/assets/index-83d04ae3.js:37:10120)
    at dre (https://rjsf-team.github.io/react-jsonschema-form/assets/index-83d04ae3.js:37:31714)
    at p0e (https://rjsf-team.github.io/react-jsonschema-form/assets/index-83d04ae3.js:37:32134)
    at https://rjsf-team.github.io/react-jsonschema-form/assets/index-83d04ae3.js:37:37097
    at Wq (https://rjsf-team.github.io/react-jsonschema-form/assets/index-83d04ae3.js:40:37096)
    at Age (https://rjsf-team.github.io/react-jsonschema-form/assets/index-83d04ae3.js:37:9034)
    at H3 (https://rjsf-team.github.io/react-jsonschema-form/assets/index-83d04ae3.js:37:33430)
(anonymous) @ index-83d04ae3.js:1221

This error occurs in the Chrome console due to the attempted base64 encoding using 'btoa', which fails when encountering characters outside the Latin1 range, such as those in the Chinese language.

This issue arises because 'btoa' is limited to encoding Latin1 characters and cannot handle characters from non-Latin1 languages, resulting in the displayed error message.

Environment

- OS: Ubuntu 23.10
- Node: v20.0.0
- npm:

Anything else?

To enhance the sharing functionality in the playground, it's recommended to switch the text encoding from Latin1 to UTF-8. This change will expand character support, enabling encoding for a broader range of characters, including Asian languages like Chinese and Japanese, by utilizing the UTF-8 encoding standard.

To facilitate this modification, adjustments are required in two sections: the load method within Playground.tsx and the implementation of the share button in onShare within Header.tsx.

Replace references to atob and btoa with the following functions:

function encode64(text: string): string {
  return btoa(String.fromCharCode(...new TextEncoder().encode(text)))
}

function decode64(text: string): string {
  return new TextDecoder().decode(Uint8Array.from(atob(text), (c) => c.charCodeAt(0)))
}

If needed, I can submit a PR later to enhance this functionality.

@orange-guo orange-guo added bug needs triage Initial label given, to be assigned correct labels and assigned labels Jan 5, 2024
@nickgros
Copy link
Contributor

nickgros commented Jan 5, 2024

@orange-guo Same issue as #3698. We'd appreciate a PR as long as it doesn't break existing playground links!

@nickgros nickgros added playground and removed needs triage Initial label given, to be assigned correct labels and assigned labels Jan 5, 2024
orange-guo added a commit to orange-guo/react-jsonschema-form that referenced this issue Jan 9, 2024
… encoding and decoding functions using the utf-8 charset to support the characters outside the latin1 range. (rjsf-team#4024)
orange-guo added a commit to orange-guo/react-jsonschema-form that referenced this issue Jan 9, 2024
orange-guo added a commit to orange-guo/react-jsonschema-form that referenced this issue Jan 9, 2024
orange-guo added a commit to orange-guo/react-jsonschema-form that referenced this issue Jan 9, 2024
orange-guo added a commit to orange-guo/react-jsonschema-form that referenced this issue Jan 9, 2024
orange-guo added a commit to orange-guo/react-jsonschema-form that referenced this issue Jan 9, 2024
orange-guo added a commit to orange-guo/react-jsonschema-form that referenced this issue Jan 9, 2024
orange-guo added a commit to orange-guo/react-jsonschema-form that referenced this issue Jan 11, 2024
orange-guo added a commit to orange-guo/react-jsonschema-form that referenced this issue Jan 11, 2024
orange-guo added a commit to orange-guo/react-jsonschema-form that referenced this issue Jan 17, 2024
heath-freenome pushed a commit that referenced this issue Jan 18, 2024
…charset to support the characters outside the latin1 range. (#4024) (#4034)

* Enhancement(Share): Declare and export an object that provides base64 encoding and decoding functions using the utf-8 charset to support the characters outside the latin1 range. (#4024)

* Enhancement(Share): Add the 'base64.test.ts' to test the base64. (#4024)

* Enhancement(Share): Update the base64 reference in 'Playground' to the new customized base64 in 'utils' (#4024).

* Enhancement(Share): Update 'CHANGELOG.md' (#4024).

* Enhancement(Share): Update 'CHANGELOG.md' (#4024).

* Enhancement(Share): Add test to test the platform behavior (#4024).

* Enhancement: Fix comments and updating utility-functions.md to add introduction of base64 object

* Update CHANGELOG.md

* Update CHANGELOG.md

* Update base64.ts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants