Skip to content

Encrypt stored data with the current key version and add a re-encrypt task - #15199

Merged
loks0n merged 4 commits into
mainfrom
claude/openssl-key-v2-encryption
Nov 2, 2026
Merged

loks0n merged 4 commits into
mainfrom
claude/openssl-key-v2-encryption

Conversation

@loks0n

@loks0n loks0n commented Nov 2, 2026

Copy link
Copy Markdown
Member

What

Stacked on #15198 (merge that first). After this PR, stored data is encrypted with the current key version, and a task re-encrypts data left under older versions. That lets V1 eventually be removed entirely instead of staying configured for decryption.

  • The encrypt filter writes with _APP_OPENSSL_KEY_CURRENT and stores the version. Decryption still picks the key by the stored version.
  • Storage uploads encrypt with the current key and record openSSLVersion.
  • New task bin/encryption re-encrypts every value using the encrypt filter that is stored under another version:
    • It covers the platform database, every project in _APP_REGION, and user-database columns created with encrypt: true.
    • It is a dry run unless --commit=true, reads in batches (--batch, default 100), and resumes with --after=<projectId>.
    • It reads each value raw to see its version, so a rerun only touches what is left.
    • It preserves $updatedAt.

Runbook (after #15198's step 2, current=2)

Per region:

  • docker compose exec appwrite encryption: dry run that prints counts per project and collection.
  • docker compose exec appwrite encryption --commit=true: writes. If it stops, rerun with --after=<last project ID reported done>.
  • Run the dry run again and expect Found 0 documents.

Storage files already encrypted on disk with openSSLVersion=1 are not rewritten. V1 must stay configured until those are re-encrypted too, which is a separate follow-up.

Tests

tests/unit/Database/EncryptFilterTest.php checks that:

  • the filter writes the current version
  • a V1 value decrypts after switching to V2
  • an update re-encrypts the value under V2
  • the value reads without V1 afterwards

Both tests fail against the original code, which always writes version 1. Not covered: the task against a real database.

🤖 Generated with Claude Code

…on task

The encrypt filter and storage uploads use _APP_OPENSSL_KEY_CURRENT and
record the version. bin/encryption re-encrypts stored values left under
an older version. It is a dry run unless --commit=true and resumes with
--after.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@luxor-code-review

luxor-code-review Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

Review complete. 🟡 3 medium

💬 Inline comments (3)

🧹 Nitpicks (1) — 🟢 1 low
  • 🟢 Encryption task injects a callable, against repo convention (Encryption.php:40) — The Encryption CLI task injects getProjectDB as a callable and takes it as a callable $getProjectDB parameter (src/Appwrite/Platform/Tasks/Encryption.php:40,48).

This PR introduces key-version-aware encryption: _APP_OPENSSL_KEY_V2 / _APP_OPENSSL_KEY_CURRENT / _APP_OPENSSL_KEY_OLDEST variables replace the single hard-coded v1 key, the database encrypt filter and storage file uploads record the key version used, and a new encryption CLI task (registered in Services/Tasks.php, exposed via bin/encryption and chmod'd in the Dockerfile) re-encrypts stale database values project by project with cursor pagination.

Files Change
Dockerfile, bin/encryption, app/config/variables.php Ship the encryption task binary, chmod it in the image, and document the new key-rotation env vars.
app/init/database/filters.php, .../Buckets/Files/Create.php Switch encrypt/decrypt to EncryptionKey::current()/get(version) and record openSSLVersion on file documents.
src/Appwrite/Platform/Tasks/Encryption.php, Services/Tasks.php New re-encryption CLI task with dry-run support, stale detection, and batched per-project updates.
tests/unit/Database/EncryptFilterTest.php Unit coverage for the updated encrypt/decrypt filter round-trip.

Reviewed commit: b44115c

@ghost

ghost commented Nov 2, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

The pull request switches encrypted database values and eligible storage uploads to the configured current key version while retaining version-based decryption. It adds a dry-run/commit task to re-encrypt older database values across platform, project, and user-defined collections, with batching, resume support, and preserved update dates.

Latest changes: The latest commit counts documents in commit mode only when the transaction successfully re-encrypts them, while retaining candidate counts for dry runs.

Verdict New comments Fixed Still open
✅ Approved 0 0 0
📂 Walkthrough · 8
File Change
Dockerfile Makes the encryption CLI entrypoint executable in the image.
app/config/variables.php Updates key-rotation guidance to include the re-encryption task.
app/init/database/filters.php Writes encrypted values with the configured current key version and fails if it is unavailable.
bin/encryption Adds the shell entrypoint for the encryption task.
src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Create.php Encrypts eligible uploads with the current key and records the key version.
src/Appwrite/Platform/Services/Tasks.php Registers the encryption CLI task.
src/Appwrite/Platform/Tasks/Encryption.php Scans eligible database values and transactionally re-encrypts values from older key versions; counts successful writes.
tests/unit/Database/EncryptFilterTest.php Covers current-version writes, decryption across versions, re-encryption, and missing-current-key behavior.

Reviewed the commits since 0f91715 · Details · Comment @hansi-codes review to re-run, or mention @hansi-codes with a question.

@github-actions

github-actions Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

Security rules

WARNING (2 new) — review signal only; does not fail the job.

php.appwrite.weak-secret-env-default (2)

Secret-like env var (SECRET, KEY, PASS, PASSWORD, TOKEN) read with a non-empty literal default. If the variable is unset the process silently runs with a shared, guessable value. Default to '' and fail or disable the feature when it is empty.

  • src/Appwrite/Auth/EncryptionKey.php:40 — EncryptionKey::version()
    • What's wrong: _APP_OPENSSL_KEY_CURRENT falls back to '1' when unset, so every install that does not set it shares that value.
    • How to fix: Read System::getEnv('_APP_OPENSSL_KEY_CURRENT', '') and fail or disable the dependent connection when it is empty; keep the example value in .env / app/config/variables.php.
  • src/Appwrite/Auth/EncryptionKey.php:64 — EncryptionKey::accepted()
    • What's wrong: _APP_OPENSSL_KEY_OLDEST falls back to '1' when unset, so every install that does not set it shares that value.
    • How to fix: Read System::getEnv('_APP_OPENSSL_KEY_OLDEST', '') and fail or disable the dependent connection when it is empty; keep the example value in .env / app/config/variables.php.
6 existing findings tracked in .semgrep/baseline.json
  • php.appwrite.guest-write-without-abuse-limit (3)
  • php.appwrite.secret-compare-timing (1)
  • php.appwrite.weak-secret-env-default (2)

Posted by Checks / Rules. Re-runs update this comment in place. Rule details and baseline: .semgrep/README.md.

ghost
ghost previously requested changes Nov 2, 2026

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Tier B · 1 blocking finding to address. Summary

Reading and writing in one transaction with the row locked keeps a client
update made in between from being overwritten with the value read.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions

github-actions Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → claude/openssl-key-v2-encryption (after).

Metric Before After Change
🚀 Requests/sec 213.89 217.91 ⚪ +1.9%
⏱️ Latency P50 79.63 ms 80.22 ms ⚪ +0.7%
⏱️ Latency P95 186.92 ms 186.44 ms ⚪ -0.3%
Per-scenario breakdown & investigation details

Metrics below reflect the current branch (after). Δ P95 compares against the base.

Scenario P50 (ms) P95 (ms) Requests RPS Δ P95 (ms)
API total 80.22 186.44 13,680 217.91 -0.48
Account 154.67 281.54 720 12.29 +15.36
TablesDB 76.97 142.89 7,440 122.25 -1.43
Storage 73.33 160.24 3,600 60.08 -17.78
Functions 117.22 228.88 1,920 32.68 +12.66

Top API waits (after)

API request Max wait (ms)
functions.create 422.77
account.name.update 409.81
functions.variables.update 374.24
account.prefs.update 361.45
functions.variables.create 302.66

@ghost
ghost dismissed their stale review November 2, 2026 15:24

The findings that requested changes are resolved. See the summary comment for what is still open.

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Tier A · Looks good to merge. Summary

@luxor-code-review luxor-code-review Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adds version-aware OpenSSL key rotation via a new EncryptionKey flow, an encryption CLI re-encryption task, and wiring across the Dockerfile, variables, database filters, and file upload encryption.

Key findings

Comment thread app/init/database/filters.php Resolved
…does not decrypt

The encrypt filter and storage uploads throw when the current version has
no key, instead of storing false. The encryption task treats only full
envelopes as ciphertext, and skips a value whose recorded key is gone
instead of rewriting it as an encryption of nothing.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@loks0n
loks0n changed the base branch from claude/openssl-key-v2-598956 to main November 2, 2026 15:37

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Tier A · See the inline comments. Summary

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Tier S · Looks good to merge. Summary

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.

1 participant