From 57f3a208fa03669f378a1ee6f49073e221aa58a0 Mon Sep 17 00:00:00 2001 From: Theo Gastinel Date: Sun, 11 Oct 2026 00:26:21 +0200 Subject: [PATCH] GastinelClaims : correctif securite pseudos (pas de 'exact gagne' en cas de collision, boutons TPA par UUID) Co-Authored-By: Claude Sonnet 5.5 --- .../gastinelclaims/core/PseudoMatcher.java | 79 +++++++++++++------ .../core/PseudoMatcherTest.java | 50 ++++++++++-- 2 files changed, 101 insertions(+), 28 deletions(-) diff --git a/src/main/java/com/gastinelclaims/core/PseudoMatcher.java b/src/main/java/com/gastinelclaims/core/PseudoMatcher.java index 506b67d..5967c08 100644 --- a/src/main/java/com/gastinelclaims/core/PseudoMatcher.java +++ b/src/main/java/com/gastinelclaims/core/PseudoMatcher.java @@ -46,6 +46,30 @@ public final class PseudoMatcher { return sb.toString(); } + /** Vrai si le pseudo ne contient que [A-Za-z0-9_] (sinon les boutons doivent passer par l'UUID). */ + public static boolean pseudoSimple(String nom) { + if (nom == null || nom.isEmpty()) return false; + for (int i = 0; i < nom.length(); i++) { + char c = nom.charAt(i); + if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_')) return false; + } + return true; + } + + private static final java.util.regex.Pattern UUID_STRICT = java.util.regex.Pattern.compile( + "[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}"); + + /** Parse strict d'un UUID texte (36 caractères, format canonique) ; vide sinon. Jamais un pseudo. */ + public static java.util.Optional parseUuidStrict(String s) { + if (s == null || !UUID_STRICT.matcher(s).matches()) return java.util.Optional.empty(); + try { return java.util.Optional.of(UUID.fromString(s)); } catch (IllegalArgumentException e) { return java.util.Optional.empty(); } + } + + /** Argument à mettre dans un bouton : le pseudo s'il est simple, sinon l'UUID du joueur. */ + public static String argumentBouton(UUID id, String nom) { + return pseudoSimple(nom) ? nom : id.toString(); + } + /** Forme tapable dans un argument word() de Brigadier : [0-9A-Za-z_.+-] seulement. */ public static String tapable(String nom) { if (nom == null) return ""; @@ -75,32 +99,41 @@ public final class PseudoMatcher { public static Resultat resoudre(String saisie, Collection enLigne, Collection cache) { if (saisie == null || saisie.isEmpty()) return INCONNU; - Resultat r = etape(saisie, dedoublonner(enLigne), false); - if (r.statut() != Statut.INCONNU) return r; - return etape(saisie, dedoublonner(cache), true); + List ligne = dedoublonner(enLigne); + List tous = dedoublonner(concat(ligne, cache)); + String norm = normaliser(saisie); + + List exacts = new ArrayList<>(); + for (Profil p : tous) if (p.nom().equalsIgnoreCase(saisie)) exacts.add(p); + if (!exacts.isEmpty()) { + // Jamais de « l'exact gagne » silencieux : si un AUTRE compte (en ligne ou en cache) a la même forme + // normalisée (usurpateur du pseudo sans guillemet, profil fantôme...), on refuse et on liste. + List candidats = new ArrayList<>(exacts); + if (!norm.isEmpty()) { + for (Profil p : tous) if (!candidats.contains(p) && normaliser(p.nom()).equals(norm)) candidats.add(p); + } + if (candidats.size() == 1) return new Resultat(Statut.TROUVE, exacts.get(0), List.of(exacts.get(0)), false); + return new Resultat(Statut.AMBIGU, null, candidats, false); + } + if (norm.isEmpty()) return INCONNU; + // Pas d'exact : forme normalisée, joueurs en ligne d'abord, puis cache. + List proches = filtrerNormalise(ligne, norm); + if (proches.isEmpty()) proches = filtrerNormalise(tous, norm); + if (proches.size() == 1) return new Resultat(Statut.TROUVE, proches.get(0), proches, true); + if (proches.size() > 1) return new Resultat(Statut.AMBIGU, null, proches, true); + return INCONNU; } - /** - * strict (cache) : un pseudo exact qui partage sa forme normalisée avec un AUTRE compte reste AMBIGU - * (profils fantômes d'anciens essais) : on ne désigne pas un compte au hasard. - */ - private static Resultat etape(String saisie, List profils, boolean strict) { - List exacts = new ArrayList<>(); - for (Profil p : profils) if (p.nom().equalsIgnoreCase(saisie)) exacts.add(p); - String norm = normaliser(saisie); - List proches = new ArrayList<>(); - if (!norm.isEmpty()) for (Profil p : profils) if (normaliser(p.nom()).equals(norm)) proches.add(p); + private static List filtrerNormalise(List profils, String norm) { + List out = new ArrayList<>(); + for (Profil p : profils) if (normaliser(p.nom()).equals(norm)) out.add(p); + return out; + } - if (exacts.size() == 1 && (!strict || proches.size() <= 1)) { - return new Resultat(Statut.TROUVE, exacts.get(0), List.of(exacts.get(0)), false); - } - if (!exacts.isEmpty()) { - // plusieurs exacts (casse différente) ou exact + comptes proches dans le cache : jamais au hasard - return new Resultat(Statut.AMBIGU, null, List.copyOf(strict ? proches : exacts), false); - } - if (proches.size() == 1) return new Resultat(Statut.TROUVE, proches.get(0), proches, true); - if (proches.size() > 1) return new Resultat(Statut.AMBIGU, null, List.copyOf(proches), true); - return INCONNU; + private static List concat(Collection a, Collection b) { + List l = new ArrayList<>(a); + l.addAll(b); + return l; } private static List dedoublonner(Collection profils) { diff --git a/src/test/java/com/gastinelclaims/core/PseudoMatcherTest.java b/src/test/java/com/gastinelclaims/core/PseudoMatcherTest.java index 774bbc3..0b7b5ad 100644 --- a/src/test/java/com/gastinelclaims/core/PseudoMatcherTest.java +++ b/src/test/java/com/gastinelclaims/core/PseudoMatcherTest.java @@ -37,7 +37,7 @@ class PseudoMatcherTest { @Test void sansGuillemetVisePseudoAvecGuillemetEnLigne() { - var r = PseudoMatcher.resoudre("gabybou201", List.of(THEO_P, GABY_P), List.of(FANTOME_P, GABY_P)); + var r = PseudoMatcher.resoudre("gabybou201", List.of(THEO_P, GABY_P), List.of(GABY_P)); assertEquals(Statut.TROUVE, r.statut()); assertEquals(GABY, r.profil().id()); assertEquals("gabybou201\"", r.profil().nom()); @@ -45,20 +45,60 @@ class PseudoMatcherTest { } @Test - void enLigneEstPrioritaireSurLeCacheMemeAvecFantome() { - var r = PseudoMatcher.resoudre("GabyBou201", List.of(GABY_P), List.of(FANTOME_P, BIZARRE_P, GABY_P)); + void enLigneEstPrioritaireSurLeCacheSansExact() { + var r = PseudoMatcher.resoudre("GabyBou201", List.of(GABY_P), List.of(BIZARRE_P, GABY_P)); assertTrue(r.trouve()); assertEquals(GABY, r.profil().id()); } @Test - void exactEnLigneNeEstPasDetourne() { + void usurpateurExactEnLigneAvecVraiCompteEnLigneEstAmbigu() { + // l'usurpateur « gabybou201 » (sans guillemet) et le vrai « gabybou201" » sont connectés : jamais « l'exact gagne » var r = PseudoMatcher.resoudre("gabybou201", List.of(FANTOME_P, GABY_P), List.of()); + assertEquals(Statut.AMBIGU, r.statut()); + assertEquals(2, r.candidats().size()); + assertNull(r.profil()); + } + + @Test + void exactEnLigneAvecVraiCompteEnCacheEstAmbigu() { + // usurpateur « gabybou201 » en ligne, vrai « gabybou201" » hors ligne (cache) + var r = PseudoMatcher.resoudre("gabybou201", List.of(FANTOME_P), List.of(GABY_P)); + assertEquals(Statut.AMBIGU, r.statut()); + assertEquals(2, r.candidats().size()); + } + + @Test + void exactEnCacheAvecUsurpateurEnLigneProcheEstAmbigu() { + var r = PseudoMatcher.resoudre("gabybou201", List.of(GABY_P), List.of(FANTOME_P)); + assertEquals(Statut.AMBIGU, r.statut()); + } + + @Test + void exactSansCollisionResteExact() { + var r = PseudoMatcher.resoudre("TOTOCHE10", List.of(THEO_P, GABY_P), List.of()); assertTrue(r.trouve()); - assertEquals(GABY_FANTOME, r.profil().id()); + assertEquals(THEO, r.profil().id()); assertFalse(r.approximatif()); } + @Test + void pseudoSimpleEtArgumentBouton() { + assertTrue(PseudoMatcher.pseudoSimple("Steve_1")); + assertFalse(PseudoMatcher.pseudoSimple("gabybou201\"")); + assertEquals("Steve_1", PseudoMatcher.argumentBouton(THEO, "Steve_1")); + assertEquals(GABY.toString(), PseudoMatcher.argumentBouton(GABY, "gabybou201\"")); + } + + @Test + void parseUuidStrict() { + assertEquals(GABY, PseudoMatcher.parseUuidStrict(GABY.toString()).orElseThrow()); + assertTrue(PseudoMatcher.parseUuidStrict("gabybou201").isEmpty()); + assertTrue(PseudoMatcher.parseUuidStrict("128c0f10964337ff941dafa49cdb30ec").isEmpty()); + assertTrue(PseudoMatcher.parseUuidStrict(GABY + "x").isEmpty()); + assertTrue(PseudoMatcher.parseUuidStrict(null).isEmpty()); + } + @Test void deuxEnLigneProchesSansExactEstAmbigu() { var r = PseudoMatcher.resoudre("gabybou201", List.of(GABY_P, BIZARRE_P), List.of());