Skip to main content

zn-gate for OpenCode

Install zn-gate for OpenCode: a small plugin that blocks reads of .env files and credential files, scrubs tool output and blocks pasted secrets.

View .md

On Windows PowerShell, type npm.cmd, zn.cmd and npx.cmd instead of npm, zn and npx in every command on this page (PowerShell blocks the .ps1 versions by default, with "running scripts is disabled on this system"). More in Troubleshooting.

Before you start

  1. Node.js 18 or later. Get the LTS version from nodejs.org, then open a new terminal and check node --version.
  2. OpenCode. If you do not have it yet, follow OpenCode's install page. Its npm command is:
npm install -g opencode-ai

Other ways (Homebrew, the install script): see OpenCode's install page.

Install zn-gate

Paste each line in a terminal and press Enter after each one:

npm install -g zn-gate
zn hooks install opencode

On macOS and Linux, if npm install -g fails with EACCES, follow npm's guide to fixing permission errors.

Fix it without administrator rights:

mkdir -p ~/.npm-global && npm config set prefix ~/.npm-global
echo 'export PATH="$HOME/.npm-global/bin:$PATH"' >> ~/.zshrc

Bash users: write to ~/.bashrc instead of ~/.zshrc.

Then open a new terminal and run npx -y zn-gate init. A Node.js version manager such as nvm also avoids the problem.

zn-gate installs a small OpenCode plugin. Then restart OpenCode.

The install also prints a long note that starts with optional: to let OpenCode use the secrets kept in zn's vault, add the "secrets" MCP server to opencode.json under "mcp": and then a line of JSON. It is optional: it only matters if you want OpenCode to use the secrets kept in zn's vault. You can ignore it, and the protection below works without it.

Remove it

To remove the plugin, run zn hooks uninstall opencode and restart OpenCode.

What you get

  • Reads of .env files with values, credential files and ~/.zn are blocked in every tool.
  • Every tool's output is scrubbed before the model sees it, and so is the preview in the terminal interface.
  • A secret pasted into a prompt is blocked (OpenCode shows it as an error).
  • If the zn plugin cannot run, calls and messages are blocked. If the Node.js it was installed with is gone, the blocked call says so in plain words and gives the repair step: run zn-gate check, then zn hooks install opencode.

Limits

  • Not tested on Windows.
  • zn-gate check confirms zn's rules for OpenCode but does not start OpenCode.

Keep your .env values in the vault

The agent cannot read .env files with real values. Move the values into zn-gate's encrypted vault, and the file keeps markers such as __ZN_STRIPE_KEY__:

zn unlock            # creates the vault the first time
zn env shadow .env

After this your own app also reads the markers: start it with zn-gate's run command once you allow it. env shadow tells you how to undo it. More: the vault.

Check that it works

In a new terminal, run zn-gate check. It should show OK for OpenCode. What the output means.

Further reading