Give Each Tool the Least It Needs

A language model with tools can be fooled, and no prompt or filter fully prevents that. What decides how bad a successful attack gets is what the fooled model can still reach: the tools it is offered, what each tool accepts, and what the program running those tools is allowed to touch. This lesson is about shrinking all three, so that each tool does one job and an attacker who takes over the conversation finds very little at the end of it.

Excessive agency: more power than the job needs

Recall how an assistant with tools works. The model never runs anything itself: it asks for a function by name with some arguments, and your code runs that function and hands back the result. A direct injection in the user's message, or an indirect one hidden in a document the assistant reads, can steer which function the model asks for and with which arguments. Some of those requests will slip past every prompt-level defence, so the useful question is what happens when one does.

The answer is set by what the assistant could do, not by what it is meant to do. The OWASP Top 10 for LLM Applications calls this risk Excessive Agency. It covers damaging actions carried out because a model produced an unexpected, ambiguous or manipulated output. The cause of that output does not matter: a plain mistake by the model counts as much as an attack. OWASP names three root causes, and each one is a different way of handing the model more than its job needs.

The three root causes of Excessive Agency
Root causeWhat it meansExample
Excessive functionalityThe model is offered tools, or tool features, the task never usesA tool meant to read documents can also edit and delete them
Excessive permissionsA tool runs with more rights on the systems behind it than it needsA read-only lookup connects with a database account that may also insert and delete
Excessive autonomyA high-impact action happens without a person approving itMessages are deleted or sent with no confirmation from the user

OWASP's own scenario puts the three together. An email assistant only needs to read mail, but its mail tool can also send. A crafted incoming email tells the model to search the inbox and forward anything sensitive to the attacker, and nothing stops it. Each root cause has its own fix: a tool that can only read, an account for that tool with read-only access to the mailbox, and a person who presses send on every draft. Any one of them would have broken this attack, and the first two do not depend on the model behaving well at all.

The rest of this page is about functionality and permissions: making each tool narrow and giving it, and the program around it, as little access as possible. Trying any of these attacks is for systems you own or have explicit permission to test, such as your own copy of an application; testing someone else's system without their permission is not.

True or false?

An assistant that never acts without asking the user cannot suffer from excessive agency.

False. Autonomy is only one of the three causes. A tool that can read far more than its job needs is excessive functionality, even if every action is confirmed.

Excessive agency can cause harm even when nobody is attacking the assistant.

True. OWASP counts any unexpected or ambiguous output, including a plain mistake by the model. A tool with too much power turns that mistake into damage.

Open-ended tools and narrow tools

Some tools are open-ended by design. A tool that takes a file path can read any file the program can open. A tool that takes a database query can run any query. A tool that takes a shell command can do almost anything the computer can do, and a tool that takes a web address can fetch from, or send data to, any server. Each was written with one job in mind, but its argument is wide enough to express every other job too, and the model writes that argument.

A narrow tool is built for one purpose, and its arguments can only describe that purpose. Instead of read_file(path) for answering questions from a help centre, offer read_article(name) that can open only help articles. Instead of run_sql(query) for checking stock, offer get_stock(product_id) that runs one fixed, read-only query with the id filled in.

Open-ended tools and narrow replacements
Open-ended toolWhat a fooled model can reachNarrow tool for the real job
read_file(path)Every file the program may open: keys, configuration, other users' dataread_article(name), only help articles
run_sql(query)Every table, and writes if the account allows themget_stock(product_id), one fixed read-only query
run_shell(command)Anything the account can do on the machinerestart_service(), one fixed command with no arguments
fetch_url(url)Any server, including one that collects stolen dataget_weather(city), one known service

Narrowing also means removing. OWASP's first mitigation for Excessive Agency is to offer only the tools the system needs: if the assistant never has to fetch a web page, it should have no tool that can. A tool left over from development, still registered but no longer used, is extra reach with no benefit.

A narrow tool does not make the model harder to fool. It changes what a fooled model can ask for. If the only file tool can open help articles, the worst a successful injection can do with it is read a help article.

