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 à :
1 Parent
4b7f16c5af
révision
184dbbf82a
3 fichiers modifiés
+110
-27
Aucun fichier ne correspond à votre recherche
@@ -150,9 +150,9 @@ public final class TpaCommands {
|
||||
.append(Component.literal(type == Type.TPA
|
||||
? nomMoi + " souhaite se téléporter vers toi. "
|
||||
: 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(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));
|
||||
cible.sendSystemMessage(msg);
|
||||
return 1;
|
||||
@@ -171,6 +171,13 @@ public final class TpaCommands {
|
||||
if (trouvee.isEmpty()) {
|
||||
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 {
|
||||
Cherche c = chercherEnLigne(server, nomDemandeur);
|
||||
if (c.ambigu != null) return echec(ctx, c.ambigu);
|
||||
@@ -220,6 +227,9 @@ public final class TpaCommands {
|
||||
if (nomCible == 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.");
|
||||
} 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 {
|
||||
Cherche c = chercherEnLigne(server, nomCible);
|
||||
if (c.ambigu != null) return echec(ctx, c.ambigu);
|
||||
|
||||
@@ -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<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. */
|
||||
public static String tapable(String nom) {
|
||||
if (nom == null) return "";
|
||||
@@ -75,34 +99,43 @@ public final class PseudoMatcher {
|
||||
|
||||
public static Resultat resoudre(String saisie, Collection<Profil> enLigne, Collection<Profil> 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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<Profil> profils, boolean strict) {
|
||||
List<Profil> exacts = new ArrayList<>();
|
||||
for (Profil p : profils) if (p.nom().equalsIgnoreCase(saisie)) exacts.add(p);
|
||||
List<Profil> ligne = dedoublonner(enLigne);
|
||||
List<Profil> tous = dedoublonner(concat(ligne, cache));
|
||||
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)) {
|
||||
return new Resultat(Statut.TROUVE, exacts.get(0), List.of(exacts.get(0)), false);
|
||||
}
|
||||
List<Profil> exacts = new ArrayList<>();
|
||||
for (Profil p : tous) if (p.nom().equalsIgnoreCase(saisie)) exacts.add(p);
|
||||
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);
|
||||
// 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, List.copyOf(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) {
|
||||
List<Profil> out = new ArrayList<>();
|
||||
for (Profil p : profils) if (normaliser(p.nom()).equals(norm)) out.add(p);
|
||||
return out;
|
||||
}
|
||||
|
||||
private static List<Profil> concat(Collection<Profil> a, Collection<Profil> b) {
|
||||
List<Profil> l = new ArrayList<>(a);
|
||||
l.addAll(b);
|
||||
return l;
|
||||
}
|
||||
|
||||
private static List<Profil> dedoublonner(Collection<Profil> profils) {
|
||||
Map<UUID, Profil> m = new LinkedHashMap<>();
|
||||
for (Profil p : profils) if (p != null && p.id() != null && p.nom() != null) m.putIfAbsent(p.id(), p);
|
||||
|
||||
@@ -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());
|
||||
|
||||
Référencer dans un nouveau ticket
Bloquer un utilisateur