Skip to content

chore(security): 🔒 gitleaks カスタムルール拡張による AI サービス (Groq等) APIキー検知の強化 - #486

Merged
genzouw merged 5 commits into
mainfrom
security/enhance-gitleaks-ai-keys-4609932599233604344
Aug 3, 2026
Merged

genzouw merged 5 commits into
mainfrom
security/enhance-gitleaks-ai-keys-4609932599233604344

Conversation

@genzouw

@genzouw genzouw commented Aug 2, 2026 •

Copy link
Copy Markdown
Owner

背景

事前調査により、本リポジトリは gitleaks, trufflehog, trivy, secretlint 等の多層的な漏洩防御策が既に導入されており、高いセキュリティ水準を維持していることを確認しました。一方で、近年の AI アプリケーション開発で利用が増加している一部の最新 AI サービス(Groq, OpenRouter, DeepSeek 等)の API キーに対する検知パターンが既存の monopo-ai-api-key ルールに含まれておらず、カバレッジ拡張の余地があることがわかりました。

現状認識(事前調査結果のサマリー)

  • 既存防御策: gitleaks.yml, codeql.yml, trivy.yml, secretlint.yml や、Husky による pre-commit および pre-push フックが重厚に導入済み。
  • 未カバー領域: 既存の .gitleaks.toml における monopo-ai-api-key は OpenAI と Anthropic 程度に留まっており、Groq や OpenRouter, DeepSeek の API キーをハードコードしても検知をすり抜ける。
  • 直近の漏洩リスク兆候: 特になし(.env の履歴への混入等も見られず、防御機構自体は正常に稼働している)。

このPRで導入・強化するもの

  • 対象: 既存 .gitleaks.toml の monopo-ai-api-key ルールの拡張、および docs/security/leak-prevention.md への説明追記。
  • ツール名とバージョン: gitleaks (既存の運用バージョンを継続利用。設定のみの強化)
  • 期待される効果: 開発中や AI エージェントのプロンプト検証などでハードコードされがちな Groq, OpenRouter, DeepSeek などの最新 LLM サービスの API キーについて、コミット前および CI でのプッシュ時に即座に検知・拒否可能になる。

検知漏れリスクと補完策

  • 検知できないケース: パターンベースであるため、未知のプロバイダや固定プレフィックスを持たない(単なるランダム文字列の)独自APIキーはすり抜ける可能性がある。
  • 補完策: 高エントロピー検知(detect-secrets 等)や有効性検証ベース(Trufflehog)、GitHub Secret Scanning との組み合わせにより多層的に防御する。

マージ前に必要な手動作業(チェックリスト)

レビュアーは PR をマージする前に必ず以下を実施してください。
本 PR の CI は手動作業完了を前提に通る設計です。

  • リポジトリ管理者による GitHub Settings → Code security → Push protection の有効化(まだ有効でない場合)の確認。

マージ後の確認手順

  • 次の push / PR で既存の gitleaks.yml などの CI が green になることを確認
  • 意図的にダミーの gsk_ プレフィックスキーなどを追加しようとした際、ローカルの pre-commit 段階でブロックされることを確認

ロールバック手順

本 PR は正規表現パターンの追加のみであるため、誤検知が多発した場合は git revert にてコミットを打ち消すことで安全かつ即座に元の検知レベルに戻せます。

参考情報

  • 公式ドキュメント: https://github.com/gitleaks/gitleaks
  • 比較検討した他案: 新規のシークレットスキャンツール追加(既存の Gitleaks のカスタムルール設定で十分対応可能かつオーバーヘッドがないため却下)
  • 直近の関連 PR / Issue: なし

PR created automatically by Jules for task 4609932599233604344 started by @genzouw

Summary by CodeRabbit

  • セキュリティ
    • Groq、OpenRouter、DeepSeek のAPIキー検出に対応しました。
    • 既存のOpenAIおよびAnthropicのAPIキー検出は引き続き利用できます。
    • 対応するシークレット検出範囲をドキュメントに反映しました。

…eepSeek) APIキー混入検知の強化

- .gitleaks.toml の `monopo-ai-api-key` ルールに Groq, OpenRouter, DeepSeek のパターンを追加
- docs/security/leak-prevention.md のドキュメント更新

Co-authored-by: genzouw <29957+genzouw@users.noreply.github.com>
@google-labs-jules

Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@github-actions github-actions Bot added the docs label Aug 2, 2026
@coderabbitai

coderabbitai Bot commented Aug 2, 2026 •

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@genzouw, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 59 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: d810380b-41e7-4bae-bce0-926cb2da595c

📥 Commits

Reviewing files that changed from the base of the PR and between c17ff2a and 453807e.

📒 Files selected for processing (1)
  • .gitleaks.toml
📝 Walkthrough

Walkthrough

Gitleaksのmonopo-ai-api-keyルールに、Groq、OpenRouter、DeepSeekのAPIキー検知パターンを追加しました。セキュリティドキュメントにも検知対象を反映しました。

Changes

AI APIキー検知対象の拡張

