Skip to content

Update clients to interact with (new) server encrypted databases#2113

Merged
penberg merged 5 commits intotursodatabase:mainfrom
avinassh:encryption-client
Jul 4, 2025
Merged

Update clients to interact with (new) server encrypted databases#2113
penberg merged 5 commits intotursodatabase:mainfrom
avinassh:encryption-client

Conversation

@avinassh
Copy link
Copy Markdown
Member

@avinassh avinassh commented Jun 30, 2025

This patch enables client to send encryption key header for encrypted databases. The header we are required to send is x-turso-encryption-key.

To connect with encrypted database, either remote or offline write, you may use the builder pattern to provide the encryption context. Ex.:

let encryption = if let Ok(key) = std::env::var("LIBSQL_ENCRYPTION_KEY") {
        Some(EncryptionContext {
            key: EncryptionKey::Base64Encoded(key),
        })
    } else {
        None
    };
let db_builder =
        Builder::new_synced_database(db_path, sync_url, auth_token).remote_encryption(encryption);

This patch also comes with a full example: https://github.com/avinassh/libsql/blob/5b95a1928b821855fced3880caf7871ce032c58b/libsql/examples/encryption_sync.rs

@avinassh avinassh marked this pull request as draft June 30, 2025 09:46
@avinassh avinassh force-pushed the encryption-client branch 7 times, most recently from 1271b0c to 3d73ecf Compare July 4, 2025 15:03
@avinassh avinassh force-pushed the encryption-client branch 6 times, most recently from 1d319a3 to e6a1bdc Compare July 4, 2025 16:47
@avinassh avinassh changed the title wip: add encryption support on client side Update clients to interact with (new) server encrypted databases Jul 4, 2025
@avinassh avinassh marked this pull request as ready for review July 4, 2025 16:48
@avinassh avinassh requested review from Copilot, levydsa and penberg July 4, 2025 16:52
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR enables clients to provide an optional encryption context so that the x-turso-encryption-key header is sent on all sync/remote database requests.

  • Introduce EncryptionContext and EncryptionKey types and export them in the public API
  • Extend SyncContext, HTTP sender, and builders to accept and propagate the encryption context
  • Update tests and examples to pass the new remote_encryption parameter

Reviewed Changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
libsql/src/sync/test.rs Updated test calls to include the new remote_encryption parameter
libsql/src/sync.rs Added remote_encryption field and header injection in sync logic
libsql/src/local/database.rs Propagate remote_encryption when constructing SyncContext
libsql/src/lib.rs Export EncryptionContext and EncryptionKey types
libsql/src/hrana/hyper.rs Add remote_encryption to HTTP sender and include header in requests
libsql/src/database/builder.rs Set default remote_encryption and add builders for encryption
libsql/src/database.rs Define EncryptionKey/EncryptionContext and update DB variants
libsql/examples/encryption_sync.rs New example demonstrating encrypted sync usage
libsql/Cargo.toml Added base64 dependency for encryption encoding
Comments suppressed due to low confidence (2)

libsql/src/database/builder.rs:309

  • [nitpick] The remote_encryption setter on Builder<RemoteReplica> takes EncryptionContext where other builders accept Option<EncryptionContext>. Consider unifying these signatures for consistency across builder APIs.
        pub fn remote_encryption(mut self, encryption_context: EncryptionContext) -> Builder<RemoteReplica> {

libsql/src/lib.rs:135

  • EncryptionContext and EncryptionKey are only exported under the sync feature, but they’re also needed for remote-only scenarios. Consider exporting them under remote (and/or replication) features so they’re available when sync is disabled.
    pub use database::EncryptionContext;

Comment thread libsql/src/sync.rs
Comment on lines +310 to 315
if let Some(remote_encryption) = &self.remote_encryption {
req = req.header("x-turso-encryption-key", remote_encryption.key.as_string());
}

let req = req.body(body.clone().into()).expect("valid body");

Copy link

Copilot AI Jul 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The code for adding the x-turso-encryption-key header is duplicated across multiple methods in SyncContext. Consider extracting this into a helper method to reduce duplication and improve maintainability.

Suggested change
if let Some(remote_encryption) = &self.remote_encryption {
req = req.header("x-turso-encryption-key", remote_encryption.key.as_string());
}
let req = req.body(body.clone().into()).expect("valid body");
self.add_encryption_header(&mut req);

Copilot uses AI. Check for mistakes.
@avinassh avinassh force-pushed the encryption-client branch from e6a1bdc to 3c42428 Compare July 4, 2025 17:03
@penberg penberg added this pull request to the merge queue Jul 4, 2025
Merged via the queue into tursodatabase:main with commit a2e9dd4 Jul 4, 2025
19 checks passed
@avinassh avinassh deleted the encryption-client branch July 4, 2025 17:51
@avinassh avinassh mentioned this pull request Jul 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants