Module 3 · Lesson 6 · 17 min read

Tool security and permissions

Tool Security and Permissions

Introduction

Every tool you give the agent is a door into your system. If the agent is tricked or makes a mistake, the tool is what turns the error into real harm. In this lesson we learn the principle of least privilege, input validation, human approval, and the risk of indirect injection through tool results.

Learning objectives

  • Apply the principle of least privilege when designing tools
  • Detect input risks such as path traversal in file tools
  • Identify the tools that require human approval before execution
  • Explain indirect prompt injection through tool results and ways to reduce it

Why are tools the biggest point of risk?

A language model without tools may write wrong text, and that is annoying. But an agent that has a tool for deleting files or sending email may turn a misunderstanding into irreversible harm. The OWASP list of risks for language model applications ranks "excessive agency" among the top risks, because the model can be tricked by its very nature, so it must not be the only line of defense.

The golden rule: assume the model may request anything, then design the tools so that the harm stays limited even in the worst case.

Checking inputs and retrieved contentInput & Retrieved-Content FilteringLeast privilege for each toolLeast PrivilegeHuman approval for sensitive actionsHuman-in-the-LoopIsolated execution and resource limitsSandbox & LimitsTracing, monitoring and alertingTracing & Monitoring
Defense in depth: do not rely on a single layer; check the inputs, restrict permissions, ask for approval, isolate execution, and monitor.

The principle of least privilege

Give each tool only the minimum permissions needed for its task:

  • A tool that reads student reports connects to the database with a read-only account, not an administrator account.
  • The file tool works inside a specific folder, not on the whole file system.
  • The database search tool accepts specific arguments and builds the query itself, and does not accept full SQL text from the model.
  • API keys stay in the application and are never written into the instructions or the model's context.

Input validation: the path traversal example

Suppose a file_reader tool reads files from a docs folder. If the model requests the path ../../etc/passwd and the tool naively joins it to the folder, it will read a file outside the allowed folder. This is a well-known attack called path traversal. The request may come from a model error or from malicious instructions slipped into a document.

Human approval for sensitive actions

For some tools technical validation is not enough, because the question is not "is the request well-formed?" but "does the user really want this effect?" Sending an email in the university's name, deleting a record or paying an amount all deserve a human approval step in which the user sees exactly what will happen before execution. You will study this pattern in depth in the human in the loop lesson.

A practical example: a permissions gate

The following example is plain Python. We build a gate that every call passes through: it checks that the tool is on this agent's allowed list, prevents path traversal, and requests approval for tools with an effect:

python
from pathlib import PurePosixPath

POLICY = {
    "file_reader": {"risk": "read"},
    "db_search":   {"risk": "read"},
    "send_email":  {"risk": "write", "needs_approval": True},
}
ALLOWED_FOR_AGENT = {"file_reader", "db_search", "send_email"}
BASE = PurePosixPath("/srv/academy/docs")

def safe_path(relative: str) -> PurePosixPath:
    parts = PurePosixPath(relative).parts
    if PurePosixPath(relative).is_absolute() or ".." in parts:
        raise PermissionError("Path outside the allowed folder")
    return BASE / relative

def gate(call: dict, approved: bool = False) -> str:
    name, args = call["name"], call["args"]
    if name not in ALLOWED_FOR_AGENT or name not in POLICY:
        return "Rejected: tool not allowed for this agent"
    if name == "file_reader":
        try:
            args["path"] = str(safe_path(args["path"]))
        except PermissionError as e:
            return f"Rejected: {e}"
    if POLICY[name].get("needs_approval") and not approved:
        return f"Awaiting the user's approval for: {name} {args}"
    return f"Allowed to execute: {name} {args}"

print(gate({"name": "file_reader", "args": {"path": "syllabus.txt"}}))
print(gate({"name": "file_reader", "args": {"path": "../../etc/passwd"}}))
print(gate({"name": "delete_db", "args": {}}))
print(gate({"name": "send_email", "args": {"to": "dean@example.edu"}}))
print(gate({"name": "send_email", "args": {"to": "dean@example.edu"}}, approved=True))

The gate does not trust the model: every decision in it relies on a policy written in code, not on what the model says about itself or about the tool.

Multiple layers of defense

No single protection is enough, so we combine several layers:

  1. An allowed tool list for each agent according to its role.
  2. Strict validation of arguments with a schema and constraints.
  3. Minimal runtime permissions and an isolated environment (sandbox) for dangerous tools.
  4. Human approval for actions with an effect.
  5. An audit log of every call and its result.

And remember from the tool schemas lesson that tool annotations in MCP are untrusted by default; a tool's claim that it is "read-only" is no substitute for your policy.

Summary

Tools are what turn the model's errors or its being tricked into actual harm, so the model must not be the only line of defense. The principle of least privilege limits the effect of any error, and input validation prevents attacks such as path traversal. Sensitive actions need human approval, and tool results are treated as data, not commands, to avoid indirect injection. Effective protection is multiple layers: an allowlist, validation, isolation, approval and an audit log.

Terms

TermEnglishDefinition
Least privilegeLeast PrivilegeGiving each component the minimum permissions needed for its task
Path traversalPath TraversalUsing ../ to reach files outside the allowed folder
Indirect prompt injectionIndirect Prompt InjectionMalicious instructions hidden in external content that the agent reads through a tool
Human approvalHuman ApprovalA step in which a human confirms the action before it is carried out
AllowlistAllowlistAn explicit list of what is allowed, rejecting everything else
Audit logAudit LogA permanent record of every tool call, its arguments and its result

Review questions

  1. Why must we not rely on the model alone to prevent misuse of tools?
  2. How do you prevent a file reading tool from reaching files outside its folder?
  3. Give three examples of tools that need human approval and explain why.
  4. How does indirect prompt injection happen, and what is the most important way to reduce its effect?

References