fix(release-cleanup): 強化輸入驗證、請求逾時與錯誤處理

解決 AI review 的安全性與健壯性問題:
- 驗證 GITEA_SERVER_URL 為合法 http/https URL(降低 SSRF 風險)
- 驗證 GITEA_REPOSITORY 為 owner/repo 形式並拒絕路徑穿越
- 為所有 fetch 請求加上 30 秒逾時(AbortSignal.timeout),避免卡死
- fetchAllPages 解析前檢查 content-type,非 JSON 時拋出明確錯誤
- selectReleasesToDelete 對無效 created_at 防呆,避免 NaN 排序
- index.js 匯出 main 並加上直接執行守衛,錯誤輸出含類型與堆疊

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Jeffery
2026-06-26 10:55:45 +08:00
co-authored by Claude Opus 4.8
parent 1f27a46f7b
commit 154ab032bf
5 changed files with 96 additions and 12 deletions
+24 -5
View File
@@ -14,7 +14,7 @@ import { separator, fail } from './logger.js'
*
* @returns {Promise<void>} 流程完成時 resolve;設定驗證或讀取 API 失敗時 reject。
*/
async function main() {
export async function main() {
const config = loadConfig()
const client = new GiteaClient({ token: config.token })
@@ -24,7 +24,26 @@ async function main() {
separator()
}
main().catch((error) => {
fail(error.message)
process.exit(1)
})
/**
* 將錯誤輸出至 stderr,並盡量保留可供除錯的上下文(錯誤類型與堆疊),
* 以便區分設定驗證錯誤與網路/API 請求錯誤。
* @param {unknown} error 捕捉到的錯誤
*/
function reportFatal(error) {
if (error instanceof Error) {
fail(`${error.name}: ${error.message}`)
if (error.stack) {
process.stderr.write(`${error.stack}\n`)
}
} else {
fail(String(error))
}
}
// 僅在直接以 `node index.js` 執行時啟動主流程;被測試 import 時不自動執行,方便撰寫整合測試。
if (import.meta.url === `file://${process.argv[1]}`) {
main().catch((error) => {
reportFatal(error)
process.exit(1)
})
}