From 4b9231971c064ba47e2aab4794e97331b6d7cbb4 Mon Sep 17 00:00:00 2001 From: Ryo Takahashi <38057616+HighBridgeDragon@users.noreply.github.com> Date: Sun, 25 Oct 2026 19:48:02 +0900 Subject: [PATCH 1/7] =?UTF-8?q?chore:=20docs=20=E3=83=87=E3=82=A3=E3=83=AC?= =?UTF-8?q?=E3=82=AF=E3=83=88=E3=83=AA=E3=82=92=20.gitignore=20=E3=81=AB?= =?UTF-8?q?=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.gitignore b/.gitignore index 6028350..cf55c77 100644 --- a/.gitignore +++ b/.gitignore @@ -161,3 +161,7 @@ sketch storybook-static/ # End of https://www.toptal.com/developers/gitignore/api/node,react,storybookjs + +# Documentation managed in GitHub Issues +docs/ + From 7c10367007a9689bad33dd9d74172a21124247e0 Mon Sep 17 00:00:00 2001 From: Ryo Takahashi <38057616+HighBridgeDragon@users.noreply.github.com> Date: Sun, 25 Oct 2026 19:48:41 +0900 Subject: [PATCH 2/7] =?UTF-8?q?feat(mise):=20bun=20=E3=81=AE=E3=83=90?= =?UTF-8?q?=E3=83=BC=E3=82=B8=E3=83=A7=E3=83=B3=E3=82=92=201.4.4=20?= =?UTF-8?q?=E3=81=AB=E5=9B=BA=E5=AE=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mise.lock | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ mise.toml | 2 ++ 2 files changed, 54 insertions(+) create mode 100644 mise.lock create mode 100644 mise.toml diff --git a/mise.lock b/mise.lock new file mode 100644 index 0000000..11cf4bb --- /dev/null +++ b/mise.lock @@ -0,0 +1,52 @@ +# @generated - this file is auto-generated by `mise lock` https://mise.jdx.dev/dev-tools/mise-lock.html + +lockfile_version = 3 + +[[tools.bun]] +version = "1.4.4" +backend = "core:bun" +specifiers = ["1.4.4"] + +[tools.bun."platforms.linux-arm64"] +checksum = "sha256:baa6bb36f51d6e58561e02bbea825038a9df454f673d6d776e8bbf86f8c7850e" +url = "https://github.com/oven-sh/bun/releases/download/bun-v1.4.4/bun-linux-aarch64.zip" + +[tools.bun."platforms.linux-arm64-musl"] +checksum = "sha256:57f01b02ac7113c61c935f28f38c2eab849e3c970dab21f8b640a87b1ef65009" +url = "https://github.com/oven-sh/bun/releases/download/bun-v1.4.4/bun-linux-aarch64-musl.zip" + +[tools.bun."platforms.linux-x64"] +checksum = "sha256:bf8ccbfb2b0b80159e0f9a6d15c55317add8077534dbc6130a3edb52fccb395c" +url = "https://github.com/oven-sh/bun/releases/download/bun-v1.4.4/bun-linux-x64.zip" + +[tools.bun."platforms.linux-x64-baseline"] +checksum = "sha256:e0446eb55995772eb9894bebae9a6be368d985a8b9271ee124486f9946eedd76" +url = "https://github.com/oven-sh/bun/releases/download/bun-v1.4.4/bun-linux-x64-baseline.zip" + +[tools.bun."platforms.linux-x64-musl"] +checksum = "sha256:a5a8ccc5e74dcb883fe974f125a7bdb3b41e5195ed1b870e73f17effd3eb2b2d" +url = "https://github.com/oven-sh/bun/releases/download/bun-v1.4.4/bun-linux-x64-musl.zip" + +[tools.bun."platforms.linux-x64-musl-baseline"] +checksum = "sha256:6e9c37098b293920aed28bfb51ab240e26d23bfdd9bbfecb3ad428ac166549bc" +url = "https://github.com/oven-sh/bun/releases/download/bun-v1.4.4/bun-linux-x64-musl-baseline.zip" + +[tools.bun."platforms.macos-arm64"] +checksum = "sha256:c240d7f0ccb90c263a849fcdb09b7ee62630d921348ed6aafda86b9d577bc510" +url = "https://github.com/oven-sh/bun/releases/download/bun-v1.4.4/bun-darwin-aarch64.zip" + +[tools.bun."platforms.macos-x64"] +checksum = "sha256:50a3491f73abc2fe81673171a85762d3bc2cc1680ccecadd983de99adf77977e" +url = "https://github.com/oven-sh/bun/releases/download/bun-v1.4.4/bun-darwin-x64.zip" + +[tools.bun."platforms.macos-x64-baseline"] +checksum = "sha256:d4a155adfbfd09155eb3ba05c9e3a544f39297d7a68616d3c7759f67ab5c47dc" +url = "https://github.com/oven-sh/bun/releases/download/bun-v1.4.4/bun-darwin-x64-baseline.zip" + +[tools.bun."platforms.windows-x64"] +checksum = "sha256:cd2c0fccc78f3ede1f50ca827a15f063eda13d4d150a3d0674f4f6e323d864a5" +url = "https://github.com/oven-sh/bun/releases/download/bun-v1.4.4/bun-windows-x64.zip" + +[tools.bun."platforms.windows-x64-baseline"] +checksum = "sha256:0f20ce6674d4f2dbcc8c5d59fdf99f7ba45f8c3b367e0f5781cbfa30a1c78d99" +url = "https://github.com/oven-sh/bun/releases/download/bun-v1.4.4/bun-windows-x64-baseline.zip" diff --git a/mise.toml b/mise.toml new file mode 100644 index 0000000..637ae58 --- /dev/null +++ b/mise.toml @@ -0,0 +1,2 @@ +[tools] +bun = "1.4.4" From c72b8620aac0a61e583123d82e46b2e63a5dc44b Mon Sep 17 00:00:00 2001 From: Ryo Takahashi <38057616+HighBridgeDragon@users.noreply.github.com> Date: Sun, 25 Oct 2026 19:50:11 +0900 Subject: [PATCH 3/7] =?UTF-8?q?feat(scripts):=20=E4=B8=8A=E6=B5=81=20Bun?= =?UTF-8?q?=20=E3=81=AE=20Security=20Advisories=20=E7=85=A7=E5=90=88?= =?UTF-8?q?=E3=82=B9=E3=82=AF=E3=83=AA=E3=83=97=E3=83=88=E3=82=92=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/audit-bun.test.ts | 223 ++++++++++++++++++++++++++++++++++++ scripts/audit-bun.ts | 235 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 458 insertions(+) create mode 100644 scripts/audit-bun.test.ts create mode 100644 scripts/audit-bun.ts diff --git a/scripts/audit-bun.test.ts b/scripts/audit-bun.test.ts new file mode 100644 index 0000000..29f9d18 --- /dev/null +++ b/scripts/audit-bun.test.ts @@ -0,0 +1,223 @@ +import { describe, expect, it } from "bun:test"; +import { + type Advisory, + auditBun, + checkAdvisories, + compareSemver, + extractBunVersionFromToml, + parseVersionRange, +} from "./audit-bun"; + +describe("compareSemver", () => { + it("同じバージョンを 0 と判定する", () => { + expect(compareSemver("1.4.4", "1.4.4")).toBe(0); + }); + + it("パッチバージョンの大小を正しく判定する", () => { + expect(compareSemver("1.4.4", "1.4.3")).toBeGreaterThan(0); + expect(compareSemver("1.4.3", "1.4.4")).toBeLessThan(0); + }); + + it("マイナーバージョンの大小を正しく判定する", () => { + expect(compareSemver("1.5.0", "1.4.9")).toBeGreaterThan(0); + expect(compareSemver("1.3.14", "1.4.0")).toBeLessThan(0); + }); + + it("メジャーバージョンの大小を正しく判定する", () => { + expect(compareSemver("2.0.0", "1.9.9")).toBeGreaterThan(0); + expect(compareSemver("0.9.0", "1.0.0")).toBeLessThan(0); + }); + + it("v プレフィックスを許容する", () => { + expect(compareSemver("v1.4.4", "1.4.4")).toBe(0); + }); + + it("プレリリースサフィックスが付いた場合も基本パッチ番号で比較できる", () => { + expect(compareSemver("1.4.4-canary.1", "1.4.3")).toBeGreaterThan(0); + expect(compareSemver("1.4.4-canary.1", "1.4.4")).toBeLessThanOrEqual(0); + }); +}); + +describe("parseVersionRange", () => { + it("単一条件(< 1.4.4)を評価できる", () => { + const isVulnerable = parseVersionRange("< 1.4.4"); + expect(isVulnerable("1.4.3")).toBe(true); + expect(isVulnerable("1.4.4")).toBe(false); + expect(isVulnerable("1.4.5")).toBe(false); + }); + + it("単一条件(<= 1.4.3)を評価できる", () => { + const isVulnerable = parseVersionRange("<= 1.4.3"); + expect(isVulnerable("1.4.3")).toBe(true); + expect(isVulnerable("1.4.4")).toBe(false); + }); + + it("複合条件(>= 1.3.14, < 1.4.4)を評価できる", () => { + const isVulnerable = parseVersionRange(">= 1.3.14, < 1.4.4"); + expect(isVulnerable("1.3.13")).toBe(false); + expect(isVulnerable("1.3.14")).toBe(true); + expect(isVulnerable("1.4.0")).toBe(true); + expect(isVulnerable("1.4.3")).toBe(true); + expect(isVulnerable("1.4.4")).toBe(false); + }); + + it("空白なしの条件(<1.4.4)を評価できる", () => { + const isVulnerable = parseVersionRange("<1.4.4"); + expect(isVulnerable("1.4.3")).toBe(true); + expect(isVulnerable("1.4.4")).toBe(false); + }); +}); + +describe("extractBunVersionFromToml", () => { + it("ダブルクォートで指定されたバージョンを抽出できる", () => { + const toml = ` +[tools] +bun = "1.4.4" +node = "22.0.0" +`; + expect(extractBunVersionFromToml(toml)).toBe("1.4.4"); + }); + + it("シングルクォートで指定されたバージョンを抽出できる", () => { + const toml = "bun = '1.4.4'"; + expect(extractBunVersionFromToml(toml)).toBe("1.4.4"); + }); + + it("前後に空白やコメントがあっても抽出できる", () => { + const toml = ` +# bun version +bun = "1.4.4" # latest +`; + expect(extractBunVersionFromToml(toml)).toBe("1.4.4"); + }); + + it("bun 指定がない場合は null を返す", () => { + const toml = ` +[tools] +node = "22.0.0" +`; + expect(extractBunVersionFromToml(toml)).toBeNull(); + }); +}); + +describe("checkAdvisories", () => { + const sampleAdvisories: Advisory[] = [ + { + ghsa_id: "GHSA-cf8x-9m69-3gg3", + summary: "HTTP/3 QPACK memory exhaustion", + severity: "high", + vulnerabilities: [ + { + package: { name: "bun" }, + vulnerable_version_range: ">= 1.3.14, < 1.4.4", + patched_versions: "1.4.4", + }, + ], + }, + { + ghsa_id: "GHSA-low-0001", + summary: "Low risk issue", + severity: "low", + vulnerabilities: [ + { + package: { name: "bun" }, + vulnerable_version_range: "< 1.4.4", + patched_versions: "1.4.4", + }, + ], + }, + ]; + + it("脆弱なバージョン(1.4.3)では High の Advisory を検知して hasCriticalOrHigh=true となる", () => { + const result = checkAdvisories("1.4.3", sampleAdvisories); + expect(result.hasCriticalOrHigh).toBe(true); + expect( + result.matched.some((a) => a.ghsa_id === "GHSA-cf8x-9m69-3gg3"), + ).toBe(true); + }); + + it("修正版バージョン(1.4.4)では該当なしとなり hasCriticalOrHigh=false となる", () => { + const result = checkAdvisories("1.4.4", sampleAdvisories); + expect(result.hasCriticalOrHigh).toBe(false); + expect(result.matched.length).toBe(0); + }); +}); + +describe("auditBun (End-to-End & Fail-open)", () => { + it("High 脆弱性該当時は終了コード 1 を返す", async () => { + const mockFetch = async () => + new Response( + JSON.stringify([ + { + ghsa_id: "GHSA-cf8x-9m69-3gg3", + summary: "HTTP/3 QPACK issue", + severity: "high", + vulnerabilities: [ + { + package: { name: "bun" }, + vulnerable_version_range: "< 1.4.4", + patched_versions: "1.4.4", + }, + ], + }, + ]), + { status: 200 }, + ); + + const exitCode = await auditBun({ + version: "1.4.3", + fetchFn: mockFetch as unknown as typeof fetch, + }); + expect(exitCode).toBe(1); + }); + + it("脆弱性がない場合は終了コード 0 を返す", async () => { + const mockFetch = async () => + new Response( + JSON.stringify([ + { + ghsa_id: "GHSA-cf8x-9m69-3gg3", + summary: "HTTP/3 QPACK issue", + severity: "high", + vulnerabilities: [ + { + package: { name: "bun" }, + vulnerable_version_range: "< 1.4.4", + patched_versions: "1.4.4", + }, + ], + }, + ]), + { status: 200 }, + ); + + const exitCode = await auditBun({ + version: "1.4.4", + fetchFn: mockFetch as unknown as typeof fetch, + }); + expect(exitCode).toBe(0); + }); + + it("ネットワークエラー(Fail-open)時は警告を出し終了コード 0 を返す", async () => { + const mockFetch = async () => { + throw new Error("Network unreachable (offline)"); + }; + + const exitCode = await auditBun({ + version: "1.4.3", + fetchFn: mockFetch as unknown as typeof fetch, + }); + expect(exitCode).toBe(0); + }); + + it("API レートリミット(403)時も Fail-open で終了コード 0 を返す", async () => { + const mockFetch = async () => + new Response("API rate limit exceeded", { status: 403 }); + + const exitCode = await auditBun({ + version: "1.4.3", + fetchFn: mockFetch as unknown as typeof fetch, + }); + expect(exitCode).toBe(0); + }); +}); diff --git a/scripts/audit-bun.ts b/scripts/audit-bun.ts new file mode 100644 index 0000000..13fe87d --- /dev/null +++ b/scripts/audit-bun.ts @@ -0,0 +1,235 @@ +import { readFileSync } from "node:fs"; +import { resolve } from "node:path"; + +export interface Vulnerability { + package?: { name?: string }; + vulnerable_version_range?: string; + patched_versions?: string; +} + +export interface Advisory { + ghsa_id: string; + summary: string; + severity: "critical" | "high" | "moderate" | "low" | string; + vulnerabilities?: Vulnerability[]; +} + +/** + * セマンティックバージョン文字列をパースして [major, minor, patch] の数値タプルを返します。 + * プレリリース記号(-canary など)や v プレフィックスにも対応します。 + */ +export function parseSemver(v: string): [number, number, number] { + const cleaned = v.trim().replace(/^v/, ""); + const mainPart = cleaned.split("-")[0]; + const parts = mainPart.split(".").map((n) => Number.parseInt(n, 10)); + return [parts[0] || 0, parts[1] || 0, parts[2] || 0]; +} + +/** + * 2 つのセマンティックバージョンを比較します。 + * v1 > v2 => 正の数, v1 < v2 => 負の数, v1 == v2 => 0 + */ +export function compareSemver(v1: string, v2: string): number { + const [maj1, min1, pat1] = parseSemver(v1); + const [maj2, min2, pat2] = parseSemver(v2); + + if (maj1 !== maj2) return maj1 - maj2; + if (min1 !== min2) return min1 - min2; + if (pat1 !== pat2) return pat1 - pat2; + + // 同一の主要番号で片方にプレリリースがある場合、プレリリース付きは通常版より小さい + const hasPre1 = v1.includes("-"); + const hasPre2 = v2.includes("-"); + if (hasPre1 && !hasPre2) return -1; + if (!hasPre1 && hasPre2) return 1; + + return 0; +} + +/** + * 単一またはカンマ区切りのバージョン範囲条件を評価する関数を返します。 + * 例: ">= 1.3.14, < 1.4.4", "< 1.4.4" + */ +export function parseVersionRange(range: string): (v: string) => boolean { + const clauses = range + .split(",") + .map((c) => c.trim()) + .filter(Boolean); + + const parsedClauses = clauses.map((clause) => { + const match = clause.match(/^([><=!]+)\s*(.+)$/); + if (!match) { + return () => false; + } + const op = match[1]; + const target = match[2].trim(); + + return (ver: string) => { + const cmp = compareSemver(ver, target); + switch (op) { + case ">": + return cmp > 0; + case ">=": + return cmp >= 0; + case "<": + return cmp < 0; + case "<=": + return cmp <= 0; + case "=": + case "==": + return cmp === 0; + default: + return false; + } + }; + }); + + return (v: string) => parsedClauses.every((check) => check(v)); +} + +/** + * mise.toml の文字列から bun バージョンを抽出します。 + */ +export function extractBunVersionFromToml(content: string): string | null { + const match = content.match(/bun\s*=\s*["']([^"']+)["']/); + return match ? match[1] : null; +} + +/** + * Advisory 一覧とバージョンを照合し、該当する Advisory を抽出します。 + */ +export function checkAdvisories( + currentVersion: string, + advisories: Advisory[], +): { hasCriticalOrHigh: boolean; matched: Advisory[] } { + const matched: Advisory[] = []; + let hasCriticalOrHigh = false; + + for (const advisory of advisories) { + const vulns = advisory.vulnerabilities || []; + const isTargetVulnerable = vulns.some((vuln) => { + if (!vuln.vulnerable_version_range) return false; + const isVulnerable = parseVersionRange(vuln.vulnerable_version_range); + return isVulnerable(currentVersion); + }); + + if (isTargetVulnerable) { + matched.push(advisory); + const severity = (advisory.severity || "").toLowerCase(); + if (severity === "critical" || severity === "high") { + hasCriticalOrHigh = true; + } + } + } + + return { hasCriticalOrHigh, matched }; +} + +export interface AuditBunOptions { + version?: string; + tomlPath?: string; + fetchFn?: typeof fetch; +} + +/** + * Bun のバージョンを上流 GitHub Security Advisories と照合します。 + * 終了コード: 0(安全またはオフライン通過), 1(High/Critical 検出) + */ +export async function auditBun(options: AuditBunOptions = {}): Promise { + const fetchFn = options.fetchFn || globalThis.fetch; + let version = options.version; + + if (!version) { + const tomlPath = options.tomlPath || resolve(process.cwd(), "mise.toml"); + try { + const tomlContent = readFileSync(tomlPath, "utf-8"); + version = extractBunVersionFromToml(tomlContent) || undefined; + } catch (e) { + console.warn(`[WARN] ${tomlPath} を読み込めませんでした:`, e); + } + } + + if (!version) { + console.warn( + "[WARN] 検証対象の Bun バージョンを特定できませんでした。検査をスキップします。", + ); + return 0; + } + + console.log(`[audit:bun] 検証対象の Bun バージョン: ${version}`); + + const apiUrl = + "https://api.github.com/repos/oven-sh/bun/security-advisories?state=published&per_page=100"; + const headers: Record = { + "User-Agent": "bun-audit-hook", + }; + + const token = process.env.GITHUB_TOKEN || process.env.GH_TOKEN; + if (token) { + headers.Authorization = `Bearer ${token}`; + } + + let advisories: Advisory[] = []; + try { + const res = await fetchFn(apiUrl, { headers }); + if (!res.ok) { + console.warn( + `[WARN] GitHub Security Advisories API への接続に失敗しました (ステータス: ${res.status})。オフライン等のため検査をスキップします。`, + ); + return 0; + } + advisories = (await res.json()) as Advisory[]; + } catch (err) { + console.warn( + "[WARN] ネットワーク不通のため GitHub Security Advisories の取得をスキップします:", + (err as Error).message, + ); + return 0; + } + + const { hasCriticalOrHigh, matched } = checkAdvisories(version, advisories); + + if (matched.length === 0) { + console.log( + `[audit:bun] Bun ${version} に該当する公開された脆弱性はありません。`, + ); + return 0; + } + + console.log( + `\n[audit:bun] 注意: Bun ${version} に影響する脆弱性が ${matched.length} 件検出されました:`, + ); + for (const a of matched) { + console.log(` - [${a.severity.toUpperCase()}] ${a.ghsa_id}: ${a.summary}`); + for (const v of a.vulnerabilities || []) { + console.log( + ` 影響範囲: ${v.vulnerable_version_range} (修正版: ${v.patched_versions || "未定"})`, + ); + } + } + + if (hasCriticalOrHigh) { + console.error( + "\n[ERROR] High または Critical の脆弱性が検出されたため、処理を中断します。", + ); + return 1; + } + + console.warn( + "\n[WARN] Moderate または Low の脆弱性のみのため、処理を続行します。", + ); + return 0; +} + +// CLI 直実行時 +if (import.meta.main) { + const args = process.argv.slice(2); + let cliVersion: string | undefined; + const verIdx = args.indexOf("--version"); + if (verIdx !== -1 && args[verIdx + 1]) { + cliVersion = args[verIdx + 1]; + } + + const exitCode = await auditBun({ version: cliVersion }); + process.exit(exitCode); +} From e7a4ed848bab6c7875ef376ffded14fbf24a19db Mon Sep 17 00:00:00 2001 From: Ryo Takahashi <38057616+HighBridgeDragon@users.noreply.github.com> Date: Sun, 25 Oct 2026 19:51:14 +0900 Subject: [PATCH 4/7] =?UTF-8?q?feat(husky):=20bun.lock=20=E3=81=A8=20mise.?= =?UTF-8?q?toml=20=E3=81=AE=E5=A4=89=E6=9B=B4=E3=82=92=E6=A4=9C=E6=9F=BB?= =?UTF-8?q?=E3=81=99=E3=82=8B=20pre-push=20=E3=83=95=E3=83=83=E3=82=AF?= =?UTF-8?q?=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitattributes | 2 ++ .husky/pre-push | 83 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 .husky/pre-push diff --git a/.gitattributes b/.gitattributes index 41a7caf..76a38d1 100644 --- a/.gitattributes +++ b/.gitattributes @@ -4,3 +4,5 @@ *.jsx text eol=lf *.css text eol=lf *.json text eol=lf +.husky/* text eol=lf + diff --git a/.husky/pre-push b/.husky/pre-push new file mode 100644 index 0000000..3f29863 --- /dev/null +++ b/.husky/pre-push @@ -0,0 +1,83 @@ +#!/bin/sh + +# Bun が利用できない環境(GUI Git クライアント等)では警告を出してスキップする +if ! command -v bun >/dev/null 2>&1; then + echo "[WARN] bun コマンドが見つからないため、セキュリティ監査をスキップします。" + exit 0 +fi + +CHECK_LOCK=0 +CHECK_BUN_VERSION=0 + +ZERO_SHA="0000000000000000000000000000000000000000" + +# push されるすべての ref 行を処理(Windows Git Bash の \r 混入を除去) +while read -r local_ref local_sha remote_ref remote_sha; do + local_sha=$(echo "$local_sha" | tr -d '\r') + remote_sha=$(echo "$remote_sha" | tr -d '\r') + + # ブランチ削除時はスキップ + if [ "$local_sha" = "$ZERO_SHA" ] || [ -z "$local_sha" ]; then + continue + fi + + # 差分ファイルの取得 + if [ "$remote_sha" = "$ZERO_SHA" ]; then + # 新規ブランチ push 時: リモート未反映のコミット群から変更ファイル一覧を取得 + COMMITS=$(git rev-list "$local_sha" --not --remotes 2>/dev/null) + if [ -n "$COMMITS" ]; then + CHANGED_FILES=$(git diff-tree --no-commit-id --name-only -r $COMMITS 2>/dev/null) + else + # コミットが見つからない場合はブランチの直近ツリーを走査 + CHANGED_FILES=$(git diff-tree --no-commit-id --name-only -r "$local_sha" 2>/dev/null) + fi + else + # 通常更新時: リモートとローカルの差分を取得 + CHANGED_FILES=$(git diff-tree --no-commit-id --name-only -r "$remote_sha" "$local_sha" 2>/dev/null) + fi + + if echo "$CHANGED_FILES" | grep -q "^bun\.lock$"; then + CHECK_LOCK=1 + fi + if echo "$CHANGED_FILES" | grep -q "^mise\.toml$"; then + CHECK_BUN_VERSION=1 + fi +done + +# 1. 依存パッケージの脆弱性検査 (bun.lock 変更時) +if [ "$CHECK_LOCK" -eq 1 ]; then + echo "[pre-push] bun.lock の変更を検知しました。依存パッケージの脆弱性を検査します..." + AUDIT_OUTPUT=$(bun audit --audit-level=critical 2>&1) + AUDIT_EXIT=$? + + if [ "$AUDIT_EXIT" -ne 0 ]; then + # オフラインや通信エラー(error: POST ... など行頭に error: がある場合)は警告を出して通過 + if echo "$AUDIT_OUTPUT" | grep -qE '^error:'; then + echo "[WARN] bun audit で通信エラーが発生しました。オフライン等のため push を許可します:" + echo "$AUDIT_OUTPUT" + else + echo "[ERROR] bun.lock に Critical レベルの脆弱性が検出されました。push を中断します:" + echo "$AUDIT_OUTPUT" + echo "" + echo "回避して push する必要がある場合は 'git push --no-verify' を使用してください。" + exit 1 + fi + else + echo "[pre-push] bun.lock の Critical 脆弱性検査に合格しました。" + fi +fi + +# 2. Bun ランタイム本体の脆弱性検査 (mise.toml 変更時) +if [ "$CHECK_BUN_VERSION" -eq 1 ]; then + echo "[pre-push] mise.toml の変更を検知しました。固定された Bun の脆弱性を検査します..." + bun scripts/audit-bun.ts + BUN_AUDIT_EXIT=$? + + if [ "$BUN_AUDIT_EXIT" -ne 0 ]; then + echo "[ERROR] 固定された Bun バージョンに重大な脆弱性が検出されたため、push を中断します。" + echo "回避して push する必要がある場合は 'git push --no-verify' を使用してください。" + exit 1 + fi +fi + +exit 0 From 703f6bb3c738c0bc8ee65f8b74430d19e32e71e4 Mon Sep 17 00:00:00 2001 From: Ryo Takahashi <38057616+HighBridgeDragon@users.noreply.github.com> Date: Sun, 25 Oct 2026 19:52:36 +0900 Subject: [PATCH 5/7] =?UTF-8?q?docs:=20mise=20=E3=81=AB=E3=82=88=E3=82=8B?= =?UTF-8?q?=E7=92=B0=E5=A2=83=E6=A7=8B=E7=AF=89=E3=81=A8=E8=84=86=E5=BC=B1?= =?UTF-8?q?=E6=80=A7=E6=A4=9C=E6=9F=BB=E3=81=AE=E5=88=A9=E7=94=A8=E6=89=8B?= =?UTF-8?q?=E9=A0=86=E3=82=92=20README=20=E3=81=AB=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 39 +++++++++++++++++++++++++++++++++++++-- package.json | 6 +++++- 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 33b9339..77c6bdb 100644 --- a/README.md +++ b/README.md @@ -8,13 +8,26 @@ Vite + React 19 + TypeScript のプロジェクトテンプレート - Vite 8 - TypeScript 5.9 - Biome (Linter / Formatter) -- Husky (pre-commit hook) +- Husky (pre-commit / pre-push hook) +- mise (ランタイムバージョン管理) - Claude Code GitHub Actions (コードレビュー / Issue対応) +## 開発環境のセットアップ + +本テンプレートは `mise` で Bun のバージョンを固定しています。 + +```bash +# mise が導入されている場合、指定バージョンをインストール +mise install + +# 依存パッケージの導入 +bun install +``` + ## 使い方 1. "Use this template" ボタンからリポジトリを作成 -2. `bun install` +2. `mise install && bun install` 3. `bun run dev` ## スクリプト一覧 @@ -25,6 +38,28 @@ Vite + React 19 + TypeScript のプロジェクトテンプレート - `bun run format` — Biome でフォーマット - `bun run check:fix` — Biome でリント+自動修正 - `bun run preview` — ビルド結果のプレビュー +- `bun run test` — 単体テスト実行 +- `bun run audit:deps` — 依存パッケージの脆弱性検査(Critical) +- `bun run audit:tools` — 上流 Bun ランタイムの脆弱性照合 +- `bun run audit:all` — 依存パッケージおよび Bun ランタイムの全件検査 + +## セキュリティ検査(Git フック) + +Husky によるローカルフックを設定しています。 + +- **pre-commit**: ステージされたファイルに Biome による検査・整形を実行します。 +- **pre-push**: + - `bun.lock` の変更時: `bun audit --audit-level=critical` を実行し、Critical 脆弱性があれば push を中断します。 + - `mise.toml` の変更時: 上流リポジトリ(`oven-sh/bun`)の公開 Advisory と照合し、High 以上の脆弱性があれば push を中断します。 + - オフライン時や外部 API 障害時は、警告を表示した上で push を許可します。 + +### フックのスキップ + +緊急時やオフライン時にフックの検査を回避して push する必要がある場合は、`--no-verify` オプションを使用します。 + +```bash +git push --no-verify +``` ## 規約 diff --git a/package.json b/package.json index 59539bb..0115494 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,11 @@ "format": "biome format --write .", "check:fix": "biome check --write .", "preview": "vite preview", - "prepare": "husky" + "prepare": "husky", + "test": "bun test", + "audit:deps": "bun audit --audit-level=critical", + "audit:tools": "bun scripts/audit-bun.ts", + "audit:all": "bun run audit:deps && bun run audit:tools" }, "dependencies": { "react": "^19.2.7", From fc62b74f6b4bab628069ac31a305852dc14fc77b Mon Sep 17 00:00:00 2001 From: Ryo Takahashi <38057616+HighBridgeDragon@users.noreply.github.com> Date: Sun, 25 Oct 2026 20:02:00 +0900 Subject: [PATCH 6/7] =?UTF-8?q?fix(scripts):=20TOML=20=E6=AD=A3=E8=A6=8F?= =?UTF-8?q?=E8=A1=A8=E7=8F=BE=E3=81=AE=E5=8E=B3=E5=AF=86=E5=8C=96=E3=81=A8?= =?UTF-8?q?=E3=83=91=E3=83=83=E3=82=B1=E3=83=BC=E3=82=B8=E5=90=8D=E5=88=A4?= =?UTF-8?q?=E5=AE=9A=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .husky/pre-push | 22 +++++++++++++++++----- scripts/audit-bun.test.ts | 28 ++++++++++++++++++++++++++++ scripts/audit-bun.ts | 11 ++++++++++- 3 files changed, 55 insertions(+), 6 deletions(-) mode change 100644 => 100755 .husky/pre-push diff --git a/.husky/pre-push b/.husky/pre-push old mode 100644 new mode 100755 index 3f29863..d08095c --- a/.husky/pre-push +++ b/.husky/pre-push @@ -21,19 +21,31 @@ while read -r local_ref local_sha remote_ref remote_sha; do continue fi + CHANGED_FILES="" + # 差分ファイルの取得 if [ "$remote_sha" = "$ZERO_SHA" ]; then - # 新規ブランチ push 時: リモート未反映のコミット群から変更ファイル一覧を取得 + # 新規ブランチ push 時: リモート未反映のコミット群から変更ファイル一覧を取得(マージコミット含む) COMMITS=$(git rev-list "$local_sha" --not --remotes 2>/dev/null) if [ -n "$COMMITS" ]; then - CHANGED_FILES=$(git diff-tree --no-commit-id --name-only -r $COMMITS 2>/dev/null) + CHANGED_FILES=$(git log --name-only --pretty=format: $COMMITS 2>/dev/null | sort -u) else # コミットが見つからない場合はブランチの直近ツリーを走査 - CHANGED_FILES=$(git diff-tree --no-commit-id --name-only -r "$local_sha" 2>/dev/null) + CHANGED_FILES=$(git diff-tree --no-commit-id --name-only -r -m "$local_sha" 2>/dev/null) fi else - # 通常更新時: リモートとローカルの差分を取得 - CHANGED_FILES=$(git diff-tree --no-commit-id --name-only -r "$remote_sha" "$local_sha" 2>/dev/null) + # 通常更新時: remote_sha がローカルに存在するか確認(fetch 漏れや force push 対策) + if git cat-file -e "$remote_sha" 2>/dev/null; then + CHANGED_FILES=$(git log --name-only --pretty=format: "$remote_sha..$local_sha" 2>/dev/null | sort -u) + else + # remote_sha が存在しない場合はローカルの未 push コミットへフォールバック + COMMITS=$(git rev-list "$local_sha" --not --remotes 2>/dev/null) + if [ -n "$COMMITS" ]; then + CHANGED_FILES=$(git log --name-only --pretty=format: $COMMITS 2>/dev/null | sort -u) + else + CHANGED_FILES=$(git diff-tree --no-commit-id --name-only -r -m "$local_sha" 2>/dev/null) + fi + fi fi if echo "$CHANGED_FILES" | grep -q "^bun\.lock$"; then diff --git a/scripts/audit-bun.test.ts b/scripts/audit-bun.test.ts index 29f9d18..3c8b95c 100644 --- a/scripts/audit-bun.test.ts +++ b/scripts/audit-bun.test.ts @@ -91,6 +91,15 @@ bun = "1.4.4" # latest expect(extractBunVersionFromToml(toml)).toBe("1.4.4"); }); + it("コメント行や xbun などの別名にはマッチしない", () => { + const toml = ` +# bun = "1.0.0" +xbun = "2.0.0" +bun = "1.4.4" +`; + expect(extractBunVersionFromToml(toml)).toBe("1.4.4"); + }); + it("bun 指定がない場合は null を返す", () => { const toml = ` [tools] @@ -141,6 +150,25 @@ describe("checkAdvisories", () => { expect(result.hasCriticalOrHigh).toBe(false); expect(result.matched.length).toBe(0); }); + + it("package.name が bun 以外の場合は脆弱性があっても無視する", () => { + const otherAdvisories: Advisory[] = [ + { + ghsa_id: "GHSA-other-0001", + summary: "Other package issue", + severity: "high", + vulnerabilities: [ + { + package: { name: "other-tool" }, + vulnerable_version_range: "< 2.0.0", + }, + ], + }, + ]; + const result = checkAdvisories("1.4.3", otherAdvisories); + expect(result.hasCriticalOrHigh).toBe(false); + expect(result.matched.length).toBe(0); + }); }); describe("auditBun (End-to-End & Fail-open)", () => { diff --git a/scripts/audit-bun.ts b/scripts/audit-bun.ts index 13fe87d..cd2bdb5 100644 --- a/scripts/audit-bun.ts +++ b/scripts/audit-bun.ts @@ -27,6 +27,8 @@ export function parseSemver(v: string): [number, number, number] { /** * 2 つのセマンティックバージョンを比較します。 + * 注意: 本スクリプトは安定版(SemVer X.Y.Z)を想定しています。 + * canary 等のプレリリース識別子は簡易比較(安定版より小さい扱い)となります。 * v1 > v2 => 正の数, v1 < v2 => 負の数, v1 == v2 => 0 */ export function compareSemver(v1: string, v2: string): number { @@ -89,14 +91,16 @@ export function parseVersionRange(range: string): (v: string) => boolean { /** * mise.toml の文字列から bun バージョンを抽出します。 + * コメント行や別名(xbun など)に誤一致しないよう行頭で判定します。 */ export function extractBunVersionFromToml(content: string): string | null { - const match = content.match(/bun\s*=\s*["']([^"']+)["']/); + const match = content.match(/^\s*bun\s*=\s*["']([^"']+)["']/m); return match ? match[1] : null; } /** * Advisory 一覧とバージョンを照合し、該当する Advisory を抽出します。 + * 対象パッケージが bun 本体(または関連パッケージ)のものに限定して検査します。 */ export function checkAdvisories( currentVersion: string, @@ -109,6 +113,10 @@ export function checkAdvisories( const vulns = advisory.vulnerabilities || []; const isTargetVulnerable = vulns.some((vuln) => { if (!vuln.vulnerable_version_range) return false; + const pkgName = vuln.package?.name?.toLowerCase(); + if (pkgName && pkgName !== "bun" && !pkgName.includes("bun")) { + return false; + } const isVulnerable = parseVersionRange(vuln.vulnerable_version_range); return isVulnerable(currentVersion); }); @@ -158,6 +166,7 @@ export async function auditBun(options: AuditBunOptions = {}): Promise { console.log(`[audit:bun] 検証対象の Bun バージョン: ${version}`); + // TODO: Advisory が 100 件を超えた場合は Link ヘッダーによるページネーション走査を検討 const apiUrl = "https://api.github.com/repos/oven-sh/bun/security-advisories?state=published&per_page=100"; const headers: Record = { From f8af6053ee2bfcd6c541eb1bd90fb112117ae00b Mon Sep 17 00:00:00 2001 From: Ryo Takahashi <38057616+HighBridgeDragon@users.noreply.github.com> Date: Sun, 25 Oct 2026 20:02:17 +0900 Subject: [PATCH 7/7] =?UTF-8?q?docs:=20GITHUB=5FTOKEN=20=E8=A8=AD=E5=AE=9A?= =?UTF-8?q?=E3=81=AB=E3=82=88=E3=82=8B=E3=83=AC=E3=83=BC=E3=83=88=E5=88=B6?= =?UTF-8?q?=E9=99=90=E5=AF=BE=E7=AD=96=E3=81=AE=E8=BF=BD=E8=A8=98=E3=81=A8?= =?UTF-8?q?=20gitattributes=20=E6=95=B4=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitattributes | 1 - README.md | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitattributes b/.gitattributes index 76a38d1..c635601 100644 --- a/.gitattributes +++ b/.gitattributes @@ -5,4 +5,3 @@ *.css text eol=lf *.json text eol=lf .husky/* text eol=lf - diff --git a/README.md b/README.md index 77c6bdb..1cd66b5 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,7 @@ Husky によるローカルフックを設定しています。 - `bun.lock` の変更時: `bun audit --audit-level=critical` を実行し、Critical 脆弱性があれば push を中断します。 - `mise.toml` の変更時: 上流リポジトリ(`oven-sh/bun`)の公開 Advisory と照合し、High 以上の脆弱性があれば push を中断します。 - オフライン時や外部 API 障害時は、警告を表示した上で push を許可します。 + - ※GitHub API の未認証レートリミット(60回/時)対策として、CI や共有 IP 環境では `GITHUB_TOKEN` または `GH_TOKEN` の環境変数設定を推奨します。 ### フックのスキップ