Skip to content
Myr

fix(utils/url): prevent double encoding in `safeEncodeURI` (#4306)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Yusuke Wada · committed by GitHub · parent 28f981a · Browse files · 2 files+15 added−3 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+15 added−3 removed

  • tests:+10 added−0 removed
  • other:+5 added−3 removed

2 files+15 added−3 removed

modified:src/utils/url.test.ts+10 added
317 unmodified lines
318 expect(319 safeEncodeURI('https://example.com/%E3%81%93%E3%82%93%E3%81%AB%E3%81%A1%E3%81%AF?abc')320 ).toBe('https://example.com/%E3%81%93%E3%82%93%E3%81%AB%E3%81%A1%E3%81%AF?abc')321 })322323 it('Should be the same strings', async () => {324 expect(325 safeEncodeURI(326 'https://login.microsoftonline.com/redacted-id&scope=email%20openid%20profile%20offline_access&redirect_uri=https%3A%2F%2Fredacted-domain.org%2Fapi%2Fauth%2Fcallback&client-request-id=redacted-id&response_mode=query&client_info=1&x-client-SKU=msal.js.node&x-client-VER=3.6.4&x-client-OS=linux&x-client-CPU=x64&response_type=code&code_challenge=7C75MO8zdoJZn7_sIpxcFEQzPvzWIMzD2tUf-lqevR0&code_challenge_method=S256'327 )328 ).toBe(329 'https://login.microsoftonline.com/redacted-id&scope=email%20openid%20profile%20offline_access&redirect_uri=https%3A%2F%2Fredacted-domain.org%2Fapi%2Fauth%2Fcallback&client-request-id=redacted-id&response_mode=query&client_info=1&x-client-SKU=msal.js.node&x-client-VER=3.6.4&x-client-OS=linux&x-client-CPU=x64&response_type=code&code_challenge=7C75MO8zdoJZn7_sIpxcFEQzPvzWIMzD2tUf-lqevR0&code_challenge_method=S256'330 )331 })332 })333})
modified:src/utils/url.ts+5 added-3 removed
316 unmodified lines
317 * The input string may or may not escaped by encodeURI.318 */319export const safeEncodeURI = (str: string): string => {320 try {321 // decodeURI will throw if str contains %322 // In that case, we can assume that str is not encoded323 return encodeURI(decodeURI(str))321 // Try to decode the string322 const decoded = decodeURI(str)323 // If successful, encode only if it's different from the original324 return decoded === str ? encodeURI(str) : str325 } catch (e) {326 if (e instanceof URIError) {327 // If decoding fails, the string is not encoded, so encode it328 return encodeURI(str)329 }330 throw e331 }
1 unmodified line