Skip to content

Commit 094fe45

Browse files
authored
fix: add newlines if they are missing (#50)
1 parent 889c36d commit 094fe45

2 files changed

Lines changed: 47 additions & 5 deletions

File tree

src/index.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ type Options = {
1515
cwd?: string;
1616
};
1717

18+
const begin = "-----BEGIN RSA PRIVATE KEY-----";
19+
const end = "-----END RSA PRIVATE KEY-----";
20+
1821
export function getPrivateKey(options: Options = {}): string | null {
1922
const env = options.env || process.env;
2023
const cwd = options.cwd || process.cwd();
@@ -31,11 +34,17 @@ export function getPrivateKey(options: Options = {}): string | null {
3134
privateKey = Buffer.from(privateKey, "base64").toString();
3235
}
3336

34-
const begin = "-----BEGIN RSA PRIVATE KEY-----";
35-
const end = "-----END RSA PRIVATE KEY-----";
3637
if (privateKey.includes(begin) && privateKey.includes(end)) {
37-
// Full key with new lines
38-
return privateKey.replace(/\\n/g, "\n");
38+
// newlines are escaped
39+
if (privateKey.indexOf("\\n") !== -1) {
40+
privateKey = privateKey.replace(/\\n/g, "\n");
41+
}
42+
43+
// newlines are missing
44+
if (privateKey.indexOf("\n") === -1) {
45+
privateKey = addNewlines(privateKey);
46+
}
47+
return privateKey;
3948
}
4049

4150
throw new Error(
@@ -65,4 +74,10 @@ export function getPrivateKey(options: Options = {}): string | null {
6574
return null;
6675
}
6776

77+
function addNewlines(privateKey: string): string {
78+
const middleLength = privateKey.length - begin.length - end.length - 2;
79+
const middle = privateKey.substr(begin.length + 1, middleLength);
80+
return `${begin}\n${middle.trim().replace(/\s+/g, "\n")}\n${end}`;
81+
}
82+
6883
getPrivateKey.VERSION = VERSION;

test/get-private-key.test.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,16 @@ const readdirSync = fs.readdirSync as jest.Mock;
1010
const readFileSync = fs.readFileSync as jest.Mock;
1111

1212
const PRIVATE_KEY =
13-
"-----BEGIN RSA PRIVATE KEY-----\n ... \n-----END RSA PRIVATE KEY-----";
13+
"-----BEGIN RSA PRIVATE KEY-----\n7HjkPK\nKLm395\nAIBII\n-----END RSA PRIVATE KEY-----";
14+
15+
const PRIVATE_KEY_NO_NEWLINES =
16+
"-----BEGIN RSA PRIVATE KEY----- 7HjkPK KLm395 AIBII -----END RSA PRIVATE KEY-----";
17+
18+
const PRIVATE_KEY_NO_NEWLINES_MULTIPLE_SPACES =
19+
"-----BEGIN RSA PRIVATE KEY----- 7HjkPK KLm395 AIBII -----END RSA PRIVATE KEY-----";
20+
21+
const PRIVATE_KEY_ESCAPED_NEWLINES =
22+
"-----BEGIN RSA PRIVATE KEY-----\\n7HjkPK\\nKLm395\\nAIBII\\n-----END RSA PRIVATE KEY-----";
1423

1524
describe("getPrivateKey", () => {
1625
beforeEach(() => {
@@ -123,6 +132,24 @@ describe("getPrivateKey", () => {
123132
expect(result).toEqual(PRIVATE_KEY);
124133
});
125134

135+
it("PRIVATE_KEY contains no newlines", () => {
136+
process.env.PRIVATE_KEY = PRIVATE_KEY_NO_NEWLINES;
137+
const result = getPrivateKey();
138+
expect(result).toEqual(PRIVATE_KEY);
139+
});
140+
141+
it("PRIVATE_KEY contains consecutive spaces", () => {
142+
process.env.PRIVATE_KEY = PRIVATE_KEY_NO_NEWLINES_MULTIPLE_SPACES;
143+
const result = getPrivateKey();
144+
expect(result).toEqual(PRIVATE_KEY);
145+
});
146+
147+
it("PRIVATE_KEY contains escaped newlines", () => {
148+
process.env.PRIVATE_KEY = PRIVATE_KEY_ESCAPED_NEWLINES;
149+
const result = getPrivateKey();
150+
expect(result).toEqual(PRIVATE_KEY);
151+
});
152+
126153
it("PRIVATE_KEY invalid", () => {
127154
process.env.PRIVATE_KEY = "invalid";
128155
expect(() => getPrivateKey()).toThrow(

0 commit comments

Comments
 (0)