From df2adb203c423dbcbd8672705027dfd1eb868053 Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Sun, 29 Jan 2023 10:01:19 +0000 Subject: [PATCH] Fix Uncaught DOMException: Failed to execute 'atob' on 'Window' There is a missing import within the `uint8-to-base64` javascript package which assumes that `atob` and `btoa` are present and exported instead of using the `window.atob` and `window.btoa` functions. This previously worked but as far as I can see things have become more strict and this no longer works. The dependency is small and I do not believe that we gain much from having this code as an external dependency. I think instead we should just consume this dependency and bring the code directly into Gitea itself - the code is itself just some standard incantation for creating base64 arrays in javascript. Therefore this PR simply removes the dependency on `uint8-to-base64` and rewrites the functions used in it. Fix #22507 Signed-off-by: Andrew Thornton --- package-lock.json | 11 ---------- package.json | 1 - web_src/js/features/user-auth-webauthn.js | 25 ++++++++++++++++------- 3 files changed, 18 insertions(+), 19 deletions(-) diff --git a/package-lock.json b/package-lock.json index 60f0a0f8e6235..50dddde13f897 100644 --- a/package-lock.json +++ b/package-lock.json @@ -39,7 +39,6 @@ "swagger-ui-dist": "4.15.5", "tippy.js": "6.3.7", "tributejs": "5.1.3", - "uint8-to-base64": "0.2.0", "vue": "3.2.45", "vue-bar-graph": "2.0.0", "vue-loader": "17.0.1", @@ -8856,11 +8855,6 @@ "integrity": "sha512-boAm74ubXHY7KJQZLlXrtMz52qFvpsbOxDcZOnw/Wf+LS4Mmyu7JxmzD4tDLtUQtmZECypJ0FrCz4QIe6dvKRA==", "dev": true }, - "node_modules/uint8-to-base64": { - "version": "0.2.0", - "resolved": "https://registry.npmjs.org/uint8-to-base64/-/uint8-to-base64-0.2.0.tgz", - "integrity": "sha512-r13jrghEYZAN99GeYpEjM107DOxqB65enskpwce8rRHVAGEtaWmsF5GqoGdPMf8DIXc9XyAJTdvlvRZi4LsszA==" - }, "node_modules/unbox-primitive": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/unbox-primitive/-/unbox-primitive-1.0.2.tgz", @@ -16366,11 +16360,6 @@ "integrity": "sha512-boAm74ubXHY7KJQZLlXrtMz52qFvpsbOxDcZOnw/Wf+LS4Mmyu7JxmzD4tDLtUQtmZECypJ0FrCz4QIe6dvKRA==", "dev": true }, - "uint8-to-base64": { - "version": "0.2.0", - "resolved": "https://registry.npmjs.org/uint8-to-base64/-/uint8-to-base64-0.2.0.tgz", - "integrity": "sha512-r13jrghEYZAN99GeYpEjM107DOxqB65enskpwce8rRHVAGEtaWmsF5GqoGdPMf8DIXc9XyAJTdvlvRZi4LsszA==" - }, "unbox-primitive": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/unbox-primitive/-/unbox-primitive-1.0.2.tgz", diff --git a/package.json b/package.json index 842804211cf86..7821fbb580054 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,6 @@ "swagger-ui-dist": "4.15.5", "tippy.js": "6.3.7", "tributejs": "5.1.3", - "uint8-to-base64": "0.2.0", "vue": "3.2.45", "vue-bar-graph": "2.0.0", "vue-loader": "17.0.1", diff --git a/web_src/js/features/user-auth-webauthn.js b/web_src/js/features/user-auth-webauthn.js index f11a49864dedc..c86f6314edf55 100644 --- a/web_src/js/features/user-auth-webauthn.js +++ b/web_src/js/features/user-auth-webauthn.js @@ -1,8 +1,19 @@ import $ from 'jquery'; -import {encode, decode} from 'uint8-to-base64'; const {appSubUrl, csrfToken} = window.config; +function encodeToBase64(toEncode) { + const output = []; + for (let i = 0; i < toEncode.length; i++) { + output.push(String.fromCharCode(toEncode[i])); + } + return window.btoa(output.join('')); +} + +function decodeFromBase64(toDecode) { + return Uint8Array.from(window.atob(toDecode), (c) => c.charCodeAt(0)); +} + export function initUserAuthWebAuthn() { if ($('.user.signin.webauthn-prompt').length === 0) { return; @@ -14,9 +25,9 @@ export function initUserAuthWebAuthn() { $.getJSON(`${appSubUrl}/user/webauthn/assertion`, {}) .done((makeAssertionOptions) => { - makeAssertionOptions.publicKey.challenge = decode(makeAssertionOptions.publicKey.challenge); + makeAssertionOptions.publicKey.challenge = decodeFromBase64(makeAssertionOptions.publicKey.challenge); for (let i = 0; i < makeAssertionOptions.publicKey.allowCredentials.length; i++) { - makeAssertionOptions.publicKey.allowCredentials[i].id = decode(makeAssertionOptions.publicKey.allowCredentials[i].id); + makeAssertionOptions.publicKey.allowCredentials[i].id = decodeFromBase64(makeAssertionOptions.publicKey.allowCredentials[i].id); } navigator.credentials.get({ publicKey: makeAssertionOptions.publicKey @@ -87,7 +98,7 @@ function verifyAssertion(assertedCredential) { // Encode an ArrayBuffer into a base64 string. function bufferEncode(value) { - return encode(value) + return encodeToBase64(value) .replace(/\+/g, '-') .replace(/\//g, '_') .replace(/=/g, ''); @@ -184,11 +195,11 @@ function webAuthnRegisterRequest() { }).done((makeCredentialOptions) => { $('#nickname').closest('div.field').removeClass('error'); - makeCredentialOptions.publicKey.challenge = decode(makeCredentialOptions.publicKey.challenge); - makeCredentialOptions.publicKey.user.id = decode(makeCredentialOptions.publicKey.user.id); + makeCredentialOptions.publicKey.challenge = decodeFromBase64(makeCredentialOptions.publicKey.challenge); + makeCredentialOptions.publicKey.user.id = decodeFromBase64(makeCredentialOptions.publicKey.user.id); if (makeCredentialOptions.publicKey.excludeCredentials) { for (let i = 0; i < makeCredentialOptions.publicKey.excludeCredentials.length; i++) { - makeCredentialOptions.publicKey.excludeCredentials[i].id = decode(makeCredentialOptions.publicKey.excludeCredentials[i].id); + makeCredentialOptions.publicKey.excludeCredentials[i].id = decodeFromBase64(makeCredentialOptions.publicKey.excludeCredentials[i].id); } }