@anonympins/fingerprint 0.5.1 → 0.5.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/CHANGELOG.md +27 -0
  2. package/README.md +118 -115
  3. package/package.json +1 -1
  4. package/src/js/dynamic-wasm.js +52 -13
  5. package/src/js/fingerprint.builder.js +169 -168
  6. package/src/js/fingerprint.client.js +1271 -1189
  7. package/src/js/fingerprint.client.obfuscated.js +1 -1
  8. package/src/js/fingerprint.js +436 -162
  9. package/src/js/fingerprint.utils.js +30 -0
  10. package/src/js/gpu_pow.solver.js +96 -1
  11. package/src/js/library.js +40 -21
  12. package/src/js/mongodb-store.js +165 -79
  13. package/src/js/pow.solver.inline.js +627 -30
  14. package/src/js/pow.solver.js +453 -31
  15. package/src/js/problem-manager.js +1 -1
  16. package/src/js/redis-store.js +114 -42
  17. package/src/js/tests/dns-circuit-breaker.test.js +55 -0
  18. package/src/js/tests/fingerprint.client.init.test.js +143 -143
  19. package/src/js/tests/fingerprint.client.test.js +128 -128
  20. package/src/js/tests/fingerprint.engine.test.js +370 -370
  21. package/src/js/tests/fingerprint.test.js +119 -5
  22. package/src/js/tests/gpu_pow.test.js +81 -0
  23. package/src/js/tests/ip-reputation.test.js +1 -1
  24. package/src/js/tests/pow.solver.test.js +9 -6
  25. package/src/js/tests/usefulWorkSolver.test.js +162 -0
  26. package/src/php/Challenge/ChallengeUtils.php +137 -35
  27. package/src/php/FingerprintClient.php +22 -5
  28. package/src/php/FingerprintEngine.php +335 -58
  29. package/src/php/Optimization/Optimization.php +23 -16
  30. package/src/php/Optimization/OptimizationOperators.php +27 -0
  31. package/src/php/ProblemManager.php +1 -1
  32. package/src/php/Store/MongoDbStore.php +184 -104
  33. package/src/php/Store/RedisStore.php +91 -11
  34. package/src/php/Tests/ChallengeUtilsTest.php +285 -285
  35. package/src/php/Tests/DnsCircuitBreakerTest.php +106 -0
  36. package/src/php/Tests/IpReputationTest.php +1 -1
  37. package/src/php/Tests/PowTest.php +1 -1
  38. package/src/php/Tests/RequestUtilsTest.php +29 -0
  39. package/src/php/Tests/TLSClientHelloParserTest.php +177 -117
  40. package/src/php/Utils/BigInt.php +202 -202
  41. package/src/php/Utils/RequestUtils.php +74 -21
  42. package/src/php/Utils/TLSClientHelloParser.php +212 -116
