すべてを検証してサニタイズする
モデルが提供した入力を信頼できないものとして扱います。
「すべてを検証してサニタイズする」はCoddyKit上の無料MCP Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはMCP Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 MCP Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Inputs Are Untrusted
Every argument the model passes to a tool is untrusted input. The model may be honest, but its arguments can be shaped by injected text, so verify them. 🔎
Validate Before You Act
Check shape, type, and range before a tool does anything real. Validation at the door turns a vague bad call into a clear, safe rejection.
Let Pydantic Help
Type hints and Pydantic models reject malformed arguments before your code runs. This input must be a positive int, so a string or a negative value is refused early.
from pydantic import Field
@mcp.tool()
def get_page(n: int = Field(gt=0, le=100)) -> str:
return load(n)Bound the Values
Cap sizes, lengths, and counts. A limit field on a query keeps the model from asking for a million rows and turning one call into a denial of service.
Sanitize for the Destination
Sanitizing means making input safe for where it lands. A value safe in a log can be dangerous in a shell, a SQL string, or a file path.
Never Build SQL by Hand
String-concatenated SQL invites injection. Use parameterized queries so the database treats model input strictly as a value, never as executable code.
cur.execute(
"SELECT * FROM orders WHERE id = ?",
(order_id,),
)Guard the Shell
If a tool runs a command, never paste arguments into a shell string. Pass an argument list and avoid shell parsing so input cannot smuggle in extra commands.
Resolve and Confine Paths
For file tools, resolve the path to its canonical form and confirm it stays inside your allowed root. Reject anything with a path traversal like dot-dot.
p = (ROOT / name).resolve()
if not p.is_relative_to(ROOT):
raise ValueError("path escapes root")Fail Closed
When input does not pass a check, stop and return a clear error. Failing closed beats guessing, because a wrong guess can be exactly what an attacker wants.
Do Not Trust Tool Output Either
Data your tool returns can carry injected instructions onward. Where it matters, label or escape fetched content so the model treats it as data, not orders.
Validate at Every Boundary
Check input as it enters your tool and again before it hits a database, file, or API. Each boundary is a fresh chance to catch something unsafe.
Quick Check
Which choice best protects a SQL query from injection?
Recap
Treat all tool inputs as untrusted: validate types and ranges, sanitize for the destination, confine paths, and fail closed. Distrust the input. ✅
よくある質問
「すべてを検証してサニタイズする」レッスンは無料ですか?
はい。「すべてを検証してサニタイズする」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、MCP Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 MCP Academyコースには全4レッスンが含まれています。
「すべてを検証してサニタイズする」で何を学びますか?
モデルが提供した入力を信頼できないものとして扱います。 ブラウザで直接実行するハンズオンコードでMCP Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
MCP Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのMCP Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。
「すべてを検証してサニタイズする」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このMCP Academyレッスンでコードを書いて実行できますか?
はい。すべてのMCP Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- MCP特有の脅威
- 最小権限のツールアクセス
- すべてを検証してサニタイズする
- 破壊的な操作を保護する