Skip to content

SSH passphrase fixes and improvements - #12800

Merged
sergiou87 merged 3 commits into
developmentfrom
ssh-passphrase-improvements
Aug 18, 2021
Merged

sergiou87 merged 3 commits into
developmentfrom
ssh-passphrase-improvements

Conversation

@sergiou87

@sergiou87 sergiou87 commented Aug 18, 2021 •

Copy link
Copy Markdown
Member

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.ts or spawn.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:

  1. When we get the prompt from the trampoline askpass handler, prompt the user for their SSH key passphrase.
  2. If the user checked the Remember passphrase checkbox, keep the passphrase they entered in memory, associated to the unique trampoline token (and the hash of the SSH key file, of course).
  3. Pass the passphrase to git so it can continue the operation.
  4. Once the git operation finished, look for the passphrase associated to its trampoline token and store it in the keychain / credential manager.
  5. Remove the passphrase from memory.

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:

  1. Enter the wrong password 3 times with Remember passphrase unchecked. And, of course, it doesn't remember the passphrase ✅
  2. Enter the wrong password 3 times with Remember passphrase checked. And it still doesn't remember the passphrase ✅
  3. Enter the right password with Remember passphrase unchecked. And, of course, it doesn't remember the passphrase but the command works ✅
  4. Enter the right password with Remember passphrase checked. It does remember the passphrase and the command works ✅
  5. I fetch again and it just works because it remembered the password 🎉
ssh-key-passphrase-improvements.mp4

Release notes

Notes: [Fixed] Wrong SSH key passphrases are not stored.

@sergiou87
sergiou87 requested a review from tidy-dev August 18, 2021 12:09
@sergiou87
sergiou87 changed the base branch from development to ssh-askpass-macos August 18, 2021 12:10
@sergiou87
sergiou87 force-pushed the ssh-passphrase-improvements branch from d394a09 to 734c6f3 Compare August 18, 2021 12:11
Comment thread app/src/lib/git/core.ts
}

const result = await withTrampolineEnv(async env => {
const sshEnvironment = await getSSHEnvironment()

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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 😄

Comment thread app/src/lib/git/core.ts
})

const exitCode = result.exitCode
const exitCode = result.exitCode

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

Comment thread app/src/lib/git/spawn.ts
commandName,
() =>
new Promise<ProcessOutput>(async (resolve, reject) => {
const sshEnv = await getSSHEnvironment()

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Same cleanup I mentioned in core.ts 😍

Comment on lines +1 to +20
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
}
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

Comment on lines +30 to +59
/**
* 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)
}
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This chunk is important!

Comment thread app/src/lib/ssh/ssh.ts
Comment on lines -76 to -110

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)
}
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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).

Comment on lines +129 to +139
const trampolineToken = this.environmentVariables.get(
'DESKTOP_TRAMPOLINE_TOKEN'
)

if (trampolineToken === undefined) {
throw new Error(`The trampoline token is missing`)
}

return {
identifier,
trampolineToken,

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I included the trampoline token to the trampoline commands because it was very convenient.

Comment on lines +34 to +43
// 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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This is very very important!!!

DESKTOP_ASKPASS_SCRIPT: getAskPassScriptPath(),
})

await storePendingSSHKeyPassphrase(token)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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 tidy-dev 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.

✨ 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."

Base automatically changed from ssh-askpass-macos to development August 18, 2021 13:30
@sergiou87
sergiou87 merged commit 132c8dd into development Aug 18, 2021
@sergiou87
sergiou87 deleted the ssh-passphrase-improvements branch August 18, 2021 13:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants