Skip to content

Validating the private key string #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"dependencies": {
"@types/jsonwebtoken": "^7.1.33",
"faye-websocket": "0.9.3",
"jsonwebtoken": "7.1.9"
"jsonwebtoken": "7.1.9",
"node-forge": "0.7.1"
},
"devDependencies": {
"@types/chai": "^3.4.34",
Expand Down
9 changes: 9 additions & 0 deletions src/auth/credential.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

import * as jwt from 'jsonwebtoken';
import * as forge from 'node-forge';

// Use untyped import syntax for Node built-ins
import fs = require('fs');
Expand Down Expand Up @@ -167,6 +168,14 @@ export class Certificate {
if (typeof errorMessage !== 'undefined') {
throw new FirebaseAppError(AppErrorCodes.INVALID_CREDENTIAL, errorMessage);
}

try {
forge.pki.privateKeyFromPem(this.privateKey);
} catch (error) {
throw new FirebaseAppError(
AppErrorCodes.INVALID_CREDENTIAL,
'Failed to parse private key: ' + error);
}
}
}

Expand Down
10 changes: 9 additions & 1 deletion test/unit/auth/credential.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ describe('Credential', () => {
}).to.throw('Certificate object must contain a string "client_email" property');
});

it('should throw if certificate object does not contain a valid "private_key"', () => {
it('should throw if certificate object does not contain a "private_key"', () => {
mockCertificateObject.private_key = '';

expect(() => {
Expand All @@ -185,6 +185,14 @@ describe('Credential', () => {
}).to.throw('Certificate object must contain a string "private_key" property');
});

it('should throw if certificate object does not contain a valid "private_key"', () => {
mockCertificateObject.private_key = 'invalid.key';

expect(() => {
return new Certificate(mockCertificateObject);
}).to.throw('Failed to parse private key: Error: Invalid PEM formatted message.');
});

it('should not throw given a valid certificate object', () => {
expect(() => {
return new Certificate(mockCertificateObject);
Expand Down