Skip to content

Commit 3bd8409

Browse files
Carlos Garciaclaude
andcommitted
Corrige fallos de seguridad y robustez en la función slug() de Tools.
- Escapa caracteres especiales del separador en expresiones regulares - Mejora el manejo del truncamiento por maxLength evitando separadores finales - Valida y maneja cadenas vacías correctamente - Optimiza el orden de operaciones aplicando strtolower antes del procesamiento - Añade comentarios explicativos para mayor claridad del código 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 20bdada commit 3bd8409

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

Core/Tools.php

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -597,14 +597,32 @@ public static function siteUrl(): string
597597
*/
598598
public static function slug(string $text, string $separator = '-', int $maxLength = 0): string
599599
{
600-
$text = self::ascii($text);
601-
$text = preg_replace('/[^A-Za-z0-9]+/', $separator, $text);
602-
$text = preg_replace('/' . $separator . '{2,}/', $separator, $text);
600+
// Convertir a ASCII y a minúsculas
601+
$text = strtolower(self::ascii($text));
602+
603+
// Reemplazar caracteres no alfanuméricos con el separador
604+
$text = preg_replace('/[^a-z0-9]+/', $separator, $text);
605+
606+
// Escapar el separador para usar en regex y eliminar separadores consecutivos
607+
$escapedSeparator = preg_quote($separator, '/');
608+
$text = preg_replace('/' . $escapedSeparator . '{2,}/', $separator, $text);
609+
610+
// Eliminar separadores al inicio y final
603611
$text = trim($text, $separator);
604-
$text = strtolower($text);
605-
return $maxLength > 0 ?
606-
substr($text, 0, $maxLength) :
607-
$text;
612+
613+
// Si queda una cadena vacía, devolver una cadena vacía
614+
if ($text === '') {
615+
return '';
616+
}
617+
618+
// Aplicar límite de longitud si se especifica
619+
if ($maxLength > 0 && strlen($text) > $maxLength) {
620+
$text = substr($text, 0, $maxLength);
621+
// Limpiar separadores finales que puedan haber quedado tras el corte
622+
$text = rtrim($text, $separator);
623+
}
624+
625+
return $text;
608626
}
609627

610628
/**

0 commit comments

Comments
 (0)