COPY TO PROGRAM ran a shell command on the database server

VERICTO-080 Critical In CI (offline CLI): BLOCKED

COPY users TO PROGRAM 'gzip > /tmp/users.csv.gz'; runs a shell command on the database host, as the operating-system user that runs PostgreSQL, and pipes the table into it. Any command works there, not just gzip, which makes it a way to run code on the server and move data off it. If you did not expect it, treat it as a security incident.

How to recover the data

  • Find who ran it and from where (database logs with the role and client address, application logs).
  • Remove the privilege that allowed it: revoke superuser and membership in pg_execute_server_program from every role that does not strictly need them.
  • Inspect the host for files the command wrote (here, /tmp/users.csv.gz) and for anything else it may have run, and rotate the credentials of the role that was used.
  • If table data left the server, handle it as a data exposure under your incident process.

Why it happens

PostgreSQL allows COPY ... PROGRAM to superusers and members of pg_execute_server_program. Applications and tools that connect as a superuser, often postgres because it was the default, inherit that power, so a generated export or an injected statement can use it.

How to fix it

Export through the client instead, which needs no server-side privileges:

COPY users TO STDOUT WITH (FORMAT csv);

In psql, \copy users TO 'users.csv' WITH (FORMAT csv) writes the file on your machine. Never let applications or agents connect as a superuser.

How to stop it from happening again

VERICTO-080 · COPY … TO/FROM PROGRAM (shell execution) · Severity: Critical · AST node: CopyStmt > PROGRAM

The statement the rule reports

-- export.sql
COPY users TO PROGRAM 'gzip > /tmp/users.csv.gz';

Real CLI output

$ npx @vericto/vericto-cli@2.3.0 check --offline export.sql
note: offline mode: 42 standard rules at default severities, engine 5.1.0. Custom rules, overrides, sensitive columns, allowlists and the audit trail apply online only.
✖ export.sql  BLOCKED  VERICTO-080 critical
    CopyStmt > PROGRAM
    fix: Use client-side \copy or COPY … TO STDOUT, never PROGRAM
1 blocked · 0 allowed · 0 flagged · 0 monitored   (ruleset standard@engine-5.1.0 · mode: offline (engine 5.1.0, 42 standard rules, no workspace policy))

✖ Vericto blocked 1 statement  (ruleset standard@engine-5.1.0 · mode: offline (engine 5.1.0, 42 standard rules, no workspace policy))
  ✖ export.sql  BLOCKED  VERICTO-080 critical
      CopyStmt > PROGRAM
      fix: Use client-side \copy or COPY … TO STDOUT, never PROGRAM

This is Vericto enforcing a rule — the exit code is intentional, not a crash.
To allow a finding on purpose, add an inline comment on the statement:
    -- vericto:ignore[VERICTO-080] <reason>   (a reason is required)

The CLI blocks it and exits with code 1, so the CI job fails before the merge.

The corrected version

-- export.sql
COPY users TO STDOUT WITH (FORMAT csv);
$ npx @vericto/vericto-cli@2.3.0 check --offline export.sql
note: offline mode: 42 standard rules at default severities, engine 5.1.0. Custom rules, overrides, sensitive columns, allowlists and the audit trail apply online only.
0 blocked · 1 allowed · 0 flagged · 0 monitored   (ruleset standard@engine-5.1.0 · mode: offline (engine 5.1.0, 42 standard rules, no workspace policy))

Try it free, no account

The CLI checks your .sql files on your machine with the 42 standard rules: no sign-up, no API key, and no SQL leaves your computer. It is open source (AGPL-3.0-only).

npx @vericto/vericto-cli@2.3.0 check --offline migrations/

On every pull request

The vericto/check-sql GitHub Action (Apache-2.0) runs the same offline check on the .sql files a PR changes and annotates the findings on the PR. Save it as .github/workflows/vericto.yml:

on: pull_request
jobs:
  vericto:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7
        with: { fetch-depth: 0 }
      - uses: vericto/check-sql@v1
        with: { offline: true, upload-sarif: false }

At runtime, for AI agents

The SQL an AI agent writes never goes through a PR. The Vericto TCP proxy checks each statement between the application or agent and the database, and the MCP check_sql tool lets the agent check SQL before it runs it. Both use the runtime profile, which also blocks schema changes.

The AST condition in the engine

The rule is a deterministic predicate over the AST, in src/rules/evaluator.rs of the open source engine (AGPL-3.0-only), at this page's commit:

"VERICTO-080" => find(stmts, |s| {
    s.kind == StatementKind::Copy && s.copy_is_program
})
.map(|s| {
    let mut v = violation(
        rule,
        s,
        Some("Use client-side \\copy or COPY … TO STDOUT, never PROGRAM".to_string()),
    );
    v.ast_node_path = "CopyStmt > PROGRAM".to_string();
    v
}),

Frequently asked questions