Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

Neon 6 min read How-to mediumAdept

Why SQL template tags are not vulnerable to SQL injection attacks

SQL template tags like Neon's `sql` function look like string interpolation but are safe from SQL injection because they are JavaScript tagged template functions. The tag receives the literal string fragments and the interpolated values separately, rebuilds the query with `$1`, `$2` placeholders, and passes the values as a parameter array to a parameterized query. The same pattern appears in Kysely and Postgres.js. A plain template literal passed to `client.query` remains vulnerable.

Related posts

  • Oops-Proof Your Vibe Code With Neon: Because Mistakes Happen

    Neon mediumAdept

    Neon's built-in safety features act as guardrails for databases behind AI-generated ("vibe coded") apps: point-in-time restore, snapshots, protected branches, IP allowlisting, mandatory SSL/TLS (sslmode=require) and password resets, each with brief console steps. The most concrete guidance covers SQL injection, contrasting string-interpolated queries with parameterized `$1` placeholders in pg. It also shows that Neon's serverless driver tagged-template `sql` function treats interpolated values as data. The practical advice is to check AI-generated db code for unparameterized queries and take a snapshot before risky schema changes.

  • @neondatabase/serverless hits 1.0.0

    Neon mediumAdept

    Neon's JavaScript/TypeScript serverless driver, which carries Postgres queries over HTTP and WebSockets for environments without raw TCP such as V8 isolates and browsers, reaches 1.0.0 after a refactor that adds auto-generated types via api-extractor and CI tests across Node, Bun, Deno, browsers, Cloudflare Workers, Vercel and the Prisma/Drizzle adapters. Tagged-template queries are now composable, because placeholder numbering is deferred until execution. A breaking change makes calling sql(...) as a plain function an error, since that form used ordinary string interpolation and invited SQL injection. New sql.query and sql.unsafe helpers cover the parameterized and trusted-raw-SQL cases. Binary inserts over HTTP now hex-encode roughly 10x faster, though WebSockets remain the recommended path for large blobs.

  • Is “SQL from the frontend” viable?

    Neon easyNovice

    Letting the frontend query the database directly, via tools like PostgREST or Firebase, can sharply speed up development by removing backend abstraction layers and frontend/backend team dependencies, which makes it attractive for prototypes and many apps. It does not have to replace the backend, which still holds secrets and sensitive logic. Exposing a database safely would require query-shape and parameter allow-listing with tooling to maintain the lists, per-IP rate limiting, and database-level authorization such as Postgres Row-Level Security. The verdict is that the approach is not production-ready yet, and Neon is exploring it through its RLS work, now part of the Neon Data API.

  • pg_graphql v1.0

    Supabase mediumAdept

    pg_graphql, a PostgreSQL extension that compiles GraphQL requests into a single SQL query, reaches 1.0 with a SemVer backwards-compatibility guarantee. Running inside Postgres instead of as a separate server keeps its memory footprint negligible on 1 GB Free Plan instances. That design also gives it Postgres roles and RLS for security, an always-in-sync schema with no N+1 queries, and ACID rollback across multi-mutation requests. The original pure-SQL transpiler hit limits near 4,000 lines because SQL functions pass by value and the planner stops inlining deeply nested calls, so it was rewritten in Rust with pgx. After the rewrite, overhead is under 300µs per request, about 1,060 queries/sec per connection from SQL, and about 645 req/s through the full HTTP and auth stack on Free Plan hardware.

  • Bringing psql’s \d to your web browser

    Neon mediumAdept

    Neon's web SQL Editor now supports psql backslash introspection commands like \d, \l, \dx and \sf, which live in psql's C client code rather than the Postgres server. Instead of compiling psql to WASM with emscripten, the C was translated to JavaScript via RegExp search-and-replace, reimplementations of helpers like sprintf, and a test script comparing output against real psql until every command matched; most fixes involved pointer arithmetic. Because backslash commands must be handled separately, SQL is now split client-side with a small open-source lexer using sticky RegExps that understands quoted identifiers, nested comments, escape and dollar-quoted strings, so only significant semicolons terminate statements.

The quest for software engineering knowledge.

Look:
Layout: