Skip to content

feat: add encryption key for React Native - #11736

Merged
gioboa merged 6 commits into
typeorm:masterfrom
HtSpChakradharCholleti:patch-1
Jan 18, 2026
Merged

gioboa merged 6 commits into
typeorm:masterfrom
HtSpChakradharCholleti:patch-1

Conversation

@HtSpChakradharCholleti

@HtSpChakradharCholleti HtSpChakradharCholleti commented Oct 23, 2025 •

Copy link
Copy Markdown
Contributor

Close #11737

Description of change

Pull-Request Checklist

  • [ Yes] Code is up-to-date with the master branch
  • [ Yes] This pull request links relevant issues as Fixes #11737
  • There are new or updated unit tests validating the change
  • Documentation has been updated to reflect this change

Summary by CodeRabbit

  • New Features
    • React Native database connections now support optional encryption keys. Users can configure an encryption key through connection options to enable encrypted data storage for supported databases, enhancing data security.

@coderabbitai

coderabbitai Bot commented Oct 23, 2025 •

Copy link
Copy Markdown
Contributor

Walkthrough

Introduces optional encryptionKey parameter to React Native connection options. The property is added to the interface definition and conditionally propagated to the database open call when provided.

Changes

Cohort / File(s) Summary
React Native encryption key support
src/driver/react-native/ReactNativeConnectionOptions.ts, src/driver/react-native/ReactNativeDriver.ts
Added optional encryptionKey?: string property to connection options interface. Modified driver to conditionally pass encryption key to sqlite.openDatabase when supplied.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

🐰 A key to encrypt, so safe and so sound,
Through React Native paths, our secrets are bound!
With just a small prop, encryption takes flight,
The database whispers: secure through the night! ✨

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Title Check ✅ Passed The pull request title "feat:add encryption key for React Native" directly and clearly describes the main changes in the changeset. The PR adds an optional encryptionKey property to the ReactNativeConnectionOptions interface and implements the logic to propagate this key to the database connection in ReactNativeDriver.ts. The title accurately reflects this primary change, is concise and specific, and provides sufficient clarity for developers scanning the commit history to understand that the PR adds encryption key support for React Native database connections.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
src/driver/react-native/ReactNativeDriver.ts (1)

924-926: Implementation correctly forwards the encryption key.

The conditional propagation of encryptionKey is implemented correctly and won't affect existing usage.

Consider adding validation to ensure the encryption key is a non-empty string:

 if (this.options.encryptionKey) {
+    if (typeof this.options.encryptionKey !== 'string' || this.options.encryptionKey.trim() === '') {
+        throw new Error('encryptionKey must be a non-empty string')
+    }
     options.encryptionKey = this.options.encryptionKey;
 }

This would provide clearer feedback if an invalid key is provided, though the underlying library may also perform its own validation.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d7867eb and 76949bc.

📒 Files selected for processing (2)
  • src/driver/react-native/ReactNativeConnectionOptions.ts (1 hunks)
  • src/driver/react-native/ReactNativeDriver.ts (1 hunks)
🔇 Additional comments (1)
src/driver/react-native/ReactNativeConnectionOptions.ts (1)

29-32: Document encryption library requirements or remove untested code.

The encryptionKey property is syntactically correct, but the implementation is incomplete. While the code passes encryptionKey to openDatabase() (ReactNativeDriver.ts, lines 924-925), the standard react-native-sqlite-storage library does not natively support this parameter.

No tests or documentation exist explaining which library fork/version is required for encryption support. Either clarify which library variant supports this feature (e.g., react-native-sqlcipher-storage) with appropriate documentation and tests, or remove this untested code.

@pkg-pr-new

pkg-pr-new Bot commented Oct 25, 2025 •

Copy link
Copy Markdown
  • typeorm-sql-js-example

    npm i https://pkg.pr.new/typeorm/typeorm@11736
    

commit: 2ce80ee

@coveralls

coveralls commented Oct 25, 2025 •

Copy link
Copy Markdown

Coverage Status

coverage: 80.93% (-0.005%) from 80.935%
when pulling 2ce80ee on HtSpChakradharCholleti:patch-1
into a0d190d on typeorm:master.

@qodo-free-for-open-source-projects

qodo-free-for-open-source-projects Bot commented Jan 7, 2026 •

