Skip to content

fix(documents): pass the encryption option through (fixes #1253) - #1257

Open
NiKrause wants to merge 1 commit into
orbitdb:mainfrom
NiKrause:fix/documents-encryption-option
Open

NiKrause wants to merge 1 commit into
orbitdb:mainfrom
NiKrause:fix/documents-encryption-option

Conversation

@NiKrause

Copy link
Copy Markdown
Contributor

Fixes #1253

Thanks to @georgemac510 for the report and the diagnosis in #1253.

Bug

Documents destructures its option as encrypt and forwards encrypt to Database, but orbitdb.open() passes it as encryption (src/orbitdb.js, open() and the Database(...) call). Events, KeyValue and KeyValueIndexed already use encryption. So orbitdb.open(name, { type: 'documents', encryption }) silently ignores the encryption config. Entries and payloads are stored and replicated in plaintext, and no error is raised.

Affected: @orbitdb/core 3.0.2 (per #1253), 4.0.0, and main at 2ee8387.

Fix

src/databases/documents.js: encrypt → encryption in the parameter list and in the Database(...) call. That is the only change to src/.

Test

In test/orbitdb-encryption.test.js there is a new block, Documents database is encrypted in storage. It mirrors the existing storage tests for the default (events) database:

  • payload bytes are encrypted in storage (encryption: { data })
  • entry bytes are encrypted in storage (encryption: { replication })

Each test puts a document that contains a unique plaintext marker. It then reads the raw entry bytes with db.log.storage.get(hash) and asserts that:

  • the marker does not appear in those bytes
  • the payload or entry is an opaque Uint8Array that does not CBOR-decode
  • the document still round-trips through db.get

Without the fix (on main at 2ee8387), both new tests fail at the first assertion because the marker is found in the stored bytes:

AssertionError [ERR_ASSERTION]: Expected values to be strictly equal:
true !== false
    at .../test/orbitdb-encryption.test.js:399:7
  // strictEqual(new TextDecoder().decode(bytes).includes(marker), false)

test/orbitdb-encryption.test.js: 7 passing, 2 failing.

With the fix:

  • test/orbitdb-encryption.test.js: 9 passing, 0 failing
  • full npm test (Node 22): 546 passing, 0 failing
  • npm run lint: clean

🤖 Generated with Claude Code

Documents destructured its option as `encrypt` and forwarded `encrypt` to
Database, while orbitdb.open() passes it as `encryption`. A documents
database opened with encryption therefore stored plaintext without error.

Adds storage tests for a documents database with data and with replication
encryption; both fail without the fix because the plaintext marker is
found in the raw entry bytes.

Fixes orbitdb#1253

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

@aphelionz aphelionz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, thanks for the fix and the tests. The rename matches what Events, KeyValue and KeyValueIndexed already do.

Non-blocking notes, fine as follow-ups:

  • Replication coverage: the new tests check local storage only. #1253 also reports plaintext replication, so a two-peer Documents test (like the Events suite at lines 65-301) and a wrong-key test that expects Could not decrypt payload would cover that path.
  • Test cleanup: afterEach does not reset db1. If open() throws, afterEach drops the previous suite's closed db, and that error replaces the real one. Setting db1 = undefined after close fixes it. The existing storage suite has the same pattern.
  • Pre-existing doc drift: the Database JSDoc in src/database.js (lines 42-43) still documents options.encryptFn / options.decryptFn. The real option is params.encryption with the { replication, data } shape from src/orbitdb.js.

const Documents = ({ indexBy } = DefaultOptions) => async ({ ipfs, identity, address, name, access, directory, meta, headsStorage, entryStorage, indexStorage, referencesCount, syncAutomatically, onUpdate, encrypt }) => {
const database = await Database({ ipfs, identity, address, name, access, directory, meta, headsStorage, entryStorage, indexStorage, referencesCount, syncAutomatically, encrypt })
const Documents = ({ indexBy } = DefaultOptions) => async ({ ipfs, identity, address, name, access, directory, meta, headsStorage, entryStorage, indexStorage, referencesCount, syncAutomatically, onUpdate, encryption }) => {
const database = await Database({ ipfs, identity, address, name, access, directory, meta, headsStorage, entryStorage, indexStorage, referencesCount, syncAutomatically, encryption })

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Optional, since you're on this line: onUpdate is destructured on line 28 but not passed to Database. Events and KeyValue forward it. Adding onUpdate here would let a wrapper (like KeyValueIndexed does for KeyValue) build an index on top of Documents. This bug is pre-existing and does not block this PR.

@aphelionz

Copy link
Copy Markdown
Member

Thanks for this. One more thing on the same line: Documents receives onUpdate and never passes it to Database(...), so a Documents onUpdate callback never runs. KeyValue and Events forward it. Could you add it here too?

Also worth a test: a Documents db written in plaintext before this fix, then reopened with encryption, may fail to decode its old entries. The release notes should say what users can do with existing data.

This branch has not been deployed

No deployments
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.

Documents store silently drops the encryption option (typo: encrypt vs encryption)

2 participants