refactor(calculate-version): 強化片段截斷記憶體安全與抽出區段驗證

- releases 在 Array.from 前先以長度上限截斷字串,避免將可能極大的回應整個陣列化
- config 將 repository 區段驗證抽為具名函式 isValidRepoSegment 提升可讀性

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Jeffery
2026-06-26 14:14:30 +08:00
co-authored by Claude Opus 4.8
parent 876a7fb659
commit 4aa1393af2
2 changed files with 14 additions and 7 deletions
+4 -2
View File
@@ -66,8 +66,10 @@ async function fetchReleases(baseUrl, options = {}) {
try {
pageJson = JSON.parse(text);
} catch {
// 以字元(而非 UTF-16 碼元)截斷回傳內容片段,避免拆分多位元組字元造成亂碼
const contentSnippet = Array.from(text).slice(0, API_ERROR_SNIPPET_LENGTH).join('');
// 以 UTF-16 長度粗略上限截斷(避免將可能極大的回應整個陣列化),
// 再以字元(而非 UTF-16 碼元)精準截斷,避免拆分多位元組字元造成亂碼
const boundedText = text.slice(0, API_ERROR_SNIPPET_LENGTH * 2);
const contentSnippet = Array.from(boundedText).slice(0, API_ERROR_SNIPPET_LENGTH).join('');
throw new Error(`release API 回傳資料無法解析 (page=${page}),回應內容片段:「${contentSnippet}`);
}