Copy link
Copy Markdown

PR Code Suggestions ✨

Latest suggestions up to 2ce80ee

CategorySuggestion                                                                                                                                    Impact
General
Conditionally include encryption key

Conditionally include the encryptionKey in the connection options only when it
is defined to avoid passing undefined to the database library.

src/driver/react-native/ReactNativeDriver.ts [920-928]

 const options = Object.assign(
     {},
     {
         name: this.options.database,
         location: this.options.location,
-        encryptionKey: this.options.encryptionKey,
+        ...(this.options.encryptionKey && { encryptionKey: this.options.encryptionKey }),
     },
     this.options.extra || {},
 )

[To ensure code accuracy, apply this suggestion manually]

Suggestion importance[1-10]: 6

__

Why: The suggestion improves robustness by ensuring the encryptionKey property is only added to the options object if it has a value, preventing potential issues with the underlying library handling an undefined key.

Low
  • More

Previous suggestions

Suggestions up to commit 391dea4
CategorySuggestion                                                                                                                                    Impact
General
Conditionally include encryption key property

Conditionally add the encryptionKey to the connection options object only when
it has a value to prevent potential issues with the underlying library.

src/driver/react-native/ReactNativeDriver.ts [920-928]

 const options = Object.assign(
     {},
     {
         name: this.options.database,
         location: this.options.location,
-        encryptionKey: this.options.encryptionKey,
+        ...(this.options.encryptionKey && { encryptionKey: this.options.encryptionKey }),
     },
     this.options.extra || {},
 )
Suggestion importance[1-10]: 6

__

Why: The suggestion correctly identifies a potential issue where passing encryptionKey: undefined could cause problems with the underlying library and proposes a robust, idiomatic fix.

Low
Suggestions up to commit 1962bb8
CategorySuggestion                                                                                                                                    Impact
Security
Validate encryption key before use

Add validation to ensure the encryptionKey is a non-empty string before it is
used to create the database connection.

src/driver/react-native/ReactNativeDriver.ts [928-930]

 if (this.options.encryptionKey) {
+    if (typeof this.options.encryptionKey !== 'string' || this.options.encryptionKey.trim().length === 0) {
+        throw new Error('Encryption key must be a non-empty string');
+    }
     options.encryptionKey = this.options.encryptionKey;
 }
Suggestion importance[1-10]: 6

__

Why: The suggestion correctly identifies a lack of input validation for the new encryptionKey, and proposing a check for a non-empty string improves the robustness and prevents potential runtime errors.

Low

@qodo-free-for-open-source-projects

Copy link
Copy Markdown

PR Code Suggestions ✨

No code suggestions found for the PR.

@qodo-free-for-open-source-projects

Copy link
Copy Markdown

PR Code Suggestions ✨

No code suggestions found for the PR.

@gioboa gioboa changed the title feat:add encryption key for React Native feat: add encryption key for React Native Jan 17, 2026
@qodo-free-for-open-source-projects

qodo-free-for-open-source-projects Bot commented Jan 17, 2026 •

Copy link
Copy Markdown

Code Review by Qodo (Alpha)

(Review updated until commit 2ce80ee)

🐞 Bugs (1) 📘 Rule Violations (0) 📎 Requirement Gaps (0) 💡 Suggestions (0)

Grey Divider


Action Required

✅ 1. Encryption Key Override via Extra Options 🐞 Bug
Suggestion Impact:The commit implements the suggested fix by moving encryptionKey into the base options object (before extra options in Object.assign), ensuring it cannot be overridden. However, instead of using the exact recommended approach, it removes the conditional assignment block and places encryptionKey directly in the initial options object, achieving the same security goal through a cleaner implementation.

code diff:

                 {
                     name: this.options.database,
                     location: this.options.location,
+                    encryptionKey: this.options.encryptionKey,
                 },
                 this.options.extra || {},
             )
-            if (this.options.encryptionKey) {
-                options.encryptionKey = this.options.encryptionKey
-            }

Description
• The encryptionKey can be silently overridden by options.extra due to Object.assign merge order
• When options.extra contains an 'encryptionKey' property, it will override the explicitly
  configured encryptionKey
• This creates a security vulnerability where encryption can be disabled or replaced with an
  incorrect key without warning
• Users may expect encryption to be active based on their configuration, but it could be silently
  bypassed

