diff options
Diffstat (limited to 'packages/main')
-rw-r--r-- | packages/main/src/index.ts | 3 | ||||
-rw-r--r-- | packages/main/src/userAgent.ts | 61 |
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'; |
41 | import { createRootStore } from './stores/RootStore'; | 41 | import { createRootStore } from './stores/RootStore'; |
42 | import { reduceUserAgent } from './userAgent'; | ||
42 | 43 | ||
43 | const isDevelopment = import.meta.env.MODE === 'development'; | 44 | const 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. |
68 | const originalUserAgent = app.userAgentFallback; | 69 | const originalUserAgent = app.userAgentFallback; |
69 | const userAgent = originalUserAgent.replaceAll(/ ([Ss]ophie|Electron)\/[0-9.]+/g, ''); | 70 | const 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. |
71 | if (!isDevelopment) { | 72 | if (!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 | |||
29 | const electronUAParts = / (sophie|Electron)\/[^\s]+/g; | ||
30 | const 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$/; | ||
31 | const 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 | */ | ||
46 | export 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 | } | ||