enforcing-nophi-logging
できること
このスキルは、ログ、トレース、エラーレポートが書き出されたり送信されたりする前に、保護対象の健康情報を取り除くフィルターを追加します。臨床テキストの処理が、患者の詳細をログに漏らすおそれのあるチーム向けです。
使う場面
- ✓OpenMedのサービスが、患者の詳細を含みうるリクエストのテキスト、モデルの出力、エラーメッセージをログに残しているとき。
- ✓ログやエラーの追跡が、匿名化の境界の外にある中央のサービスへ送られているとき。
- ✓生のテキストの代わりに、件数やハッシュを載せた構造化ログのフィールドが必要なとき。
スキルのリンク
https://github.com/maziyarpanahi/openmed/blob/master/skills/enforcing-nophi-logging/SKILL.mdコピーしてAI(Claude、ChatGPT、Cursor)に貼り付け、指示に従うよう頼んでください。
使い方
いちばん簡単な方法:メッセージをコピーしてAIに貼り付けるだけ。AIがスキルを読んで指示に従います。
貼り付け用メッセージ
Claude Code、Codex、Cursorの場合:プロジェクトのフォルダで実行するとスキルがインストールされます。
SKILL.mdをダウンロードして、エージェントのスキル用フォルダに入れてください。
AIへの頼み方の例
“当社のOpenMed APIはリクエストのテキストをログに残し、エラーの追跡サービスにスタックトレースを送ることがあります。患者の情報が保存されないよう、フィルターを追加してください。”
毎週5つの新しいスキル。1分で読めます。
仕組み
エージェントは、ログの設定に、速いパターンフィルターとモデルによる補助の処理を追加します。そうすると、レコードは出力される前に清められ、確認できないレコードはブロックされます。
技術的な内容(元のSKILL.md)
作者による元のテキスト(英語)をGitHubからそのまま表示しています。
Enforcing No-PHI Logging
Logs are a top breach vector: a clinical string lands in a log line, gets shipped to a centralized log store and an error tracker, and is now PHI sitting outside the de-id boundary. OpenMed's local-first stance says no raw PHI in logs, caches, or error reports — this skill enforces it with a redaction guard that runs before any record is emitted.
When to use this skill
- An OpenMed service logs request text, model output, or exception messages.
- You ship logs/traces to a centralized store or error tracker (Sentry, ELK).
- You need a
logging.Filter(or OTel processor) that redacts PHI pre-emit. - You want structured, no-PHI log fields (offsets, hashes, counts) for debugging.
Quick start — a redacting logging.Filter
import logging
import re
import openmed
# Cheap regex pre-filter for the highest-risk structured identifiers. This runs
# on every record, so keep it fast; the model is the fallback for free-text PHI.
_FAST_PATTERNS = [
(re.compile(r"\b\d{3}-\d{2}-\d{4}\b"), "[SSN]"),
(re.compile(r"\b\d{16}\b"), "[CARD]"),
(re.compile(r"\b[\w.+-]+@[\w-]+\.[\w.-]+\b"), "[EMAIL]"),
(re.compile(r"\b(?:\+?\d[\d().\-\s]{7,}\d)\b"), "[PHONE]"),
]
class NoPHIFilter(logging.Filter):
"""Redact PHI from a log record before it is emitted. Fail closed."""
def __init__(self, model_name: str | None = None, use_model: bool = True):
super().__init__()
self.model_name = model_name
self.use_model = use_model
def filter(self, record: logging.LogRecord) -> bool:
try:
message = record.getMessage()
record.msg = self._scrub(message)
record.args = () # message already rendered & scrubbed
except Exception:
# Never let the logger leak on error — drop the message, keep the level.
record.msg = "[REDACTED: scrub error]"
record.args = ()
return True # keep the (now-clean) record
def _scrub(self, text: str) -> str:
for pattern, tag in _FAST_PATTERNS:
text = pattern.sub(tag, text)
if not self.use_model:
return text
# Model fallback for free-text PHI (names, locations, dates). Replace by
# offset, right-to-left, so earlier offsets stay valid.
spans = openmed.extract_pii(text, model_name=self.model_name) \
if self.model_name else openmed.extract_pii(text)
for e in sorted(spans.entities, key=lambda s: s.start, reverse=True):
text = text[:e.start] + f"[{e.label}]" + text[e.end:]
return text
# Attach to every handler that might emit clinical text.
handler = logging.StreamHandler()
handler.addFilter(NoPHIFilter(model_name="OpenMed/Privacy-PII-Detection"))
logging.getLogger("openmed.service").addHandler(handler)
Prefer structured, no-PHI fields
Don't log the note and scrub it — log about it without the text in the first place:
logger.info(
"deidentified note",
extra={
"doc_id": doc_id, # opaque id, not the text
"phi_entity_count": len(result.entities),
"phi_labels": sorted({e.label for e in result.entities}),
"char_len": len(text),
# offsets/hashes for debugging; never the plaintext span
"phi_offsets": [(e.start, e.end) for e in result.entities],
},
)
Redaction is the safety net; not logging PHI is the actual fix.
OpenTelemetry / error trackers
- Spans: add a
SpanProcessor.on_end(or attribute hook) that runs the same_scrubover string span attributes and events before export. - Error trackers: register a
before_sendhook (e.g. Sentry) that scrubs exception messages, breadcrumbs, and request bodies. Stack traces often embed the offending input — scrub the message, not just the frames.
Workflow
- Inventory sinks. List every place a clinical string can reach: app logs, access logs, OTel spans, error tracker, crash reports, request/response dumps.
- Install the regex pre-filter for structured identifiers (SSN, card, email, phone) — fast, runs on every record.
- Add the model fallback (
openmed.extract_pii) for free-text PHI on the sinks that carry clinical narrative; skip it on hot paths where regex suffices. - Switch to structured fields. Replace "log the text" with "log counts, labels, offsets, ids".
- Fail closed. On any scrub error,
よくある質問
すべてのログ行をフィルターしますか?
はい。各レコードに対して、よくある識別子には速いパターンフィルター、自由記述の詳細にはモデルを使います。
フィルターが失敗したらどうなりますか?
安全側に倒れるように設計されているので、安全でないレコードは出力されず、ブロックされます。
トレースやエラーレポートも対象ですか?
はい。通常のログに加えて、OpenTelemetryのスパンやエラーの追跡も対象です。
詳細
作者による元の説明
Add a logging and telemetry guard that scrubs or blocks PHI from logs, traces, and error reports around an OpenMed deployment. Use when the user wants a Python logging.Filter that redacts protected health information before records are emitted, wants to keep PHI out of OpenTelemetry spans or error trackers, needs structured no-PHI log fields, or is worried that logs and stack traces are leaking patient data. Trigger on \"scrub logs\", \"redact PHI from logs\", \"no-PHI logging\", \"logging filter\", \"telemetry redaction\", \"logs leaking patient data\", or \"OpenTelemetry redaction\" in an Op
repo: maziyarpanahi/openmed
人気
- GitHubのスター
- 5.8k
- ランキング
- #14693
- ライセンス
- Apache-2.0
- 更新日
- 2026/11/2
コメント
- …
このリポジトリの他のスキル
annotating-variants
5.8kエージェントが、無料の公開アノテーターを使って、遺伝子バリアントの作用を予測し、HGVS名をゲノム上の位置に変換し、集団の頻度を付与できるよう支援します。
ask-openmed
5.8kエージェントが、臨床テキストの依頼に合ったOpenMedのワークフローを選べるよう支援します。患者データが含まれる可能性がある場合は、必ずプライバシーの確認から始めます。
assembling-fhir-bundles
5.8kエージェントが、複数のFHIR健康記録を、内部の参照を正しくつないだ一つのトランザクションBundleにまとめ、EHRに送れる状態にします。
auditing-deid-leakage
5.8kエージェントが、匿名化済みの臨床テキストに残っている識別子(SSN、カード番号、メール、日付など)をスキャンし、残っていれば公開を止めます。
似ているスキル
reverse-engineer-anything
422kREAツールを使って、出荷済みのアプリやパッケージの中身を調べ、機能の説明、版の比較、再構築の手順をエージェントが支援します。
1password
394k1Password CLIをセットアップし、サインインして、秘密情報の値を会話に表示せずに読み取ったり注入したりするようエージェントを支援します。
healthcheck
394kOpenClawを動かすコンピューターやサーバーを、SSH、ファイアウォール、更新、バックアップなどのセキュリティリスクについて監査し、安全な修正案を提案するようエージェントを支援します。