Skip to content

test(worker): verifyGitHubSignature HMAC unit tests (patch)#228

Merged
liplus-lin-lay merged 1 commit into
mainfrom
227-testworker-verifygithubsignature-hmac-verification-tests
Jun 21, 2026
Merged

test(worker): verifyGitHubSignature HMAC unit tests (patch)#228
liplus-lin-lay merged 1 commit into
mainfrom
227-testworker-verifygithubsignature-hmac-verification-tests

Conversation

@liplus-lin-lay

Copy link
Copy Markdown
Member

Closes #227

概要

webhook 受け口の HMAC-SHA256 署名検証 verifyGitHubSignature をテストでカバーする。index.ts の module-private 関数を worker/src/signature.ts に抽出(export 付き・挙動は不変)し、index.ts は import に差し替えた。index.ts は agent / oauth / store のフルグラフを import するため関数を直接 import できず、純 HMAC ロジックを切り出すことで軽量 unit test に載せられるようにした。

影響スコープ

patch(挙動不変)。署名検証ロジックはバイト単位でそのまま移動しただけで、呼び出し側(const valid = await verifyGitHubSignature(...))も変更なし。ユーザー/システム観測可能なランタイム変更はない。

変更ファイル

ファイル 種別 内容
worker/src/signature.ts 新規 verifyGitHubSignature を verbatim 抽出+export。trust boundary を説明する先頭コメント追加
worker/src/index.ts 変更 inline 定義を削除し import { verifyGitHubSignature } from "./signature.js"; に差し替え
worker/test/signature.test.ts 新規 tsx --test 純 unit test。期待署名は node:cryptocreateHmac で独立生成(被テスト関数を使わない oracle)

テスト内容

  • 正しい署名(同一 secret+同一 body)→ true
  • 本文改竄(body A で署名し body B で検証)→ false
  • 別 secret で検証 → false
  • 署名フォーマット不一致(空文字 / sha256= prefix 無し / 長さ違いの hex)→ すべて false

確認

  • npm testtsx --test test/*.test.ts && vitest run)全 pass。
    • tsx phase: 53 pass / 0 fail(既存 47 + 新規 6)
    • vitest phase: 25 pass / 0 fail
    • 合計 78 pass(既存 72 + 新規 6)
  • npx tsc --noEmit: 抽出した signature.tsindex.ts の import 差し替えは型エラーなし。src/agent.ts の既存エラー2件(MCP SDK の型重複・本 PR と無関係)は変更前から存在し、本変更では増減なし。

Move the module-private verifyGitHubSignature out of worker/src/index.ts into
a new worker/src/signature.ts (exported verbatim, behavior unchanged) and
replace the inline definition with an import. index.ts pulls the full agent /
oauth / store graph, so importing the function directly was infeasible for a
lightweight unit test; extracting it isolates the pure HMAC-SHA256 logic.

Add worker/test/signature.test.ts (tsx --test, node:crypto as an independent
signature oracle): valid signature, tampered body, wrong secret, and malformed
signature formats (empty / no sha256= prefix / wrong-length hex) all asserted.

署名検証は trust boundary(送信元が本物の GitHub であることと本文改竄なしを
同時保証する)であり subtractive の明示保護領域のためテスト価値が高い。
挙動は不変で、関数を移動して import に差し替えただけ。

Refs #227
@liplus-lin-lay liplus-lin-lay linked an issue Jun 21, 2026 that may be closed by this pull request
@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Updated (UTC)
✅ Deployment successful!
View logs
github-webhook-mcp 72bf112 Jun 21 2026, 05:14 AM

@liplus-lin-lay liplus-lin-lay left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

AI self-review (auto mode)

実差分・テスト本体・CI を確認(subagent 報告の裏取り込み)。

コード(挙動不変の検証)

  • signature.ts: verifyGitHubSignature をバイト単位で移動+export。crypto ロジック・expected === signature 比較とも一切変更なし。trust boundary を説明するコメント追加。
  • index.ts: inline 定義を削除し import { verifyGitHubSignature } from "./signature.js"; を追加。call site(env.GITHUB_WEBHOOK_SECRET, body, sig)は不変。
  • → 関数の移動のみ。ユーザー/システム観測可能なランタイム変更なし。

テスト

  • signature.test.ts: 期待署名を node:cryptocreateHmac独立生成(被験関数で生成しない=自己検証回避)。
  • 6ケース: 正常署名→true / 本文改竄→false / 別secret→false / 空文字→false / sha256= prefix 無し→false / 長さ違い hex→false。
  • 既存 test/*.test.ts の様式(node:test + node:assert/strict)に追従。tsx --test glob が自動で拾う。

分類

  • patch 妥当。

CI

  • CI / test / Workers Builds 3本 pass。ローカル npm test = tsx 53 + vitest 25 = 78本 green、tsc は本変更起因の新規エラーなし(agent.ts の2件は baseline 既存)。

→ approve 相当。squash merge へ。

@liplus-lin-lay liplus-lin-lay merged commit 07c6e4c into main Jun 21, 2026
3 checks passed
@liplus-lin-lay liplus-lin-lay deleted the 227-testworker-verifygithubsignature-hmac-verification-tests branch June 21, 2026 05:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

test(worker): verifyGitHubSignature HMAC verification tests

1 participant