Skip to content

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:

typescript
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 ​

PropertyTypeDefaultDescription
classificationenum'public'Security classification level governing export restrictions, masking, and auditing.
privacyenum'plain'Physical storage transformation: 'plain', 'hash' (one-way HMAC-SHA256), or 'encrypt' (AES-256-GCM).
visibilityenum'full'How the value is rendered in API responses: 'full', 'masked' (renders as ••••••••), or 'hidden' (completely omitted).
publicbooleantrueWhen false, excludes the field completely from unauthenticated public REST endpoints.
publicEditbooleanfalseControls whether unauthenticated public mutations can modify this specific branch.
filterbooleanfalseGenerates a dedicated B-tree index (idx_{seed}_{alias}) in D1 for fast SQL WHERE queries.
sortbooleanfalseWhen true, enables SQL ORDER BY operations on this column.
searchbooleanfalseIncludes 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:

Application-Level Encryption and Blind Indexing Architecture

  • 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_KEY via crypto.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:

  1. When a branch has both privacy: 'encrypt' (or classification: 'confidential') and filter: true, the Botanical Engine automatically provisions a companion column:
    sql
    ALTER TABLE content_users ADD COLUMN email_bidx TEXT;
    CREATE INDEX idx_users_email_bidx ON content_users (email_bidx);
  2. 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))
    hmacKey is the raw UTF-8 bytes of PRIVACY_MASTER_KEY. The same HMAC also backs privacy: 'hash' fields (see Confidential Data).
  3. 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 against email_bidx in O(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:

bash
# Generate key
node -e "console.log(require('crypto').randomBytes(32).toString('hex'))"

Add to .dev.vars:

bash
PRIVACY_MASTER_KEY=4a2f8c9b... # 64 hex characters

Warning: 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:

bash
npx wrangler secret put PRIVACY_MASTER_KEY

Key derivation and rotation (accepted limitation) ​

PrivacyService uses one root secret for two purposes, with two different derivations:

PurposeDerivation
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 *_bidx columns). 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 a v2: 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:

  1. public: Non-sensitive marketing content, blog posts, and public assets. Full visibility across public APIs.
  2. internal: Internal business operational data. Available to authenticated CMS users; stripped from public endpoints when public: false.
  3. confidential: Personal Identifiable Information (PII) such as phone numbers, billing addresses, or employee data. Defaults to visibility: 'masked' (••••••••) on read endpoints unless accessed by authorized roles.
  4. restricted: Highly sensitive credentials, API keys, or verification secrets. Always paired with privacy: 'hash' or privacy: 'encrypt' and visibility: 'hidden'.

Example Branch Configuration ​

typescript
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
    }
  }
]