-
Notifications
You must be signed in to change notification settings - Fork 1.5k
fix: encrypt saved FTP/SFTP credentials #2566
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
MYounas126
wants to merge
5
commits into
Acode-Foundation:main
from
MYounas126:fix/encrypt-remote-credentials
Closed
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8a91240
fix: encrypt saved FTP/SFTP credentials at rest (#2561)
MYounas126 57d8c16
fix: declare SecureStore source-file and use durable commit() for sec…
MYounas126 07ae258
docs: trim over-verbose header comment in secureStorageList (review f…
MYounas126 e57354e
fix(secure-store): remove plaintext fallback; fail closed on encrypti…
MYounas126 f7e8fe8
Merge origin/main into fix/encrypt-remote-credentials
MYounas126 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| /** | ||
| * Secure persistence for the remote-storage list (saved FTP/SFTP servers). | ||
| * | ||
| * Historically the list lived in `localStorage.storageList`, which the WebView | ||
| * writes to disk in cleartext — so every saved server password sat unencrypted | ||
| * in `app_webview/.../Local Storage/leveldb`. See issue #2561. | ||
| * | ||
| * This module moves the list out of localStorage into a native, hardware-backed | ||
| * encrypted store (AES256-GCM, via the `system.secure*` bridge). The *shape* of | ||
| * the data is unchanged: the in-memory list is identical to what callers used to | ||
| * read from localStorage (including credential-bearing URLs), so nothing | ||
| * downstream needs to change — only where the bytes rest on disk. | ||
| * | ||
| * Access is synchronous (an in-memory cache) so existing synchronous callers | ||
| * keep working. The cache is hydrated once at startup by `hydrate()`, which MUST | ||
| * be awaited early in onDeviceReady, before any UI that reads the list. | ||
| */ | ||
|
|
||
| const SECURE_KEY = "storageList"; | ||
| const LEGACY_KEY = "storageList"; // the old localStorage key | ||
|
|
||
| /** @type {Array|null} in-memory source of truth; null until hydrated */ | ||
| let cache = null; | ||
|
|
||
| function parse(json) { | ||
| try { | ||
| const value = JSON.parse(json); | ||
| return Array.isArray(value) ? value : []; | ||
| } catch (_) { | ||
| return []; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Load the list into memory, migrating any legacy plaintext localStorage copy | ||
| * into the encrypted store first. Safe to call more than once. | ||
| * @returns {Promise<void>} | ||
| */ | ||
| async function hydrate() { | ||
| // 1. One-time migration: if a legacy plaintext list exists in localStorage, | ||
| // move it into the encrypted store, then remove the plaintext copy. | ||
| const legacy = localStorage.getItem(LEGACY_KEY); | ||
| if (legacy != null) { | ||
| try { | ||
| // Only drop the plaintext copy AFTER the encrypted write succeeds, | ||
| // so a failure here never loses the user's saved servers. | ||
| await window.system.secureSet(SECURE_KEY, legacy); | ||
| localStorage.removeItem(LEGACY_KEY); | ||
| } catch (error) { | ||
| // Migration failed — keep the legacy copy and fall back to it this | ||
| // session rather than lose data. Try again next launch. | ||
| window.log?.("error", `secureStorageList migration failed: ${error}`); | ||
| cache = parse(legacy); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| // 2. Load from the encrypted store. | ||
| try { | ||
| const stored = await window.system.secureGet(SECURE_KEY); | ||
| cache = stored ? parse(stored) : []; | ||
| } catch (error) { | ||
| window.log?.("error", `secureStorageList hydrate failed: ${error}`); | ||
| cache = []; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * The saved remote-storage list (synchronous). | ||
| * Falls back to a one-shot legacy read if called before hydrate() (defensive; | ||
| * should not happen in normal boot order). | ||
| * @returns {Array} | ||
| */ | ||
| function get() { | ||
| if (cache == null) { | ||
| const legacy = localStorage.getItem(LEGACY_KEY); | ||
| return legacy != null ? parse(legacy) : []; | ||
| } | ||
| return cache; | ||
| } | ||
|
|
||
| /** | ||
| * Persist the list. Updates the in-memory cache immediately and flushes to the | ||
| * encrypted store. The returned promise resolves once the flush completes; | ||
| * synchronous callers may ignore it (the cache is already updated). | ||
| * @param {Array} list | ||
| * @returns {Promise<void>} | ||
| */ | ||
| function set(list) { | ||
| cache = Array.isArray(list) ? list : []; | ||
| const json = JSON.stringify(cache); | ||
| return window.system.secureSet(SECURE_KEY, json).catch((error) => { | ||
| window.log?.("error", `secureStorageList save failed: ${error}`); | ||
| }); | ||
| } | ||
|
|
||
| export default { hydrate, get, set }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
src/plugins/system/android/com/foxdebug/system/SecureStore.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| package com.foxdebug.system; | ||
|
|
||
| import android.content.Context; | ||
| import android.content.SharedPreferences; | ||
| import androidx.security.crypto.EncryptedSharedPreferences; | ||
| import androidx.security.crypto.MasterKeys; | ||
| import java.io.IOException; | ||
| import java.security.GeneralSecurityException; | ||
|
|
||
| /** | ||
| * Small encrypted key/value store for secrets that must not sit in cleartext on | ||
| * disk (e.g. saved SFTP/FTP server credentials, previously kept in the WebView's | ||
| * localStorage.storageList). Values are encrypted at rest with a hardware-backed | ||
| * master key via AndroidX Security-Crypto (AES256-GCM), the same mechanism the | ||
| * auth plugin already uses for the account token. | ||
| * | ||
| * Lazily initialised so a crypto/keystore failure never blocks plugin startup. | ||
| */ | ||
| public class SecureStore { | ||
|
|
||
| private static final String PREF_NAME = "acode_secure_store"; | ||
|
|
||
| private final Context context; | ||
| private SharedPreferences prefs; | ||
|
|
||
| public SecureStore(Context context) { | ||
| this.context = context.getApplicationContext(); | ||
| } | ||
|
|
||
| private SharedPreferences prefs() { | ||
| if (prefs != null) return prefs; | ||
| try { | ||
| String masterKeyAlias = MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC); | ||
| prefs = EncryptedSharedPreferences.create( | ||
| PREF_NAME, | ||
| masterKeyAlias, | ||
| context, | ||
| EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV, | ||
| EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM | ||
| ); | ||
| } catch (GeneralSecurityException | IOException e) { | ||
|
bajrangCoder marked this conversation as resolved.
|
||
| // Same fallback the existing EncryptedPreferenceManager uses: a private | ||
| // (app-sandbox) prefs file. Not encrypted, but still off the WebView's | ||
| // localStorage and unreadable by other apps. | ||
| prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE); | ||
| } | ||
| return prefs; | ||
| } | ||
|
|
||
| /** Store a value. Passing null removes the key. */ | ||
| public void set(String key, String value) { | ||
| if (value == null) { | ||
| remove(key); | ||
| return; | ||
| } | ||
| prefs().edit().putString(key, value).apply(); | ||
|
greptile-apps[bot] marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| /** Return the stored value, or null if absent. */ | ||
| public String get(String key) { | ||
| return prefs().getString(key, null); | ||
| } | ||
|
|
||
| public void remove(String key) { | ||
| prefs().edit().remove(key).apply(); | ||
| } | ||
|
|
||
| public boolean contains(String key) { | ||
| return prefs().contains(key); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.