DEV Community

Muhammad Ahmad
Muhammad Ahmad

Posted on

Inspecting SQLite 100-Byte Headers, WAL Logs, and Deleted Freelist Rows Locally in the Browser

If you have ever needed to inspect a .sqlite or .db file on a locked-down work laptop, you know the usual trade-off: either install desktop software like DB Browser for SQLite, or upload a potentially sensitive database to a random online converter and hope the server actually deletes it.

Most browser-based SQLite viewers try to solve this by fetching a precompiled .wasm binary from a third-party CDN—which works fine on home Wi-Fi, then silently breaks under a strict corporate Content-Security-Policy (CSP). Even when they do load, almost all of them are strictly read-only, ignore companion -wal files, and miss unvacuumed deleted data hiding in freelist pages.

While building the SQLite & DuckDB Viewer, Editor, ERD & Forensic Studio on KivaroTools, I ended up going down the rabbit hole of SQLite 3's binary file format and building a zero-CDN TypeScript B-tree parser and serializer that runs 100% in browser memory. Here are the four SQLite internals and edge cases that catch engineers off guard—and how to inspect and fix them locally.

1. The 100-Byte Header Tells You More Than the File Extension

A .db, .sqlite, and .sqlite3 file are identical under the hood. Every valid SQLite 3 binary starts with a 100-byte header on Page 1 (bytes 0..99). Reading just five offsets tells you immediately whether a file is valid, whether it uses WAL mode, and whether deleted records are still sitting inside it:

Offset (Size) Header Field Values Why It Matters
0..15 (16 B) Magic String "SQLite format 3\0" Confirms a real SQLite 3 file (rules out Access, dBase .dbf, or SQLCipher-encrypted .crypt files).
16..17 (2 B) Page Size 512–65536 B Modern SQLite defaults to 4096 bytes per B-tree page.
18..19 (2 B) Write/Read Version 1 = Rollback, 2 = WAL 2 means recent writes may live in a companion .db-wal file, not the main .db.
32..39 (8 B) Freelist Pages Two 32-bit uints A non-zero count means unvacuumed deleted pages still exist on disk.
56..59 (4 B) Text Encoding 1 = UTF-8, 2/3 = UTF-16 Controls how TEXT payloads are decoded from B-tree leaf cells.

If a .db file refuses to open in any SQLite tool, checking bytes 0..15 immediately tells you whether it is actually SQLite or a different binary format.

2. Missing Recent Rows from Uncheckpointed .db-wal Files

If your app runs in Write-Ahead Logging (WAL) mode (header bytes 18..19 equal 2), copying app.db off a container or server without checkpointing often leaves your most recent INSERT and UPDATE transactions behind in app.db-wal.

Before grabbing a snapshot from production or staging, flush the WAL log into the main database file:


PRAGMA wal_checkpoint(TRUNCATE);

Enter fullscreen mode Exit fullscreen mode

If you already pulled both files separately, you need a viewer that can attach the companion .db-wal alongside the main .db file—otherwise you are looking at stale state.

3. Deleted Rows Survive DELETE FROM Until You Run VACUUM

One of the easiest privacy mistakes when sharing a "cleaned" SQLite database for debugging or support is running DELETE FROM users WHERE ...; and sending the .sqlite file over Slack or email.

SQLite does not zero out deleted bytes on DELETE. It marks those B-tree pages as free and adds them to the freelist (tracked at header bytes 32..39). The raw strings—emails, tokens, wire details—stay readable inside the binary file until you explicitly rebuild it:


VACUUM;

Enter fullscreen mode Exit fullscreen mode

In our forensic test fixture (forensic_places_and_sms.sqlite), two deleted wire rows sit right inside freelist space until carved or wiped. If you share .sqlite dumps with teammates or clients, always inspect freelist pages and scrub PII before exporting.

4. Unindexed Foreign Keys and 17-Digit Browser/iOS Timestamps

Two other headaches pop up constantly when auditing real-world SQLite databases:

  • Unindexed Foreign Keys: SQLite automatically indexes PRIMARY KEY columns, not FOREIGN KEY columns. As soon as a table grows, JOIN queries and cascading deletes fall back to full table scans. Running PRAGMA foreign_key_check; catches orphaned child rows, and adding explicit indexes on every *_id foreign key fixes the query plan.

  • Opaque Epoch Timestamps: If you inspect Chromium History (WebKit 1601 microsecond timestamps) or Apple iOS sms.db / chat.db (CoreData 2001 nanosecond timestamps), raw 17-digit integers are unreadable without manual epoch math. Decoding them inline into UTC timestamps saves a lot of scratchpad SQL.

Putting It Together in a Browser-Local Studio

I wanted a single local workspace where I could drop a .sqlite, .db, .sqlite3, .db-wal, .duckdb, or .sql file without uploading anything to a server—and actually edit and save binary .sqlite files rather than hitting a read-only wall.