security

Code

src/driver/react-native/ReactNativeDriver.ts[925]

+                    encryptionKey: this.options.encryptionKey,
Evidence
The code uses Object.assign to merge options, with this.options.extra as the rightmost argument. In
JavaScript, Object.assign processes arguments left-to-right, with later arguments overwriting
earlier ones. Therefore, any encryptionKey property in this.options.extra will override the
explicitly configured value at line 925. This is demonstrated by comparing with the Cordova driver
which uses the same pattern but doesn't have the security-sensitive encryptionKey field.

src/driver/react-native/ReactNativeDriver.ts[920-928]
src/driver/cordova/CordovaDriver.ts[68-75]

Agent Prompt
## Issue Description
The encryptionKey configuration can be silently overridden by options.extra due to Object.assign merge order. This creates a security vulnerability where encryption settings can be bypassed.

## Issue Context
In JavaScript, Object.assign merges objects left-to-right, with later arguments overwriting earlier ones. Currently, this.options.extra is the rightmost argument, allowing it to override the explicitly configured encryptionKey.

## Fix Focus Areas
- src/driver/react-native/ReactNativeDriver.ts[920-928]

Recommended approach: Reverse the merge order so that encryptionKey is applied after extra options:
```typescript
const options = Object.assign(
   {},
   this.options.extra || {},
   {
       name: this.options.database,
       location: this.options.location,
       encryptionKey: this.options.encryptionKey,
   },
)
```

Alternatively, if extra options should be able to override some fields but not encryptionKey, explicitly filter it out from extra options before merging.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Grey Divider


Previous review results

Results up to commit 391dea4


🐞 Bugs (1) 📘 Rule Violations (0) 📎 Requirement Gaps (0) 💡 Suggestions (0)

Grey Divider
Action Required
1. Encryption Key Override Vulnerability 🐞 Bug
Description
• The encryptionKey can be silently overridden by the extra options object, potentially disabling
  database encryption without warning
• Object.assign merges options with this.options.extra as the rightmost argument, allowing it to
  override the explicitly set encryptionKey
• This creates a security risk where encryption could be unintentionally disabled or modified
  through configuration

security

Code

src/driver/react-native/ReactNativeDriver.ts[R922-928]

                {
                    name: this.options.database,
                    location: this.options.location,
+                    encryptionKey: this.options.encryptionKey,
                },
                this.options.extra || {},
            )
Evidence
The Object.assign pattern in ReactNativeDriver allows this.options.extra to override encryptionKey.
This differs from other SQLite drivers which use PRAGMA commands to set encryption keys after
connection, preventing override. The CordovaDriver uses the same vulnerable pattern.

src/driver/react-native/ReactNativeDriver.ts[920-928]
src/driver/sqlite/SqliteDriver.ts[162-164]
src/driver/better-sqlite3/BetterSqlite3Driver.ts[145-148]
src/driver/cordova/CordovaDriver.ts[68-75]

Agent Prompt
# Issue Description
The encryptionKey can be silently overridden by this.options.extra in the Object.assign call, creating a security vulnerability where database encryption could be disabled without warning.

# Issue Context
In the createDatabaseConnection method, Object.assign merges options in order: empty object, explicit options (including encryptionKey), then this.options.extra. Since Object.assign applies properties from right to left, any encryptionKey property in this.options.extra will override the explicitly set this.options.encryptionKey.

# Fix Focus Areas
- src/driver/react-native/ReactNativeDriver.ts[920-928]

# Recommended Fix
Option 1: Apply encryptionKey after the merge:
```typescript
const options = Object.assign(
   {},
   {
       name: this.options.database,
       location: this.options.location,
   },
   this.options.extra || {},
)
if (this.options.encryptionKey !== undefined) {
   options.encryptionKey = this.options.encryptionKey
}
```

Option 2: Explicitly exclude encryptionKey from extra:
```typescript
const { encryptionKey: _, ...safeExtra } = this.options.extra || {}
const options = Object.assign(
   {},
   {
       name: this.options.database,
       location: this.options.location,
       encryptionKey: this.options.encryptionKey,
   },
   safeExtra,
)
```

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Grey Divider
Results up to commit 4fd7a68


🐞 Bugs (1) 📘 Rule Violations (0) 📎 Requirement Gaps (0) 💡 Suggestions (0)