True or false?

A tool whose description says it reads only help articles is a narrow tool, even if it accepts any path.

False. The description is text the model reads, and the model can be fooled. What the tool accepts and what its code allows decide its reach.

Replacing a general query tool with a tool that runs one fixed query limits the damage of a successful injection.

True. However the model is steered, the only thing it can ask that tool to do is run the one query with a different id.

Least privilege for the tool and for the process

The idea behind narrow tools is older than language models. In their 1975 paper on protecting information in computer systems, Saltzer and Schroeder wrote that every program and every user should operate using the least set of privileges necessary to complete the job, mainly because this limits the damage an accident or an error can cause. This is the principle of least privilege, and NIST's definition extends it to processes acting on behalf of users.

For an assistant it applies at two levels. The first is each tool: it can do only its one job, as the previous section described. The second is the process that runs the tools, meaning the operating system account the program runs under, the database account it connects with, and the keys it holds for other services. Code has bugs, and a check in a tool can be written wrongly. When the process itself can read only what the tools need, a mistake in a tool still reaches little.

What a fooled model can reach
  • The whole machine
  • What the process account may read
  • What the tool may open
  • SSH keys
  • Other users' data
  • Help articles

Each ring is a separate control. The tool's own check keeps it inside the inner ring. If that check fails, the process account keeps it inside the middle ring, and everything outside it stays unreadable no matter what the model asks for. The OWASP guidance on path traversal says it the same way: run the application with the least file system permissions it needs, so a traversal reaches as little as possible.

In practice this means a few concrete choices. A tool that only reads data connects with an account that can only read. A tool for the signed-in user's files uses access scoped to that user, not an account that can open everyone's files. The program runs under its own operating system account, which can read only the folders it needs and cannot read secrets that belong to other software on the same machine.

True or false?

Once every tool checks its arguments correctly, the account the program runs under no longer matters.

False. A check can contain a bug or be skipped by a new tool added later. A narrow account limits the damage when that happens, so the two layers protect each other.

A read-only tool should connect to the database with an account that has only read permission.

True. Then even a tool that is tricked into sending a write has its request rejected by the database itself.

Path traversal: a path that leaves its folder

Even a narrow tool often has one dangerous argument: a file name. Suppose read_article(name) joins the name to its folder, /srv/articles, and opens the result. For returns.md it opens /srv/articles/returns.md, as intended.

But a file path can climb. In a path, .. means the parent folder, so each .. goes up one level. Joined to the folder, ../../etc/passwd goes up from /srv/articles to /srv, then to /, and points at /etc/passwd. This is path traversal (also called directory traversal): a caller supplies a path that points outside the folder it was meant for. The caller here is the model, so anyone who can steer the model can try it.

There is a second form that needs no dots at all. In Python, if the second part of a join is an absolute path, the folder is thrown away. Both os.path.join and the / operator of pathlib behave this way, as their documentation states.

Joining does not keep a path inside its folder
from pathlib import Path

base = Path("/srv/articles")

print(base / "returns.md")          # /srv/articles/returns.md
print(base / "../../etc/passwd")    # /srv/articles/../../etc/passwd
print(base / "/etc/passwd")         # /etc/passwd

The middle line is the trap that catches careful readers. The joined path still starts with /srv/articles, so a check that only compares the beginning of the string passes it, yet the operating system follows the .. parts and opens a file outside the folder. The check has to look at where the path really goes.

So the safe order is: join the name to the fixed folder, resolve the result, check that the resolved path is inside the folder, and only then open it. In pathlib, resolve() makes the path absolute, removes the .. parts and follows symbolic links, so a link inside the folder that points elsewhere is caught too. is_relative_to() then compares whole path components.

Resolve first, check, then open the path you checked
from pathlib import Path

BASE = Path("/srv/articles").resolve()


def read_article(name: str) -> str:
    target = (BASE / name).resolve()
    if not target.is_relative_to(BASE):
        raise PermissionError("That article is not available.")
    return target.read_text(encoding="utf-8")

