Files
saasshop/app/Support/BackUrl.php

37 lines
941 B
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Support;
class BackUrl
{
/**
* back 参数安全护栏(用于 Blade 中 `{!! !!}` 输出的 href 场景):
* - 仅允许站内相对路径(/ 开头)
* - 拒绝引号/尖括号(防属性注入/XSS
* - 拒绝 nested back=(防 URL 膨胀/绕过)
*/
public static function sanitizeForLinks(string $incomingBack): string
{
$incomingBack = (string) $incomingBack;
if ($incomingBack === '') {
return '';
}
if (!str_starts_with($incomingBack, '/')) {
return '';
}
if (preg_match('/["\'<>]/', $incomingBack)) {
return '';
}
// 拒绝 back 自身再包含 back=(避免无限嵌套导致 URL 膨胀,且容易绕过页面侧护栏)
if (preg_match('/(?:^|[?&])back=/', $incomingBack)) {
return '';
}
return $incomingBack;
}
}