From 0cfb18271be884a97cb87a51248bd02af1daa62c Mon Sep 17 00:00:00 2001 From: Sigurd Spieckermann <2206639+sisp@users.noreply.github.com> Date: Sun, 25 Aug 2024 12:44:34 +0200 Subject: [PATCH] fix(git): keep trailing newline in private SSH key (#31005) --- lib/util/git/private-key.spec.ts | 6 ++++-- lib/util/git/private-key.ts | 6 +++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/util/git/private-key.spec.ts b/lib/util/git/private-key.spec.ts index f5fe50ac5589b3..1f365b0f5e544a 100644 --- a/lib/util/git/private-key.spec.ts +++ b/lib/util/git/private-key.spec.ts @@ -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(); }); @@ -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'; diff --git a/lib/util/git/private-key.ts b/lib/util/git/private-key.ts index 5e929479366a28..2b8828e6064146 100644 --- a/lib/util/git/private-key.ts +++ b/lib/util/git/private-key.ts @@ -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)', @@ -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 { const keyFileName = upath.join(os.tmpdir() + '/git-private-gpg.key'); await fs.outputFile(keyFileName, this.key);