Field Policies & Application-Level Encryption (ALE)
BeechCMS provides field-level security policies and zero-knowledge encryption primitives directly integrated into the Botanical Engine and Cloudflare D1.
Granular Branch Policies
Every Branch can define an optional policies configuration object controlling privacy, security classification, indexing, and API visibility:
policies?: {
classification?: 'public' | 'internal' | 'confidential' | 'restricted'
privacy?: 'plain' | 'hash' | 'encrypt'
visibility?: 'full' | 'masked' | 'hidden'
public?: boolean
publicEdit?: boolean
filter?: boolean
sort?: boolean
search?: boolean
}Policy Properties Breakdown
| Property | Type | Default | Description |
|---|---|---|---|
classification | enum | 'public' | Security classification level governing export restrictions, masking, and auditing. |
privacy | enum | 'plain' | Physical storage transformation: 'plain', 'hash' (one-way HMAC-SHA256), or 'encrypt' (AES-256-GCM). |
visibility | enum | 'full' | How the value is rendered in API responses: 'full', 'masked' (renders as ••••••••), or 'hidden' (completely omitted). |
public | boolean | true | When false, excludes the field completely from unauthenticated public REST endpoints. |
publicEdit | boolean | false | Controls whether unauthenticated public mutations can modify this specific branch. |
filter | boolean | false | Generates a dedicated B-tree index (idx_{seed}_{alias}) in D1 for fast SQL WHERE queries. |
sort | boolean | false | When true, enables SQL ORDER BY operations on this column. |
search | boolean | false | Includes this field in SQLite FTS5 full-text indexing and search ranking. |
NOTE
Custom branches do not support a unique policy in D1 (only the system slug carries a UNIQUE constraint). For full public read isolation, combine public: false with classification: 'internal' or classification: 'confidential'. Note that classification: 'confidential' automatically implies storage 'encrypt'.
Application-Level Encryption (ALE)
When privacy: 'encrypt' (or classification: 'confidential') is enabled on a branch, BeechCMS enforces AES-256-GCM authenticated encryption at rest before data touches SQLite D1:
- Format: Values are stored using the versioned envelope
v1:<iv_base64>:<ciphertext_base64>(Base64-encoded strings native to Cloudflare Workers with zero external dependencies). - Key Derivation: Symmetrically derives a 256-bit AES-GCM CryptoKey directly from
PRIVACY_MASTER_KEYviacrypto.subtle.digest('SHA-256', ...). See Key derivation and rotation (accepted limitation). - Zero-Knowledge at Edge: Database dumps, SQLite backups, and raw D1 queries reveal only encrypted ciphertext.
Blind Indexing for Encrypted Fields
A classic drawback of database encryption is the inability to search or filter encrypted columns using SQL B-tree indexes without decrypting every row in memory.
BeechCMS solves this via Blind Indexing:
- When a branch has both
privacy: 'encrypt'(orclassification: 'confidential') andfilter: true, the Botanical Engine automatically provisions a companion column:sqlALTER TABLE content_users ADD COLUMN email_bidx TEXT; CREATE INDEX idx_users_email_bidx ON content_users (email_bidx); - On every insert or update, BeechCMS computes an HMAC-SHA256 digest of the normalized input using a derived HMAC key:text
email_bidx = HMAC_SHA256(hmacKey, normalize(input))hmacKeyis the raw UTF-8 bytes ofPRIVACY_MASTER_KEY. The same HMAC also backsprivacy: 'hash'fields (see Confidential Data). - When querying
/api/content/users?email=test@example.com, BeechCMS computes the blind index hash of the search parameter and executes an indexed B-tree equality check againstemail_bidxinO(log N)time without decrypting any database rows.
Master Key Provisioning
Application-Level Encryption requires a 256-bit cryptographic master key (PRIVACY_MASTER_KEY).
Local Development
pnpm beech dev (monorepo), beech init (consumer projects) and npx @beechcms/cms@latest generate the key in .dev.vars when it is missing or empty. An existing value is never overwritten.
To generate it manually, create a 32-byte hex key and add it to .dev.vars:
# Generate key
node -e "console.log(require('crypto').randomBytes(32).toString('hex'))"Add to .dev.vars:
PRIVACY_MASTER_KEY=4a2f8c9b... # 64 hex charactersWarning: Do not change the key once protected data is stored. Existing ciphertext and blind indexes become unreadable. Without a key the API fails closed with
PRIVACY_MASTER_KEY is not configured.
Cloudflare Edge Production
Provision the key as a Cloudflare secret:
npx wrangler secret put PRIVACY_MASTER_KEYKey derivation and rotation (accepted limitation)
PrivacyService uses one root secret for two purposes, with two different derivations:
| Purpose | Derivation |
|---|---|
AES-256-GCM (encrypt) | SHA-256(PRIVACY_MASTER_KEY) |
HMAC-SHA256 (hash, blind indexes) | raw UTF-8 bytes of PRIVACY_MASTER_KEY |
There is no HKDF step and no per-purpose context string. This is a deliberate trade-off, not an oversight:
- No known attack. The AES key is a one-way digest of the master key, so it does not reveal the HMAC key. Anyone holding the master key already holds both, with or without HKDF.
- HKDF is not free.
hash()output is stored without a version tag (privacy: 'hash'values and*_bidxcolumns). Changing the HMAC key would invalidate every stored hash, and they cannot be recomputed because the plaintext is not stored. It would require a D1 data migration plus a versioned scheme (for example av2:ciphertext prefix and dual verification of hashes). - Decision. Not changed until BeechCMS gets key rotation. Adding HKDF belongs in that work: versioned envelope, dual-verify on read, lazy upgrade on write.
Operational consequence: the master key cannot be rotated today. Treat PRIVACY_MASTER_KEY as permanent, store it in a secret manager, and back it up. See the warning above about changing it.
Data Classification Tiers
BeechCMS categorizes data into 4 governance tiers:
public: Non-sensitive marketing content, blog posts, and public assets. Full visibility across public APIs.internal: Internal business operational data. Available to authenticated CMS users; stripped from public endpoints whenpublic: false.confidential: Personal Identifiable Information (PII) such as phone numbers, billing addresses, or employee data. Defaults tovisibility: 'masked'(••••••••) on read endpoints unless accessed by authorized roles.restricted: Highly sensitive credentials, API keys, or verification secrets. Always paired withprivacy: 'hash'orprivacy: 'encrypt'andvisibility: 'hidden'.
Example Branch Configuration
branches: [
{
id: 'br_01',
alias: 'phone',
label: 'Customer Phone',
type: 'text',
policies: {
classification: 'confidential',
privacy: 'encrypt',
visibility: 'masked',
public: false,
filter: true // Enables phone_bidx blind index
}
},
{
id: 'br_02',
alias: 'api_secret',
label: 'Webhook Secret',
type: 'text',
policies: {
classification: 'restricted',
privacy: 'hash',
visibility: 'hidden',
public: false
}
}
]