summaryrefslogtreecommitdiff
path: root/data/extensions/jsr@javascriptrestrictor/wrappingS-DM.js
blob: 2df5996718b4289dc6af424f479c94d7e012cc73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/** \file
 * \brief Wrappers for navigator.deviceMemory property
 *
 * \see https://xhr.spec.whatwg.org/
 *
 *  \author Copyright (C) 2019  Libor Polcak
 *  \author Copyright (C) 2021  Matus Svancar
 *
 *  \license SPDX-License-Identifier: GPL-3.0-or-later
 *  \license SPDX-License-Identifier: MPL-2.0
 */
//
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <https://www.gnu.org/licenses/>.
//
//  Alternatively, the contents of this file may be used under the terms
//  of the Mozilla Public License, v. 2.0, as described below:
//
//  This Source Code Form is subject to the terms of the Mozilla Public
//  License, v. 2.0. If a copy of the MPL was not distributed with this file,
//  You can obtain one at http://mozilla.org/MPL/2.0/.
//
//  \copyright Copyright (c) 2020 The Brave Authors.

/** \file
 * This file contains a wrapper for [navigator.deviceMemory](https://developer.mozilla.org/en-US/docs/Web/API/Navigator/deviceMemory).
 * \ingroup wrappers
 *
 * The goal is to prevent fingerprinting by modifying the return value of the `navigator.deviceMemory` parameter.
 *
 * This wrapper operates with three levels of protection:
 *
 *	* (0) - return random valid value from range [0.25 - real value]
 *	* (1) - return random valid value from range [0.25 - 8]
 *	* (2) - return 4
 *
 * These approaches are inspired by the algorithms created by [Brave Software](https://brave.com)
 * available [here](https://github.com/brave/brave-core/blob/master/chromium_src/third_party/blink/renderer/core/frame/navigator_device_memory.cc).
 *
 */

/*
 * Create private namespace
 */
(function() {
	var wrappers = [
		{
			parent_object: "Navigator.prototype",
			parent_object_property: "deviceMemory",
			wrapped_objects: [],
			helping_code: `
				let dm_prng = alea(domainHash, "navigator.deviceMemory");
				var validValues = [0.25, 0.5, 1.0, 2.0, 4.0, 8.0];
				var ret = 4;
				var realValue = navigator.deviceMemory;
				if(args[0]!=2 && realValue==0.25){
					ret = realValue;
				}
				else if(args[0]==0){
					var maxIndex = validValues.indexOf(realValue);
					if(maxIndex == -1){
						maxIndex = validValues.length-1;
					}
					ret = validValues[Math.floor((dm_prng()*(maxIndex+1)))];
				}
				else if(args[0]==1){
					ret = validValues[Math.floor(dm_prng()*(validValues.length))];
				}
			`,
			post_wrapping_code: [
				{
					code_type: "object_properties",
					parent_object: "Navigator.prototype",
					parent_object_property: "deviceMemory",
					wrapped_objects: [],
					/**  \brief replaces navigator.deviceMemory getter
					 *
					 * Depending on level chosen this property returns:
					 *	* (0) - random valid value from range [0.25 - real value]
					 *	* (1) - random valid value from range [0.25 - 8]
					 *	* (2) - 4
					 */
					wrapped_properties: [
						{
							property_name: "get",
							property_value: `
								function() {
									return ret;
								}`,
						},
					],
				}
			],
		},
	]
	add_wrappers(wrappers);
})();