diff --git a/app/config.js b/app/config.js index 3aa85fb..4e37a2a 100644 --- a/app/config.js +++ b/app/config.js @@ -55,12 +55,16 @@ function assertHttpUrl(name, value) { } } -// 驗證 repository 為 owner/repo 格式(僅允許字母數字與 . _ -,且拒絕 . 與 .. 路徑穿越段) +// repository 每段名稱的最大長度與允許字元(字母數字與 . _ -) +const MAX_REPO_NAME_LENGTH = 100; +const REPO_SEGMENT_PATTERN = /^[A-Za-z0-9._-]+$/; + +// 驗證 repository 為 owner/repo 格式(拒絕 . 與 .. 路徑穿越段、空段、過長或含非法字元的段) function assertRepository(name, value) { const parts = value.split('/'); const isRepoFormatValid = parts.length === 2 - && parts.every((part) => part.length > 0 && part.length <= 100 - && /^[A-Za-z0-9._-]+$/.test(part) + && parts.every((part) => part.length > 0 && part.length <= MAX_REPO_NAME_LENGTH + && REPO_SEGMENT_PATTERN.test(part) && part !== '.' && part !== '..'); if (!isRepoFormatValid) { throw new Error(`${name} 格式錯誤,必須為 owner/repo`); diff --git a/app/releases.js b/app/releases.js index 3799e93..51982d3 100644 --- a/app/releases.js +++ b/app/releases.js @@ -3,6 +3,9 @@ // 每頁取得的 release 筆數 const RELEASES_PER_PAGE = 10; +// JSON 解析失敗時,附在錯誤訊息中的回應內容片段最大字元數 +const API_ERROR_SNIPPET_LENGTH = 200; + /** * 以分頁方式取得指定 Gitea repo 的所有 release,並回傳合併後的陣列。 * @@ -64,7 +67,7 @@ async function fetchReleases(baseUrl, options = {}) { pageJson = JSON.parse(text); } catch { // 以字元(而非 UTF-16 碼元)截斷回傳內容片段,避免拆分多位元組字元造成亂碼 - const contentSnippet = Array.from(text).slice(0, 200).join(''); + const contentSnippet = Array.from(text).slice(0, API_ERROR_SNIPPET_LENGTH).join(''); throw new Error(`release API 回傳資料無法解析 (page=${page}),回應內容片段:「${contentSnippet}」`); }