Repository navigation
SSH passphrase fixes and improvements - #12800
Conversation
ff0541f to
d2072a8
Compare
d394a09 to
734c6f3
Compare
| } | ||
|
|
||
| const result = await withTrampolineEnv(async env => { | ||
| const sshEnvironment = await getSSHEnvironment() |
There was a problem hiding this comment.
Since I added some SSH knowledge to the trampoline, I moved this SSH env variables to withTrampolineEnv, cleaning up the code a bit here and in spawn.ts 😄
| }) | ||
|
|
||
| const exitCode = result.exitCode | ||
| const exitCode = result.exitCode |
There was a problem hiding this comment.
All the code from this file below this line has just been indented. There is a very important (and long) comment in trampoline-environment.ts that explains why, but just a summary/spoiler: we want withTrampolineEnv to get any result returned by git/spawnAndComplete, and catch any error thrown by them.
That way, withTrampolineEnv can tell when the git operation succeeded or not and, according to that, store any SSH key passphrase associated to the git operation or not.
| commandName, | ||
| () => | ||
| new Promise<ProcessOutput>(async (resolve, reject) => { | ||
| const sshEnv = await getSSHEnvironment() |
There was a problem hiding this comment.
Same cleanup I mentioned in core.ts 😍
| import { getFileHash } from '../file-system' | ||
| import { TokenStore } from '../stores' | ||
|
|
||
| const appName = __DEV__ ? 'GitHub Desktop Dev' : 'GitHub Desktop' | ||
| const SSHKeyPassphraseTokenStoreKey = `${appName} - SSH key passphrases` | ||
|
|
||
| async function getHashForSSHKey(keyPath: string) { | ||
| return getFileHash(keyPath, 'sha256') | ||
| } | ||
|
|
||
| /** Retrieves the passphrase for the SSH key in the given path. */ | ||
| export async function getSSHKeyPassphrase(keyPath: string) { | ||
| try { | ||
| const fileHash = await getHashForSSHKey(keyPath) | ||
| return TokenStore.getItem(SSHKeyPassphraseTokenStoreKey, fileHash) | ||
| } catch (e) { | ||
| log.error('Could not retrieve passphrase for SSH key:', e) | ||
| return null | ||
| } | ||
| } |
There was a problem hiding this comment.
These are the same functions that previously lived in ssh.ts, just brought them here to live with other passphrase-related functions that I needed to add.
| /** | ||
| * This map contains the SSH key passphrases that are pending to be stored. | ||
| * What this means is that a git operation is currently in progress, and the | ||
| * user wanted to store the passphrase for the SSH key, however we don't want | ||
| * to store it until we know the git operation finished successfully. | ||
| */ | ||
| const SSHKeyPassphrasesToStore = new Map<string, SSHKeyPassphraseEntry>() | ||
|
|
||
| /** | ||
| * Keeps the SSH key passphrase in memory to be stored later if the ongoing git | ||
| * operation succeeds. | ||
| * | ||
| * @param operationGUID A unique identifier for the ongoing git operation. In | ||
| * practice, it will always be the trampoline token for the | ||
| * ongoing git operation. | ||
| * @param keyPath Path to the SSH key. | ||
| * @param passphrase Passphrase for the SSH key. | ||
| */ | ||
| export async function keepSSHKeyPassphraseToStore( | ||
| operationGUID: string, | ||
| keyPath: string, | ||
| passphrase: string | ||
| ) { | ||
| try { | ||
| const keyHash = await getHashForSSHKey(keyPath) | ||
| SSHKeyPassphrasesToStore.set(operationGUID, { keyHash, passphrase }) | ||
| } catch (e) { | ||
| log.error('Could not store passphrase for SSH key:', e) | ||
| } | ||
| } |
There was a problem hiding this comment.
This chunk is important!
|
|
||
| const appName = __DEV__ ? 'GitHub Desktop Dev' : 'GitHub Desktop' | ||
| const SSHKeyPassphraseTokenStoreKey = `${appName} - SSH key passphrases` | ||
|
|
||
| async function getHashForSSHKey(keyPath: string) { | ||
| return getFileHash(keyPath, 'sha256') | ||
| } | ||
|
|
||
| /** Retrieves the passphrase for the SSH key in the given path. */ | ||
| export async function getSSHKeyPassphrase(keyPath: string) { | ||
| try { | ||
| const fileHash = await getHashForSSHKey(keyPath) | ||
| return TokenStore.getItem(SSHKeyPassphraseTokenStoreKey, fileHash) | ||
| } catch (e) { | ||
| log.error('Could not retrieve passphrase for SSH key:', e) | ||
| return null | ||
| } | ||
| } | ||
|
|
||
| /** Stores the passphrase for the SSH key in the given path. */ | ||
| export async function storeSSHKeyPassphrase( | ||
| keyPath: string, | ||
| passphrase: string | ||
| ) { | ||
| try { | ||
| const fileHash = await getHashForSSHKey(keyPath) | ||
| await TokenStore.setItem( | ||
| SSHKeyPassphraseTokenStoreKey, | ||
| fileHash, | ||
| passphrase | ||
| ) | ||
| } catch (e) { | ||
| log.error('Could not store passphrase for SSH key:', e) | ||
| } | ||
| } |
There was a problem hiding this comment.
Just stuff I moved to ssh-key-passphrase.ts (except for storeSSHKeyPassphrase, which was removed).
| } = await trampolineUIHelper.promptSSHKeyPassphrase(keyPath) | ||
|
|
||
| if (passphrase !== undefined && storePassphrase) { | ||
| keepSSHKeyPassphraseToStore(operationGUID, keyPath, passphrase) |
There was a problem hiding this comment.
Another important bit. Now the UI prompt we show to the user just tells us if the user wanted to remember the passphrase, and if so we just "keep it around" in memory, associated to the operationGUID (which is just the trampoline token).
| const trampolineToken = this.environmentVariables.get( | ||
| 'DESKTOP_TRAMPOLINE_TOKEN' | ||
| ) | ||
|
|
||
| if (trampolineToken === undefined) { | ||
| throw new Error(`The trampoline token is missing`) | ||
| } | ||
|
|
||
| return { | ||
| identifier, | ||
| trampolineToken, |
There was a problem hiding this comment.
I included the trampoline token to the trampoline commands because it was very convenient.
| // The code below assumes a few things in order to manage SSH key passphrases | ||
| // correctly: | ||
| // 1. `withTrampolineEnv` is only used in the functions `git` (core.ts) and | ||
| // `spawnAndComplete` (spawn.ts) | ||
| // 2. Those two functions always thrown an error when something went wrong, | ||
| // and just return a result when everything went fine. | ||
| // | ||
| // With those two premises in mind, we can safely assume that right after | ||
| // `fn` has been invoked, we can store the SSH key passphrase for this git | ||
| // operation if there was one pending to be stored. |
There was a problem hiding this comment.
This is very very important!!!
| DESKTOP_ASKPASS_SCRIPT: getAskPassScriptPath(), | ||
| }) | ||
|
|
||
| await storePendingSSHKeyPassphrase(token) |
There was a problem hiding this comment.
If the app got to this point, it meant the git operation didn't fail, and therefore we can safely store any pending SSH key passphrase associated to it 😌
tidy-dev
left a comment
There was a problem hiding this comment.
✨ Makes sense - For my own summary, you are saying "User gave me passphrase and asked me to store it. Let's try it! If it works, store it. If not, delete it so we can ask for a new one."
Description
This PR addresses an issue discovered in #2579 (comment)
It sits on top of #12782 so that I could test it properly, because doing so on Windows would be very painful for me 😆
Basically, the problem with the implementation prior to this PR is that we were storing the SSH key passphrase immediately after the user entered it, regardless of whether the git operation succeeded or not. Because of that, if the user entered the wrong passphrase, that would be stored immediately and never asked again, resulting in a loop of despair where Desktop would automagically read the (bad) passphrase from the Keychain and giving it to git to just fail miserably.
This problem was a bit challenging to fix, because the code handling the prompt (
trampoline-askpass-handler.ts) and the code handling the git operation (core.tsorspawn.ts) are mostly disconnected, and we must be sure to store password A for operation X only when operation X succeeds, but not for operations Y or Z.In order to solve this, I've leveraged the use of unique tokens for the trampoline: for the time a git operation is running, we create a unique token that the trampoline will use to make it harder to exploit for malicious purposes. That token, effectively, identifies that specific git operation uniquely. Thanks to that, we can do this:
Remember passphrasecheckbox, keep the passphrase they entered in memory, associated to the unique trampoline token (and the hash of the SSH key file, of course).The main caveat of this approach is that if the user enters the right passphrase but git fails for whatever other reason, the passphrase won't be remembered and the user will need to enter it in future attempts. An alternative would be not storing the passphrase only if the error returned by the git operation was an authentication error. However, in that case, if the password was wrong but the git operation failed in some other way before noticing that (hence not reporting an authentication failure), the bad password might be stored in the keychain and would be imposible to fix unless the user manually deleted the stored password.
Screenshots
The video below shows how I:
Remember passphraseunchecked. And, of course, it doesn't remember the passphrase ✅Remember passphrasechecked. And it still doesn't remember the passphrase ✅Remember passphraseunchecked. And, of course, it doesn't remember the passphrase but the command works ✅Remember passphrasechecked. It does remember the passphrase and the command works ✅ssh-key-passphrase-improvements.mp4
Release notes
Notes: [Fixed] Wrong SSH key passphrases are not stored.