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
+10 -5
View File
@@ -59,13 +59,18 @@ function assertHttpUrl(name, value) {
const MAX_REPO_NAME_LENGTH = 100;
const REPO_SEGMENT_PATTERN = /^[A-Za-z0-9._-]+$/;
// 驗證 repository 為 owner/repo 格式(拒絕 . .. 路徑穿越段、空段、過長或含非法字元的段)
// 單一 repository 區段是否合法:長度在範圍內、僅含允許字元,且非 . .. 路徑穿越段
function isValidRepoSegment(part) {
return part.length > 0
&& part.length <= MAX_REPO_NAME_LENGTH
&& REPO_SEGMENT_PATTERN.test(part)
&& part !== '.' && part !== '..';
}
// 驗證 repository 為 owner/repo 格式(恰兩段,且每段皆為合法區段)
function assertRepository(name, value) {
const parts = value.split('/');
const isRepoFormatValid = parts.length === 2
&& parts.every((part) => part.length > 0 && part.length <= MAX_REPO_NAME_LENGTH
&& REPO_SEGMENT_PATTERN.test(part)
&& part !== '.' && part !== '..');
const isRepoFormatValid = parts.length === 2 && parts.every(isValidRepoSegment);
if (!isRepoFormatValid) {
throw new Error(`${name} 格式錯誤,必須為 owner/repo`);
}