修复发布构建本机路径泄漏并补齐产物检查

This commit is contained in:
xuans committed 2026-09-20 02:01:06 +08:00
1 parent ba6c6d13e5
commit a21240109a
13 files changed
+436 -28

No files matched your search

+10 -1
View File
@@ -14,6 +14,7 @@ build/release/
│ └── linux-amd64/
│ ├── scert
│ ├── scert-admin
│ ├── scert-nginx
│ ├── web/
│ └── SHA256SUMS
├── Scert-20260915-V0.1.0-linux-amd64.tar.gz
@@ -52,12 +53,20 @@ powershell -ExecutionPolicy Bypass -File .\build\build-release.ps1 -OutputDirect
`manifest.json` 记录源码提交、已跟踪文件是否未提交、实际 Go 工具链、目标平台、是否请求桌面端以及文件和归档的 SHA256。`source_dirty=true` 的包仅用于开发,正式发布必须从已复审提交构建。未跟踪本地产物不计入源码脏状态,也不会自动打包。
## 本机路径保护
核心服务、辅助程序和 Wails 桌面版统一使用 `-trimpath`。手动构建也必须保留此参数,例如 `go build -trimpath -o scert ./cmd/scert` 和 `wails build -trimpath -nopackage`。前端生产构建明确关闭源码映射。
归档前逐个读取 Go 二进制构建信息,确认 `-trimpath=true`;同时扫描发布目录全部文件,拒绝工作区、用户目录、Windows 的 Go/Node 安装目录、实际 Go 工具链、模块缓存、构建缓存、临时目录和输出目录路径残留。Linux 的通用 `/usr/bin` 等运行时目录不因安装工具而被禁用。检查覆盖正反斜杠、JSON 转义、UTF-8 和 UTF-16。发现残留或无法读取构建信息时停止归档,保留失败目录用于排查。
清单仅记录 `trimpath=true` 和 `local_path_check=passed`,不会记录本机扫描路径。该检查不删除模块导入路径、相对源码文件名或程序运行时需要的部署路径,也不保证识别源码中人为写死且不属于本次构建环境的其他机器路径。
仅构建本轮 Linux 服务候选包示例:
```powershell
powershell -ExecutionPolicy Bypass -File .\build\build-release.ps1 -Target linux-amd64 -SkipDesktop -OutputDirectory .\build\release-candidate-20260915
```
此命令不构建 Windows 或 Wails 桌面端,不代表跨平台发布验收。归档包含 `scert`、`scert-admin`、Web 静态资源、安装器、systemd 模板和部署说明。解压后先运行 `sha256sum -c SHA256SUMS`,再按部署说明操作。
此命令不构建 Windows 或 Wails 桌面端,不代表跨平台发布验收。归档包含 `scert`、`scert-admin`、`scert-nginx`、Web 静态资源、安装器、systemd 模板和部署说明。解压后先运行 `sha256sum -c SHA256SUMS`,再按部署说明操作。`scert-nginx` 仅随包提供,普通安装器不会安装或启用它;启用前仍需单独完成 root 策略、本地 socket、Nginx 接线和实机验收。
构建通过不等于生产验收通过;真实 DNS、ACME、SSH、Windows Service、systemd、WebView2 和 HTTPS 仍需在授权环境复核。
+64 -4
View File
@@ -107,7 +107,7 @@ function Invoke-DesktopBuild {
if ($TargetName.StartsWith("linux-")) {
$env:CGO_ENABLED = "1"
}
& wails build -nopackage -platform ($TargetName.Replace("-", "/")) -o "ScertDesktop"
& wails build -trimpath -nopackage -platform ($TargetName.Replace("-", "/")) -o "ScertDesktop"
if ($LASTEXITCODE -ne 0) {
throw "构建 $TargetName 的 Scert 桌面版失败。"
}
@@ -194,6 +194,7 @@ function Invoke-ReleaseArchive {
$arguments = @(
"run",
"-trimpath",
"./cmd/scert-release",
"--source", $Source,
"--output", $Destination,
@@ -222,6 +223,42 @@ function Invoke-ReleaseArchive {
}
}
function Test-ReleasePaths {
param(
[Parameter(Mandatory = $true)]
[string]$Source,
[Parameter(Mandatory = $true)]
[string[]]$Binaries,
[Parameter(Mandatory = $true)]
[string[]]$ForbiddenPaths
)
$arguments = @("run", "-trimpath", "./cmd/scert-release", "--verify-only", "--source", $Source)
foreach ($binary in $Binaries) {
$arguments += @("--binary", $binary)
}
foreach ($path in ($ForbiddenPaths | Where-Object { -not [string]::IsNullOrWhiteSpace($_) } | Select-Object -Unique)) {
$arguments += @("--forbid-path", $path)
}
$checkGoOS = $env:GOOS
$checkGoArch = $env:GOARCH
$checkCGOEnabled = $env:CGO_ENABLED
try {
$env:GOOS = ""
$env:GOARCH = ""
$env:CGO_ENABLED = "0"
& go @arguments
if ($LASTEXITCODE -ne 0) {
throw "发布路径检查未通过,停止归档。"
}
}
finally {
$env:GOOS = $checkGoOS
$env:GOARCH = $checkGoArch
$env:CGO_ENABLED = $checkCGOEnabled
}
}
$backend = Resolve-SafeDirectory -Path $BackendDirectory -Label "后端目录"
$output = Resolve-SafeDirectory -Path $OutputDirectory -Label "输出目录"
$desktop = Resolve-SafeDirectory -Path $DesktopDirectory -Label "桌面项目目录"
@@ -313,6 +350,22 @@ try {
if ($LASTEXITCODE -ne 0) {
throw "无法取得实际构建使用的 Go 工具链版本。"
}
$goEnvironmentJSON = (& go env -json GOROOT GOPATH GOMODCACHE GOCACHE GOTMPDIR)
if ($LASTEXITCODE -ne 0) {
throw "无法取得本机构建路径,拒绝发布。"
}
$goEnvironment = ($goEnvironmentJSON -join "`n") | ConvertFrom-Json
$forbiddenPaths = @(
(Split-Path -Parent $scriptDirectory), $backend, $desktop, $frontend, $deploy, $output,
$env:USERPROFILE, $env:HOME, [System.IO.Path]::GetTempPath(),
$goEnvironment.GOROOT, $goEnvironment.GOMODCACHE, $goEnvironment.GOCACHE, $goEnvironment.GOTMPDIR
) + $goEnvironment.GOPATH.Split([System.IO.Path]::PathSeparator)
if ($env:OS -eq "Windows_NT") {
$forbiddenPaths += @(
(Split-Path -Parent (Get-Command go).Source),
(Split-Path -Parent (Get-Command node).Source)
)
}
foreach ($item in $Target) {
$parts = $item.Split("-", 2)
$goos = $parts[0]
@@ -323,10 +376,15 @@ try {
$extension = if ($goos -eq "windows") { ".exe" } else { "" }
$scertPath = Join-Path $targetDirectory ("scert" + $extension)
$adminPath = Join-Path $targetDirectory ("scert-admin" + $extension)
$binaries = @(("scert" + $extension), ("scert-admin" + $extension))
Write-Host "正在构建 $goos/$goarch 的 Scert……"
Invoke-GoBuild -OperatingSystem $goos -Architecture $goarch -Destination $scertPath -Package "./cmd/scert"
Invoke-GoBuild -OperatingSystem $goos -Architecture $goarch -Destination $adminPath -Package "./cmd/scert-admin"
if ($goos -eq "linux") {
Invoke-GoBuild -OperatingSystem $goos -Architecture $goarch -Destination (Join-Path $targetDirectory "scert-nginx") -Package "./cmd/scert-nginx"
$binaries += "scert-nginx"
}
$webPath = Join-Path $targetDirectory "web"
New-Item -ItemType Directory -Path $webPath -Force | Out-Null
@@ -346,6 +404,7 @@ try {
$desktopExtension = if ($goos -eq "windows") { ".exe" } else { "" }
$desktopPath = Join-Path $targetDirectory ("ScertDesktop" + $desktopExtension)
Invoke-DesktopBuild -ProjectDirectory $desktop -TargetName $item -Destination $desktopPath
$binaries += ("ScertDesktop" + $desktopExtension)
$file = Get-Item -LiteralPath $desktopPath
$hash = Get-FileHash -LiteralPath $desktopPath -Algorithm SHA256
$manifestArtifacts.Add([ordered]@{
@@ -361,6 +420,7 @@ try {
}
}
Test-ReleasePaths -Source $targetDirectory -Binaries $binaries -ForbiddenPaths $forbiddenPaths
Write-TargetChecksums -TargetDirectory $targetDirectory | Out-Null
foreach ($file in (Get-ChildItem -LiteralPath $targetDirectory -Recurse -File | Sort-Object FullName)) {
$hash = Get-FileHash -LiteralPath $file.FullName -Algorithm SHA256
@@ -375,9 +435,7 @@ try {
if ($goos -eq "linux") {
$archivePath = Join-Path $output "$releaseName-$item.tar.gz"
Invoke-ReleaseArchive -Source $targetDirectory -Destination $archivePath -Format "tar.gz" -Executable @(
"install-linux.sh", "scert", "scert-admin"
)
Invoke-ReleaseArchive -Source $targetDirectory -Destination $archivePath -Format "tar.gz" -Executable (@("install-linux.sh") + $binaries)
}
else {
$archivePath = Join-Path $output "$releaseName-$item.zip"
@@ -408,6 +466,8 @@ $manifest = [ordered]@{
source_commit = $sourceCommit.Trim()
source_dirty = $sourceDirty
go_toolchain = $goVersion.Trim()
trimpath = $true
local_path_check = "passed"
targets = @($Target)
desktop_requested = -not [bool]$SkipDesktop
generated_at = [DateTimeOffset]::UtcNow.ToString("o")