Skip to content

security: Tor delivery, deleted-content scrubbing, DNS rebinding pin - #155

Merged
JordanCoin merged 5 commits into
mainfrom
claude/open-prs-merge-review-e6z0ep
Nov 19, 2026
Merged

JordanCoin merged 5 commits into
mainfrom
claude/open-prs-merge-review-e6z0ep

Conversation

@JordanCoin

Copy link
Copy Markdown
Owner

This PR closes three privacy gaps that docs/THREAT_MODEL.md used to list as known limitations.

1. request send respects Tor mode

Before: in Tor mode, sending by email (SMTP), Twilio fax or Lob mail warned that it would go direct, then sent anyway. A non-interactive run sent with no warning at all.

Now:

  • SMTP goes through Tor. net.open_tor_stream is a small SOCKS5 client written with the standard library only.
    • The hostname is sent to Tor as a SOCKS DOMAINNAME, so it is resolved at the exit relay and never looked up locally.
    • It uses stream-isolation credentials, so each send gets its own circuit.
    • It fails closed: if Tor is unreachable or refuses, nothing connects directly.
    • _TorSMTP gives that socket to smtplib. STARTTLS still checks the certificate against the SMTP host.
  • Twilio fax and Lob mail use their REST APIs through egress_client (gateways/http.py). Their SDKs made their own connections, outside the egress policy.
    • POST requests are not retried.
    • In Tor mode without socksio, sending is refused rather than sent direct.
  • SendGrid is refused in Tor mode.
  • request send checks the Tor proxy before sending and exits if it is down.
    • --direct sends one request without Tor, with a warning; --tor forces Tor.
  • The fax and mail extras no longer need the twilio or lob SDKs.

2. Deleted text no longer lingers in the database files

Before: deleted text survived in three places:

  • free pages in the database file;
  • the rollback journal, which SQLite unlinked after each transaction, leaving the text in the filesystem's free space;
  • old FTS5 index segments, with word positions.

Now:

  • Every connection sets secure_delete=ON, journal_mode=PERSIST and temp_store=MEMORY. That covers get_engine, the plaintext Alembic engine and SQLCipher.
  • The -journal file is zeroed after each session, and after migrations and seeding. It is overwritten, fsynced and truncated under an EXCLUSIVE lock.
  • The FTS5 index is optimized when page text is deleted or replaced. A per-connection TEMP trigger notices the change; optimizing merges the old segments so they are freed and zeroed.
  • Cost, measured on ext4:
    • ingest is about 15% slower (200 pages: 190 → 220 ms; 10k pages: 9.2 → 10.6 s);
    • deleting a 20-page document takes 9 → 25 ms in a 200-page database, and 11 → 291 ms in a 10k-page one.
  • What this doesn't reach: the THREAT_MODEL says it plainly — SSD wear-levelling, filesystem snapshots and backups.

3. Downloads are pinned against DNS rebinding

Before: check_download_target resolved the hostname and required public addresses, but the connection then resolved the name again. A rebinding DNS server could answer the check with a public address and the connection with 127.0.0.1.

Now: in direct mode, download_to_file and DocumentCloud's asset text fetch run under net.public_addresses_only().

  • The name is resolved once, every address must be public, and the socket is opened to an address from that same answer.
  • TLS SNI, certificate checks and the Host header still use the hostname.
  • Tor mode is unchanged, since the exit relay resolves the name.

Docs

  • THREAT_MODEL:
    • The "request send over Tor" row now lists what still identifies the user: the login, the API key and the name on the request.
    • Known Limitations 2 and 6 are rewritten.
    • New limitation 9 covers download pinning.
  • CHANGELOG: new ### Security entries.
  • GUIDE: delivery section updated.
  • README: privacy note updated.

Not verified

  • Twilio and Lob REST calls are built from their API docs. They have not been tried against the live services.
  • Many SMTP providers block Tor exit nodes. The docs say so, and --direct is the explicit escape hatch.

Checks

  • Gate: ruff is clean and the suite gives 1509 passed.
    • That count was with socksio installed locally. In CI the SOCKS-dependent tests skip without it.
  • New test files: test_security_sqlite_remnants.py, plus Tor delivery and rebinding tests.

Generated by Claude Code

check_download_target resolved a download's hostname and required public
addresses, but the connection resolved again, so a rebinding DNS server could
answer a public address to the check and 127.0.0.1 to the connection.

In DIRECT mode download_to_file (and DocumentCloud's asset text fetch) now
run under net.public_addresses_only(): egress_client's own transport routes
those requests to a separate pool whose network backend resolves the name,
requires every address to be public, and opens the socket to an address from
that same answer. TLS SNI, certificate verification and Host still use the
hostname. Tor mode is unchanged (the exit resolves the name).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016LaGpHLuSRHzGARb38X7YS
Deleted rows stayed in free pages, a delete copied the deleted text into the
rollback journal, and SQLite unlinked that journal after every transaction,
leaving it in the filesystem's free space. FTS5 kept a deleted page's words
(with positions) in older index segments.

Every connection the app opens (get_engine, the plaintext alembic engine,
SQLCipher) now sets secure_delete=ON, journal_mode=PERSIST and
temp_store=MEMORY. After each get_session (and after migrations and seeding)
the persistent -journal file is overwritten with zeros, fsynced and truncated
under an EXCLUSIVE lock, and when page text was deleted or replaced (seen by
a per-connection TEMP trigger) the FTS5 index is merged ('optimize') so the
old segments are freed and zeroed.

Measured on ext4: ingest +15% (200 pages 190->220 ms; 10k pages 9.2->10.6 s),
deleting a 20-page document 9->25 ms at 200 pages, 11->291 ms at 10k pages.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016LaGpHLuSRHzGARb38X7YS
request send warned in Tor mode that email (SMTP), Twilio fax and Lob mail
all went direct, then sent anyway; a non-interactive run sent silently.

- SMTP goes through the Tor SOCKS proxy: net.open_tor_stream is a small
  stdlib SOCKS5 client (hostname sent as DOMAINNAME, resolved at the exit;
  stream-isolation credentials) and _TorSMTP uses it as smtplib's socket.
  STARTTLS still verifies the certificate against the SMTP host.
- Twilio Fax and Lob Letters are called over their REST APIs through
  egress_client (gateways/http.py) instead of their SDKs, which make their
  own connections outside the egress policy. POST is not retried. Tor mode
  without socksio refuses (TorUnavailableError), never goes direct.
- SendGrid is refused in Tor mode.
- request send checks the Tor proxy before sending and exits if it is down.
  --direct sends one request without Tor (warned); --tor forces Tor.
- twilio and lob are no longer needed by the fax and mail extras.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016LaGpHLuSRHzGARb38X7YS
…pinning

THREAT_MODEL: request send over Tor and what still identifies the user
(login, API key, the name on the request); Known Limitations 2 and 6
rewritten to say what the persistent-journal scrub, secure_delete and the
full-text merge do and do not reach; new limitation 9 on download pinning.
CHANGELOG Security entries, GUIDE delivery section, README privacy note.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016LaGpHLuSRHzGARb38X7YS
…ends 'rcpt TO:')

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016LaGpHLuSRHzGARb38X7YS
@JordanCoin
JordanCoin merged commit 5a109a5 into main Nov 19, 2026
9 checks passed
@JordanCoin JordanCoin mentioned this pull request Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants