GastinelTpa : correctif securite pseudos (pas de 'exact gagne' en cas de collision, boutons TPA par UUID)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Cette révision appartient à :
Theo GastineletClaude Sonnet 5.5 a révisé 2026-10-11 00:26:21 +02:00
1 Parent 4b7f16c5af
révision 184dbbf82a
3 fichiers modifiés
+113 -30

Aucun fichier ne correspond à votre recherche

+12 -2
Voir le fichier
@@ -150,9 +150,9 @@ public final class TpaCommands {
.append(Component.literal(type == Type.TPA .append(Component.literal(type == Type.TPA
? nomMoi + " souhaite se téléporter vers toi. " ? nomMoi + " souhaite se téléporter vers toi. "
: nomMoi + " souhaite que tu te téléportes vers lui. ").withStyle(ChatFormatting.YELLOW)) : nomMoi + " souhaite que tu te téléportes vers lui. ").withStyle(ChatFormatting.YELLOW))
.append(bouton("[Accepter]", "/tpaccept " + PseudoMatcher.tapable(nomMoi), ChatFormatting.GREEN, "Accepter la demande")) .append(bouton("[Accepter]", "/tpaccept " + PseudoMatcher.argumentBouton(moi.getUUID(), nomMoi), ChatFormatting.GREEN, "Accepter la demande"))
.append(Component.literal(" ")) .append(Component.literal(" "))
.append(bouton("[Refuser]", "/tpadeny " + PseudoMatcher.tapable(nomMoi), ChatFormatting.RED, "Refuser la demande")) .append(bouton("[Refuser]", "/tpadeny " + PseudoMatcher.argumentBouton(moi.getUUID(), nomMoi), ChatFormatting.RED, "Refuser la demande"))
.append(Component.literal(" (expire dans 60 s)").withStyle(ChatFormatting.GRAY)); .append(Component.literal(" (expire dans 60 s)").withStyle(ChatFormatting.GRAY));
cible.sendSystemMessage(msg); cible.sendSystemMessage(msg);
return 1; return 1;
@@ -171,6 +171,13 @@ public final class TpaCommands {
if (trouvee.isEmpty()) { if (trouvee.isEmpty()) {
return echec(ctx, "Aucune demande de téléportation en attente."); return echec(ctx, "Aucune demande de téléportation en attente.");
} }
} else if (PseudoMatcher.parseUuidStrict(nomDemandeur).isPresent()) {
// Bouton / UUID : recherche directe dans le registre des demandes, jamais par pseudo.
UUID id = PseudoMatcher.parseUuidStrict(nomDemandeur).get();
trouvee = REGISTRE.find(moi.getUUID(), id);
if (trouvee.isEmpty()) {
return echec(ctx, "Aucune demande en attente de la part de ce joueur (peut-être expirée).");
}
} else { } else {
Cherche c = chercherEnLigne(server, nomDemandeur); Cherche c = chercherEnLigne(server, nomDemandeur);
if (c.ambigu != null) return echec(ctx, c.ambigu); if (c.ambigu != null) return echec(ctx, c.ambigu);
@@ -220,6 +227,9 @@ public final class TpaCommands {
if (nomCible == null) { if (nomCible == null) {
annulee = REGISTRE.cancel(moi.getUUID(), null); annulee = REGISTRE.cancel(moi.getUUID(), null);
if (annulee.isEmpty()) return echec(ctx, "Tu n'as aucune demande de téléportation en cours à annuler."); if (annulee.isEmpty()) return echec(ctx, "Tu n'as aucune demande de téléportation en cours à annuler.");
} else if (PseudoMatcher.parseUuidStrict(nomCible).isPresent()) {
annulee = REGISTRE.cancel(moi.getUUID(), PseudoMatcher.parseUuidStrict(nomCible).get());
if (annulee.isEmpty()) return echec(ctx, "Tu n'as aucune demande en cours vers ce joueur.");
} else { } else {
Cherche c = chercherEnLigne(server, nomCible); Cherche c = chercherEnLigne(server, nomCible);
if (c.ambigu != null) return echec(ctx, c.ambigu); if (c.ambigu != null) return echec(ctx, c.ambigu);
+56 -23
Voir le fichier
@@ -46,6 +46,30 @@ public final class PseudoMatcher {
return sb.toString(); 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<UUID> 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. */ /** Forme tapable dans un argument word() de Brigadier : [0-9A-Za-z_.+-] seulement. */
public static String tapable(String nom) { public static String tapable(String nom) {
if (nom == null) return ""; if (nom == null) return "";
@@ -75,32 +99,41 @@ public final class PseudoMatcher {
public static Resultat resoudre(String saisie, Collection<Profil> enLigne, Collection<Profil> cache) { public static Resultat resoudre(String saisie, Collection<Profil> enLigne, Collection<Profil> cache) {
if (saisie == null || saisie.isEmpty()) return INCONNU; if (saisie == null || saisie.isEmpty()) return INCONNU;
Resultat r = etape(saisie, dedoublonner(enLigne), false); List<Profil> ligne = dedoublonner(enLigne);
if (r.statut() != Statut.INCONNU) return r; List<Profil> tous = dedoublonner(concat(ligne, cache));
return etape(saisie, dedoublonner(cache), true); String norm = normaliser(saisie);
List<Profil> 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<Profil> 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<Profil> 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;
} }
/** private static List<Profil> filtrerNormalise(List<Profil> profils, String norm) {
* strict (cache) : un pseudo exact qui partage sa forme normalisée avec un AUTRE compte reste AMBIGU List<Profil> out = new ArrayList<>();
* (profils fantômes d'anciens essais) : on ne désigne pas un compte au hasard. for (Profil p : profils) if (normaliser(p.nom()).equals(norm)) out.add(p);
*/ return out;
private static Resultat etape(String saisie, List<Profil> profils, boolean strict) { }
List<Profil> exacts = new ArrayList<>();
for (Profil p : profils) if (p.nom().equalsIgnoreCase(saisie)) exacts.add(p);
String norm = normaliser(saisie);
List<Profil> proches = new ArrayList<>();
if (!norm.isEmpty()) for (Profil p : profils) if (normaliser(p.nom()).equals(norm)) proches.add(p);
if (exacts.size() == 1 && (!strict || proches.size() <= 1)) { private static List<Profil> concat(Collection<Profil> a, Collection<Profil> b) {
return new Resultat(Statut.TROUVE, exacts.get(0), List.of(exacts.get(0)), false); List<Profil> l = new ArrayList<>(a);
} l.addAll(b);
if (!exacts.isEmpty()) { return l;
// 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<Profil> dedoublonner(Collection<Profil> profils) { private static List<Profil> dedoublonner(Collection<Profil> profils) {
+45 -5
Voir le fichier
@@ -37,7 +37,7 @@ class PseudoMatcherTest {
@Test @Test
void sansGuillemetVisePseudoAvecGuillemetEnLigne() { 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(Statut.TROUVE, r.statut());
assertEquals(GABY, r.profil().id()); assertEquals(GABY, r.profil().id());
assertEquals("gabybou201\"", r.profil().nom()); assertEquals("gabybou201\"", r.profil().nom());
@@ -45,20 +45,60 @@ class PseudoMatcherTest {
} }
@Test @Test
void enLigneEstPrioritaireSurLeCacheMemeAvecFantome() { void enLigneEstPrioritaireSurLeCacheSansExact() {
var r = PseudoMatcher.resoudre("GabyBou201", List.of(GABY_P), List.of(FANTOME_P, BIZARRE_P, GABY_P)); var r = PseudoMatcher.resoudre("GabyBou201", List.of(GABY_P), List.of(BIZARRE_P, GABY_P));
assertTrue(r.trouve()); assertTrue(r.trouve());
assertEquals(GABY, r.profil().id()); assertEquals(GABY, r.profil().id());
} }
@Test @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()); 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()); assertTrue(r.trouve());
assertEquals(GABY_FANTOME, r.profil().id()); assertEquals(THEO, r.profil().id());
assertFalse(r.approximatif()); 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 @Test
void deuxEnLigneProchesSansExactEstAmbigu() { void deuxEnLigneProchesSansExactEstAmbigu() {
var r = PseudoMatcher.resoudre("gabybou201", List.of(GABY_P, BIZARRE_P), List.of()); var r = PseudoMatcher.resoudre("gabybou201", List.of(GABY_P, BIZARRE_P), List.of());