[PR #8337] feat(plugin): add field-level encryption plugin for sensitive data at rest #24808

Open
opened 2026-04-15 22:34:10 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/8337
Author: @Daymannovaes
Created: 3/3/2026
Status: 🔄 Open

Base: mainHead: feat/plugin-field-encryption


📝 Commits (2)

📊 Changes

3 files changed (+307 additions, -0 deletions)

View changed files

packages/better-auth/src/plugins/field-encryption/field-encryption.test.ts (+157 -0)
packages/better-auth/src/plugins/field-encryption/index.ts (+149 -0)
📝 packages/better-auth/src/plugins/index.ts (+1 -0)

📄 Description

Summary

Adds a fieldEncryption plugin that provides transparent encryption at rest for any schema field. Developers specify which fields on which models to encrypt — the plugin handles encrypt-on-write and decrypt-on-read via schema transforms.

Design decisions:

  • Reuses better-auth's existing symmetricEncrypt/symmetricDecrypt (XChaCha20-Poly1305)
  • Supports key rotation via SecretConfig (versioned envelope format $ba$<version>$<ciphertext>)
  • Falls back to the auth instance's secretConfig if no explicit key is provided
  • Unencrypted values pass through transparently, enabling gradual migration of existing data

Usage:

import { fieldEncryption } from "better-auth/plugins";

const auth = betterAuth({
  plugins: [
    fieldEncryption({
      fields: {
        user: ["phoneNumber", "ssn"],
      },
    }),
  ],
});

Summary by cubic

Adds a fieldEncryption plugin that encrypts selected schema fields at rest and decrypts them on read. Uses XChaCha20-Poly1305 with versioned envelopes and supports key rotation to make securing PII straightforward.

  • New Features
    • Configure fields per model; encrypt-on-write and decrypt-on-read via schema transforms.
    • Reuses symmetricEncrypt/symmetricDecrypt with versioned envelopes ($ba$<version>$...) for key rotation via SecretConfig.
    • Falls back to the auth instance secret; passes through null/empty and already-encrypted values; safely returns original on decryption errors for gradual migration.
    • Adds tests (create/update, multi-field, special chars), exports the plugin from plugins/index, and fixes linting (no functional changes).

Written for commit 2b871b3723. Summary will update on new commits.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/better-auth/better-auth/pull/8337 **Author:** [@Daymannovaes](https://github.com/Daymannovaes) **Created:** 3/3/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `feat/plugin-field-encryption` --- ### 📝 Commits (2) - [`2ffbb68`](https://github.com/better-auth/better-auth/commit/2ffbb6862ee545de1056c98624ea66dae74ae768) feat: new plugin for field encryption - [`2b871b3`](https://github.com/better-auth/better-auth/commit/2b871b3723f7fc8e6e2352b0d53488c57cf292ae) fix linting ### 📊 Changes **3 files changed** (+307 additions, -0 deletions) <details> <summary>View changed files</summary> ➕ `packages/better-auth/src/plugins/field-encryption/field-encryption.test.ts` (+157 -0) ➕ `packages/better-auth/src/plugins/field-encryption/index.ts` (+149 -0) 📝 `packages/better-auth/src/plugins/index.ts` (+1 -0) </details> ### 📄 Description ## Summary Adds a `fieldEncryption` plugin that provides transparent encryption at rest for any schema field. Developers specify which fields on which models to encrypt — the plugin handles encrypt-on-write and decrypt-on-read via schema transforms. **Design decisions:** - Reuses better-auth's existing `symmetricEncrypt`/`symmetricDecrypt` (XChaCha20-Poly1305) - Supports key rotation via `SecretConfig` (versioned envelope format `$ba$<version>$<ciphertext>`) - Falls back to the auth instance's `secretConfig` if no explicit key is provided - Unencrypted values pass through transparently, enabling gradual migration of existing data **Usage:** ```typescript import { fieldEncryption } from "better-auth/plugins"; const auth = betterAuth({ plugins: [ fieldEncryption({ fields: { user: ["phoneNumber", "ssn"], }, }), ], }); ``` <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Adds a `fieldEncryption` plugin that encrypts selected schema fields at rest and decrypts them on read. Uses XChaCha20-Poly1305 with versioned envelopes and supports key rotation to make securing PII straightforward. - **New Features** - Configure fields per model; encrypt-on-write and decrypt-on-read via schema transforms. - Reuses `symmetricEncrypt`/`symmetricDecrypt` with versioned envelopes (`$ba$<version>$...`) for key rotation via `SecretConfig`. - Falls back to the auth instance secret; passes through null/empty and already-encrypted values; safely returns original on decryption errors for gradual migration. - Adds tests (create/update, multi-field, special chars), exports the plugin from `plugins/index`, and fixes linting (no functional changes). <sup>Written for commit 2b871b3723f7fc8e6e2352b0d53488c57cf292ae. Summary will update on new commits.</sup> <!-- End of auto-generated description by cubic. --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-04-15 22:34:10 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#24808