@anonympins/fingerprint 0.3.6 → 0.3.8

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.
@@ -6,9 +6,9 @@ declare(strict_types=1);
6
6
  namespace Anonympins\Fingerprint\Utils;
7
7
 
8
8
  use Anonympins\Fingerprint\FingerprintBuilder;
9
- use Anonympins\Fingerprint\Store\StoreManager;
10
9
  use Anonympins\Fingerprint\Optimization\Optimization;
11
10
  use Anonympins\Fingerprint\RequestContext;
11
+ use Anonympins\Fingerprint\Store\StoreManager;
12
12
 
13
13
  /**
14
14
  * Classe utilitaire pour l'analyse des requêtes et le calcul des scores de suspicion.
@@ -906,10 +906,20 @@ class RequestUtils
906
906
  $subnetData = $store->get($key) ?? [
907
907
  'highScoreCount' => 0,
908
908
  'deviceIds' => [],
909
+ 'highScoreDevices' => [],
909
910
  'lastActivity' => 0
910
911
  ];
911
912
 
912
- $subnetData['highScoreCount']++;
913
+ if (!isset($subnetData['highScoreDevices'])) {
914
+ $subnetData['highScoreDevices'] = [];
915
+ }
916
+
917
+ $currentDeviceContributions = $subnetData['highScoreDevices'][$deviceId] ?? 0;
918
+ if ($currentDeviceContributions < 5 && $finalScore < 95) {
919
+ $subnetData['highScoreDevices'][$deviceId] = $currentDeviceContributions + 1;
920
+ $subnetData['highScoreCount']++;
921
+ }
922
+
913
923
  if (!in_array($deviceId, $subnetData['deviceIds'])) {
914
924
  $subnetData['deviceIds'][] = $deviceId;
915
925
  }
@@ -917,7 +927,12 @@ class RequestUtils
917
927
 
918
928
  // Limiter la taille du tableau des deviceIds pour éviter une consommation mémoire excessive.
919
929
  if (count($subnetData['deviceIds']) > 100) {
920
- array_shift($subnetData['deviceIds']);
930
+ $oldDeviceId = array_shift($subnetData['deviceIds']);
931
+ if (isset($subnetData['highScoreDevices'][$oldDeviceId])) {
932
+ $oldContributions = $subnetData['highScoreDevices'][$oldDeviceId];
933
+ $subnetData['highScoreCount'] = max(0, $subnetData['highScoreCount'] - $oldContributions);
934
+ unset($subnetData['highScoreDevices'][$oldDeviceId]);
935
+ }
921
936
  }
922
937
 
923
938
  // TTL de 24 heures pour les données de sous-réseau.
@@ -945,16 +960,28 @@ class RequestUtils
945
960
  return ['subnetScore' => 0.0];
946
961
  }
947
962
 
963
+ // Application d'une décroissance temporelle (demi-vie de 30 minutes soit 1800 secondes)
964
+ $now = time();
965
+ $inactivitySec = $now - ($subnetData['lastActivity'] ?? $now);
966
+ $halfLives = (int)floor($inactivitySec / 1800);
967
+
968
+ $highScoreCount = $subnetData['highScoreCount'] ?? 0;
969
+ $deviceCount = isset($subnetData['deviceIds']) ? count($subnetData['deviceIds']) : 0;
970
+
971
+ if ($halfLives > 0) {
972
+ $highScoreCount = max(0, (int)floor($highScoreCount / pow(2, $halfLives)));
973
+ $deviceCount = max(0, (int)floor($deviceCount / pow(2, $halfLives)));
974
+ }
975
+
948
976
  $score = 0.0;
949
977
 
950
978
  // Pénalité basée sur le nombre de devices uniques vus depuis ce sous-réseau.
951
- $deviceCount = count($subnetData['deviceIds']);
952
979
  if ($deviceCount > 10) {
953
980
  $score += min(80.0, ($deviceCount - 10) * 5);
954
981
  }
955
982
 
956
983
  // Pénalité basée sur le nombre de scores élevés enregistrés.
957
- $score += min(40.0, $subnetData['highScoreCount'] * 2);
984
+ $score += min(40.0, $highScoreCount * 2);
958
985
 
959
986
  return ['subnetScore' => min(100.0, $score)];
960
987
  }
@@ -28,9 +28,9 @@ if (!$autoloaded) {
28
28
  exit(1);
29
29
  }
30
30
 
31
- use Anonympins\Fingerprint\Utils\RequestUtils;
32
- use Anonympins\Fingerprint\Store\StoreManager;
33
31
  use Anonympins\Fingerprint\Optimization\Optimization;
32
+ use Anonympins\Fingerprint\Store\StoreManager;
33
+ use Anonympins\Fingerprint\Utils\RequestUtils;
34
34
 
35
35
  // 1. Récupération des arguments CLI
36
36
  $configPath = $argv[1] ?? null;