diff options
author | Kristóf Marussy <kristof@marussy.com> | 2021-12-24 03:07:06 +0100 |
---|---|---|
committer | Kristóf Marussy <kristof@marussy.com> | 2021-12-24 03:09:02 +0100 |
commit | 5c301457e6a03823b069eec0235ee569342a7e49 (patch) | |
tree | c841d1da6fdeee08324d5903327f1caf754979e2 /packages/main/src/userAgent.ts | |
parent | refactor: Load ui and service in parallel (diff) | |
download | sophie-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.
Diffstat (limited to 'packages/main/src/userAgent.ts')
-rw-r--r-- | packages/main/src/userAgent.ts | 61 |
1 files changed, 61 insertions, 0 deletions
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 | } | ||