@@ -94,4 +94,89 @@ describe('loadServiceKey', () => {
94
94
const result = await loadServiceKey ( JSON . stringify ( invalidServiceKey ) ) ;
95
95
expect ( result ) . toEqual ( invalidServiceKey ) ; // It returns the object as-is, validation is minimal
96
96
} ) ;
97
+
98
+ it ( 'should handle escaped newlines in private key from AWS Secrets Manager' , async ( ) => {
99
+ const serviceKeyWithEscapedNewlines = {
100
+ ...mockServiceKey ,
101
+ private_key : '-----BEGIN PRIVATE KEY-----\\ntest-key\\n-----END PRIVATE KEY-----' ,
102
+ } ;
103
+ const jsonString = JSON . stringify ( serviceKeyWithEscapedNewlines ) ;
104
+
105
+ const result = await loadServiceKey ( jsonString ) ;
106
+ expect ( result ) . not . toBeNull ( ) ;
107
+ expect ( result ?. private_key ) . toBe (
108
+ '-----BEGIN PRIVATE KEY-----\ntest-key\n-----END PRIVATE KEY-----' ,
109
+ ) ;
110
+ } ) ;
111
+
112
+ it ( 'should handle double-escaped newlines in private key' , async ( ) => {
113
+ // When you have \\n in JavaScript, JSON.stringify converts it to \\\\n
114
+ // But we want to test the case where the JSON string contains \\n (single backslash + n)
115
+ const serviceKeyWithEscapedNewlines = {
116
+ ...mockServiceKey ,
117
+ private_key : '-----BEGIN PRIVATE KEY-----\\ntest-key\\n-----END PRIVATE KEY-----' ,
118
+ } ;
119
+ // This will create a JSON string where the private_key contains literal \n (backslash-n)
120
+ const jsonString = JSON . stringify ( serviceKeyWithEscapedNewlines ) ;
121
+
122
+ const result = await loadServiceKey ( jsonString ) ;
123
+ expect ( result ) . not . toBeNull ( ) ;
124
+ expect ( result ?. private_key ) . toBe (
125
+ '-----BEGIN PRIVATE KEY-----\ntest-key\n-----END PRIVATE KEY-----' ,
126
+ ) ;
127
+ } ) ;
128
+
129
+ it ( 'should handle private key without any newlines' , async ( ) => {
130
+ const serviceKeyWithoutNewlines = {
131
+ ...mockServiceKey ,
132
+ private_key : '-----BEGIN PRIVATE KEY-----test-key-----END PRIVATE KEY-----' ,
133
+ } ;
134
+ const jsonString = JSON . stringify ( serviceKeyWithoutNewlines ) ;
135
+
136
+ const result = await loadServiceKey ( jsonString ) ;
137
+ expect ( result ) . not . toBeNull ( ) ;
138
+ expect ( result ?. private_key ) . toBe (
139
+ '-----BEGIN PRIVATE KEY-----\ntest-key\n-----END PRIVATE KEY-----' ,
140
+ ) ;
141
+ } ) ;
142
+
143
+ it ( 'should not modify private key that already has proper formatting' , async ( ) => {
144
+ const jsonString = JSON . stringify ( mockServiceKey ) ;
145
+
146
+ const result = await loadServiceKey ( jsonString ) ;
147
+ expect ( result ) . not . toBeNull ( ) ;
148
+ expect ( result ?. private_key ) . toBe ( mockServiceKey . private_key ) ;
149
+ } ) ;
150
+
151
+ it ( 'should handle base64 encoded service key' , async ( ) => {
152
+ const jsonString = JSON . stringify ( mockServiceKey ) ;
153
+ const base64Encoded = Buffer . from ( jsonString ) . toString ( 'base64' ) ;
154
+
155
+ const result = await loadServiceKey ( base64Encoded ) ;
156
+ expect ( result ) . not . toBeNull ( ) ;
157
+ expect ( result ) . toEqual ( mockServiceKey ) ;
158
+ } ) ;
159
+
160
+ it ( 'should handle base64 encoded service key with escaped newlines' , async ( ) => {
161
+ const serviceKeyWithEscapedNewlines = {
162
+ ...mockServiceKey ,
163
+ private_key : '-----BEGIN PRIVATE KEY-----\\ntest-key\\n-----END PRIVATE KEY-----' ,
164
+ } ;
165
+ const jsonString = JSON . stringify ( serviceKeyWithEscapedNewlines ) ;
166
+ const base64Encoded = Buffer . from ( jsonString ) . toString ( 'base64' ) ;
167
+
168
+ const result = await loadServiceKey ( base64Encoded ) ;
169
+ expect ( result ) . not . toBeNull ( ) ;
170
+ expect ( result ?. private_key ) . toBe (
171
+ '-----BEGIN PRIVATE KEY-----\ntest-key\n-----END PRIVATE KEY-----' ,
172
+ ) ;
173
+ } ) ;
174
+
175
+ it ( 'should handle invalid base64 strings gracefully' , async ( ) => {
176
+ // This looks like base64 but isn't valid
177
+ const invalidBase64 = 'SGVsbG8gV29ybGQ=' ; // "Hello World" in base64, not valid JSON
178
+
179
+ const result = await loadServiceKey ( invalidBase64 ) ;
180
+ expect ( result ) . toBeNull ( ) ;
181
+ } ) ;
97
182
} ) ;
0 commit comments