A few details in this code matter:

  • The check runs on the resolved path. On its own, is_relative_to() only compares text and does not treat .. specially.
  • Comparing strings with startswith is not enough either. /srv/articles-old starts with /srv/articles but is a different folder; comparing whole components tells them apart.
  • resolve() does not fail when the file is missing, so the containment check must always follow it.
  • The file opened is target, the path that was checked, never one rebuilt from the original input.
  • The refusal message names no path. An error that reveals where files live helps an attacker refine the next try.
What the check sees for each name
Name from the modelResolved pathVerdict
returns.md/srv/articles/returns.mdOpened
../../etc/passwd/etc/passwdRefused
/etc/passwd/etc/passwdRefused
../articles-old/draft.md/srv/articles-old/draft.mdRefused

True or false?

If a joined path starts with the folder name as text, the file it opens is inside that folder.

False. A path like /srv/articles/../../etc/passwd starts with the folder name but opens /etc/passwd once the operating system follows the dots.

Joining a name to a folder and resolving the result, then checking that it is inside the folder, catches both ../ climbs and absolute paths.

True. Both kinds end up as a resolved path outside the folder, and the containment check refuses it whatever the input looked like.

Allowlists beat blocklists

There are two ways to decide whether an input is acceptable. A blocklist (also called a denylist) lists known bad values and rejects them: remove ../, reject names containing /etc. An allowlist lists the known good values and rejects everything else. Allowlisting is the same idea as denying by default, applied to the values an argument may take.

A blocklist has to anticipate every bad input, and attackers only need one it missed. Path traversal shows how quickly it falls apart.

Inputs that slip past a filter that removes ../
InputWhy the filter misses it
....//Removing the ../ in the middle leaves the characters around it, which form ../ again
..\Systems that accept \ as a separator climb with it too
%2e%2e%2fEncoded dots and slash, which become ../ when a later step, such as a web framework, decodes the text after the filter has run
/etc/passwdHas no ../ at all, and the join drops the folder anyway

The same weakness hits blocklists of words in prompts. A filter that rejects "ignore previous instructions" misses the same instruction reworded, translated into another language, or written in an encoding the model can still read. OWASP's guidance on prompt injection says such a filter is not a security boundary and advises enforcing permissions and validating parameters at the tool instead.

The strongest fix for a file tool keeps the model's text out of the path entirely. The tool holds a fixed table from short names to stored paths. The model picks a name, the code looks it up, and an unknown name is refused. Nothing the model writes ever becomes part of a path, so there is nothing to traverse, encode or reword.

An allowlist: the model chooses a name, the code chooses the file
from pathlib import Path

BASE = Path("/srv/articles")

ARTICLES = {
    "returns": BASE / "returns.md",
    "shipping": BASE / "shipping.md",
    "warranty": BASE / "warranty.md",
}


def read_article(name: str) -> str:
    path = ARTICLES.get(name)
    if path is None:
        return "Unknown article. Available: " + ", ".join(ARTICLES)
    return path.read_text(encoding="utf-8")

The tool's definition can also list the allowed names, for example as an enum in its JSON Schema parameters, which helps the model choose a valid one. That list is a hint to the model. The dictionary lookup in code is the control, because it runs whatever the model sends.

When the set of files cannot be fixed in advance, for example because users add new articles, the resolve-and-check approach from the previous section is the protection. A simple allowlist of characters for names, such as letters, digits, - and _ with no separators, adds a further layer on top of it but never replaces the containment check.

True or false?

Removing every ../ from a file name before joining it to a folder makes the path safe.

False. Input like ....// turns back into ../ after one removal, and absolute paths, backslashes and encoded forms are not caught at all.

Listing the allowed article names in the tool definition is enough, because the model will only send those names.

False. A fooled model can send any string. The list helps an honest model; only the lookup in code refuses what is not on it.

With a table from names to stored paths, the model's text never becomes part of a file path.

True. The model only selects a key. The path that is opened was written by the developer, so traversal has nothing to work with.