fix(backurl): avoid duplicating back query when already present

This commit is contained in:
萝卜
2026-03-14 21:17:02 +00:00
parent 24e4cb4b0b
commit 84e860e403
2 changed files with 32 additions and 0 deletions

View File

@@ -114,6 +114,11 @@ class BackUrl
return $base . '?' . $backQuery; return $base . '?' . $backQuery;
} }
// 若 path 自身已包含 back=(调用方误用),则不再追加,避免重复 back 造成 URL 膨胀/绕过。
if (preg_match('/(?:^|&)back=/', $qs)) {
return $base . '?' . $qs;
}
if ($preferFirst) { if ($preferFirst) {
return $base . '?' . $backQuery . '&' . $qs; return $base . '?' . $backQuery . '&' . $qs;
} }

View File

@@ -0,0 +1,27 @@
<?php
namespace Tests\Unit;
use App\Support\BackUrl;
use Illuminate\Support\Arr;
use Tests\TestCase;
class BackUrlWithBackNoDuplicateTest extends TestCase
{
public function test_with_back_should_not_duplicate_when_path_already_has_back_query(): void
{
$back = '/admin/platform-orders/2';
$path = '/admin/site-subscriptions/2?' . Arr::query([
'back' => $back,
'order_sync_status' => 'syncable',
]);
$url = BackUrl::withBack($path, $back);
$this->assertSame($path, $url);
// 防止重复 back= 出现两次
$this->assertSame(1, substr_count($url, 'back='));
}
}