Skip to Content
harness-kit

command-policy

The built-in Policy command-policy, against dangerous command lines: production kubectl, the wrong package manager, a subagent that should only read: its defaults, its options and a configuration that writes it.

To learn what it guards against and see it at work, read the guide: Commands.

Defaults

DefaultValue
On by defaultno: an entry turns it on
Fail modeclosed
Timeout10000 ms
EventsPreToolUse
ToolsBash
Agent typesevery agent, or as its options set

Options

OptionTypeDefaultDescription
agentTypesstring[]—The agent types the Policy runs for, such as verifier; every agent and the main session when absent.
aliasesstring[]—Other names of the tool, such as k.
allowstring[] | object—The commands allowed, each a prefix of the command's words (rollout status, * for every command), inline or { file } with one item per line.
alwaysDenyTokensobject[]—Words refused before anything else, among the tool's arguments or, with anywhere, in any word of any command.
contextobject—Where the context comes from: a flag (context.flag) or a command (context.command).
default"allow" | "ask" | "deny"—What a command on no list gets: allow (the default), ask or deny.
denystring[] | object—The commands refused, as allow writes them.
denyArgsstring[]—Patterns an argument must match whole to be refused before anything else, such as db:drop.
denyAssignmentsboolean—Refuses a line that sets a variable for what it runs (PATH=… npm …, env NAME=…, env -i, export), so no allowed command runs another program through PATH or NODE_OPTIONS.
denyBareboolean—Refuses the tool run without words (a bare yarn, which installs), with the deny list.
denyWritesboolean—Refuses a line that writes a file (a redirection, tee, an in-place edit, a copy, a move or a removal), /dev/null aside.
exemptWhenobject—When a refused command is let through: outsideRepository, when it surely runs in another repository.
globalValueFlagsstring[]—The options that take the next word as their value, such as -n; every other word that starts with - is an option on its own.
launchersstring[]—Word sequences that run the tool, such as bundle exec.
localstring[] | object—Commands allowed without the context being looked up, as allow writes them.
messagestring—The fix the agent reads with every refusal, in place of the table's own.
productionstring[] | object—Patterns of production contexts, in which everything is refused.
subcommandAliasesobject—A first word that stands for others, such as kl for k8s logs.
subcommandPrefixstring[]—Words a command of the tool must start with to be screened, such as k8s; they are not part of its words.
toolstring—The program the table screens, by the last part of its path, such as kubectl; without it the table screens every command.
packageManager"bun" | "npm" | "pnpm" | "yarn"—With the preset package-manager, the repository's own package manager, whose commands are not refused; detected when absent.
preset"argocd" | "bun" | "django-db" | "helm" | "kubectl" | "npm" | "package-manager" | "pnpm" | "prisma-db" | "rails-db" | "yarn"—A command table of the kit, whose fields the ones written beside it replace, such as kubectl or package-manager.

Example

A configuration that writes every option, a comment above each:

import { defineConfig } from '@microeinhundert/harness-kit';

export default defineConfig({
  // The Policy entries, in the order their Decisions merge in.
  policies: [
    {
      // The Policy's name, unique in the configuration; messages and logs name the Policy by it.
      id: 'command-policy',
      // The constructor's options, which it decodes.
      options: {
        // The agent types the Policy runs for, such as `verifier`; every agent and the main session
        // when absent.
        agentTypes: ['verifier'],
        // Other names of the tool, such as `k`.
        aliases: ['k'],
        // The commands allowed, each a prefix of the command's words (`rollout status`, `*` for
        // every command), inline or `{ file }` with one item per line.
        allow: ['rollout status'],
        // Words refused before anything else, among the tool's arguments or, with `anywhere`, in
        // any word of any command.
        alwaysDenyTokens: [
          {
            // Whether the word is refused in any word of any command the line runs, not only among
            // the tool's arguments.
            anywhere: true,
            // The word that is refused wherever it stands.
            token: 'argocd',
          },
        ],
        // Where the context comes from: a flag (`context.flag`) or a command (`context.command`).
        context: {
          // The command that prints the context, its program and arguments, run once per command
          // line when the flag is absent; a failed or empty answer leaves the context unknown.
          command: ['kubectl'],
          // The option that names the context on the command line, such as `--context`, also as
          // `--context=<name>`; it is read anywhere in the command.
          flag: '--context',
          // Options of the screened command copied into the command that prints the context, after
          // its program.
          passFlags: ['--kubeconfig'],
          // The context an unknown one counts as, matched against the production patterns; without
          // it an unknown context counts as production.
          unknown: 'staging',
        },
        // What a command on no list gets: `allow` (the default), `ask` or `deny`.
        default: 'allow',
        // The commands refused, as `allow` writes them.
        deny: ['rollout status'],
        // Patterns an argument must match whole to be refused before anything else, such as
        // `db:drop`.
        denyArgs: ['db:drop'],
        // Refuses a line that sets a variable for what it runs (`PATH=… npm …`, `env NAME=…`,
        // `env -i`, `export`), so no allowed command runs another program through `PATH` or
        // `NODE_OPTIONS`.
        denyAssignments: true,
        // Refuses the tool run without words (a bare `yarn`, which installs), with the deny list.
        denyBare: true,
        // Refuses a line that writes a file (a redirection, `tee`, an in-place edit, a copy, a move
        // or a removal), `/dev/null` aside.
        denyWrites: true,
        // When a refused command is let through: `outsideRepository`, when it surely runs in
        // another repository.
        exemptWhen: {
          // Exempts a command that surely runs in another git repository than the session's,
          // through a chain of plain `cd`s joined by `;` or `&&` before it, or through one of the
          // `folderFlags`; a command whose folder is in doubt is not exempt.
          outsideRepository: {
            // Options that name the folder a command runs in, such as `--prefix <folder>`.
            folderFlags: ['--prefix'],
          },
        },
        // The options that take the next word as their value, such as `-n`; every other word that
        // starts with `-` is an option on its own.
        globalValueFlags: ['-n'],
        // Word sequences that run the tool, such as `bundle exec`.
        launchers: ['bundle exec'],
        // Commands allowed without the context being looked up, as `allow` writes them.
        local: ['rollout status'],
        // The fix the agent reads with every refusal, in place of the table's own.
        message: 'Ask the user to run it against production.',
        // Patterns of production contexts, in which everything is refused.
        production: ['prod'],
        // A first word that stands for others, such as `kl` for `k8s logs`.
        subcommandAliases: {
          kl: 'k8s logs',
        },
        // Words a command of the tool must start with to be screened, such as `k8s`; they are not
        // part of its words.
        subcommandPrefix: ['k8s'],
        // The program the table screens, by the last part of its path, such as `kubectl`; without
        // it the table screens every command.
        tool: 'kubectl',
        // With the preset `package-manager`, the repository's own package manager, whose commands
        // are not refused; detected when absent.
        packageManager: 'bun',
        // A command table of the kit, whose fields the ones written beside it replace, such as
        // `kubectl` or `package-manager`.
        preset: 'argocd',
      },
      // The name of the kit's Policy constructor that builds the Policy.
      use: 'command-policy',
    },
  ],
});

On this page