wri / gfw

Global Forest Watch: An online, global, near-real time forest monitoring tool
https://www.globalforestwatch.org
MIT License
269 stars 92 forks source link

[FLAG-923] Pasting a special character into search by location lead to an error occurred #4836

Closed SARodrigues closed 2 weeks ago

SARodrigues commented 1 month ago

Overview

The FE makes use of btoa() to Base64 encode parameters for the URL. However, this method fails when strings include characters that take more than one byte, which is the case of "Bình Thuận", the string that when searched caused the FE to crash.

This is known as the Unicode Problem.

The MDN Web Docs offer a solution, which passes by first encoding the string with TextEncoder (and later using the TextDecoder counterpart to reverse it), which is what this PR implements.

I've created a separate util (utils/url-param.js) exclusive for this processing, with comments so that future developers are aware of this issue. I've chosen not to add these methods to utils/url.js as that file contains extra processing, including functions to remap legacy datasets, and I feel like the objective of these new very specific functions would not be clear.

Testing

  1. Visit https://gfw-staging-pr-4836.herokuapp.com/map
  2. On the sidebar, click "Search"
  3. Fill in "Bình Thuận" in the search box
  4. Verify that the application does not crash

Tracking

FLAG-923