In the KivaroTools SQLite & DuckDB Viewer, Editor, ERD & Forensic Studio, everything runs locally in browser memory across five tabs:

  • SQL & DuckDB Analytical Runner + Inline Editor: Edit cells inline, add rows or tables, run standard SQLite queries plus DuckDB analytical syntax (SUMMARIZE orders;, SELECT * EXCLUDE (metadata_json) FROM orders WHERE status = 'PAID';, GROUP BY ALL), and automatically decode Chrome 1601 / Apple 2001 timestamps.

  • Visual ER Diagrams & ORM Schemas: Generate interactive Vector .svg, Mermaid, and DBML ER diagrams (mapping both declared FOREIGN KEY constraints and implicit *_id relations) and compile directly to Drizzle ORM (schema.ts), Prisma (schema.prisma), or PostgreSQL migrations:


import { sqliteTable, integer, text } from 'drizzle-orm/sqlite-core';



export const users = sqliteTable('users', {

  id: integer('id').primaryKey({ autoIncrement: true }),

  organization_id: integer('organization_id')

    .notNull()

    .references(() => organizations.id),

  full_name: text('full_name').notNull(),

  plan_tier: text('plan_tier').notNull(),

});

Enter fullscreen mode Exit fullscreen mode
  • 12-Point Linter & Auto-Optimizer: Flags unindexed foreign keys, orphaned rows, and schema issues with a 1-click auto-fix.

  • 100-Byte Header, WAL & Freelist Forensics: Inspects Page 1 binary headers, attaches .db-wal logs, carves deleted freelist records, and runs 1-click PII anonymization and residue wiping.

  • 11-Format Export & Side-by-Side Diff (sqldiff): Save modified binary .sqlite files (passing PRAGMA integrity_check), export CWE-1236 formula-safe Excel (.xlsx), RFC 4180 CSV, SQL dumps, or diff two SQLite databases locally to produce ALTER TABLE migration patches.

Honest Scope & Privacy Boundaries

Your database files stay inside local browser memory and are never uploaded to KivaroTools servers. Because processing happens in client RAM, files run smoothly up to roughly 100–250 MB. For multi-gigabyte databases, heavy batch ETL, or SQLCipher / WhatsApp .crypt encrypted files, desktop DB Browser for SQLite is still the right tool. (And if you need columnar .parquet, .avro, or .feather files instead of SQLite, we keep those in a separate Parquet, Avro & Feather Viewer.)

The Stack

Built with Next.js (App Router), React, TypeScript, and Tailwind CSS, deployed on Cloudflare Workers with a self-contained zero-CDN TypeScript B-tree parser so strict corporate firewalls and CSP rules do not break local file loading.

If you want to test it without grabbing a local .db file, there are three built-in sample databases (SaaS Prod, Forensic DB, and Chrome / iOS) you can load in one click. Curious what SQLite edge cases or export formats you run into most often in production—let me know in the comments.

Top comments (2)

Collapse
 
userxnom profile image
Abdullah Naseer •

Section 3 is the one I would put first in the post, Muhammad — it is the only section where the mistake is invisible. DELETE FROM, then send the file over Slack, and the emails and tokens are still readable in the freelist because SQLite never zeroes the bytes. Your forensic fixture makes that concrete: two deleted wire rows sitting in freelist space until carved or wiped.

The question your own tool raises is the verification step. If someone runs the 1-click PII anonymization on the visible rows, does it also wipe the freelist pages, or does the original text stay readable underneath the masked rows? The cheap check I would add before anyone shares a file is mechanical: run VACUUM, then re-read header bytes 32-39 and confirm the freelist count is zero before the file leaves the laptop. Is that the check your linter runs, or does it only flag that freelist pages exist?

Collapse
 
muhammadahmadsandhu01 profile image
Muhammad Ahmad •

Great point on leading with Section 3—the freelist leak really is the one that catches teams off guard because SELECT looks completely clean while the raw bytes are still sitting right there in the file.

To answer both of your questions directly:

  1. The 1-click Anonymize PII & Wipe Residue action in Tab 4 doesn't just mask visible rows in place—it rebuilds/vacuums the binary so deleted freelist residue is wiped when you export the new .sqlite file. (Because we serialize a fresh B-tree from the sanitized in-memory tables, the old unallocated pages aren't carried over.)
  2. Right now, the linter flags non-zero freelist pages from header bytes 32–39 and surfaces carved records in Tab 4, and once you run the wipe/rebuild you can verify bytes 32–39 drop to zero in the header inspector. Making that post-export byte-32..39 == 0 assertion an explicit pass/fail badge in the export cert is actually a really clean mechanical verification step—definitely worth adding.

Thanks for reading closely and testing the edge cases!