Layer / File(s) Summary
検知ルールとドキュメントの更新
.gitleaks.toml, docs/security/leak-prevention.md
monopo-ai-api-keyルールにGroq、OpenRouter、DeepSeekのAPIキー形式を追加しました。関連ドキュメントに検知対象を追記しました。

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

  • genzouw/monopo#425: 同じmonopo-ai-api-keyルールと関連ドキュメントの検知対象を変更しています。
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed タイトルは、Gitleaksのカスタムルールを拡張してGroqなどのAIサービスのAPIキー検知を強化する主な変更を明確に示しています。
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch security/enhance-gitleaks-ai-keys-4609932599233604344

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@qodo-code-review

Copy link
Copy Markdown

PR Summary by Qodo

gitleaksのAI APIキー検知をGroq/OpenRouter/DeepSeek対応で拡張

✨ Enhancement 📝 Documentation 🕐 10-20 Minutes

Grey Divider

AI Description

• gitleaksカスタムルールでGroq/OpenRouter/DeepSeekのAPIキー形式を検知対象に追加
• 既存のAI APIキールール説明を更新し、検知カバレッジ拡張を明文化
Diagram

graph TD
  Dev(("開発者")) --> Hooks["Git hooks"] --> Scan["Gitleaks scan"] --> Cfg[".gitleaks.toml"]
  Repo["対象コード"] --> Scan --> Block{"検知時ブロック"}
  CI["CI: gitleaks.yml"] --> Scan
  Docs["docs/security/leak-prevention.md"] --> Dev
  subgraph Legend
    direction LR
    _actor(("人")) ~~~ _proc["処理/ジョブ"] ~~~ _file["ファイル"] ~~~ _dec{"判定"}
  end
Loading
High-Level Assessment

The following are alternative approaches to this PR:

1. GitHub Secret Scanningのカスタムパターン併用
  • ➕ Push Protectionでサーバサイドで強制ブロックできる(ローカル回避の影響を受けにくい)
  • ➕ GitHub UIで検知・対応フローが標準化される
  • ➖ リポジトリ設定(権限/プラン/組織ポリシー)に依存し、導入の自由度が下がる
  • ➖ ローカル段階の早期フィードバックは別途フックが必要
2. 高エントロピー検知の強化(detect-secrets等のルール/運用強化)
  • ➕ プレフィックスを持たない未知のキー形式にも一定対応できる
  • ➕ 正規表現メンテの負担を減らせる
  • ➖ 誤検知やレビュー負荷が増えやすい
  • ➖ 短い/低エントロピーなキーは取りこぼしがあり得る
3. gitleaksの上流ルール更新・追従を優先
  • ➕ 自前ルールの保守コストを下げられる可能性
  • ➕ コミュニティで改善される検知に乗れる
  • ➖ 新興プロバイダのキー形式が即時にカバーされないことがある
  • ➖ リポジトリ固有の要件(例示/運用方針)を反映しづらい

Recommendation: 本PRの「既存gitleaks運用は維持しつつ、.gitleaks.tomlの正規表現を拡張する」方針が最小コストで効果が高い。加えて、権限・運用が許すならGitHub Secret Scanning/Push Protection(チェックリストにある確認事項)を併用すると、ローカルフック回避時の最後の砦として補完できる。

Files changed (2) +3 / -3

Documentation (1) +1 / -1
leak-prevention.mdGitleaksのカスタムルール説明にAIトークン例(Groq等)を追記 +1/-1

Gitleaksのカスタムルール説明にAIトークン例(Groq等)を追記

• CI上で適用される gitleaks のカスタムルール例に、SaaS・AIトークン(Groq/OpenRouter/DeepSeek)を含む旨を追記しました。運用上の検知対象がドキュメントからも読み取れるようになります。

docs/security/leak-prevention.md

Other (1) +2 / -2
.gitleaks.tomlAI APIキー検知ルールにGroq/OpenRouter/DeepSeekのパターンを追加 +2/-2

AI APIキー検知ルールにGroq/OpenRouter/DeepSeekのパターンを追加

• 既存の monopo-ai-api-key ルールの説明文を更新し、対象プロバイダを明確化しました。正規表現に Groq(gsk_)、OpenRouter(sk-or-v1-)、DeepSeek想定(hex 32桁の sk-)のキー形式を追加して検知カバレッジを拡張しました。

.gitleaks.toml

@qodo-code-review

qodo-code-review Bot commented Aug 2, 2026 •

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (0) 📘 Rule violations (0) 📜 Skill insights (0)

Context used
✅ Compliance rules (platform): 34 rules

Grey Divider


Remediation recommended

1. Overbroad sk-32hex regex ✓ Resolved 🐞 Bug ☼ Reliability
Description
monopo-ai-api-key に追加された sk-[a-f0-9]{32} は sk- + 32桁hex という汎用的な形状にマッチし得るため、APIキー以外の識別子等でも誤検知して
push/PR 時の Gitleaks CI
を不要に失敗させる可能性があります。コンテキスト(変数名/代入)や境界条件なしで部分一致できるため、誤検知時の運用負荷・フック回避(no-verify)誘発リスクが上がります。
Code

.gitleaks.toml[89]

+regex = '''(?i)(?:sk-proj-[a-zA-Z0-9\-_]{100,}|sk-[a-zA-Z0-9]{48}|sk-ant-api03-[a-zA-Z0-9\-_]{95}|gsk_[a-zA-Z0-9]{33}|sk-or-v1-[a-f0-9]{64}|sk-[a-f0-9]{32})'''
Relevance

●●● Strong

過去に gitleaks ルールの誤検知低減(境界/緩い(?i)の見直し)修正が受け入れられている。

PR-#425
PR-#405
PR-#202

ⓘ Recommendations generated based on similar findings in past PRs

Evidence
PRで monopo-ai-api-key の正規表現に sk-[a-f0-9]{32} が追加され、境界/コンテキストなしで sk-
という短いプレフィックスにマッチ可能になっています。一方で本リポジトリの Gitleaks
は全push/全PRで実行されるため、誤検知は即CI失敗につながります。さらに同一設定内の他ルールは誤検知低減のためにキーワードや境界を要求しており、この追加パターンだけが相対的に緩いことが分かります。

.gitleaks.toml[86-92]
.github/workflows/gitleaks.yml[6-13]
.gitleaks.toml[43-49]
.gitleaks.toml[142-146]
PR-#425

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

### Issue description
`.gitleaks.toml` の `monopo-ai-api-key` ルールに追加された `sk-[a-f0-9]{32}` が汎用的で、コンテキスト/境界なしに部分一致できるため誤検知を起こし得ます。誤検知は `.github/workflows/gitleaks.yml` により全push/全PRでCI失敗に直結します。

### Issue Context
- 現状の正規表現は `(?i)(?: ... | sk-[a-f0-9]{32})` で、`sk-` という短いプレフィックスに対する制約が弱いです。
- 同一ファイル内の他ルールでは、誤検知を抑えるためにキーワードや境界を要求しています(例: `monopo-cloud-id` の `account_id/project_id`、`monopo-pinecone-api-key` の `pinecone|pc` + `key` など)。

### Fix Focus Areas
- .gitleaks.toml[86-92]

### Suggested fix approaches
1) **境界条件を追加**(低コスト)
- `sk-[a-f0-9]{32}` を `\bsk-[a-f0-9]{32}\b` にして、長い識別子文字列の一部への誤マッチを減らす。

2) **コンテキスト要件を追加**(誤検知抑制を優先する場合)
- `deepseek` / `DEEPSEEK_API_KEY` 等のキー名・変数名(`[:=]` を含む)と組み合わせて検知する別ルールに分離する、または同ルール内でコンテキスト付きにする。

いずれかを採用し、既存の OpenAI/Anthropic/Groq/OpenRouter パターンには影響がないことを確認してください。

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Grey Divider

To customize comments, go to the Qodo configuration screen, or learn more in the docs.

Qodo Logo

Comment thread .gitleaks.toml Outdated

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In @.gitleaks.toml:
- Line 89: Update the regex in the gitleaks configuration to match Groq keys as
gsk_ followed by exactly 52 alphanumeric characters, with a terminating word
boundary so longer tokens are not partially matched. Preserve the other
credential patterns unchanged.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 25083531-d4d8-43be-8dd1-1d13bd6f8226

📥 Commits

Reviewing files that changed from the base of the PR and between a662dc8 and c17ff2a.

📒 Files selected for processing (2)
  • .gitleaks.toml
  • docs/security/leak-prevention.md

Comment thread .gitleaks.toml Outdated
genzouw added 2 commits August 3, 2026 07:42
monopo-ai-api-key ルールの汎用 sk-[a-f0-9]{32} パターンは、先頭・末尾の境界指定がなく、
task-xxx や risk-xxx のような無関係な識別子内の32桁hex部分にも部分一致し、
Gitleaks CI を不要に失敗させる可能性があった。\bsk-[a-f0-9]{32}\b として
単語境界を追加し、独立したトークンとして出現する場合のみ検知するように修正。

レビューコメント: #486 (comment)
レビュアー: qodo-code-review
優先度: high
Groq の API キーは gsk_ + 52桁の英数字が正しい仕様だが、monopo-ai-api-key
ルールでは gsk_[a-zA-Z0-9]{33} と桁数が誤っており、かつ末尾の境界指定もなかった
ため、本来のキーより短い/長いトークンの一部にも部分一致し得た。
gsk_[a-zA-Z0-9]{52}\b に修正し、正しい桁数のキーのみを、末尾が独立した
トークンである場合に限り検知するようにした。

レビューコメント: #486 (comment)
レビュアー: coderabbitai
優先度: high
@genzouw
genzouw enabled auto-merge (squash) August 3, 2026 00:37
@genzouw
genzouw merged commit 0ef0dd0 into main Aug 3, 2026
36 checks passed
@genzouw
genzouw deleted the security/enhance-gitleaks-ai-keys-4609932599233604344 branch August 3, 2026 00:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant