aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Kristóf Marussy <kristof@marussy.com>2021-12-24 03:07:06 +0100
committerLibravatar Kristóf Marussy <kristof@marussy.com>2021-12-24 03:09:02 +0100
commit5c301457e6a03823b069eec0235ee569342a7e49 (patch)
treec841d1da6fdeee08324d5903327f1caf754979e2
parentrefactor: Load ui and service in parallel (diff)
downloadsophie-5c301457e6a03823b069eec0235ee569342a7e49.tar.gz
sophie-5c301457e6a03823b069eec0235ee569342a7e49.tar.zst
sophie-5c301457e6a03823b069eec0235ee569342a7e49.zip
feat: User-agent reduction
Unfortunately, the reduced user-agent doesn't fool the google login form, but at least reduces the amount of leaked information.
-rw-r--r--packages/main/src/index.ts3
-rw-r--r--packages/main/src/userAgent.ts61
2 files changed, 63 insertions, 1 deletions
diff --git a/packages/main/src/index.ts b/packages/main/src/index.ts
index 4308d55..b6df67b 100644
--- a/packages/main/src/index.ts
+++ b/packages/main/src/index.ts
@@ -39,6 +39,7 @@ import {
39 openDevToolsWhenReady, 39 openDevToolsWhenReady,
40} from './devTools'; 40} from './devTools';
41import { createRootStore } from './stores/RootStore'; 41import { createRootStore } from './stores/RootStore';
42import { reduceUserAgent } from './userAgent';
42 43
43const isDevelopment = import.meta.env.MODE === 'development'; 44const isDevelopment = import.meta.env.MODE === 'development';
44 45
@@ -66,7 +67,7 @@ app.commandLine.appendSwitch(
66 67
67// Remove sophie and electron from the user-agent string to avoid detection. 68// Remove sophie and electron from the user-agent string to avoid detection.
68const originalUserAgent = app.userAgentFallback; 69const originalUserAgent = app.userAgentFallback;
69const userAgent = originalUserAgent.replaceAll(/ ([Ss]ophie|Electron)\/[0-9.]+/g, ''); 70const userAgent = reduceUserAgent(originalUserAgent);
70// Removing the electron version breaks redux devtools, so we only do this in production. 71// Removing the electron version breaks redux devtools, so we only do this in production.
71if (!isDevelopment) { 72if (!isDevelopment) {
72 app.userAgentFallback = userAgent; 73 app.userAgentFallback = userAgent;
diff --git a/packages/main/src/userAgent.ts b/packages/main/src/userAgent.ts
new file mode 100644
index 0000000..298e565
--- /dev/null
+++ b/packages/main/src/userAgent.ts
@@ -0,0 +1,61 @@
1/*
2 * Copyright (C) 2021-2022 Kristóf Marussy <kristof@marussy.com>
3 *
4 * This file is part of Sophie.
5 *
6 * Sophie is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as
8 * published by the Free Software Foundation, version 3.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 *
18 * SPDX-License-Identifier: AGPL-3.0-only
19 */
20
21/**
22 * @file Based on the javascript code snippet available at
23 * https://github.com/GoogleChrome/developer.chrome.com/blob/c33f7f4f2964e7deb28aee44de745c3725b70063/site/en/docs/privacy-sandbox/user-agent/snippets/index.md
24 *
25 * Used under the Apache 2.0 license according to
26 * https://github.com/GoogleChrome/developer.chrome.com/blob/c33f7f4f2964e7deb28aee44de745c3725b70063/LICENSE
27 */
28
29const electronUAParts = / (sophie|Electron)\/[^\s]+/g;
30const chromeUAs = /^Mozilla\/5\.0 \(((?<platform>Lin|Win|Mac|X11; C|X11; L)+[^\)]+)\) AppleWebKit\/537.36 \(KHTML, like Gecko\) Chrome\/(?<major>\d+)[\d\.]+(?<mobile>[ Mobile]*) Safari\/537\.36$/;
31const unifiedPlatform = {
32 'Lin': 'Linux; Android 10; K',
33 'Win': 'Windows NT 10.0; Win64; x64',
34 'Mac': 'Macintosh; Intel Mac OS X 10_15_7',
35 'X11; C': 'X11; CrOS x86_64',
36 'X11; L': 'X11; Linux x86_64',
37};
38
39/**
40 * Reduces the information exposed in the user-agent string.
41 *
42 * @param userAgent The original user-agent string.
43 * @returns The reduces user-agent string.
44 * @see https://developer.chrome.com/docs/privacy-sandbox/user-agent/
45 */
46export function reduceUserAgent(userAgent: string): string {
47 const userAgentWithoutElectron = userAgent.replaceAll(electronUAParts, '');
48 const matched = chromeUAs.exec(userAgentWithoutElectron) as unknown as {
49 groups: {
50 platform: 'Lin' | 'Win' | 'Mac' | 'X11; C' | 'X11; L',
51 major: string,
52 mobile: string,
53 }
54 };
55 if (matched) {
56 return `Mozilla/5.0 (${unifiedPlatform[matched.groups.platform]}) ` +
57 `AppleWebKit/537.36 (KHTML, like Gecko) ` +
58 `Chrome/${matched.groups.major}.0.0.0${matched.groups.mobile} Safari/537.36`
59 }
60 return userAgentWithoutElectron;
61}