Read-only notebooks, and my first trust bug
How SQLly notebooks stay read-only and untrusted until you say so, and the forgeable trust marker that was caught and fixed before it shipped.
A notebook is a file full of SQL that someone else may have written. You'd be careful running a script a stranger emailed you. A notebook deserves the same care, plus a little more, because it looks friendly and it already knows which connections to use.
This last post in the series on SQLly notebooks is about the safety model: read-only cells, trust, and the checks that run every time something happens. It's also about a bug in my own first version of trust that would have let a hostile notebook skip the trust check entirely. It was caught before release, but I want to tell it straight.
A notebook from another computer: the trust banner, the trust dialog, Run All with read-only cells, the UPDATE held until Allow writes, and a shared notebook asking to map its connections. Watch the whole recording, with chapters.
Cells start read-only, and the database enforces it
A new SQL cell is Read-only. It runs on a copy of its connection with SQLly's read-only setting on, and on engines that have one, inside the database's own read-only session or transaction. A statement that tries to write fails in the database even if SQLly's own check missed it. Plan cells follow the same rule: the query they explain runs on the read-only copy, and only as a single statement.
SQLly's own check is there to stop the obvious cases early and explain why: "This cell is read-only and its SQL changes data. Choose Allow writes to run it." But I want to be clear about what it is. It classifies statements by their keywords for each engine, and a stored procedure call that writes can look like a read. The database's read-only session is the real guard. Connections marked read-only in SQLly stay read-only whatever a notebook says.
To let a cell write, click Allow writes in its header. SQLly asks "Let fix_freight change data?", names the connection, and on production adds "This is a production connection." When the cell runs, its statements still go through the change review with the connection's own rules: confirmation, refusing UPDATE or DELETE without WHERE, and the production lock. A cell that changes data runs alone on its connection, and a notebook with one can't be scheduled, viewed as a dashboard or presented. When local data needs that cell's rows, SQLly copies the result on screen and never runs the cell again behind your back.
Trust lives on your computer, never in the file
Opening a notebook never runs SQL and never connects to a database. A notebook you created in SQLly is trusted as you edit and save it. Anything else opens untrusted: a file from another computer, a Jupyter import, something a colleague sent, or a notebook whose SQL or connections changed outside SQLly after you trusted it. An untrusted notebook shows a banner and runs nothing. You can read every cell first.
Trust notebook opens a dialog listing every connection the notebook uses and which of your connections each one maps to. Untick any it shouldn't use, pick full trust or read-only trust, and only tick Allow cells that change data if it really needs to. A notebook made on another computer names connections that mean nothing here, so SQLly asks you to map each one to a connection of yours first. A connection you remap after trusting waits for your OK again, so pointing a notebook at production is always your decision.
The bug: a trust marker anyone could copy
Here's what the first version of trust did. When SQLly saved a notebook, it wrote a marker into the file: a salted hash of this machine's id. When you opened a notebook, if the marker matched this machine, it was "made here" and trusted. Otherwise, banner.
It passed every test it had. It was also wrong in two ways.
First, every notebook saved on your machine carried the same marker. So anyone who had one notebook you'd ever shared, from a repo, a ticket, an email attachment, had the marker. Copy it into a hostile notebook, send it to you, and it opens trusted. No banner. Its cells would run when you pressed Run, on any connection it named (it could copy your connection ids from the same shared file) or on whatever connection the tab was using. And because the file itself said which cells were read-only, it could ship a DELETE with writes already allowed. The only thing left between that and your data was the change review, with whatever confirmation, WHERE check and production lock that connection has.
Second, the marker said nothing about content. A trusted notebook whose SQL changed outside SQLly, after a git pull or a sync from a shared folder, stayed trusted, because the marker was still there. That's exactly the moment you'd want to look before running.
A security review of the notebook code caught both before any of it shipped. The trust code had been written and merged into the notebook work, but it was never in a release. I'm telling you anyway because "we have a trust banner" sounds like safety, and that version wasn't.
How trust works now
Nothing in the file says "trusted". SQLly no longer writes the marker, and ignores one if it finds it. Trust records live in trust.json in SQLly's notebook data folder, readable only by you, with up to eight trusted versions kept per notebook.
Each record is tied to a fingerprint of everything that can run: cell ids, names, kinds and order, every non-Markdown source, connection slots and what they map to, safety policies, cell settings, parameters and the run mode. Markdown text, the title and collapsed cells aren't part of it, so editing prose elsewhere keeps a notebook trusted. Change the SQL or a connection outside SQLly and it opens untrusted, with "This notebook's queries or connections changed outside SQLLY since you trusted it." A copy of a notebook file gets its own id the first time you save it, so the copy and the original never share trust, run history or local data.
The tests that used to lean on the marker now check the opposite: a copied marker grants nothing, SQL changed on disk opens untrusted and runs nothing, Markdown-only edits keep trust, and a remap needs a new grant even after a save and a reopen.
Trust alone doesn't let a cell run
Right before each cell runs, SQLly checks it again with the notebook's current trust and the connection's current facts, so a decision made earlier can't be replayed after something changed. An untrusted notebook runs nothing. A cell on a connection that isn't mapped is refused, and one on a connection that wasn't allowed, or was remapped since, waits for you. A read-only cell whose SQL writes is refused before the database sees it. A writing cell is refused under read-only trust and needs its own Allow writes otherwise.
Exports, copying results into local data, a diff that reads another connection, a plan cell's parameter sweep, sharing with a cloud AI model, schedules and presenting a dashboard go through the same kind of check. In the desktop app, every cell run, refusal, trust decision and export is written as one JSON line to a local audit log, with a hash of the SQL but not the SQL itself.
What a hostile file still might do, and what's not done
SQLly scans a notebook file before parsing it, both when the app opens one (from a file, recent files, a restored session or a recovery copy) and in sqlly-cli notebook run and validate. The scan refuses connection entries that contain passwords or connection strings, files over 32 MiB, nesting deeper than 64 levels, strings over 4 MiB, more than 5,000 cells, 500 parameters or 100 connections, and attachment paths that are absolute or climb out of the notebook's folder, and the app says why it refused. Smaller concerns show as a warning under the trust banner. Attachments also have a rule of their own when they run: a path outside the notebook's folder is never read until you pick that file on this computer, and one that leads out through a linked folder is refused.
Saved results are encrypted with XChaCha20-Poly1305 using a key in your system keychain, and filed under the connection, database, login and safety settings that fetched them, so another login never sees them. Results the CLI keeps for its runs go into the same encrypted store. An untrusted notebook shows no saved results at all. Deleting a saved result overwrites it with zeros first, which is best effort on SSDs and copy-on-write file systems. Trust records are protected by file permissions only. Anyone who can write to your user data folder can already change SQLly's settings, so I didn't add a signature on top. The audit log names your operating-system user, not a SQLly account. And an old .sqlnb.json notebook without a stored id opens untrusted every time until you save it once as .sqllynb.
All of it is on Security model and Connections and safety. If you find a way to get a notebook to run something you didn't agree to, please tell me before you tell anyone else, and I'll fix it first.