fix(deploy): 跨域来源未配置时默认放行
Test deployment public URL (Linux) / test-linux (push) Waiting to run
Test start.ps1 (Windows) / test-windows (push) Waiting to run

This commit is contained in:
2026-09-27 18:00:03 +08:00
parent a00ee10b37
commit 89cc554c04
12 changed files with 62 additions and 50 deletions
+22 -16
View File
@@ -1,4 +1,4 @@
# 升级旧部署时从公开 API 地址补齐浏览器来源,并校验显式配置。
# 浏览器来源为可选项:缺失或留空时保持宽松策略,显式配置时校验。
. (Join-Path $PSScriptRoot "Initialize-PublicApiBaseUrl.ps1")
function Initialize-SecurityOrigin {
@@ -7,26 +7,32 @@ function Initialize-SecurityOrigin {
$content = Get-Content $Path -Raw -Encoding UTF8
if ($null -eq $content) { $content = "" }
$apiUrl = Get-PublicEnvValue $content "CONFIG_PUBLIC_API_BASE_URL"
if (-not (ConvertTo-PublicApiBaseUrl $apiUrl)) {
throw "Configure a valid CONFIG_PUBLIC_API_BASE_URL first"
}
$apiUri = [Uri]$apiUrl
$derived = $apiUri.GetLeftPart([UriPartial]::Authority)
$current = Get-PublicEnvValue $content "CONFIG_SECURITY_ORIGIN"
if (-not $current -or
($current -eq "http://127.0.0.1,http://localhost" -and $derived -ne "http://127.0.0.1")) {
$content = Set-PublicEnvValue $content "CONFIG_SECURITY_ORIGIN" $derived
[System.IO.File]::WriteAllText(
(Resolve-Path $Path),
$content,
[System.Text.UTF8Encoding]::new($false)
)
Write-Host " [OK] Browser origin: $derived" -ForegroundColor Green
if (-not $current) {
Write-Host " [OK] Browser origin is not configured; backend allows all origins" -ForegroundColor Green
return
}
# The legacy sample value was not a user-selected allowlist. Clear it on non-local upgrades.
if ($current -eq "http://127.0.0.1,http://localhost") {
$apiUrl = Get-PublicEnvValue $content "CONFIG_PUBLIC_API_BASE_URL"
$normalizedApiUrl = ConvertTo-PublicApiBaseUrl $apiUrl
if ($normalizedApiUrl) {
$derived = ([Uri]$normalizedApiUrl).GetLeftPart([UriPartial]::Authority)
if ($derived -notin @('http://127.0.0.1', 'http://localhost')) {
$content = Set-PublicEnvValue $content "CONFIG_SECURITY_ORIGIN" ""
[System.IO.File]::WriteAllText(
(Resolve-Path $Path),
$content,
[System.Text.UTF8Encoding]::new($false)
)
Write-Host " [OK] Cleared legacy browser-origin sample; backend allows all origins" -ForegroundColor Green
return
}
}
}
if ($current.StartsWith(',') -or $current.EndsWith(',') -or $current.Contains(',,')) {
throw "CONFIG_SECURITY_ORIGIN contains an empty origin"
}
+4 -2
View File
@@ -6,16 +6,18 @@ New-Item -ItemType Directory -Path $tempDir | Out-Null
try {
$path = Join-Path $tempDir 'test.env'
[System.IO.File]::WriteAllText($path, "CONFIG_PUBLIC_API_BASE_URL=https://zaowua.com/api`n")
$before = Get-Content $path -Raw
Initialize-SecurityOrigin -Path $path
$content = Get-Content $path -Raw
if ((Get-PublicEnvValue $content 'CONFIG_SECURITY_ORIGIN') -ne 'https://zaowua.com') { throw 'Missing origin was not initialized' }
if ((Get-PublicEnvValue $content 'CONFIG_SECURITY_ORIGIN')) { throw 'Missing origin was unexpectedly initialized' }
if ($content -ne $before) { throw 'Missing origin changed the environment' }
$before = $content
Initialize-SecurityOrigin -Path $path
if ((Get-Content $path -Raw) -ne $before) { throw 'Repeated initialization changed the environment' }
[System.IO.File]::WriteAllText($path, "CONFIG_PUBLIC_API_BASE_URL=https://zaowua.com/api`nCONFIG_SECURITY_ORIGIN=http://127.0.0.1,http://localhost`n")
Initialize-SecurityOrigin -Path $path
if ((Get-PublicEnvValue (Get-Content $path -Raw) 'CONFIG_SECURITY_ORIGIN') -ne 'https://zaowua.com') { throw 'Sample origin was not migrated' }
if ((Get-PublicEnvValue (Get-Content $path -Raw) 'CONFIG_SECURITY_ORIGIN')) { throw 'Sample origin was not cleared' }
[System.IO.File]::WriteAllText($path, "CONFIG_PUBLIC_API_BASE_URL=https://zaowua.com/api`nCONFIG_SECURITY_ORIGIN=https://zaowua.com,https://www.zaowua.com`n")
$before = Get-Content $path -Raw
+19 -18
View File
@@ -1,6 +1,6 @@
#!/usr/bin/env bash
# 升级旧部署时补齐浏览器来源;显式配置始终保留并校验。
# 浏览器来源为可选项:缺失或留空时保持宽松策略,显式配置时校验。
# 不 source .env,避免执行环境文件中的内容。
security_origin_script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
@@ -23,27 +23,28 @@ init_security_origin() {
return 1
}
api_url="$(public_url_read_env_value "$file" "CONFIG_PUBLIC_API_BASE_URL")"
public_url_validate "$api_url" || {
echo "❌ 请先配置有效的 CONFIG_PUBLIC_API_BASE_URL" >&2
return 1
}
scheme="${api_url%%://*}"
rest="${api_url#*://}"
authority="${rest%%/*}"
origin="${scheme}://${authority}"
security_origin_valid "$origin" || return 1
current="$(public_url_read_env_value "$file" "CONFIG_SECURITY_ORIGIN")"
if [ -z "$current" ] || {
[ "$current" = 'http://127.0.0.1,http://localhost' ] &&
[ "$origin" != 'http://127.0.0.1' ];
}; then
public_url_write_env_value "$file" "CONFIG_SECURITY_ORIGIN" "$origin"
echo " ✓ 已配置浏览器来源: $origin"
if [ -z "$current" ]; then
echo " ✓ 未配置浏览器来源,后端将默认允许所有来源"
return 0
fi
# 旧版样例值不是用户选择的白名单;非本机部署升级时清空,恢复缺省策略。
if [ "$current" = 'http://127.0.0.1,http://localhost' ]; then
api_url="$(public_url_read_env_value "$file" "CONFIG_PUBLIC_API_BASE_URL")"
if public_url_validate "$api_url"; then
scheme="${api_url%%://*}"
rest="${api_url#*://}"
authority="${rest%%/*}"
origin="${scheme}://${authority}"
if [ "$origin" != 'http://127.0.0.1' ] && [ "$origin" != 'http://localhost' ]; then
public_url_write_env_value "$file" "CONFIG_SECURITY_ORIGIN" ""
echo " ✓ 已清理历史浏览器来源样例值,后端将默认允许所有来源"
return 0
fi
fi
fi
local -a origins
case "$current" in
,*|*,|*,,*)
+1 -1
View File
@@ -246,7 +246,7 @@ grep -qx 'NUXT_PUBLIC_BASE_APIURL=/api' .env
grep -qx 'NUXT_PUBLIC_BASE_SOCKETURL=ws://10.0.0.8/socket.io' .env
grep -qx 'NUXT_PUBLIC_SG_APIURL=/asg-api' .env
grep -qx 'CONFIG_PUBLIC_API_BASE_URL=http://10.0.0.8/api' .env
grep -qx 'CONFIG_SECURITY_ORIGIN=http://10.0.0.8' .env
grep -qx 'CONFIG_SECURITY_ORIGIN=' .env
grep -qx 'EASYAI_PROXY_BIND_IP=127.0.0.1' .env
grep -qx 'EASYAI_INFRA_BIND_IP=127.0.0.1' .env
grep -qx 'GATEWAY_INBOUND_TCP_LISTEN_HOST=172.21.0.6' .env
+4 -2
View File
@@ -16,8 +16,10 @@ assert_value() {
cat > "$tmp_dir/missing.env" <<'ENV'
CONFIG_PUBLIC_API_BASE_URL=https://zaowua.com/api
ENV
before="$(shasum -a 256 "$tmp_dir/missing.env" | cut -d ' ' -f 1)"
init_security_origin "$tmp_dir/missing.env" > /dev/null
assert_value "$tmp_dir/missing.env" 'https://zaowua.com'
assert_value "$tmp_dir/missing.env" ''
[ "$before" = "$(shasum -a 256 "$tmp_dir/missing.env" | cut -d ' ' -f 1)" ]
first_hash="$(shasum -a 256 "$tmp_dir/missing.env" | cut -d ' ' -f 1)"
init_security_origin "$tmp_dir/missing.env" > /dev/null
[ "$first_hash" = "$(shasum -a 256 "$tmp_dir/missing.env" | cut -d ' ' -f 1)" ]
@@ -27,7 +29,7 @@ CONFIG_PUBLIC_API_BASE_URL=https://zaowua.com/api
CONFIG_SECURITY_ORIGIN=http://127.0.0.1,http://localhost
ENV
init_security_origin "$tmp_dir/sample.env" > /dev/null
assert_value "$tmp_dir/sample.env" 'https://zaowua.com'
assert_value "$tmp_dir/sample.env" ''
cat > "$tmp_dir/preserved.env" <<'ENV'
CONFIG_PUBLIC_API_BASE_URL=https://zaowua.com/api