From 87c2992107c075bbac17eaecec5c22a9e99aaf39 Mon Sep 17 00:00:00 2001
From: Arie Peterson <arie@greenhost.nl>
Date: Fri, 16 Feb 2024 16:02:16 +0100
Subject: [PATCH] Load webauthn javascript only when necessary

Also prevent javascript alerts from the code generated by kratos, and
show the alert in our message box instead.
---
 backend/web/static/base.js      | 14 +++++++++++---
 backend/web/templates/base.html |  1 -
 2 files changed, 11 insertions(+), 4 deletions(-)

diff --git a/backend/web/static/base.js b/backend/web/static/base.js
index 129a1e7f..9660ed5c 100644
--- a/backend/web/static/base.js
+++ b/backend/web/static/base.js
@@ -21,7 +21,7 @@
 var dashboard_url = "";
 
 // Render a message by appending the data to the messages box. The message id is
-// availble, potentially for future translations/locale handling
+// available, potentially for future translations/locale handling
 // @param string id        Message ID\
 // @param string message   Message in the default language (English)
 // @param string type      Type of message, currently only "error" renders in
@@ -402,8 +402,11 @@ function getFormElement(node, context) {
 	console.log("Getting form element", node);
 
 	if (node.attributes.node_type == "script") {
-		window.console.log("Skipping because node_type is script");
-		return '';
+		return (
+			`<script src="` +
+			node.attributes.src +
+			`" defer>`
+		);
 	}
 
 	if (node.type == "img") {
@@ -697,3 +700,8 @@ $.urlParam = function (name) {
 	}
 	return decodeURI(results[1]) || 0;
 };
+
+window.alert = function(message) {
+  window.console.log("Alert: " + message);
+  renderMessage('', message, "info");
+};
diff --git a/backend/web/templates/base.html b/backend/web/templates/base.html
index bcf15f82..b569b881 100644
--- a/backend/web/templates/base.html
+++ b/backend/web/templates/base.html
@@ -12,7 +12,6 @@
 	{% if demo %}
 	<script src="static/js/demo.js"></script>
 	{% endif %}
-        <script src="/kratos/.well-known/ory/webauthn.js"></script>
 
 	<title>Your Stackspin Account</title>
 </html>
-- 
GitLab