Safeguard
Developer Tools•

Should your CLI refuse repos owned by another user? Git's rule

Git refuses to read config or run hooks in a repo owned by someone else unless it is listed in safe.directory. Here is what that teaches CLI authors.

Safeguard Security Team
3 min read

Yes, a tool that reads configuration from the directory it runs in should be careful with directories it does not own. Git's documented default is to refuse to parse a repository's config, let alone run its hooks, when someone else owns the repository, unless the path is explicitly marked safe.

What the rule says

The safe.directory setting in Git lists directories that are considered safe even if owned by another user. Per the Git documentation, by default Git will refuse to even parse a Git config of a repository owned by someone else, and this setting allows exceptions, for example intentionally shared repositories.

Two details are worth copying.

First, the setting is only respected in protected configuration. The docs say this prevents untrusted repositories from tampering with the value. A repo cannot add itself to its own safe list.

Second, there is a deliberate opt out. Setting the value to an asterisk marks all directories safe. The documentation names it as a way to completely opt out of the check, so it exists, but it is a blunt one.

Why a CLI author should care

Developer tools run in whatever folder the user is in. That folder might be a shared build host, a cloned third-party repository, or a directory a different account wrote into. If your tool loads a config file, a plugin list or a script path from that folder, the folder's owner decides what your tool does.

The danger is a mismatch of trust. The person typing the command trusts your tool. They did not necessarily agree to trust a file someone else left next to their code.

Steps to apply it

  1. List every file your CLI reads from the working directory, including config, plugin manifests and hook scripts.
  2. For each one, decide whether it can cause code to run or network calls to be made.
  3. For those that can, check file ownership before reading. If the owner is not the current user (or an administrator), stop and say why.
  4. Provide an explicit allow mechanism stored in the user's own configuration, never in the project folder.
  5. Print a clear error that names the path and the command to allow it.
  6. Do not offer a single switch that disables the check for everything without a loud warning.

Where it applies and where it does not

This is a defence for one situation: a file on disk owned by a different account. It does not stop a repository you cloned yourself from containing a hostile config. For that, see the guide to trust prompts for untrusted folders.

SituationOwnership check helpsTrust prompt helps
Shared host, other user's folderYesYes
Repo you cloned from a strangerNoYes
Your own projectNot neededNot needed

Quick check

Can a repository add itself to safe.directory?

No. Git only respects the setting in protected configuration, which is how it stops an untrusted repository from tampering with the list.

Is setting it to an asterisk a good fix for the error?

It removes the check entirely. List the specific path instead, unless you have a controlled reason to trust everything.

Safeguard's free scan at gold.safeguard.sh/scan can inventory the dependencies your CLI loads, which is a separate check from file ownership.

Sources

Last reviewed: 19 October 2026.

Never miss an update

Weekly insights on software supply chain security, delivered to your inbox.

Self-healing security runs on Safeguard.

Your first fix PR is minutes away.

No sales call required, even your agent can complete the purchase over MCP.