@@ -1,129 +1,129 @@
1
- /**
2
- * @vitest-environment jsdom
3
- */
4
-
5
- import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest';
6
- import ClientLibrary from '../fingerprint.client.js';
7
-
8
- describe('ClientLibrary WASM Integration', () => {
9
- beforeEach(() => {
10
- // Réinitialise le cache et les mocks avant chaque test
11
- ClientLibrary._resetCache();
12
- vi.spyOn(console, 'log').mockImplementation(() => {});
13
- vi.spyOn(console, 'warn').mockImplementation(() => {});
14
-
15
- // Supprime les mocks globaux pour éviter les fuites entre les tests
16
- delete window.createFingerprintModule;
17
- });
18
-
19
- afterEach(() => {
20
- vi.restoreAllMocks();
21
- });
22
-
23
- it('should successfully load WASM module and switch hasher', async () => {
24
- // 1. Simuler un module WASM fonctionnel
25
- const mockWasmModule = {
26
- _hash_string: vi.fn((str) => 99999), // Un mock qui retourne une valeur distincte
27
- };
28
-
29
- // 2. Simuler le chargement du script et l'initialisation du module
30
- // On attache les fonctions de simulation à `window` car c'est ce que le code cherche
31
- window.createFingerprintModule = vi.fn().mockResolvedValue(mockWasmModule);
32
-
33
- // On simule l'injection du script en appelant directement `onload`
34
- vi.spyOn(document.head, 'appendChild').mockImplementation((script) => {
35
- // Simule le chargement réussi du script
36
- script.onload();
37
- return script;
38
- });
39
-
40
- // 3. Appeler la fonction d'initialisation
41
- await ClientLibrary.initializeWasm('/fake/path/to/fp.js');
42
-
43
- // 4. Vérifier que le hasher a été remplacé
44
- const wasmHash = ClientLibrary._hasher("test");
45
- expect(wasmHash).toBe(99999);
46
- expect(mockWasmModule._hash_string).toHaveBeenCalledWith("test");
47
- expect(console.log).toHaveBeenCalledWith(expect.stringContaining('WASM module loaded successfully'));
48
- });
49
-
50
- it('should gracefully fall back to JS hasher if WASM module fails to load', async () => {
51
- // 1. Simuler un échec de chargement du script
52
- vi.spyOn(document.head, 'appendChild').mockImplementation((script) => {
53
- script.onerror(new Error('Script loading failed'));
54
- return script;
55
- });
56
-
57
- // 2. Appeler la fonction d'initialisation
58
- await ClientLibrary.initializeWasm('/fake/path/to/fp.js');
59
-
60
- // 3. Vérifier que le hasher est toujours l'implémentation JS
61
- const jsHash = ClientLibrary._hasher("test");
62
- const originalJsHash = (await import('../fingerprint.builder.js')).cyrb53("test");
63
-
64
- expect(jsHash).toBe(originalJsHash);
65
- expect(console.warn).toHaveBeenCalledWith(expect.stringContaining('WASM module failed to load'), expect.any(Error));
66
- });
67
-
68
- it('should gracefully fall back if WASM module does not export _hash_string', async () => {
69
- // 1. Simuler un module WASM malformé (sans la fonction attendue)
70
- const mockWasmModule = {};
71
- window.createFingerprintModule = vi.fn().mockResolvedValue(mockWasmModule);
72
-
73
- vi.spyOn(document.head, 'appendChild').mockImplementation((script) => {
74
- script.onload();
75
- return script;
76
- });
77
-
78
- // 2. Appeler la fonction d'initialisation
79
- await ClientLibrary.initializeWasm('/fake/path/to/fp.js');
80
-
81
- // 3. Vérifier le fallback
82
- const jsHash = ClientLibrary._hasher("test");
83
- const originalJsHash = (await import('../fingerprint.builder.js')).cyrb53("test");
84
- expect(jsHash).toBe(originalJsHash);
85
- // L'assertion est maintenant plus précise : elle vérifie le message générique ET le message d'erreur spécifique.
86
- expect(console.warn).toHaveBeenCalledWith(
87
- expect.stringContaining('WASM module failed to load'),
88
- expect.objectContaining({ message: 'WASM module did not export _hash_string.' })
89
- );
90
- });
91
-
92
- it('should successfully load raw WASM and use standalone polymorphic hasher', async () => {
93
- const mockWasmInstance = {
94
- exports: {
95
- memory: { buffer: new ArrayBuffer(65536) },
96
- hash: vi.fn().mockReturnValue(123456)
97
- }
98
- };
99
-
100
- global.WebAssembly = {
101
- compile: vi.fn().mockResolvedValue({}),
102
- instantiate: vi.fn().mockResolvedValue(mockWasmInstance)
103
- };
104
-
105
- global.fetch = vi.fn().mockResolvedValue({
106
- arrayBuffer: vi.fn().mockResolvedValue(new ArrayBuffer(100))
107
- });
108
-
109
- await ClientLibrary.initializeWasm('/fp.wasm');
110
-
111
- const hash = ClientLibrary._hasher("test");
112
- expect(hash).toBe(123456);
113
- expect(mockWasmInstance.exports.hash).toHaveBeenCalledWith(0, 4);
114
- });
115
-
116
- it('should use the default JS hasher if WASM is not configured', async () => {
117
- // Ensure no WASM path is provided in the config
118
- ClientLibrary.initializeClient({});
119
-
120
- // Verify that the active hasher is the original JS implementation
121
- const jsHash = ClientLibrary._hasher("another test string");
122
- const originalJsHash = (await import('../fingerprint.builder.js')).cyrb53("another test string");
123
-
124
- expect(jsHash).toBe(originalJsHash);
125
- // Ensure no WASM-related console logs or warnings were made
126
- expect(console.log).not.toHaveBeenCalledWith(expect.stringContaining('WASM module loaded successfully'));
127
- expect(console.warn).not.toHaveBeenCalledWith(expect.stringContaining('WASM module failed to load'));
128
- });
1
+ /**
2
+ * @vitest-environment jsdom
3
+ */
4
+
5
+ import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest';
6
+ import ClientLibrary from '../fingerprint.client.js';
7
+
8
+ describe('ClientLibrary WASM Integration', () => {
9
+ beforeEach(() => {
10
+ // Réinitialise le cache et les mocks avant chaque test
11
+ ClientLibrary._resetCache();
12
+ vi.spyOn(console, 'log').mockImplementation(() => {});
13
+ vi.spyOn(console, 'warn').mockImplementation(() => {});
14
+
15
+ // Supprime les mocks globaux pour éviter les fuites entre les tests
16
+ delete window.createFingerprintModule;
17
+ });
18
+
19
+ afterEach(() => {
20
+ vi.restoreAllMocks();
21
+ });
22
+
23
+ it('should successfully load WASM module and switch hasher', async () => {
24
+ // 1. Simuler un module WASM fonctionnel
25
+ const mockWasmModule = {
26
+ _hash_string: vi.fn((str) => 99999), // Un mock qui retourne une valeur distincte
27
+ };
28
+
29
+ // 2. Simuler le chargement du script et l'initialisation du module
30
+ // On attache les fonctions de simulation à `window` car c'est ce que le code cherche
31
+ window.createFingerprintModule = vi.fn().mockResolvedValue(mockWasmModule);
32
+
33
+ // On simule l'injection du script en appelant directement `onload`
34
+ vi.spyOn(document.head, 'appendChild').mockImplementation((script) => {
35
+ // Simule le chargement réussi du script
36
+ script.onload();
37
+ return script;
38
+ });
39
+
40
+ // 3. Appeler la fonction d'initialisation
41
+ await ClientLibrary.initializeWasm('/fake/path/to/fp.js');
42
+
43
+ // 4. Vérifier que le hasher a été remplacé
44
+ const wasmHash = ClientLibrary._hasher("test");
45
+ expect(wasmHash).toBe(99999);
46
+ expect(mockWasmModule._hash_string).toHaveBeenCalledWith("test");
47
+ expect(console.log).toHaveBeenCalledWith(expect.stringContaining('WASM module loaded successfully'));
48
+ });
49
+
50
+ it('should gracefully fall back to JS hasher if WASM module fails to load', async () => {
51
+ // 1. Simuler un échec de chargement du script
52
+ vi.spyOn(document.head, 'appendChild').mockImplementation((script) => {
53
+ script.onerror(new Error('Script loading failed'));
54
+ return script;
55
+ });
56
+
57
+ // 2. Appeler la fonction d'initialisation
58
+ await ClientLibrary.initializeWasm('/fake/path/to/fp.js');
59
+
60
+ // 3. Vérifier que le hasher est toujours l'implémentation JS
61
+ const jsHash = ClientLibrary._hasher("test");
62
+ const originalJsHash = (await import('../fingerprint.builder.js')).cyrb53("test");
63
+
64
+ expect(jsHash).toBe(originalJsHash);
65
+ expect(console.warn).toHaveBeenCalledWith(expect.stringContaining('WASM module failed to load'), expect.any(Error));
66
+ });
67
+
68
+ it('should gracefully fall back if WASM module does not export _hash_string', async () => {
69
+ // 1. Simuler un module WASM malformé (sans la fonction attendue)
70
+ const mockWasmModule = {};
71
+ window.createFingerprintModule = vi.fn().mockResolvedValue(mockWasmModule);
72
+
73
+ vi.spyOn(document.head, 'appendChild').mockImplementation((script) => {
74
+ script.onload();
75
+ return script;
76
+ });
77
+
78
+ // 2. Appeler la fonction d'initialisation
79
+ await ClientLibrary.initializeWasm('/fake/path/to/fp.js');
80
+
81
+ // 3. Vérifier le fallback
82
+ const jsHash = ClientLibrary._hasher("test");
83
+ const originalJsHash = (await import('../fingerprint.builder.js')).cyrb53("test");
84
+ expect(jsHash).toBe(originalJsHash);
85
+ // L'assertion est maintenant plus précise : elle vérifie le message générique ET le message d'erreur spécifique.
86
+ expect(console.warn).toHaveBeenCalledWith(
87
+ expect.stringContaining('WASM module failed to load'),
88
+ expect.objectContaining({ message: 'WASM module did not export _hash_string.' })
89
+ );
90
+ });
91
+
92
+ it('should successfully load raw WASM and use standalone polymorphic hasher', async () => {
93
+ const mockWasmInstance = {
94
+ exports: {
95
+ memory: { buffer: new ArrayBuffer(65536) },
96
+ hash: vi.fn().mockReturnValue(123456)
97
+ }
98
+ };
99
+
100
+ global.WebAssembly = {
101
+ compile: vi.fn().mockResolvedValue({}),
102
+ instantiate: vi.fn().mockResolvedValue(mockWasmInstance)
103
+ };
104
+
105
+ global.fetch = vi.fn().mockResolvedValue({
106
+ arrayBuffer: vi.fn().mockResolvedValue(new ArrayBuffer(100))
107
+ });
108
+
109
+ await ClientLibrary.initializeWasm('/fp.wasm');
110
+
111
+ const hash = ClientLibrary._hasher("test");
112
+ expect(hash).toBe(123456);
113
+ expect(mockWasmInstance.exports.hash).toHaveBeenCalledWith(0, 4);
114
+ });
115
+
116
+ it('should use the default JS hasher if WASM is not configured', async () => {
117
+ // Ensure no WASM path is provided in the config
118
+ ClientLibrary.initializeClient({});
119
+
120
+ // Verify that the active hasher is the original JS implementation
121
+ const jsHash = ClientLibrary._hasher("another test string");
122
+ const originalJsHash = (await import('../fingerprint.builder.js')).cyrb53("another test string");
123
+
124
+ expect(jsHash).toBe(originalJsHash);
125
+ // Ensure no WASM-related console logs or warnings were made
126
+ expect(console.log).not.toHaveBeenCalledWith(expect.stringContaining('WASM module loaded successfully'));
127
+ expect(console.warn).not.toHaveBeenCalledWith(expect.stringContaining('WASM module failed to load'));
128
+ });
129
129
  });