OpenCode
Integrate sus with OpenCode plugins to automatically scan packages installed by AI agents
OpenCode's plugin system lets you extend and control agent behavior using JavaScript or TypeScript. By using the tool.execute.before hook, you can automatically route all package installations through sus for security scanning.
Why Use Plugins?
When AI agents install packages autonomously, they don't verify package integrity or check for known vulnerabilities. This makes them vulnerable to:
- Typosquatting - installing
lodashsinstead oflodash - Supply chain attacks - compromised packages like
event-stream - Malicious install scripts - packages that execute code during installation
With sus plugins, every npm install, yarn add, pnpm add, or bun add command is intercepted and routed through sus first.
Prerequisites
- sus CLI installed
- OpenCode
Setup
OpenCode plugins are JavaScript or TypeScript files placed in the .opencode/plugins/ directory.
Create the plugin
Create .opencode/plugins/sus-gateway.ts:
import type { Plugin } from "@opencode-ai/plugin"
const INSTALL_PATTERN = /^(npm (i|install|add)|yarn add|pnpm (add|i|install)|bun (add|i|install)|pip install|cargo add) (.+)$/
const UNINSTALL_PATTERN = /^(npm (uninstall|rm)|yarn remove|pnpm (rm|remove)|bun (rm|remove)|pip uninstall|cargo remove) (.+)$/
export const SusGatewayPlugin: Plugin = async ({ client }) => {
await client.app.log({
service: "sus-gateway",
level: "info",
message: "sus gateway plugin loaded",
})
return {
"tool.execute.before": async (input, output) => {
if (input.tool !== "bash") return
const cmd = output.args.command
const installMatch = cmd.match(INSTALL_PATTERN)
if (installMatch) {
const pkg = installMatch[6]
throw new Error(`Use \`sus add ${pkg}\` instead for security scanning.`)
}
const uninstallMatch = cmd.match(UNINSTALL_PATTERN)
if (uninstallMatch) {
const pkg = uninstallMatch[5]
throw new Error(`Use \`sus remove ${pkg}\` instead.`)
}
},
}
}That's itβOpenCode automatically loads plugins from this directory at startup.
JavaScript version
If you prefer plain JavaScript, create .opencode/plugins/sus-gateway.js:
const INSTALL_PATTERN = /^(npm (i|install|add)|yarn add|pnpm (add|i|install)|bun (add|i|install)|pip install|cargo add) (.+)$/
const UNINSTALL_PATTERN = /^(npm (uninstall|rm)|yarn remove|pnpm (rm|remove)|bun (rm|remove)|pip uninstall|cargo remove) (.+)$/
export const SusGatewayPlugin = async ({ client }) => {
return {
"tool.execute.before": async (input, output) => {
if (input.tool !== "bash") return
const cmd = output.args.command
const installMatch = cmd.match(INSTALL_PATTERN)
if (installMatch) {
const pkg = installMatch[6]
throw new Error(`Use \`sus add ${pkg}\` instead for security scanning.`)
}
const uninstallMatch = cmd.match(UNINSTALL_PATTERN)
if (uninstallMatch) {
const pkg = uninstallMatch[5]
throw new Error(`Use \`sus remove ${pkg}\` instead.`)
}
},
}
}How It Works
When OpenCode tries to run a package install command:
- OpenCode attempts
npm install express - Plugin intercepts via
tool.execute.before - Plugin throws an Error with instructions to use sus
- OpenCode follows the instruction and runs
sus add express - sus scans the package for vulnerabilities
- If safe, sus installs using your detected package manager
Command Mapping
The plugin intercepts these commands and routes them through sus:
| Original Command | sus Equivalent |
|---|---|
npm install pkg | sus add pkg |
yarn add pkg | sus add pkg |
pnpm add pkg | sus add pkg |
bun add pkg | sus add pkg |
pip install pkg | sus add pkg |
cargo add pkg | sus add pkg |
Commands without specific packages (like npm install to install from package.json) are allowed through.
Testing the Integration
Ask OpenCode to install a package:
Install the express packageYou should see the plugin intercept the command and instruct OpenCode to use sus instead:
π checking express@4.21.0...
β
not sus
ββ publisher: expressjs (verified)
ββ downloads: 32M/week
ββ cves: 0
ββ install scripts: none
π¦ installed
π updated AGENTS.md docs indexGlobal Configuration
To apply the sus plugin to all your projects, place it in the global plugins directory:
mkdir -p ~/.config/opencode/pluginsThen copy the plugin file to ~/.config/opencode/plugins/sus-gateway.ts.
Using npm Packages
You can also publish and install the plugin via npm. Add it to your OpenCode config:
{
"$schema": "https://opencode.ai/config.json",
"plugin": ["sus-gateway"]
}Troubleshooting
Plugin not loading
- Verify the plugin file exists at
.opencode/plugins/sus-gateway.ts - Check that the file exports a named function (not default export)
- Restart OpenCode after adding the plugin
Check plugin logs
Add logging to debug your plugin:
await client.app.log({
service: "sus-gateway",
level: "debug",
message: "Intercepted command",
extra: { command: cmd },
})TypeScript errors
If using TypeScript, install the plugin types:
npm install -D @opencode-ai/pluginOr add the types to your project's .opencode/package.json:
{
"devDependencies": {
"@opencode-ai/plugin": "latest"
}
}Next Steps
- Learn about threat detection
- Learn about AGENTS.md
- See other integration guides
On this page