Skip to content

Commit

Permalink
SNOW-728173: Remove new line for verifier the private key end (#566)
Browse files Browse the repository at this point in the history
Co-authored-by: Dicky Lau <[email protected]>
  • Loading branch information
sfc-gh-dprzybysz and sfc-gh-ext-simba-dl authored Jul 6, 2023
1 parent 72f5f09 commit cf02f0b
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
10 changes: 6 additions & 4 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,13 @@ exports.isNumber = function (value)
*
* @returns {Boolean}
*/
exports.isPrivateKey = function (value)
{
exports.isPrivateKey = function (value) {
const trimmedValue = value.trim();
// The private key is expected to be decrypted when set in the connection string
return (value.startsWith("-----BEGIN PRIVATE KEY-----") &&
value.endsWith("\n-----END PRIVATE KEY-----\n"));
// secret scanner complains about first check since it looks like private key, but it's only check
// pragma: allowlist nextline secret
return (trimmedValue.startsWith('-----BEGIN PRIVATE KEY-----') &&
trimmedValue.endsWith('\n-----END PRIVATE KEY-----'));
};

/**
Expand Down
44 changes: 44 additions & 0 deletions test/unit/util_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -589,4 +589,48 @@ describe('Util', function ()
err.response, testCase.retry403), testCase.isRetryable)
}
});

describe('isPrivateKey', () => {
[
// pragma: allowlist nextline secret
{ name: 'trimmed already key', key: '-----BEGIN PRIVATE KEY-----\ntest\n-----END PRIVATE KEY-----' },
{
name: 'key with whitespaces at the beginning',
// pragma: allowlist nextline secret
key: ' -----BEGIN PRIVATE KEY-----\ntest\n-----END PRIVATE KEY-----'
},
{
name: 'key with whitespaces at the end',
// pragma: allowlist nextline secret
key: '-----BEGIN PRIVATE KEY-----\ntest\n-----END PRIVATE KEY-----\n\n\n'
},
].forEach(({ name, key }) => {
it(`${name} is valid`, () => {
assert.ok(Util.isPrivateKey(key));
});
});

[
{ name: 'key without beginning and end', key: 'test' },
{ name: 'key with missing beginning', key: 'test\n-----END PRIVATE KEY-----' },
{
name: 'key with missing ending',
// pragma: allowlist nextline secret
key: ' -----BEGIN PRIVATE KEY-----\ntest'
},
{
name: 'key with invalid beginning',
key: '-----BEGIN PUBLIC KEY-----\ntest\n-----END PRIVATE KEY-----\n\n\n'
},
{
name: 'key with invalid end',
// pragma: allowlist nextline secret
key: '-----BEGIN PRIVATE KEY-----\ntest\n-----END PUBLIC KEY-----\n\n\n'
},
].forEach(({ name, key }) => {
it(`${name} is invalid`, () => {
assert.ok(!Util.isPrivateKey(key));
});
});
});
});

0 comments on commit cf02f0b

Please sign in to comment.