[PR #23574] [CLOSED] feat: add chat encryption at rest (v2) #50307

Closed
opened 2026-04-30 02:57:17 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/open-webui/open-webui/pull/23574
Author: @kraack-tech
Created: 4/10/2026
Status: Closed

Base: devHead: feat/chat-encryption-fixes-v2


📝 Commits (3)

  • 1a8f7d5 feat: add chat encryption at rest (v2)
  • 7aea7fa fix: preserve string semantics and guard pin toggle null path
  • 9bae142 fix: safe enqueue and deterministic backfill of old-chat encryption + handle invalid keys

📊 Changes

7 files changed (+506 additions, -60 deletions)

View changed files

📝 backend/open_webui/env.py (+16 -0)
📝 backend/open_webui/models/chat_messages.py (+53 -10)
📝 backend/open_webui/models/chats.py (+50 -27)
📝 backend/open_webui/routers/auths.py (+30 -1)
📝 backend/open_webui/routers/chats.py (+34 -22)
backend/open_webui/utils/db/chat_encryption.py (+136 -0)
backend/open_webui/utils/db/encrypt_old_chats.py (+187 -0)

📄 Description

Pull Request Checklist

  • Target branch: Verify that the pull request targets the dev branch. PRs targeting main will be immediately closed.
  • Description: Provide a concise description of the changes made in this pull request down below.
  • Changelog: Ensure a changelog entry following the format of Keep a Changelog is added at the bottom of the PR description.
  • Documentation: Add docs in Open WebUI Docs Repository. Document user-facing behavior, environment variables, public APIs/interfaces, or deployment steps.
  • Dependencies: Are there any new or upgraded dependencies? If so, explain why, update the changelog/docs, and include any compatibility notes. Actually run the code/function that uses updated library to ensure it doesn't crash.
  • Testing: Perform manual tests to verify the implemented fix/feature works as intended AND does not break any other functionality. Include reproducible steps to demonstrate the issue before the fix. Test edge cases (URL encoding, HTML entities, types). Take this as an opportunity to make screenshots of the feature/fix and include them in the PR description.
  • Agentic AI Code: Confirm this Pull Request is not written by any AI Agent or has at least gone through additional human review AND manual testing. If any AI Agent is the co-author of this PR, it may lead to immediate closure of the PR.
  • Code review: Have you performed a self-review of your code, addressing any coding standard issues and ensuring adherence to the project's coding standards?
  • Design & Architecture: Prefer smart defaults over adding new settings; use local state for ephemeral UI logic. Open a Discussion for major architectural or UX changes.
  • Git Hygiene: Keep PRs atomic (one logical change). Clean up commits and rebase on dev to ensure no unrelated commits (e.g. from main) are included. Push updates to the existing PR branch instead of closing and reopening.
  • Title Prefix: To clearly categorize this pull request, prefix the pull request title using one of the following:
    • BREAKING CHANGE: Significant changes that may affect compatibility
    • build: Changes that affect the build system or external dependencies
    • ci: Changes to our continuous integration processes or workflows
    • chore: Refactor, cleanup, or other non-functional code changes
    • docs: Documentation update or addition
    • feat: Introduces a new feature or enhancement to the codebase
    • fix: Bug fix or error correction
    • i18n: Internationalization or localization changes
    • perf: Performance improvement
    • refactor: Code restructuring for better maintainability, readability, or scalability
    • style: Changes that do not affect the meaning of the code (white space, formatting, missing semi-colons, etc.)
    • test: Adding missing tests or correcting existing tests
    • WIP: Work in progress, a temporary label for incomplete or ongoing work

Changelog Entry

Description

Adds application-level encryption of chat content at rest, controlled by two environment variables:

  • DATABASE_CHAT_ENCRYPTION_KEY: When set, chat content is encrypted before being written to the database (chat/chat_message tables) and and transparently decrypted on read. The UI experience is unchanged.
  • DATABASE_CHAT_ENCRYPT_OLD_CHATS : When true, schedules per-user background encryption of existing plaintext chats at sign-in, avoiding bulk-operation container timeouts. Once all historical chats are encrypted the flag can be disabled.

This is a database-agnostic solution that specifically addresses deployments using PostgreSQL (e.g. on Azure or AWS) where the existing SQLCipher approach is not applicable, as confirmed in Discussion #21469.

Outcome: Admin users can no longer view user chat content in plaintext via database exports or direct database access. Both features are optional and entirely controlled with the environment variables.

Changes from v1 (prior closed PR)

  • Rebased
  • Environment variables naming aligned with database naming scheme
  • tested thoroughly (unit + manual UI flows)
  • Applies encryption for the chat_message table
  • Tested against reported issues (automated code review) + tests for the chat_message handling
  • Unit tests not included

Added

  • DATABASE_CHAT_ENCRYPTION_KEY env var: enables encryption of chat content at rest. Encryption is idempotent: plaintext content is detected and encrypted; already-encrypted content is left untouched.
  • DATABASE_CHAT_ENCRYPT_OLD_CHATS env var: when true, triggers per-user background encryption of historical plaintext chats on sign-in.
  • backend/open_webui/utils/db/chat_encryption.py: centralised encrypt/decrypt helper functions.
  • backend/open_webui/utils/db/encrypt_old_chats.py: batch encryption utility function for historical chats.

Changed

  • backend/open_webui/env.py: Expose DATABASE_CHAT_ENCRYPTION_KEY and DATABASE_CHAT_ENCRYPT_OLD_CHATS configuration.
  • backend/open_webui/main.py: Initialise encryption configuration at startup.
  • backend/open_webui/models/chats.py: Encrypt chat content on write; decrypt (normalise) on read so the UI always receives plaintext.
  • backend/open_webui/routers/chats.py: Normalise chat payloads before returning to callers.
  • backend/open_webui/routers/chat_messages.py: Normalise chat_messages payloads before returning to callers.
  • backend/open_webui/routers/auths.py: Trigger old-chat encryption task on user sign-in when DATABASE_CHAT_ENCRYPT_OLD_CHATS=true.

Deprecated

  • None

Removed

  • None

Fixed

  • None

Security

  • Chat content stored in the database is now encrypted at rest when environment variables are configured. This prevents direct database access or database exports from exposing user chat content in plaintext, including admin-level accounts.

Breaking Changes

  • None

Additional Information

  • Supersedes prior closed PR

Screenshots or Videos

ow_pg

Contributor License Agreement


🔄 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/open-webui/open-webui/pull/23574 **Author:** [@kraack-tech](https://github.com/kraack-tech) **Created:** 4/10/2026 **Status:** ❌ Closed **Base:** `dev` ← **Head:** `feat/chat-encryption-fixes-v2` --- ### 📝 Commits (3) - [`1a8f7d5`](https://github.com/open-webui/open-webui/commit/1a8f7d5a3c3f94953e29c8e6e084f27dd8235572) feat: add chat encryption at rest (v2) - [`7aea7fa`](https://github.com/open-webui/open-webui/commit/7aea7fa66b461e320b2a2a3f59fda62765e8239c) fix: preserve string semantics and guard pin toggle null path - [`9bae142`](https://github.com/open-webui/open-webui/commit/9bae142c1b5cfdfeec4ef832f8ce329ba4a217aa) fix: safe enqueue and deterministic backfill of old-chat encryption + handle invalid keys ### 📊 Changes **7 files changed** (+506 additions, -60 deletions) <details> <summary>View changed files</summary> 📝 `backend/open_webui/env.py` (+16 -0) 📝 `backend/open_webui/models/chat_messages.py` (+53 -10) 📝 `backend/open_webui/models/chats.py` (+50 -27) 📝 `backend/open_webui/routers/auths.py` (+30 -1) 📝 `backend/open_webui/routers/chats.py` (+34 -22) ➕ `backend/open_webui/utils/db/chat_encryption.py` (+136 -0) ➕ `backend/open_webui/utils/db/encrypt_old_chats.py` (+187 -0) </details> ### 📄 Description # Pull Request Checklist - [x] **Target branch:** Verify that the pull request targets the `dev` branch. **PRs targeting `main` will be immediately closed.** - [x] **Description:** Provide a concise description of the changes made in this pull request down below. - [x] **Changelog:** Ensure a changelog entry following the format of [Keep a Changelog](https://keepachangelog.com/) is added at the bottom of the PR description. - [ ] **Documentation:** Add docs in [Open WebUI Docs Repository](https://github.com/open-webui/docs). Document user-facing behavior, environment variables, public APIs/interfaces, or deployment steps. - [x] **Dependencies:** Are there any new or upgraded dependencies? If so, explain why, update the changelog/docs, and include any compatibility notes. Actually run the code/function that uses updated library to ensure it doesn't crash. - [x] **Testing:** Perform manual tests to **verify the implemented fix/feature works as intended AND does not break any other functionality**. Include reproducible steps to demonstrate the issue before the fix. Test edge cases (URL encoding, HTML entities, types). Take this as an opportunity to **make screenshots of the feature/fix and include them in the PR description**. - [x] **Agentic AI Code:** Confirm this Pull Request is **not written by any AI Agent** or has at least **gone through additional human review AND manual testing**. If any AI Agent is the co-author of this PR, it may lead to immediate closure of the PR. - [x] **Code review:** Have you performed a self-review of your code, addressing any coding standard issues and ensuring adherence to the project's coding standards? - [x] **Design & Architecture:** Prefer smart defaults over adding new settings; use local state for ephemeral UI logic. Open a Discussion for major architectural or UX changes. - [x] **Git Hygiene:** Keep PRs atomic (one logical change). Clean up commits and rebase on `dev` to ensure no unrelated commits (e.g. from `main`) are included. Push updates to the existing PR branch instead of closing and reopening. - [x] **Title Prefix:** To clearly categorize this pull request, prefix the pull request title using one of the following: - **BREAKING CHANGE**: Significant changes that may affect compatibility - **build**: Changes that affect the build system or external dependencies - **ci**: Changes to our continuous integration processes or workflows - **chore**: Refactor, cleanup, or other non-functional code changes - **docs**: Documentation update or addition - **feat**: Introduces a new feature or enhancement to the codebase - **fix**: Bug fix or error correction - **i18n**: Internationalization or localization changes - **perf**: Performance improvement - **refactor**: Code restructuring for better maintainability, readability, or scalability - **style**: Changes that do not affect the meaning of the code (white space, formatting, missing semi-colons, etc.) - **test**: Adding missing tests or correcting existing tests - **WIP**: Work in progress, a temporary label for incomplete or ongoing work # Changelog Entry ### Description Adds application-level encryption of chat content at rest, controlled by two environment variables: - `DATABASE_CHAT_ENCRYPTION_KEY`: When set, chat content is encrypted before being written to the database (chat/chat_message tables) and and transparently decrypted on read. The UI experience is unchanged. - `DATABASE_CHAT_ENCRYPT_OLD_CHATS` : When `true`, schedules per-user background encryption of existing plaintext chats at sign-in, avoiding bulk-operation container timeouts. Once all historical chats are encrypted the flag can be disabled. This is a database-agnostic solution that specifically addresses deployments using PostgreSQL (e.g. on Azure or AWS) where the existing SQLCipher approach is not applicable, as confirmed in [Discussion #21469](https://github.com/open-webui/open-webui/discussions/21469). **Outcome:** Admin users can no longer view user chat content in plaintext via database exports or direct database access. Both features are optional and entirely controlled with the environment variables. **Changes from v1 (prior closed [PR](https://github.com/open-webui/open-webui/pull/23548))** - Rebased - Environment variables naming aligned with database naming scheme - tested thoroughly (unit + manual UI flows) - Applies encryption for the chat_message table - Tested against reported issues (automated code review) + tests for the chat_message handling - Unit tests not included ### Added - `DATABASE_CHAT_ENCRYPTION_KEY` env var: enables encryption of chat content at rest. Encryption is idempotent: plaintext content is detected and encrypted; already-encrypted content is left untouched. - `DATABASE_CHAT_ENCRYPT_OLD_CHATS` env var: when `true`, triggers per-user background encryption of historical plaintext chats on sign-in. - `backend/open_webui/utils/db/chat_encryption.py`: centralised encrypt/decrypt helper functions. - `backend/open_webui/utils/db/encrypt_old_chats.py`: batch encryption utility function for historical chats. ### Changed - `backend/open_webui/env.py`: Expose `DATABASE_CHAT_ENCRYPTION_KEY` and `DATABASE_CHAT_ENCRYPT_OLD_CHATS` configuration. - `backend/open_webui/main.py`: Initialise encryption configuration at startup. - `backend/open_webui/models/chats.py`: Encrypt chat content on write; decrypt (normalise) on read so the UI always receives plaintext. - `backend/open_webui/routers/chats.py`: Normalise chat payloads before returning to callers. - `backend/open_webui/routers/chat_messages.py`: Normalise chat_messages payloads before returning to callers. - `backend/open_webui/routers/auths.py`: Trigger old-chat encryption task on user sign-in when `DATABASE_CHAT_ENCRYPT_OLD_CHATS=true`. ### Deprecated - None ### Removed - None ### Fixed - None ### Security - Chat content stored in the database is now encrypted at rest when environment variables are configured. This prevents direct database access or database exports from exposing user chat content in plaintext, including admin-level accounts. ### Breaking Changes - None --- ### Additional Information - Supersedes prior closed [PR](https://github.com/open-webui/open-webui/pull/23548) ### Screenshots or Videos <img width="2535" height="958" alt="ow_pg" src="https://github.com/user-attachments/assets/e82b074d-5fce-4f89-8dbc-517a0c9d80aa" /> ### Contributor License Agreement <!-- 🚨 DO NOT DELETE THE TEXT BELOW 🚨 Keep the "Contributor License Agreement" confirmation text intact. Deleting it will trigger the CLA-Bot to INVALIDATE your PR. Your PR will NOT be reviewed or merged until you check the box below confirming that you have read and agree to the terms of the CLA. --> - [x] By submitting this pull request, I confirm that I have read and fully agree to the [Contributor License Agreement (CLA)](https://github.com/open-webui/open-webui/blob/main/CONTRIBUTOR_LICENSE_AGREEMENT), and I am providing my contributions under its terms. --- <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-30 02:57:17 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/open-webui#50307