skip to content

In MongoDB, what does the server see for a field under CSFLE deterministic, CSFLE random and Queryable Encryption, and what can each still query?

level: middleimportance: must knowfreq 35%

answer

  1. who holds the keys
  2. same input, same output?
  3. BinData subtype 6 on disk
  4. tags in __safeContent__, enxcol_ metadata

basics

~20 s

The server stores only BinData ciphertext and never holds a key. Deterministic CSFLE allows equality because equal values encrypt identically; random CSFLE allows no value queries; Queryable Encryption stays randomized yet answers configured queries through metadata.

solid answer

~40 s

In all three, the driver encrypts before sending, so `mongod` stores `BinData` subtype 6 and holds no key. With CSFLE's `AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic`, a given value always produces the same ciphertext, so the server can match bytes for `$eq`, `$ne`, `$in` and `$nin`, but repeated values are visible and low-cardinality fields risk frequency analysis. With `-Random`, every encryption differs, so automatic encryption rejects value queries and only `$exists` behaves normally. Queryable Encryption always produces different ciphertext, and adds a `__safeContent__` array to each document plus the `enxcol_.<collection>.esc` and `.ecoc` metadata collections, so the server can answer the query types configured per field. None of this stops someone holding the keys or with arbitrary write access to the collection.

code

javascript · 16 lines
javascript
// schemaMap for automatic CSFLE; the UUIDs are placeholders
const schemaMap = {
  "payments.customers": {
    bsonType: "object",
    properties: {
      cardToken: { encrypt: {
        keyId: [UUID("bffb361b-30d3-42c0-b7a4-d24a272b72e3")],
        algorithm: "AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic",
        bsonType: "string" } },
      notes: { encrypt: {
        keyId: [UUID("f3821212-e697-4d65-b740-4a6791697c6d")],
        algorithm: "AEAD_AES_256_CBC_HMAC_SHA_512-Random",
        bsonType: "string" } }
    }
  }
}

go deeper

for a junior

Remember that the client encrypts and the server never gets a key. Be able to say that deterministic means same input gives same output and random means it never does.

for a middle

Explain why equal ciphertext allows equality, why that same property leaks frequency, and which structures Queryable Encryption adds so it can stay randomized and still match.

for a senior

Choose an algorithm per field from its cardinality and its read pattern. Point out the fields where a plaintext proxy or a deterministic choice quietly gives back what encryption was meant to hide.

for a principal

State the threat you are buying against. These schemes address exfiltration and a superuser's direct reads, not a persistent insider with snapshots and query logs, and the design should name which one the business fears.

## What every scheme has in common **In-use encryption** means the client encrypts a field before it crosses the network, and only the client can decrypt it. MongoDB offers two forms: **Client-Side Field Level Encryption (CSFLE)** and **Queryable Encryption (QE)**. A deployment can use both, but not on the same collection. Whichever you choose, `mongod` stores the field as `BinData` subtype 6 and never receives a key. The manual lists the same core protections for both: - a database superuser reading the field directly - someone reading the server's memory - capture of the field over an insecure network - someone reading database or backup files on disk So a DBA with `root` sees binary blobs in those fields, while unencrypted fields in the same document stay readable. What differs is how much the server can still do with those blobs. ## CSFLE deterministic The deterministic algorithm, `AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic`, passes a deterministic initialization vector, so a given input always encrypts to the **same** output. Equality therefore works by comparing bytes, and the server never needs the plaintext. - A driver using automatic encryption allows `$eq`, `$ne`, `$in`, `$nin`, `$and`, `$or`, `$not` and `$nor` on the field. - `$group` can group on a deterministic field. - The `bsonType` must be one type, and it cannot be `double`, `decimal128`, `bool`, `object` or `array`. The price is that equal values look equal. Anyone holding the data can count how often each ciphertext appears, which the manual calls **frequency analysis recovery**, and it is most damaging on low-cardinality fields such as a status or a blood type. A card token, which is nearly unique, suffers far less than a date of birth. ## CSFLE random The random algorithm, `AEAD_AES_256_CBC_HMAC_SHA_512-Random`, encrypts a given input to a **different** output every time. It gives CSFLE's strongest confidentiality and removes the server's ability to evaluate any value predicate. - Automatic encryption returns an error for value operators on the field. - `$exists` still behaves normally, because it needs no value. - It is the only CSFLE algorithm that can encrypt a whole `object` or `array`. The manual's own workaround is a separate plaintext field that approximates the secret, such as the last four digits of an identifier. That field is then readable, so it has to be a deliberate decision. ## Queryable Encryption QE is **always non-deterministic**: the same plaintext never produces the same ciphertext, yet the field can be queried. It achieves this with structures the server maintains but cannot read. 1. Each document gets a `__safeContent__` array of binary tags. 2. Each encrypted collection gets two metadata collections, `enxcol_.<collection>.esc` and `enxcol_.<collection>.ecoc`. 3. For a query, the driver sends tokens derived from the value and data key. The server uses them with the ESC to work out the matching tags, then rewrites the predicate into a `$in` over `__safeContent__`. The server finds matching documents without learning the value, and returns them still encrypted. The fields must be declared up front with their query types: equality, range, prefix, suffix or substring. QE also omits its operations from the slow query log and `system.profile`, and redacts `currentOp` and `collStats`, so operators see less about those queries too. ## The comparison | | CSFLE deterministic | CSFLE random | Queryable Encryption | |---|---|---|---| | Same input, same ciphertext | Yes | No | No | | Queries on the value | Equality family | None | As configured per field | | Extra server structures | None | None | `__safeContent__`, ESC, ECOC | | On exfiltration, the manual's table reveals | Frequency of values, operation metadata | Frequency of values, operation metadata | Size of database, operation metadata | The manual's threat table has one column for CSFLE as a whole, so the frequency entry describes the scheme's exposure rather than something a purely random field leaks by itself. ## What this does not do Neither scheme offers guarantees against an adversary who holds the customer master key and data keys, or against arbitrary write access to the encrypted collections. QE's warning is explicit: it defends against **data exfiltration**, not against someone with persistent access, or someone who can retrieve both database snapshots and query transcripts or logs. A person with long-running access to a server can therefore learn more than a single stolen backup would show. Encryption also does not replace roles, TLS or encryption at rest; the manual presents all of them as layers used together.

  • Why not use deterministic encryption on every CSFLE field, since it keeps equality?
    Because it exposes which documents share a value, and low-cardinality fields then yield to frequency analysis. It also cannot hold `double`, `decimal128`, `bool`, `object` or `array`. The manual's rule is to use deterministic only for fields read by value, and random for sensitive fields that are not.
  • If Queryable Encryption ciphertext always differs, how can the server answer an equality query?
    It never compares the stored ciphertext. The driver derives tokens from the value and the data key, the server uses them with the ESC metadata collection to work out matching tags, then rewrites the predicate as a `$in` over each document's `__safeContent__` array.
  • What does a MongoDB client with no encryption options get from these fields?
    It sees the unencrypted fields normally and the encrypted ones as `BinData` subtype 6. It cannot query them by value, because it sends plaintext and nothing stored is plaintext. The manual states that only appropriately configured clients can read and write the encrypted fields.

  • Deterministic encryption is safe on any field because nobody can read the ciphertext.
  • A randomly encrypted CSFLE field can be filtered by value once automatic encryption is on.
  • The server decrypts fields in memory while it evaluates a query.
  • Queryable Encryption stores equal values as equal ciphertext, which is how equality matching works.
  • Client-side encryption protects data from anyone who has write access to the collection.