Skip to content

Commit

Permalink
fix(git): keep trailing newline in private SSH key (#31005)
Browse files Browse the repository at this point in the history
  • Loading branch information
sisp authored Aug 25, 2024
1 parent 4ec7eae commit 0cfb182
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
6 changes: 4 additions & 2 deletions lib/util/git/private-key.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ describe('util/git/private-key', () => {
-----BEGIN OPENSSH PRIVATE KEY-----
some-private-key with-passphrase
some-private-key with-passphrase
-----END OPENSSH PRIVATE KEY-----`);
-----END OPENSSH PRIVATE KEY-----
`);
await expect(writePrivateKey()).rejects.toThrow();
});

Expand All @@ -98,7 +99,8 @@ some-private-key with-passphrase
-----BEGIN OPENSSH PRIVATE KEY-----
some-private-key
some-private-key
-----END OPENSSH PRIVATE KEY-----`;
-----END OPENSSH PRIVATE KEY-----
`;
const privateKeyFile = upath.join(os.tmpdir() + '/git-private-ssh.key');
const publicKeyFile = `${privateKeyFile}.pub`;
const publicKey = 'some-public-key';
Expand Down
6 changes: 5 additions & 1 deletion lib/util/git/private-key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ abstract class PrivateKey {
protected abstract readonly gpgFormat: string;

constructor(key: string) {
this.key = key.trim();
this.key = key;
addSecretForSanitizing(this.key, 'global');
logger.debug(
'gitPrivateKey: successfully set (but not yet written/configured)',
Expand Down Expand Up @@ -57,6 +57,10 @@ abstract class PrivateKey {
class GPGKey extends PrivateKey {
protected readonly gpgFormat = 'openpgp';

constructor(key: string) {
super(key.trim());
}

protected async importKey(): Promise<string | undefined> {
const keyFileName = upath.join(os.tmpdir() + '/git-private-gpg.key');
await fs.outputFile(keyFileName, this.key);
Expand Down

0 comments on commit 0cfb182

Please sign in to comment.