npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

agent-sql-firewall

v0.1.3

Published

AST-based SQL firewall middleware that blocks destructive queries from LLM agents before they reach Postgres.

Readme

agent-sql-firewall

CI npm license

An open-source (MIT) SQL firewall middleware for Node.js and Express. It sits between an AI agent (LangChain, an MCP server, your own code) and Postgres, parses every query, and blocks destructive ones before they reach the database.

It blocks things like: DELETE or UPDATE without a bounded WHERE, DROP, TRUNCATE, DO blocks, stacked statements (SELECT 1; DROP TABLE ...), writes hidden inside CTEs, side-effect functions (pg_sleep, dblink, lo_import), and access to system catalogs. It fails closed: anything it cannot understand is blocked.

Install

npm install agent-sql-firewall

Quick start

import express from 'express';
import { sqlFirewall } from 'agent-sql-firewall';

const app = express();
app.use(express.json());

// db is your Postgres client, for example a pg Pool.
// Start in monitor mode: it logs what it WOULD block, but blocks nothing.
app.post('/agent/query', sqlFirewall({ mode: 'monitor' }), async (req, res) => {
  const result = await db.query(req.body.query);
  res.json(result.rows);
});

When you are happy with the logs, switch to enforcing (the default):

sqlFirewall({ readOnly: true, allowedTables: ['orders', 'customers'] })

A blocked request gets HTTP 403 with a request ID and rule codes. The audit log contains a SHA-256 hash of the query, never the SQL text or string literals.

Options

| Option | Meaning | |---|---| | mode | 'enforce' (default) or 'monitor' | | readOnly | Block every INSERT, UPDATE and DELETE | | allowedTables | Only these tables may be touched | | uniqueKeyColumns | Columns considered safe boundaries for writes (default: ['id']) | | requireTenantFilter, expectedTenantId, tenantColumns | Require a tenant filter in WHERE | | maxTokens | Reject larger queries (default 600) | | onAudit | Your own audit sink (default: JSON line on stdout) | | exposeViolations | 'codes' (default) or 'none' in the 403 body | | extractQuery | Read the SQL from somewhere other than req.body.query |

What it is not

This is one safety layer. It is not a replacement for a least-privilege database role, row-level security, or network controls. It is not SOC2 certified and has not had an independent security audit.

Performance

Measured on an Intel i5-13420H, Node v25.9.0, single thread, cache disabled (npm run bench reproduces this).

| Case | p50 | p99 | |---|---|---| | Short legitimate queries (~10-25 tokens) | 0.31 ms | 1.3 ms | | Attacks rejected by the scanner | 0.004 ms | 3.6 ms |

Cost grows roughly linearly with query size (about 0.03 ms per token), so a 100-token query takes about 3 ms. Queries over 600 tokens are rejected by default. The worst valid-but-heavy input we measured took about 25 ms at p99. Checks run synchronously on the event loop, so keep maxTokens modest.

Known limitations

  • Legitimate writes filtered by non-key columns are blocked (for example DELETE ... WHERE token = $1). By default only the id column is considered a safe boundary. Configure this with uniqueKeyColumns (for example uniqueKeyColumns: ['id', 'uuid', 'token']). List only columns that are truly unique in your schema: a non-unique column lets one statement change many rows.
  • Tenant checks look only at the outer WHERE clause, not joins or subqueries. Use Postgres row-level security for real tenant isolation.
  • Dollar-quoted strings ($$text$$) are not supported by the parser and are blocked.
  • Identifiers starting with pg_, txid_ or dblink are blocked, even as column names.
  • Queries over 600 tokens are rejected by default (maxTokens).
  • Tested with Express 4. Express 5 is expected to work but is not covered by the tests.
  • Postgres dialect only. Unusual syntax the parser cannot read is blocked.
  • A test that runs allowed queries against a real Postgres and counts changed rows is included (npm run test:pg), but it is not yet part of CI and its results are not published.

Testing

npm test runs over 230 tests: a corpus of 100+ attack vectors and legitimate queries, obfuscation mutations (case, whitespace, comments, Unicode tricks), 30,000 random and grammar-generated inputs checked by an independent oracle, worst-case input timing, and middleware tests. Coverage is about 91%.

Security

Found a bypass? Please open an issue or a private security advisory on GitHub.

License

MIT