Skip to content

Commit 886828c

Browse files
authored
Merge pull request element-hq#9496 from vector-im/travis/guests/block-ui
Validate default homeserver config before loading the app
2 parents 814b529 + fc5c93a commit 886828c

6 files changed

Lines changed: 169 additions & 40 deletions

File tree

‎README.md‎

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -109,25 +109,31 @@ You can configure the app by copying `config.sample.json` to
109109

110110
For a good example, see https://riot.im/develop/config.json.
111111

112-
1. `default_server_name` sets the default server name to use for authentication.
113-
This will trigger Riot to ask
114-
`https://<server_name>/.well-known/matrix/client` for the homeserver and
115-
identity server URLs to use. This is the recommended approach for setting a
116-
default server. However, it is also possible to use the following to directly
117-
configure each of the URLs:
118-
* `default_hs_url` sets the default homeserver URL.
119-
* `default_is_url` sets the default identity server URL (this is the server used
120-
for verifying third party identifiers like email addresses). If this is blank,
121-
registering with an email address, adding an email address to your account,
122-
or inviting users via email address will not work. Matrix identity servers are
123-
very simple web services which map third party identifiers (currently only email
124-
addresses) to matrix IDs: see http://matrix.org/docs/spec/identity_service/unstable.html
125-
for more details. Currently the only public matrix identity servers are https://matrix.org
126-
and https://vector.im. In the future, identity servers will be decentralised.
127-
* Riot will report an error if you accidentally configure both `default_server_name` _and_ `default_hs_url` since it's unclear which should take priority.
112+
1. `default_server_config` sets the default homeserver and identity server URL for
113+
Riot to use. The object is the same as returned by [https://<server_name>/.well-known/matrix/client](https://matrix.org/docs/spec/client_server/latest.html#get-well-known-matrix-client),
114+
with added support for a `server_name` under the `m.homeserver` section to display
115+
a custom homeserver name. Alternatively, the config can contain a `default_server_name`
116+
instead which is where Riot will go to get that same object - see the `.well-known`
117+
link above for more information. Note that the `default_server_name` is used to get
118+
a complete server configuration whereas the `server_name` in the `default_server_config`
119+
is for display purposes only.
120+
* *Note*: The URLs can also be individually specified as `default_hs_url` and
121+
`default_is_url`, however these are deprecated. They are maintained for backwards
122+
compatibility with older configurations. `default_is_url` is respected only
123+
if `default_hs_url` is used.
124+
* The identity server is used for verifying third party identifiers like emails
125+
and phone numbers. It is not used to store your password or account information.
126+
If not provided, the identity server defaults to vector.im unless `disable_identity_server`
127+
is set to true in the config. Currently the only two public identity servers
128+
are https://matrix.org and https://vector.im, however in future identity servers
129+
will be decentralised.
130+
* Riot will fail to load if a mix of `default_server_config`, `default_server_name`, or
131+
`default_hs_url` is specified. When multiple sources are specified, it is unclear
132+
which should take priority and therefore the application cannot continue.
128133
1. `features`: Lookup of optional features that may be `enable`d, `disable`d, or exposed to the user
129134
in the `labs` section of settings. The available optional experimental features vary from
130-
release to release.
135+
release to release. Some of the available features are described in the Labs Feature section
136+
of this README.
131137
1. `brand`: String to pass to your homeserver when configuring email notifications, to let the
132138
homeserver know what email template to use when talking to you.
133139
1. `branding`: Configures various branding and logo details, such as:

‎config.sample.json‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
{
2-
"default_hs_url": "https://matrix.org",
3-
"default_is_url": "https://vector.im",
2+
"default_server_config": {
3+
"m.homeserver": {
4+
"base_url": "https://matrix.org",
5+
"server_name": "matrix.org"
6+
},
7+
"m.identity_server": {
8+
"base_url": "https://vector.im"
9+
}
10+
},
11+
"disable_identity_server": false,
412
"disable_custom_urls": false,
513
"disable_guests": false,
614
"disable_login_language_selector": false,

‎src/i18n/strings/en_EN.json‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
{
2+
"Unexpected error preparing the app. See console for details.": "Unexpected error preparing the app. See console for details.",
3+
"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.",
4+
"Invalid configuration: no default server specified.": "Invalid configuration: no default server specified.",
25
"Riot Desktop on %(platformName)s": "Riot Desktop on %(platformName)s",
36
"Unknown device": "Unknown device",
47
"%(appName)s via %(browserName)s on %(osName)s": "%(appName)s via %(browserName)s on %(osName)s",
@@ -15,7 +18,5 @@
1518
"Need help?": "Need help?",
1619
"Chat with Riot Bot": "Chat with Riot Bot",
1720
"Explore rooms": "Explore rooms",
18-
"Room Directory": "Room Directory",
19-
"Search the room directory": "Search the room directory",
20-
"Get started with some tips from Riot Bot!": "Get started with some tips from Riot Bot!"
21+
"Room Directory": "Room Directory"
2122
}

‎src/vector/index.js‎

Lines changed: 96 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ import VectorConferenceHandler from 'matrix-react-sdk/lib/VectorConferenceHandle
4545
import Promise from 'bluebird';
4646
import request from 'browser-request';
4747
import * as languageHandler from 'matrix-react-sdk/lib/languageHandler';
48+
import {_t, _td, newTranslatableError} from 'matrix-react-sdk/lib/languageHandler';
49+
import AutoDiscoveryUtils from 'matrix-react-sdk/lib/utils/AutoDiscoveryUtils';
50+
import {AutoDiscovery} from "matrix-js-sdk/lib/autodiscovery";
4851

4952
import url from 'url';
5053

@@ -341,22 +344,37 @@ async function loadApp() {
341344
const platform = PlatformPeg.get();
342345
platform.startUpdater();
343346

344-
const MatrixChat = sdk.getComponent('structures.MatrixChat');
345-
window.matrixChat = ReactDOM.render(
346-
<MatrixChat
347-
onNewScreen={onNewScreen}
348-
makeRegistrationUrl={makeRegistrationUrl}
349-
ConferenceHandler={VectorConferenceHandler}
350-
config={configJson}
351-
realQueryParams={params}
352-
startingFragmentQueryParams={fragparts.params}
353-
enableGuest={!configJson.disable_guests}
354-
onTokenLoginCompleted={onTokenLoginCompleted}
355-
initialScreenAfterLogin={getScreenFromLocation(window.location)}
356-
defaultDeviceDisplayName={platform.getDefaultDeviceDisplayName()}
357-
/>,
358-
document.getElementById('matrixchat'),
359-
);
347+
// Don't bother loading the app until the config is verified
348+
verifyServerConfig().then((newConfig) => {
349+
const MatrixChat = sdk.getComponent('structures.MatrixChat');
350+
window.matrixChat = ReactDOM.render(
351+
<MatrixChat
352+
onNewScreen={onNewScreen}
353+
makeRegistrationUrl={makeRegistrationUrl}
354+
ConferenceHandler={VectorConferenceHandler}
355+
config={newConfig}
356+
realQueryParams={params}
357+
startingFragmentQueryParams={fragparts.params}
358+
enableGuest={!configJson.disable_guests}
359+
onTokenLoginCompleted={onTokenLoginCompleted}
360+
initialScreenAfterLogin={getScreenFromLocation(window.location)}
361+
defaultDeviceDisplayName={platform.getDefaultDeviceDisplayName()}
362+
/>,
363+
document.getElementById('matrixchat'),
364+
);
365+
}).catch(err => {
366+
console.error(err);
367+
368+
const errorMessage = err.translatedMessage
369+
|| _t("Unexpected error preparing the app. See console for details.");
370+
371+
// Like the compatibility page, AWOOOOOGA at the user
372+
const GenericErrorPage = sdk.getComponent("structures.GenericErrorPage");
373+
window.matrixChat = ReactDOM.render(
374+
<GenericErrorPage message={errorMessage} />,
375+
document.getElementById('matrixchat'),
376+
);
377+
});
360378
} else {
361379
console.error("Browser is missing required features.");
362380
// take to a different landing page to AWOOOOOGA at the user
@@ -428,4 +446,66 @@ async function loadLanguage() {
428446
}
429447
}
430448

449+
async function verifyServerConfig() {
450+
console.log("Verifying homeserver configuration");
451+
452+
// TODO: TravisR - Handle query string arguments for hs_url and is_url
453+
// We probably don't want to handle them unless the user is logged out though?
454+
455+
const config = SdkConfig.get();
456+
let wkConfig = config['default_server_config']; // overwritten later under some conditions
457+
const serverName = config['default_server_name'];
458+
const hsUrl = config['default_hs_url'];
459+
const isUrl = config['default_is_url'];
460+
461+
const incompatibleOptions = [wkConfig, serverName, hsUrl].filter(i => !!i);
462+
if (incompatibleOptions.length > 1) {
463+
throw newTranslatableError(_td(
464+
"Invalid configuration: can only specify one of default_server_config, default_server_name, " +
465+
"or default_hs_url.",
466+
));
467+
}
468+
if (incompatibleOptions.length < 1) {
469+
throw newTranslatableError(_td("Invalid configuration: no default server specified."));
470+
}
471+
472+
if (hsUrl) {
473+
console.log("Config uses a default_hs_url - constructing a default_server_config using this information");
474+
475+
wkConfig = {
476+
"m.homeserver": {
477+
"base_url": hsUrl,
478+
},
479+
};
480+
if (isUrl) {
481+
wkConfig["m.identity_server"] = {
482+
"base_url": isUrl,
483+
};
484+
}
485+
}
486+
487+
let result = null;
488+
489+
if (wkConfig) {
490+
console.log("Config uses a default_server_config - validating object");
491+
result = await AutoDiscovery.fromDiscoveryConfig(wkConfig);
492+
}
493+
494+
if (serverName) {
495+
console.log("Config uses a default_server_name - doing .well-known lookup");
496+
result = await AutoDiscovery.findClientConfig(serverName);
497+
}
498+
499+
const validatedConfig = AutoDiscoveryUtils.buildValidatedConfigFromDiscovery(serverName, result);
500+
501+
// Just in case we ever have to debug this
502+
console.log("Using homeserver config:", validatedConfig);
503+
504+
// Add the newly built config to the actual config for use by the app
505+
console.log("Updating SdkConfig with validated discovery information");
506+
SdkConfig.add({"validated_server_config": validatedConfig});
507+
508+
return SdkConfig.get();
509+
}
510+
431511
loadApp();

‎test/app-tests/joining.js‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ const ReactDOM = require('react-dom');
3737
const ReactTestUtils = require('react-addons-test-utils');
3838
const expect = require('expect');
3939
import Promise from 'bluebird';
40+
import {makeType} from "matrix-react-sdk/lib/utils/TypeUtils";
41+
import {ValidatedServerConfig} from "matrix-react-sdk/lib/utils/AutoDiscoveryUtils";
4042

4143
const test_utils = require('../test-utils');
4244
const MockHttpBackend = require('matrix-mock-request');
@@ -96,8 +98,20 @@ describe('joining a room', function() {
9698

9799
PlatformPeg.set(new WebPlatform());
98100

101+
const config = {
102+
validated_server_config: makeType(ValidatedServerConfig, {
103+
hsUrl: HS_URL,
104+
hsName: "TEST_ENVIRONMENT",
105+
hsNameIsDifferent: false, // yes, we lie
106+
isUrl: IS_URL,
107+
identityEnabled: true,
108+
}),
109+
};
110+
99111
const mc = (
100-
<MatrixChat config={{}}
112+
<MatrixChat
113+
config={config}
114+
serverConfig={config.validated_server_config}
101115
makeRegistrationUrl={()=>{throw new Error("unimplemented");}}
102116
initialScreenAfterLogin={{
103117
screen: 'directory',

‎test/app-tests/loading.js‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ import dis from 'matrix-react-sdk/lib/dispatcher';
3939
import * as test_utils from '../test-utils';
4040
import MockHttpBackend from 'matrix-mock-request';
4141
import {parseQs, parseQsFromFragment} from '../../src/vector/url_utils';
42+
import {makeType} from "matrix-react-sdk/lib/utils/TypeUtils";
43+
import {ValidatedServerConfig} from "matrix-react-sdk/lib/utils/AutoDiscoveryUtils";
4244

4345
const DEFAULT_HS_URL='http://my_server';
4446
const DEFAULT_IS_URL='http://my_is';
@@ -146,6 +148,13 @@ describe('loading:', function() {
146148
const config = Object.assign({
147149
default_hs_url: DEFAULT_HS_URL,
148150
default_is_url: DEFAULT_IS_URL,
151+
validated_server_config: makeType(ValidatedServerConfig, {
152+
hsUrl: DEFAULT_HS_URL,
153+
hsName: "TEST_ENVIRONMENT",
154+
hsNameIsDifferent: false, // yes, we lie
155+
isUrl: DEFAULT_IS_URL,
156+
identityEnabled: true,
157+
}),
149158
embeddedPages: {
150159
homeUrl: 'data:text/html;charset=utf-8;base64,PGh0bWw+PC9odG1sPg==',
151160
},
@@ -160,6 +169,7 @@ describe('loading:', function() {
160169
<MatrixChat
161170
onNewScreen={onNewScreen}
162171
config={config}
172+
serverConfig={config.validated_server_config}
163173
realQueryParams={params}
164174
startingFragmentQueryParams={fragParts.params}
165175
enableGuest={true}
@@ -616,11 +626,21 @@ describe('loading:', function() {
616626

617627
// check that we have a Login component, send a 'user:pass' login,
618628
// and await the HTTP requests.
619-
function completeLogin(matrixChat) {
629+
async function completeLogin(matrixChat) {
620630
// we expect a single <Login> component
621631
const login = ReactTestUtils.findRenderedComponentWithType(
622632
matrixChat, sdk.getComponent('structures.auth.Login'));
623633

634+
// When we switch to the login component, it'll hit the login endpoint
635+
// for proof of life and to get flows. We'll only give it one option.
636+
httpBackend.when('GET', '/login')
637+
.respond(200, {"flows": [{"type": "m.login.password"}]});
638+
httpBackend.flush(); // We already would have tried the GET /login request
639+
640+
// Give the component some time to finish processing the login flows before
641+
// continuing.
642+
await Promise.delay(100);
643+
624644
httpBackend.when('POST', '/login').check(function(req) {
625645
expect(req.data.type).toEqual('m.login.password');
626646
expect(req.data.identifier.type).toEqual('m.id.user');

0 commit comments

Comments
 (0)