Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 0ded5e0

Browse files
author
Kerry
authored
Device manager - record device client information on app start (PSG-633) (#9314)
* record device client inforamtion events on app start * matrix-client-information -> matrix_client_information * fix types * remove another unused export * add docs link * add opt in setting for recording device information
1 parent bb2f4fb commit 0ded5e0

File tree

7 files changed

+330
-1
lines changed

7 files changed

+330
-1
lines changed

src/DeviceListener.ts

+46
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ import { isSecureBackupRequired } from './utils/WellKnownUtils';
4040
import { ActionPayload } from "./dispatcher/payloads";
4141
import { Action } from "./dispatcher/actions";
4242
import { isLoggedIn } from "./utils/login";
43+
import SdkConfig from "./SdkConfig";
44+
import PlatformPeg from "./PlatformPeg";
45+
import { recordClientInformation } from "./utils/device/clientInformation";
46+
import SettingsStore, { CallbackFn } from "./settings/SettingsStore";
4347

4448
const KEY_BACKUP_POLL_INTERVAL = 5 * 60 * 1000;
4549

@@ -60,6 +64,8 @@ export default class DeviceListener {
6064
// The set of device IDs we're currently displaying toasts for
6165
private displayingToastsForDeviceIds = new Set<string>();
6266
private running = false;
67+
private shouldRecordClientInformation = false;
68+
private deviceClientInformationSettingWatcherRef: string | undefined;
6369

6470
public static sharedInstance() {
6571
if (!window.mxDeviceListener) window.mxDeviceListener = new DeviceListener();
@@ -76,8 +82,15 @@ export default class DeviceListener {
7682
MatrixClientPeg.get().on(ClientEvent.AccountData, this.onAccountData);
7783
MatrixClientPeg.get().on(ClientEvent.Sync, this.onSync);
7884
MatrixClientPeg.get().on(RoomStateEvent.Events, this.onRoomStateEvents);
85+
this.shouldRecordClientInformation = SettingsStore.getValue('deviceClientInformationOptIn');
86+
this.deviceClientInformationSettingWatcherRef = SettingsStore.watchSetting(
87+
'deviceClientInformationOptIn',
88+
null,
89+
this.onRecordClientInformationSettingChange,
90+
);
7991
this.dispatcherRef = dis.register(this.onAction);
8092
this.recheck();
93+
this.recordClientInformation();
8194
}
8295

8396
public stop() {
@@ -95,6 +108,9 @@ export default class DeviceListener {
95108
MatrixClientPeg.get().removeListener(ClientEvent.Sync, this.onSync);
96109
MatrixClientPeg.get().removeListener(RoomStateEvent.Events, this.onRoomStateEvents);
97110
}
111+
if (this.deviceClientInformationSettingWatcherRef) {
112+
SettingsStore.unwatchSetting(this.deviceClientInformationSettingWatcherRef);
113+
}
98114
if (this.dispatcherRef) {
99115
dis.unregister(this.dispatcherRef);
100116
this.dispatcherRef = null;
@@ -200,6 +216,7 @@ export default class DeviceListener {
200216
private onAction = ({ action }: ActionPayload) => {
201217
if (action !== Action.OnLoggedIn) return;
202218
this.recheck();
219+
this.recordClientInformation();
203220
};
204221

205222
// The server doesn't tell us when key backup is set up, so we poll
@@ -343,4 +360,33 @@ export default class DeviceListener {
343360
dis.dispatch({ action: Action.ReportKeyBackupNotEnabled });
344361
}
345362
};
363+
364+
private onRecordClientInformationSettingChange: CallbackFn = (
365+
_originalSettingName, _roomId, _level, _newLevel, newValue,
366+
) => {
367+
const prevValue = this.shouldRecordClientInformation;
368+
369+
this.shouldRecordClientInformation = !!newValue;
370+
371+
if (this.shouldRecordClientInformation && !prevValue) {
372+
this.recordClientInformation();
373+
}
374+
};
375+
376+
private recordClientInformation = async () => {
377+
if (!this.shouldRecordClientInformation) {
378+
return;
379+
}
380+
try {
381+
await recordClientInformation(
382+
MatrixClientPeg.get(),
383+
SdkConfig.get(),
384+
PlatformPeg.get(),
385+
);
386+
} catch (error) {
387+
// this is a best effort operation
388+
// log the error without rethrowing
389+
logger.error('Failed to record client information', error);
390+
}
391+
};
346392
}

src/components/views/settings/tabs/user/SecurityUserSettingsTab.tsx

+6
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,12 @@ export default class SecurityUserSettingsTab extends React.Component<IProps, ISt
319319
level={SettingLevel.ACCOUNT} />
320320
) }
321321
</div>
322+
<div className="mx_SettingsTab_section">
323+
<span className="mx_SettingsTab_subheading">{ _t("Sessions") }</span>
324+
<SettingsFlag
325+
name="deviceClientInformationOptIn"
326+
level={SettingLevel.ACCOUNT} />
327+
</div>
322328
</React.Fragment>;
323329
}
324330

src/i18n/strings/en_EN.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -955,6 +955,7 @@
955955
"System font name": "System font name",
956956
"Allow Peer-to-Peer for 1:1 calls (if you enable this, the other party might be able to see your IP address)": "Allow Peer-to-Peer for 1:1 calls (if you enable this, the other party might be able to see your IP address)",
957957
"Send analytics data": "Send analytics data",
958+
"Record the client name, version, and url to recognise sessions more easily in session manager": "Record the client name, version, and url to recognise sessions more easily in session manager",
958959
"Never send encrypted messages to unverified sessions from this session": "Never send encrypted messages to unverified sessions from this session",
959960
"Never send encrypted messages to unverified sessions in this room from this session": "Never send encrypted messages to unverified sessions in this room from this session",
960961
"Enable inline URL previews by default": "Enable inline URL previews by default",
@@ -1569,9 +1570,9 @@
15691570
"Okay": "Okay",
15701571
"Privacy": "Privacy",
15711572
"Share anonymous data to help us identify issues. Nothing personal. No third parties.": "Share anonymous data to help us identify issues. Nothing personal. No third parties.",
1573+
"Sessions": "Sessions",
15721574
"Where you're signed in": "Where you're signed in",
15731575
"Manage your signed-in devices below. A device's name is visible to people you communicate with.": "Manage your signed-in devices below. A device's name is visible to people you communicate with.",
1574-
"Sessions": "Sessions",
15751576
"Other sessions": "Other sessions",
15761577
"For best security, verify your sessions and sign out from any session that you don't recognize or use anymore.": "For best security, verify your sessions and sign out from any session that you don't recognize or use anymore.",
15771578
"Sidebar": "Sidebar",

src/settings/Settings.tsx

+8
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,14 @@ export const SETTINGS: {[setting: string]: ISetting} = {
740740
displayName: _td('Send analytics data'),
741741
default: null,
742742
},
743+
"deviceClientInformationOptIn": {
744+
supportedLevels: [SettingLevel.ACCOUNT],
745+
displayName: _td(
746+
`Record the client name, version, and url ` +
747+
`to recognise sessions more easily in session manager`,
748+
),
749+
default: false,
750+
},
743751
"FTUE.useCaseSelection": {
744752
supportedLevels: LEVELS_ACCOUNT_SETTINGS,
745753
default: null,

src/utils/device/clientInformation.ts

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
Copyright 2022 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import { MatrixClient } from "matrix-js-sdk/src/client";
18+
19+
import BasePlatform from "../../BasePlatform";
20+
import { IConfigOptions } from "../../IConfigOptions";
21+
22+
const formatUrl = (): string | undefined => {
23+
// don't record url for electron clients
24+
if (window.electron) {
25+
return undefined;
26+
}
27+
28+
// strip query-string and fragment from uri
29+
const url = new URL(window.location.href);
30+
31+
return [
32+
url.host,
33+
url.pathname.replace(/\/$/, ""), // Remove trailing slash if present
34+
].join("");
35+
};
36+
37+
const getClientInformationEventType = (deviceId: string): string =>
38+
`io.element.matrix_client_information.${deviceId}`;
39+
40+
/**
41+
* Record extra client information for the current device
42+
* https://github.com/vector-im/element-meta/blob/develop/spec/matrix_client_information.md
43+
*/
44+
export const recordClientInformation = async (
45+
matrixClient: MatrixClient,
46+
sdkConfig: IConfigOptions,
47+
platform: BasePlatform,
48+
): Promise<void> => {
49+
const deviceId = matrixClient.getDeviceId();
50+
const { brand } = sdkConfig;
51+
const version = await platform.getAppVersion();
52+
const type = getClientInformationEventType(deviceId);
53+
const url = formatUrl();
54+
55+
await matrixClient.setAccountData(type, {
56+
name: brand,
57+
version,
58+
url,
59+
});
60+
};

test/DeviceListener-test.ts

+122
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ limitations under the License.
1818
import { EventEmitter } from "events";
1919
import { mocked } from "jest-mock";
2020
import { Room } from "matrix-js-sdk/src/matrix";
21+
import { logger } from "matrix-js-sdk/src/logger";
2122

2223
import DeviceListener from "../src/DeviceListener";
2324
import { MatrixClientPeg } from "../src/MatrixClientPeg";
@@ -27,6 +28,9 @@ import * as BulkUnverifiedSessionsToast from "../src/toasts/BulkUnverifiedSessio
2728
import { isSecretStorageBeingAccessed } from "../src/SecurityManager";
2829
import dis from "../src/dispatcher/dispatcher";
2930
import { Action } from "../src/dispatcher/actions";
31+
import SettingsStore from "../src/settings/SettingsStore";
32+
import { mockPlatformPeg } from "./test-utils";
33+
import { SettingLevel } from "../src/settings/SettingLevel";
3034

3135
// don't litter test console with logs
3236
jest.mock("matrix-js-sdk/src/logger");
@@ -40,7 +44,10 @@ jest.mock("../src/SecurityManager", () => ({
4044
isSecretStorageBeingAccessed: jest.fn(), accessSecretStorage: jest.fn(),
4145
}));
4246

47+
const deviceId = 'my-device-id';
48+
4349
class MockClient extends EventEmitter {
50+
isGuest = jest.fn();
4451
getUserId = jest.fn();
4552
getKeyBackupVersion = jest.fn().mockResolvedValue(undefined);
4653
getRooms = jest.fn().mockReturnValue([]);
@@ -57,6 +64,8 @@ class MockClient extends EventEmitter {
5764
downloadKeys = jest.fn();
5865
isRoomEncrypted = jest.fn();
5966
getClientWellKnown = jest.fn();
67+
getDeviceId = jest.fn().mockReturnValue(deviceId);
68+
setAccountData = jest.fn();
6069
}
6170
const mockDispatcher = mocked(dis);
6271
const flushPromises = async () => await new Promise(process.nextTick);
@@ -75,8 +84,12 @@ describe('DeviceListener', () => {
7584

7685
beforeEach(() => {
7786
jest.resetAllMocks();
87+
mockPlatformPeg({
88+
getAppVersion: jest.fn().mockResolvedValue('1.2.3'),
89+
});
7890
mockClient = new MockClient();
7991
jest.spyOn(MatrixClientPeg, 'get').mockReturnValue(mockClient);
92+
jest.spyOn(SettingsStore, 'getValue').mockReturnValue(false);
8093
});
8194

8295
const createAndStart = async (): Promise<DeviceListener> => {
@@ -86,6 +99,115 @@ describe('DeviceListener', () => {
8699
return instance;
87100
};
88101

102+
describe('client information', () => {
103+
it('watches device client information setting', async () => {
104+
const watchSettingSpy = jest.spyOn(SettingsStore, 'watchSetting');
105+
const unwatchSettingSpy = jest.spyOn(SettingsStore, 'unwatchSetting');
106+
const deviceListener = await createAndStart();
107+
108+
expect(watchSettingSpy).toHaveBeenCalledWith(
109+
'deviceClientInformationOptIn', null, expect.any(Function),
110+
);
111+
112+
deviceListener.stop();
113+
114+
expect(unwatchSettingSpy).toHaveBeenCalled();
115+
});
116+
117+
describe('when device client information feature is enabled', () => {
118+
beforeEach(() => {
119+
jest.spyOn(SettingsStore, 'getValue').mockImplementation(
120+
settingName => settingName === 'deviceClientInformationOptIn',
121+
);
122+
});
123+
it('saves client information on start', async () => {
124+
await createAndStart();
125+
126+
expect(mockClient.setAccountData).toHaveBeenCalledWith(
127+
`io.element.matrix_client_information.${deviceId}`,
128+
{ name: 'Element', url: 'localhost', version: '1.2.3' },
129+
);
130+
});
131+
132+
it('catches error and logs when saving client information fails', async () => {
133+
const errorLogSpy = jest.spyOn(logger, 'error');
134+
const error = new Error('oups');
135+
mockClient.setAccountData.mockRejectedValue(error);
136+
137+
// doesn't throw
138+
await createAndStart();
139+
140+
expect(errorLogSpy).toHaveBeenCalledWith(
141+
'Failed to record client information',
142+
error,
143+
);
144+
});
145+
146+
it('saves client information on logged in action', async () => {
147+
const instance = await createAndStart();
148+
149+
mockClient.setAccountData.mockClear();
150+
151+
// @ts-ignore calling private function
152+
instance.onAction({ action: Action.OnLoggedIn });
153+
154+
await flushPromises();
155+
156+
expect(mockClient.setAccountData).toHaveBeenCalledWith(
157+
`io.element.matrix_client_information.${deviceId}`,
158+
{ name: 'Element', url: 'localhost', version: '1.2.3' },
159+
);
160+
});
161+
});
162+
163+
describe('when device client information feature is disabled', () => {
164+
beforeEach(() => {
165+
jest.spyOn(SettingsStore, 'getValue').mockReturnValue(false);
166+
});
167+
168+
it('does not save client information on start', async () => {
169+
await createAndStart();
170+
171+
expect(mockClient.setAccountData).not.toHaveBeenCalledWith(
172+
`io.element.matrix_client_information.${deviceId}`,
173+
{ name: 'Element', url: 'localhost', version: '1.2.3' },
174+
);
175+
});
176+
177+
it('does not save client information on logged in action', async () => {
178+
const instance = await createAndStart();
179+
180+
// @ts-ignore calling private function
181+
instance.onAction({ action: Action.OnLoggedIn });
182+
183+
await flushPromises();
184+
185+
expect(mockClient.setAccountData).not.toHaveBeenCalledWith(
186+
`io.element.matrix_client_information.${deviceId}`,
187+
{ name: 'Element', url: 'localhost', version: '1.2.3' },
188+
);
189+
});
190+
191+
it('saves client information after setting is enabled', async () => {
192+
const watchSettingSpy = jest.spyOn(SettingsStore, 'watchSetting');
193+
await createAndStart();
194+
195+
const [settingName, roomId, callback] = watchSettingSpy.mock.calls[0];
196+
expect(settingName).toEqual('deviceClientInformationOptIn');
197+
expect(roomId).toBeNull();
198+
199+
callback('deviceClientInformationOptIn', null, SettingLevel.DEVICE, SettingLevel.DEVICE, true);
200+
201+
await flushPromises();
202+
203+
expect(mockClient.setAccountData).toHaveBeenCalledWith(
204+
`io.element.matrix_client_information.${deviceId}`,
205+
{ name: 'Element', url: 'localhost', version: '1.2.3' },
206+
);
207+
});
208+
});
209+
});
210+
89211
describe('recheck', () => {
90212
it('does nothing when cross signing feature is not supported', async () => {
91213
mockClient.doesServerSupportUnstableFeature.mockResolvedValue(false);

0 commit comments

Comments
 (0)