Mitigation Strategy: Enable Realm File Encryption
- Description:
- Step 1: Configure Encryption Key: When initializing your Realm configuration using
Realm.Configuration()
, set theencryptionKey
property with a 64-byteData
object. This is done during Realm setup. - Step 2: Generate Secure Key: Create a cryptographically strong 64-byte random key using
SecRandomCopyBytes
(Swift) or platform-specific secure random generators. - Step 3: Securely Store Key: Store this generated key in the device's Keychain (iOS/macOS) or Keystore (Android). Avoid storing the key directly in code or insecurely.
- Step 4: Initialize Realm with Key: Ensure all Realm instances in your application are initialized using the configuration that includes the securely stored
encryptionKey
.
- Step 1: Configure Encryption Key: When initializing your Realm configuration using
- Threats Mitigated:
- Unauthorized Access to Realm Data at Rest (Severity: High): Without encryption, anyone gaining access to the device's file system can read the Realm database file directly. Encryption renders the file unreadable without the key.
- Data Breach from Lost or Stolen Devices (Severity: High): If a device is lost or stolen, encryption protects the Realm data from being accessed even if device security is compromised.
- Impact:
- Unauthorized Access to Realm Data at Rest: Significantly reduces risk by making the Realm file unusable without the encryption key.
- Data Breach from Lost or Stolen Devices: Significantly reduces risk of data exposure from physical device compromise related to Realm data.
- Currently Implemented:
- Yes, implemented in the
Data Layer
module during Realm initialization. Encryption is enabled using a key retrieved from the Keychain.
- Yes, implemented in the
- Missing Implementation:
- N/A - Currently fully implemented for Realm file encryption. Consider future enhancement of key rotation strategies.
Mitigation Strategy: Implement Robust Realm Schema Migrations
- Description:
- Step 1: Define Migration Block in Configuration: When you change your Realm schema (classes, properties), provide a migration block within
Realm.Configuration()
. This block executes automatically when Realm detects a schema version change. - Step 2: Increment Schema Version: Increase the
schemaVersion
in yourRealm.Configuration()
whenever you modify the schema. This signals Realm to run the migration. - Step 3: Write Data Migration Logic: Inside the migration block, write code to transform data from the old schema to the new schema. This might involve renaming properties, restructuring data, or handling data type changes within Realm.
- Step 4: Test Migrations Thoroughly: Test schema migrations in development and staging environments with Realm data before production deployment to prevent data loss or corruption during schema updates.
- Step 1: Define Migration Block in Configuration: When you change your Realm schema (classes, properties), provide a migration block within
- Threats Mitigated:
- Realm Data Corruption during Schema Updates (Severity: High): Schema mismatches between the application and the Realm file can lead to data corruption and application crashes when accessing Realm data after an update.
- Application Instability due to Schema Mismatches (Severity: Medium): Incorrect schema handling can cause unpredictable application behavior and crashes related to Realm operations after updates.
- Realm Data Loss during Updates (Severity: High): Improper migration logic can result in loss of data stored in Realm during schema evolution.
- Impact:
- Realm Data Corruption during Schema Updates: Significantly reduces risk of data corruption by ensuring data is correctly adapted to the new Realm schema.
- Application Instability due to Schema Mismatches: Significantly reduces risk of crashes and instability related to Realm schema changes.
- Realm Data Loss during Updates: Significantly reduces risk of data loss during schema updates by providing a controlled migration process within Realm.
- Currently Implemented:
- Partially implemented. Basic schema migrations are in place for schema changes in the
Data Layer
. Schema version is incremented.
- Partially implemented. Basic schema migrations are in place for schema changes in the
- Missing Implementation:
- Need more comprehensive testing of complex Realm schema migrations. Enhance error handling within migration blocks for better recovery from migration failures. Automate Realm migration testing.
Mitigation Strategy: Validate Data Before Writing to Realm Objects
- Description:
- Step 1: Define Realm Data Validation Rules: Establish validation rules for properties of your Realm objects. This includes data type checks, range constraints, format validation, and required field enforcement specifically for data being stored in Realm.
- Step 2: Implement Validation Logic Before Realm Writes: Implement validation checks in your code before you write data to Realm objects. This can be in data models, data access functions, or input handling components that interact with Realm.
- Step 3: Handle Realm Validation Errors: If validation fails before writing to Realm, prevent the write operation. Provide user feedback or log validation errors. Do not store invalid data in Realm.
- Threats Mitigated:
- Realm Data Integrity Issues (Severity: Medium): Invalid data in Realm can lead to application logic errors and inconsistent data states within the Realm database.
- Application Logic Vulnerabilities related to Realm Data (Severity: Medium): If the application assumes data in Realm is always valid, vulnerabilities can arise if invalid data is stored due to lack of validation.
- Impact:
- Realm Data Integrity Issues: Significantly reduces risk of data integrity problems within the Realm database by ensuring only valid data is persisted.
- Application Logic Vulnerabilities related to Realm Data: Partially reduces risk of vulnerabilities arising from processing invalid data retrieved from Realm.
- Currently Implemented:
- Partially implemented. Basic data type validation is present in some data models. Required fields are mostly enforced at the UI level, less consistently before Realm writes.
- Missing Implementation:
- Implement comprehensive validation rules for all relevant Realm object properties. Consistently apply validation logic in the data access layer before any Realm write operations. Automate Realm data validation testing.
Mitigation Strategy: Securely Manage Realm Encryption Key (Realm-Specific)
- Description:
- Step 1: Use Platform Keychain/Keystore for Realm Key: Specifically utilize iOS/macOS Keychain Services or Android Keystore to store the Realm encryption key. These are the recommended secure storage mechanisms for sensitive keys on these platforms.
- Step 2: Secure Realm Key Retrieval: Retrieve the Realm encryption key from the Keychain/Keystore only when needed to initialize Realm. Handle potential key retrieval errors gracefully.
- Step 3: Restrict Realm Key Access: Ensure that access to the Keychain/Keystore item storing the Realm key is restricted to your application only, leveraging platform security features.
- Threats Mitigated:
- Realm Encryption Key Compromise (Severity: Critical): If the Realm encryption key is compromised, the entire Realm database encryption is broken, allowing unauthorized access to all Realm data.
- Data Breach from Reverse Engineering (Realm Context) (Severity: High): Storing the Realm key insecurely within the application makes it vulnerable to extraction through reverse engineering, defeating Realm encryption.
- Impact:
- Realm Encryption Key Compromise: Significantly reduces risk of key compromise by using secure, platform-provided key storage.
- Data Breach from Reverse Engineering (Realm Context): Significantly reduces risk of key extraction from the application code, protecting Realm encryption.
- Currently Implemented:
- Yes, implemented in the
Security
module. The Realm encryption key is stored and retrieved from the Keychain.
- Yes, implemented in the
- Missing Implementation:
- N/A - Currently implemented for secure Realm key storage. Consider future enhancements like key rotation and more advanced key derivation methods for Realm encryption.
Mitigation Strategy: Minimize Logging of Realm Data
- Description:
- Step 1: Review Realm-Related Logging: Specifically review logging statements in your application that involve interactions with Realm (queries, writes, object properties).
- Step 2: Redact Sensitive Realm Data in Logs: Identify any logging that might output sensitive data retrieved from or being written to Realm. Redact or mask this sensitive Realm data in logs. Log only non-sensitive identifiers or summaries related to Realm operations.
- Step 3: Conditional Realm Data Logging: Implement conditional logging to disable verbose logging of Realm data in production builds. Enable detailed Realm data logging only in development/staging for debugging purposes.
- Threats Mitigated:
- Data Leakage of Realm Data through Logs (Severity: Medium): Sensitive data from Realm, if logged, can be exposed if logs are not properly secured or are accidentally accessed by unauthorized parties.
- Information Disclosure of Realm Data Structures (Severity: Medium): Excessive logging of Realm data can reveal details about your data model and sensitive information stored in Realm, which could be exploited.
- Impact:
- Data Leakage of Realm Data through Logs: Significantly reduces risk of accidental exposure of sensitive Realm data through application logs.
- Information Disclosure of Realm Data Structures: Partially reduces risk of revealing sensitive information about Realm data structures and content through logs.
- Currently Implemented:
- Partially implemented. Debug logging is generally disabled in production. However, some logging might still inadvertently include Realm data.
- Missing Implementation:
- Need a focused review of all Realm-related logging statements to ensure redaction or masking of sensitive Realm data. Establish clear guidelines for developers regarding logging practices for Realm interactions to minimize data exposure. Implement automated log scanning for potential Realm data leaks.