From 48a634bff4a92579786a2ea592042254c8336995 Mon Sep 17 00:00:00 2001
From: Travis Ralston <travpc@gmail.com>
Date: Wed, 29 May 2019 12:07:31 -0600
Subject: [PATCH 1/3] Use the user's pre-existing HS when config validation
 fails

Only applies if the user appears to be logged in. If the user is not logged in, we scream loudly.

This is a temporary measure for https://github.com/vector-im/riot-web/issues/9828

Fixes https://github.com/vector-im/riot-web/issues/9835
---
 src/i18n/strings/en_EN.json |   1 +
 src/vector/index.js         | 112 ++++++++++++++++++++++--------------
 2 files changed, 71 insertions(+), 42 deletions(-)

diff --git a/src/i18n/strings/en_EN.json b/src/i18n/strings/en_EN.json
index 10e57592..e5db692f 100644
--- a/src/i18n/strings/en_EN.json
+++ b/src/i18n/strings/en_EN.json
@@ -1,5 +1,6 @@
 {
     "Unexpected error preparing the app. See console for details.": "Unexpected error preparing the app. See console for details.",
+    "Your configuration appears to be invalid. Please correct the error below and re-open Riot.": "Your configuration appears to be invalid. Please correct the error below and re-open Riot.",
     "Invalid configuration: can only specify one of default_server_config, default_server_name, or default_hs_url.": "Invalid configuration: can only specify one of default_server_config, default_server_name, or default_hs_url.",
     "Invalid configuration: no default server specified.": "Invalid configuration: no default server specified.",
     "Riot Desktop on %(platformName)s": "Riot Desktop on %(platformName)s",
diff --git a/src/vector/index.js b/src/vector/index.js
index 98ba2edf..5ad5a522 100644
--- a/src/vector/index.js
+++ b/src/vector/index.js
@@ -48,6 +48,7 @@ import * as languageHandler from 'matrix-react-sdk/lib/languageHandler';
 import {_t, _td, newTranslatableError} from 'matrix-react-sdk/lib/languageHandler';
 import AutoDiscoveryUtils from 'matrix-react-sdk/lib/utils/AutoDiscoveryUtils';
 import {AutoDiscovery} from "matrix-js-sdk/lib/autodiscovery";
+import * as Lifecycle from "matrix-react-sdk/lib/Lifecycle";
 
 import url from 'url';
 
@@ -365,8 +366,17 @@ async function loadApp() {
         }).catch(err => {
             console.error(err);
 
-            const errorMessage = err.translatedMessage
+            let errorMessage = err.translatedMessage
                 || _t("Unexpected error preparing the app. See console for details.");
+            errorMessage = <span>
+                {_t(
+                    "Your configuration appears to be invalid. Please correct the error below " +
+                    "and re-open Riot."
+                )}
+                <br />
+                <br />
+                {errorMessage}
+            </span>;
 
             // Like the compatibility page, AWOOOOOGA at the user
             const GenericErrorPage = sdk.getComponent("structures.GenericErrorPage");
@@ -447,57 +457,75 @@ async function loadLanguage() {
 }
 
 async function verifyServerConfig() {
-    console.log("Verifying homeserver configuration");
+    let validatedConfig;
+    try {
+        console.log("Verifying homeserver configuration");
 
-    // Note: the query string may include is_url and hs_url - we only respect these in the
-    // context of email validation. Because we don't respect them otherwise, we do not need
-    // to parse or consider them here.
+        // Note: the query string may include is_url and hs_url - we only respect these in the
+        // context of email validation. Because we don't respect them otherwise, we do not need
+        // to parse or consider them here.
 
-    const config = SdkConfig.get();
-    let wkConfig = config['default_server_config']; // overwritten later under some conditions
-    const serverName = config['default_server_name'];
-    const hsUrl = config['default_hs_url'];
-    const isUrl = config['default_is_url'];
+        const config = SdkConfig.get();
+        let wkConfig = config['default_server_config']; // overwritten later under some conditions
+        const serverName = config['default_server_name'];
+        const hsUrl = config['default_hs_url'];
+        const isUrl = config['default_is_url'];
 
-    const incompatibleOptions = [wkConfig, serverName, hsUrl].filter(i => !!i);
-    if (incompatibleOptions.length > 1) {
-        throw newTranslatableError(_td(
-            "Invalid configuration: can only specify one of default_server_config, default_server_name, " +
-            "or default_hs_url.",
-        ));
-    }
-    if (incompatibleOptions.length < 1) {
-        throw newTranslatableError(_td("Invalid configuration: no default server specified."));
-    }
+        const incompatibleOptions = [wkConfig, serverName, hsUrl].filter(i => !!i);
+        if (incompatibleOptions.length > 1) {
+            // noinspection ExceptionCaughtLocallyJS
+            throw newTranslatableError(_td(
+                "Invalid configuration: can only specify one of default_server_config, default_server_name, " +
+                "or default_hs_url.",
+            ));
+        }
+        if (incompatibleOptions.length < 1) {
+            // noinspection ExceptionCaughtLocallyJS
+            throw newTranslatableError(_td("Invalid configuration: no default server specified."));
+        }
 
-    if (hsUrl) {
-        console.log("Config uses a default_hs_url - constructing a default_server_config using this information");
+        if (hsUrl) {
+            console.log("Config uses a default_hs_url - constructing a default_server_config using this information");
 
-        wkConfig = {
-            "m.homeserver": {
-                "base_url": hsUrl,
-            },
-        };
-        if (isUrl) {
-            wkConfig["m.identity_server"] = {
-                "base_url": isUrl,
+            wkConfig = {
+                "m.homeserver": {
+                    "base_url": hsUrl,
+                },
             };
+            if (isUrl) {
+                wkConfig["m.identity_server"] = {
+                    "base_url": isUrl,
+                };
+            }
+        }
+
+        let discoveryResult = null;
+        if (wkConfig) {
+            console.log("Config uses a default_server_config - validating object");
+            discoveryResult = await AutoDiscovery.fromDiscoveryConfig(wkConfig);
+        }
+
+        if (serverName) {
+            console.log("Config uses a default_server_name - doing .well-known lookup");
+            discoveryResult = await AutoDiscovery.findClientConfig(serverName);
+        }
+
+        validatedConfig = AutoDiscoveryUtils.buildValidatedConfigFromDiscovery(serverName, discoveryResult);
+    } catch (e) {
+        const {hsUrl, isUrl, userId} = Lifecycle.getLocalStorageSessionVars();
+        if (hsUrl && userId) {
+            console.error(e);
+            console.warn("A session was found - suppressing config error and using the session's homeserver");
+
+            console.log("Using pre-existing hsUrl and isUrl: ", {hsUrl, isUrl});
+            validatedConfig = await AutoDiscoveryUtils.validateServerConfigWithStaticUrls(hsUrl, isUrl);
+        } else {
+            // the user is not logged in, so scream
+            throw e;
         }
     }
 
-    let result = null;
 
-    if (wkConfig) {
-        console.log("Config uses a default_server_config - validating object");
-        result = await AutoDiscovery.fromDiscoveryConfig(wkConfig);
-    }
-
-    if (serverName) {
-        console.log("Config uses a default_server_name - doing .well-known lookup");
-        result = await AutoDiscovery.findClientConfig(serverName);
-    }
-
-    const validatedConfig = AutoDiscoveryUtils.buildValidatedConfigFromDiscovery(serverName, result);
     validatedConfig.isDefault = true;
 
     // Just in case we ever have to debug this

From 52c3ff02ae2b426f3ce727759802eb6fae8ab6b1 Mon Sep 17 00:00:00 2001
From: Travis Ralston <travpc@gmail.com>
Date: Wed, 29 May 2019 12:12:12 -0600
Subject: [PATCH 2/3] appease the linter

---
 src/vector/index.js | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/vector/index.js b/src/vector/index.js
index 5ad5a522..8e990146 100644
--- a/src/vector/index.js
+++ b/src/vector/index.js
@@ -371,7 +371,7 @@ async function loadApp() {
             errorMessage = <span>
                 {_t(
                     "Your configuration appears to be invalid. Please correct the error below " +
-                    "and re-open Riot."
+                    "and re-open Riot.",
                 )}
                 <br />
                 <br />

From 81ec75f81dc43681dac594369b6b00ce6dec7487 Mon Sep 17 00:00:00 2001
From: Travis Ralston <travpc@gmail.com>
Date: Fri, 31 May 2019 10:44:20 -0600
Subject: [PATCH 3/3] Change warning message

---
 src/i18n/strings/en_EN.json | 2 +-
 src/vector/index.js         | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/src/i18n/strings/en_EN.json b/src/i18n/strings/en_EN.json
index e5db692f..7f09a09f 100644
--- a/src/i18n/strings/en_EN.json
+++ b/src/i18n/strings/en_EN.json
@@ -1,6 +1,6 @@
 {
     "Unexpected error preparing the app. See console for details.": "Unexpected error preparing the app. See console for details.",
-    "Your configuration appears to be invalid. Please correct the error below and re-open Riot.": "Your configuration appears to be invalid. Please correct the error below and re-open Riot.",
+    "This installation of Riot seems to have an invalid server configuration. If you are the administrator, please correct the error below": "This installation of Riot seems to have an invalid server configuration. If you are the administrator, please correct the error below",
     "Invalid configuration: can only specify one of default_server_config, default_server_name, or default_hs_url.": "Invalid configuration: can only specify one of default_server_config, default_server_name, or default_hs_url.",
     "Invalid configuration: no default server specified.": "Invalid configuration: no default server specified.",
     "Riot Desktop on %(platformName)s": "Riot Desktop on %(platformName)s",
diff --git a/src/vector/index.js b/src/vector/index.js
index 8e990146..ad490267 100644
--- a/src/vector/index.js
+++ b/src/vector/index.js
@@ -370,8 +370,8 @@ async function loadApp() {
                 || _t("Unexpected error preparing the app. See console for details.");
             errorMessage = <span>
                 {_t(
-                    "Your configuration appears to be invalid. Please correct the error below " +
-                    "and re-open Riot.",
+                    "This installation of Riot seems to have an invalid server configuration. " +
+                    "If you are the administrator, please correct the error below",
                 )}
                 <br />
                 <br />