Grey Divider
Action Required
✅ 1. Encryption Key Silently Overridden by Extra 🐞 Bug
Suggestion Impact:The commit implemented one of the suggested solutions by merging encryptionKey directly into the base options object (lines 6) before options.extra is merged (line 8), and removed the conditional assignment (lines 10-12). This ensures the explicit encryptionKey always takes precedence over any value in options.extra.

code diff:

                 {
                     name: this.options.database,
                     location: this.options.location,
+                    encryptionKey: this.options.encryptionKey,
                 },
                 this.options.extra || {},
             )
-            if (this.options.encryptionKey) {
-                options.encryptionKey = this.options.encryptionKey
-            }

Description
• The encryptionKey can be silently overridden by options.extra due to incorrect merge order in
  Object.assign
• Object.assign merges options.extra (line 926) AFTER the base options, then encryptionKey is
  conditionally set (lines 928-930)
• If options.extra contains encryptionKey, it gets merged first, then the conditional check only
  overwrites it if this.options.encryptionKey is truthy
• This means options.extra.encryptionKey takes precedence when this.options.encryptionKey is falsy,
  creating unexpected behavior where users might think encryption is disabled but extra enables it, or
  vice versa

correctness

Code

src/driver/react-native/ReactNativeDriver.ts[R926-930]

                this.options.extra || {},
            )
+            if (this.options.encryptionKey) {
+                options.encryptionKey = this.options.encryptionKey
+            }
Evidence
The code structure shows Object.assign merging options.extra before the encryptionKey conditional
check. The BaseDataSourceOptions defines extra as 'readonly extra?: any' allowing any properties.
The conditional 'if (this.options.encryptionKey)' only sets encryptionKey when truthy, meaning if
options.extra already contains an encryptionKey value and this.options.encryptionKey is falsy, the
extra value persists. This creates ambiguous behavior where two different configuration paths can
set the same property with unclear precedence.

src/driver/react-native/ReactNativeDriver.ts[920-930]
src/data-source/BaseDataSourceOptions.ts[129-134]
src/driver/capacitor/CapacitorDriver.ts[67-68]

Agent Prompt
## Issue Description
The encryptionKey can be silently overridden by options.extra due to the order of Object.assign operations. When options.extra contains an encryptionKey property and this.options.encryptionKey is falsy, the value from extra persists, creating ambiguous configuration behavior.

## Issue Context
The current implementation:
1. Merges base options (name, location)
2. Merges options.extra which can contain any properties including encryptionKey
3. Conditionally sets encryptionKey only if this.options.encryptionKey is truthy

This creates unclear precedence where both this.options.encryptionKey and this.options.extra.encryptionKey can set the same property.

## Fix Focus Areas
- src/driver/react-native/ReactNativeDriver.ts[928-930]

Change the conditional assignment to unconditional to ensure explicit encryptionKey always takes precedence:
```typescript
if (this.options.encryptionKey !== undefined) {
   options.encryptionKey = this.options.encryptionKey
}
```

Or merge encryptionKey into the base options before extra:
```typescript
const options = Object.assign(
   {},
   {
       name: this.options.database,
       location: this.options.location,
       ...(this.options.encryptionKey && { encryptionKey: this.options.encryptionKey }),
   },
   this.options.extra || {},
)
```

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Grey Divider
Qodo Logo

@gioboa
gioboa requested a review from alumni January 17, 2026 12:31

@gioboa gioboa left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@HtSpChakradharCholleti Thanks, LGTM 👏

@gioboa
gioboa requested review from G0maa and naorpeled January 17, 2026 12:47
@qodo-free-for-open-source-projects

Copy link
Copy Markdown

Persistent review updated to latest commit 391dea4

@qodo-free-for-open-source-projects

Copy link
Copy Markdown

Persistent review updated to latest commit 2ce80ee

@gioboa
gioboa merged commit c70a65b into typeorm:master Jan 18, 2026
39 checks passed
@HtSpChakradharCholleti
HtSpChakradharCholleti deleted the patch-1 branch January 18, 2026 14:26
kranners pushed a commit to kranners/typeorm that referenced this pull request Mar 1, 2026
@pkuczynski pkuczynski added this to the 1.0 milestone Mar 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

No db encryption key support for React Native

6 participants