diff options
author | Kenta Moriuchi <moriken@kimamass.com> | 2023-04-15 05:23:28 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-14 22:23:28 +0200 |
commit | f086ec57b453fc0af763564eb80fea4b5b7f7296 (patch) | |
tree | 8c7800d918751893e4011eb86f23e8f687d4139b /ext/web/00_infra.js | |
parent | 136dce67cec749dce5989ea29e88359ef79a0045 (diff) |
fix(core): Use safe primordials wrappers (#18687)
Diffstat (limited to 'ext/web/00_infra.js')
-rw-r--r-- | ext/web/00_infra.js | 33 |
1 files changed, 27 insertions, 6 deletions
diff --git a/ext/web/00_infra.js b/ext/web/00_infra.js index efe7217de..e9d5dd48b 100644 --- a/ext/web/00_infra.js +++ b/ext/web/00_infra.js @@ -18,6 +18,7 @@ const { JSONStringify, NumberPrototypeToString, ObjectPrototypeIsPrototypeOf, + RegExpPrototypeTest, SafeArrayIterator, SafeRegExp, String, @@ -26,6 +27,7 @@ const { StringPrototypeMatch, StringPrototypePadStart, StringPrototypeReplace, + StringPrototypeReplaceAll, StringPrototypeSlice, StringPrototypeSubstring, StringPrototypeToLowerCase, @@ -274,17 +276,24 @@ function addPaddingToBase64url(base64url) { return base64url; } +const BASE64URL_PATTERN = new SafeRegExp(/^[-_A-Z0-9]*?={0,2}$/i); + /** * @param {string} base64url * @returns {string} */ function convertBase64urlToBase64(base64url) { - if (!/^[-_A-Z0-9]*?={0,2}$/i.test(base64url)) { + if (!RegExpPrototypeTest(BASE64URL_PATTERN, base64url)) { // Contains characters not part of base64url spec. throw new TypeError("Failed to decode base64url: invalid character"); } - return addPaddingToBase64url(base64url).replace(/\-/g, "+").replace( - /_/g, + return StringPrototypeReplaceAll( + StringPrototypeReplaceAll( + addPaddingToBase64url(base64url), + "-", + "+", + ), + "_", "/", ); } @@ -295,9 +304,21 @@ function convertBase64urlToBase64(base64url) { * @returns {string} */ function forgivingBase64UrlEncode(data) { - return forgivingBase64Encode( - typeof data === "string" ? new TextEncoder().encode(data) : data, - ).replace(/=/g, "").replace(/\+/g, "-").replace(/\//g, "_"); + return StringPrototypeReplaceAll( + StringPrototypeReplaceAll( + StringPrototypeReplaceAll( + forgivingBase64Encode( + typeof data === "string" ? new TextEncoder().encode(data) : data, + ), + "=", + "", + ), + "+", + "-", + ), + "/", + "_", + ); } /** |