From e86a021b8e8e17ddc5ab89399236a6649f24c19b Mon Sep 17 00:00:00 2001 From: Davis Mosenkovs Date: Wed, 12 Mar 2025 23:52:24 +0200 Subject: [PATCH] Use RFC 4648 s.5 URL-safe Base64 for URL encoding Switch to RFC 4648 Base 64 Encoding with URL and Filename Safe Alphabet. https://en.wikipedia.org/wiki/Base64#Variants_summary_table https://datatracker.ietf.org/doc/html/rfc4648#section-5 Before this change certain Mopidy backend URIs when encoded contained forward slashes. The forward slashes prevented those URIs from being loaded. --- src/js/util/format.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/js/util/format.js b/src/js/util/format.js index 1ee5080f..55f0aad9 100755 --- a/src/js/util/format.js +++ b/src/js/util/format.js @@ -317,7 +317,7 @@ const formatSimpleObjects = function (records = []) { */ const encodeUri = (rawUri = '') => { try { - return btoa(unescape(encodeURIComponent(rawUri))); + return btoa(unescape(encodeURIComponent(rawUri))).replaceAll('+', '-').replaceAll('/', '_'); } catch { console.error('Failed to encode', rawUri); return null; @@ -329,6 +329,7 @@ const encodeUri = (rawUri = '') => { * * Why base64? Some Mopidy backends have varying encoding styles, and handling all of these became * unweildy. Instead we take whatever Mopidy gives us, convert to binary, and then base64 it. + * We use RFC 4648 Base 64 Encoding with URL and Filename Safe Alphabet. * * Even when a URI is not URIEncoded, the browser will do some of it's own encoding/decoding which * we can avoid by giving a base64 string. @@ -336,7 +337,7 @@ const encodeUri = (rawUri = '') => { */ const decodeUri = (rawUri = '') => { try { - return decodeURIComponent(escape(atob(rawUri))); + return decodeURIComponent(escape(atob(rawUri.replaceAll('-', '+').replaceAll('_', '/')))); } catch { console.error('Failed to decode', rawUri); return null;