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-07-25 20:49:09 +08:00
parent f31e349409
commit 385c509086
3 changed files with 47 additions and 4 deletions
+20 -2
View File
@@ -9,6 +9,17 @@ function New-SecuritySecret {
return [Convert]::ToBase64String($bytes)
}
function New-InitialAdminPassword {
$bytes = New-Object byte[] 10
$rng = [System.Security.Cryptography.RandomNumberGenerator]::Create()
try {
$rng.GetBytes($bytes)
} finally {
$rng.Dispose()
}
return ([BitConverter]::ToString($bytes) -replace "-", "").ToLowerInvariant()
}
function Get-EnvValue {
param([string]$Content, [string]$Key)
$match = [regex]::Match($Content, "(?m)^$([regex]::Escape($Key))=(.*)$")
@@ -52,8 +63,7 @@ function Initialize-SecurityEnv {
@{ Key = "CONFIG_SECURITY_CONFIG_ENCRYPTION_KEY"; Legacy = ""; MinLength = 32 },
@{ Key = "CONFIG_OTP_HASH_SECRET"; Legacy = ""; MinLength = 32 },
@{ Key = "CONFIG_AUDIT_HASH_PEPPER"; Legacy = ""; MinLength = 32 },
@{ Key = "CONFIG_AUDIT_INTEGRITY_KEY"; Legacy = ""; MinLength = 32 },
@{ Key = "CONFIG_INITIAL_ADMIN_PASSWORD"; Legacy = ""; MinLength = 12 }
@{ Key = "CONFIG_AUDIT_INTEGRITY_KEY"; Legacy = ""; MinLength = 32 }
)
}
@@ -66,6 +76,14 @@ function Initialize-SecurityEnv {
Write-Host " [OK] Initialized security secret: $($definition.Key)" -ForegroundColor Green
}
if ($Mode -eq "new") {
$adminPassword = Get-EnvValue $content "CONFIG_INITIAL_ADMIN_PASSWORD"
if ($adminPassword.Length -lt 12) {
$content = Set-EnvValue $content "CONFIG_INITIAL_ADMIN_PASSWORD" (New-InitialAdminPassword)
Write-Host " [OK] Initialized admin password: CONFIG_INITIAL_ADMIN_PASSWORD" -ForegroundColor Green
}
}
[System.IO.File]::WriteAllText(
(Resolve-Path $Path),
$content,
+26 -1
View File
@@ -16,6 +16,20 @@ generate_security_secret() {
return 1
}
generate_initial_admin_password() {
if command -v openssl >/dev/null 2>&1; then
# 10 random bytes encoded as hexadecimal: exactly 20 alphanumeric characters.
openssl rand -hex 10 | tr -d '\r\n'
return
fi
if [ -r /dev/urandom ]; then
od -An -N10 -tx1 /dev/urandom | tr -d ' \r\n'
return
fi
echo "❌ 无法生成初始管理员密码:需要 openssl 或 /dev/urandom" >&2
return 1
}
read_env_value() {
local file="$1"
local key="$2"
@@ -67,6 +81,17 @@ ensure_security_secret() {
echo " ✓ 已初始化安全密钥: ${key}"
}
ensure_initial_admin_password() {
local file="$1"
local current
current="$(read_env_value "$file" "CONFIG_INITIAL_ADMIN_PASSWORD")"
if [ "${#current}" -ge 12 ]; then
return 0
fi
write_env_value "$file" "CONFIG_INITIAL_ADMIN_PASSWORD" "$(generate_initial_admin_password)"
echo " ✓ 已初始化管理员密码: CONFIG_INITIAL_ADMIN_PASSWORD"
}
init_security_env() {
local file="${1:-.env}"
local mode="${2:-upgrade}"
@@ -85,7 +110,7 @@ init_security_env() {
ensure_security_secret "$file" "CONFIG_OTP_HASH_SECRET"
ensure_security_secret "$file" "CONFIG_AUDIT_HASH_PEPPER"
ensure_security_secret "$file" "CONFIG_AUDIT_INTEGRITY_KEY"
ensure_security_secret "$file" "CONFIG_INITIAL_ADMIN_PASSWORD" "" 12
ensure_initial_admin_password "$file"
fi
chmod 600 "$file"
}