Skip to content

Commit 91644ce

Browse files
wip v28
1 parent 913a9b9 commit 91644ce

2 files changed

Lines changed: 24 additions & 0 deletions

File tree

packages/github/src/__tests__/jwt-helper.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ describe('GitHub App JWT helpers', () => {
3232
expect(generateGitHubAppJWT('244206', base64PrivateKey).split('.')).toHaveLength(3);
3333
});
3434

35+
it('reconstructs single-line PEM values that preserve markers but lose line breaks', () => {
36+
const privateKey = createPrivateKeyPem();
37+
const singleLinePrivateKey = privateKey.replace(/\n/g, ' ');
38+
39+
expect(normalizeGitHubAppPrivateKey(singleLinePrivateKey)).toBe(privateKey.trim());
40+
expect(generateGitHubAppJWT('244206', singleLinePrivateKey).split('.')).toHaveLength(3);
41+
});
42+
3543
it('rejects local private-key paths with an actionable configuration error', () => {
3644
expect(() =>
3745
normalizeGitHubAppPrivateKey('/Users/example/Downloads/bitcode.private-key.pem')

packages/github/src/auth/jwt-helper.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,20 @@ function decodeBase64Pem(value: string): string | null {
4545
}
4646
}
4747

48+
function canonicalizePem(value: string): string {
49+
const match = value.match(
50+
/(-----BEGIN [A-Z ]*PRIVATE KEY-----)([\s\S]*?)(-----END [A-Z ]*PRIVATE KEY-----)/,
51+
);
52+
if (!match) return value;
53+
54+
const [, begin = '', body = '', end = ''] = match;
55+
const compactBody = body.replace(/\s/g, '');
56+
if (!compactBody) return value;
57+
58+
const wrappedBody = compactBody.match(/.{1,64}/g)?.join('\n') ?? compactBody;
59+
return `${begin}\n${wrappedBody}\n${end}`;
60+
}
61+
4862
export function normalizeGitHubAppPrivateKey(privateKey: string): string {
4963
let key = stripWrappingQuotes(privateKey.trim())
5064
.replace(/\\r\\n/g, '\n')
@@ -58,6 +72,8 @@ export function normalizeGitHubAppPrivateKey(privateKey: string): string {
5872
key = decodeBase64Pem(key) ?? key;
5973
}
6074

75+
key = canonicalizePem(key);
76+
6177
if (/^\/.*\.pem$/i.test(key) || /^[A-Z]:\\.*\.pem$/i.test(key)) {
6278
throw new Error(
6379
'GITHUB_PRIVATE_KEY contains a file path. Configure the deployed environment with the PEM contents or a base64-encoded PEM, not a local .pem path.'

0 commit comments

Comments
 (0)