|
| 1 | +/* |
| 2 | + * GNU AGPL-3.0 License |
| 3 | + * |
| 4 | + * Copyright (c) 2021 - present core.ai . All rights reserved. |
| 5 | + * |
| 6 | + * This program is free software: you can redistribute it and/or modify it under |
| 7 | + * the terms of the GNU Affero General Public License as published by the Free |
| 8 | + * Software Foundation, either version 3 of the License, or (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 11 | + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 | + * See the GNU Affero General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU Affero General Public License along |
| 15 | + * with this program. If not, see https://opensource.org/licenses/AGPL-3.0. |
| 16 | + * |
| 17 | + */ |
| 18 | + |
| 19 | +/** |
| 20 | + * KernalModeTrust is a security mechanism in Phoenix that provides a trust base for core components before any |
| 21 | + * extensions are loaded. It establishes a secure communication channel between core modules and the Tauri shell, |
| 22 | + * preventing unauthorized access to sensitive information by extensions or other potentially malicious code. |
| 23 | + * |
| 24 | + * ## Purpose |
| 25 | + * |
| 26 | + * The primary purposes of KernalModeTrust are: |
| 27 | + * |
| 28 | + * 1. **Secure Boot Process**: Ensures that the application can only boot with a properly initialized trust ring. |
| 29 | + * 2. **Secure Communication**: Enables core modules to communicate securely without worrying about interception by |
| 30 | + * extensions. |
| 31 | + * 3. **API Key Management**: Provides secure storage and retrieval of Phoenix API keys. |
| 32 | + * 4. **Security Boundary**: Creates a clear security boundary between trusted core components and potentially untrusted |
| 33 | + * extensions. |
| 34 | + * |
| 35 | + * ## Implementation Details |
| 36 | + * |
| 37 | + * ### Trust Ring Initialization |
| 38 | + * |
| 39 | + * The trust ring is initialized at boot time before any extensions are loaded: |
| 40 | + * |
| 41 | + * 1. Random AES-256 keys and initialization vectors (IV) are generated using the Web Crypto API. |
| 42 | + * 2. These cryptographic materials are stored in the `window.KernalModeTrust` object. |
| 43 | + * 3. The trust relationship is established with the Tauri backend via the `initTrustRing()` function. |
| 44 | + * |
| 45 | + * The trust ring has several important security characteristics: |
| 46 | + * |
| 47 | + * 1. **Memory-Only Storage**: The random AES key-based trust ring is only kept in memory and never persisted to disk. |
| 48 | + * 2. **One-Time Use**: The trust ring is designed for one-time use and is discarded after serving its purpose. |
| 49 | + * 3. **Session Lifetime**: It is maintained in memory only until the end of the Phoenix session. |
| 50 | + * 4. **Tauri Communication**: The trust keys are communicated to the Tauri shell at boot time. |
| 51 | + * 5. **API Response Encryption**: Once an AES key is trusted by the Tauri shell, all sensitive API responses will be |
| 52 | + * encrypted with this key. This means extensions can still call sensitive APIs but will receive only encrypted |
| 53 | + * garbage responses without access to the trust key. |
| 54 | + * |
| 55 | + * ### Security Model |
| 56 | + * |
| 57 | + * KernalModeTrust implements a strict security model: |
| 58 | + * |
| 59 | + * 1. **Boot-time Only Access**: The trust ring is only available to code that loads before any extensions. |
| 60 | + * 2. **One-time Trust**: For a given Tauri window, the trust ring can only be set once. |
| 61 | + * 3. **Deliberate Removal**: Before extensions are loaded, `window.KernalModeTrust` is deleted to prevent extensions |
| 62 | + * from accessing it. |
| 63 | + * 4. **Dismantling Before Restart**: The trust ring must be dismantled before restarting the application. This is a |
| 64 | + * critical security requirement. If not dismantled, the old trust keys will still be in place when the page reloads, |
| 65 | + * but the application will lose access to them (as they were only stored in memory). As a result, the Tauri shell |
| 66 | + * will not trust any sensitive API calls from the reloaded page, as these calls will rely on the old keys that the |
| 67 | + * new page instance cannot access. This security measure intentionally prevents any page reload from maintaining |
| 68 | + * trust without explicitly dismantling the old trust ring first, ensuring that malicious code cannot bypass |
| 69 | + * security by simply reloading the window. |
| 70 | + * |
| 71 | + * ### Cryptographic Implementation |
| 72 | + * |
| 73 | + * KernalModeTrust uses strong cryptography: |
| 74 | + * |
| 75 | + * 1. **AES-256 Encryption**: Uses AES-256 in GCM mode for secure encryption/decryption. |
| 76 | + * 2. **Random Key Generation**: Cryptographically secure random number generation for keys and IVs. |
| 77 | + * 3. **Secure Key Storage**: Keys are stored securely in the Tauri backend(which is stored in OS keychain). |
| 78 | + * |
| 79 | + * ## Security Considerations |
| 80 | + * |
| 81 | + * 1. **Extension Isolation**: Extensions should never have access to KernalModeTrust to prevent potential security |
| 82 | + * breaches. |
| 83 | + * |
| 84 | + * 2. **One-time Trust**: The trust ring can only be set once per Tauri window, preventing malicious code from replacing |
| 85 | + * it. |
| 86 | + * |
| 87 | + * 3. **Complete Dismantling**: When dismantling the keyring, it's recommended to reload the page immediately to prevent |
| 88 | + * any potential exploitation of the system. |
| 89 | + * |
| 90 | + * 4. **Test Environment Handling**: Special handling exists for test environments to ensure tests can run properly |
| 91 | + * without compromising security. |
| 92 | + * |
| 93 | + * ## Conclusion |
| 94 | + * |
| 95 | + * KernalModeTrust is a critical security component in Phoenix that establishes a trust boundary between core components |
| 96 | + * and extensions. By providing secure communication channels and API key management, it helps maintain the overall |
| 97 | + * security posture of the application. |
| 98 | + * */ |
| 99 | + |
1 | 100 | // Generate random AES-256 key and GCM nonce/IV |
2 | 101 | function generateRandomKeyAndIV() { |
3 | 102 | // Generate 32 random bytes for AES-256 key |
|
0 commit comments