Skip to content

Commit f395cb3

Browse files
authored
confirm-pam (#223)
* confirm-pam * fix: タイトルを「AI Agentのコマンド実行にTouch IDを使った「人間の確認」を挟むCLIツール confirm-pam を作った」に変更 * Implement code changes to enhance functionality and improve performance * fix: confirm-pamの説明を追加し、ビルドシステムの詳細を明確化 * fix: 画像のURLを相対パスに修正 * fix: 説明文を修正し、AIによる回避の難易度を明確化
1 parent 4e6e03e commit f395cb3

8 files changed

Lines changed: 1924 additions & 318 deletions

File tree

.node-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v20.8.1
1+
v22.17.0

.textlintrc

Lines changed: 0 additions & 12 deletions
This file was deleted.

.textlintrc.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"rules": {
3+
"@textlint-ja/preset-ai-writing": true,
4+
"preset-ja-technical-writing": true,
5+
"@proofdict/proofdict": {
6+
"dictURL": "https://azu.github.io/proof-dictionary/"
7+
}
8+
}
9+
}
Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
---
2+
title: "AI Agentのコマンド実行にTouch IDを使った「人間の確認」を挟むCLIツール confirm-pam を作った"
3+
author: azu
4+
layout: post
5+
date: 2025-07-05T15:30
6+
category: Security
7+
tags:
8+
- Security
9+
- macOS
10+
- CLI
11+
- Rust
12+
---
13+
14+
macOS で Touch ID を使った「人間の確認」ができるシンプルな CLI ツール [confirm-pam](https://github.com/azu/confirm-pam) を作りました。
15+
16+
- [azu/confirm-pam: CLI tool for biometric authentication confirmation prompts](https://github.com/azu/confirm-pam)
17+
18+
このツールを使うことで、AI Agent が任意のコマンドやスクリプトの実行する前に、Touch ID による生体認証を要求できます。
19+
コマンドラインから実行される処理に対して、人間による明示的な確認ステップを追加する仕組みを提供します。
20+
21+
## confirm-pam とは
22+
23+
confirm-pam は、macOS の Touch ID を使った生体認証による確認プロンプトを提供する CLI ツールです。
24+
25+
### 主な特徴
26+
27+
- Touch ID 認証をサポート
28+
- 認証ダイアログに任意のメッセージを表示
29+
- 0(成功)、1(失敗)、2(エラー)の 3 つの終了コードで結果を判定
30+
- Rust で書かれていて、現時点だと macOS のみ対応
31+
32+
基本的な使い方は次のようになります。
33+
34+
![image](/wp-content/uploads/2025/07/05-1751706673.png)
35+
36+
```bash
37+
# 基本的な認証プロンプト
38+
confirm-pam "この操作を実行しますか?"
39+
40+
# 認証成功時の終了コードは0
41+
echo $? # 0
42+
43+
# 認証失敗時やキャンセル時の終了コードは1
44+
# Touch IDの認証を失敗またはキャンセルした場合
45+
echo $? # 1
46+
```
47+
48+
## インストール方法
49+
50+
confirm-pam は[crates.io](https://crates.io/crates/confirm-pam)で公開されているため、cargo でインストールできます。
51+
52+
```bash
53+
cargo install confirm-pam
54+
```
55+
56+
また、GitHub からソースコードをクローンしてビルドできます。
57+
58+
```bash
59+
git clone https://github.com/azu/confirm-pam.git
60+
cd confirm-pam
61+
cargo build --release
62+
```
63+
64+
### 必要な環境
65+
66+
- macOS 10.12.2 以降
67+
- Touch ID 対応デバイス
68+
- Touch ID がシステム環境設定で有効化されていること
69+
70+
## 基本的な使い方
71+
72+
confirm-pam は非常にシンプルな API を提供しています。
73+
74+
```bash
75+
confirm-pam [メッセージ]
76+
```
77+
78+
メッセージを指定すると、Touch ID の認証ダイアログにそのメッセージが表示されます。
79+
80+
![image](/wp-content/uploads/2025/07/05-1751706845.png)
81+
82+
```bash
83+
# 渡したメッセージが認証ダイアログに表示されます
84+
confirm-pam "重要な変更をコミットしようとしています。続行しますか?"
85+
```
86+
87+
終了コードによって認証結果を判定できます。例えば次のような値が返されます。
88+
89+
- `0`: 認証成功
90+
- `1`: 認証失敗またはユーザーによるキャンセル
91+
- `2`: システムエラー(Touch ID が無効化されている等)
92+
93+
## 実用例
94+
95+
confirm-pam は次のようなユースケースを想定して作成しています。
96+
97+
### `git commit --no-verify`の回避の確認
98+
99+
危険な git コマンドを実行する前に確認を挟む例です。`--no-verify` オプションのような、通常の安全チェックを回避するコマンドの実行前に認証を要求します。
100+
101+
```bash
102+
# ~/.zshrc や ~/.bashrc に追加
103+
git() {
104+
if [[ $@ == *'commit'* && $@ == *'--no-verify'* ]]; then
105+
if confirm-pam "git commit --no-verifyを実行します。続行しますか?"; then
106+
command git "$@"
107+
else
108+
echo "認証に失敗しました。操作をキャンセルします。人間による確認が必要です。"
109+
return 1
110+
fi
111+
else
112+
command git "$@"
113+
fi
114+
}
115+
```
116+
117+
Claude Code のような AI Agent は`git commit --no-verify`で pre-commit Hooks を回避してきます。
118+
Touch ID などの人間の認証を挟むことで、Hook を無視したコミットを防げます。
119+
120+
あと、人間がズルして `--no-verify` をつけてコミットするときに、確認の意味としてダイアログを出す方法としても使えます。
121+
122+
### スクリプト内での使用
123+
124+
本番環境へのデプロイなど、影響範囲が大きい操作の確認ダイアログを表示する例です。
125+
126+
```bash
127+
#!/bin/bash
128+
129+
# 危険な操作の前に確認
130+
if confirm-pam "本番環境へのデプロイを開始します。続行しますか?"; then
131+
echo "デプロイを開始します..."
132+
# デプロイ処理
133+
else
134+
echo "デプロイがキャンセルされました"
135+
exit 1
136+
fi
137+
```
138+
139+
`y`を入力しないと進めないようにする方法もありますが AI Agent は `echo "y" | コマンド` のような pipe で回避するので、人間の確認を挟みたい時に利用できます。
140+
141+
## 技術的な仕組み
142+
143+
confirm-pam は、将来的なクロスプラットフォーム対応を見据えたレイヤー化アーキテクチャを採用しています。
144+
145+
### アーキテクチャ設計
146+
147+
```
148+
main.rs (CLI エントリポイント)
149+
150+
auth/mod.rs (認証抽象化レイヤー)
151+
152+
platform/mod.rs (プラットフォーム固有実装)
153+
154+
各OS実装 (macos/linux/windows)
155+
```
156+
157+
### macOS 実装
158+
159+
macOS では FFI(Foreign Function Interface)を使用して、Rust から Swift コードを呼び出しています。
160+
161+
実装の詳細は次の通りです。
162+
163+
- Swift 実装: `src/platform/macos/auth_helper.swift` で LocalAuthentication フレームワークを使用
164+
- ビルドシステム: `build.rs``swiftc` を使って Swift コードをコンパイル
165+
- 認証処理: Touch ID による生体認証を同期的に処理
166+
167+
#### LocalAuthentication フレームワーク
168+
169+
Swift 実装では、Apple の LocalAuthentication フレームワークを使用しています。
170+
171+
```swift
172+
// 概念的な実装例
173+
import LocalAuthentication
174+
175+
let context = LAContext()
176+
let policy = LAPolicy.deviceOwnerAuthenticationWithBiometrics
177+
context.evaluatePolicy(policy, localizedReason: message) { success, error in
178+
// 認証結果の処理
179+
}
180+
```
181+
182+
これにより、システム標準の認証ダイアログが表示され、Touch ID で認証します。
183+
184+
### 抽象化レイヤー
185+
186+
`BiometricAuthenticator` トレイトによって各プラットフォームの実装を抽象化しています。
187+
188+
主要な API は次の通りです。
189+
190+
- `authenticate(message: &str) -> Result<bool>`: 認証し結果を返す
191+
- `is_available() -> Result<bool>`: 認証機能の利用可能性を確認
192+
193+
コンパイル時の条件分岐で各 OS 実装を選択し、単一のバイナリとして動作します。
194+
195+
## 今後のロードマップ
196+
197+
現在は macOS の Touch ID のみサポートしていますが、他のプラットフォームへの対応も予定しています。
198+
しかし、自分は macOS しか使ってないので、他のプラットフォームの実装を追加したい人は PR を待ってます。
199+
200+
- [Add Linux fingerprint authentication support using PAM + fprintd · Issue #1 · azu/confirm-pam](https://github.com/azu/confirm-pam/issues/1)
201+
- [Add Windows biometric authentication support using Windows Hello · Issue #2 · azu/confirm-pam](https://github.com/azu/confirm-pam/issues/2)
202+
203+
## まとめ
204+
205+
confirm-pam は、macOS で Touch ID を使った生体認証確認を簡単に追加できる CLI ツールです。
206+
AI が回避しにくい、人間による確認を挟むためのツールとして使ってみてください。
207+
208+
- リポジトリ: [azu/confirm-pam](https://github.com/azu/confirm-pam)
209+
- crates.io: [confirm-pam](https://crates.io/crates/confirm-pam)
210+
211+
Note: 認証をたくさん出すと無意識的に OK してしまうので、この辺をもっと工夫する必要が出てくるかもしれません。

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
},
99
"devDependencies": {
1010
"@proofdict/textlint-rule-proofdict": "^3.1.2",
11+
"@textlint-ja/textlint-rule-preset-ai-writing": "^1.4.0",
1112
"textlint": "^15.0.1",
1213
"textlint-rule-no-doubled-joshi": "^5.1.0",
1314
"textlint-rule-no-mix-dearu-desumasu": "^6.0.4",
14-
"textlint-rule-no-start-duplicated-conjunction": "^2.0.2"
15+
"textlint-rule-no-start-duplicated-conjunction": "^2.0.2",
16+
"textlint-rule-preset-ja-technical-writing": "^12.0.2"
1517
},
1618
"scripts": {
1719
"textlint": "git diff --name-only --diff-filter=ACMR origin/develop | grep -a '_posts/.*.md$' | xargs textlint",
214 KB
Loading
234 KB
Loading

0 commit comments

Comments
 (0)