Skip to content
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

tls: fix empty issuer/subject/infoAccess parsing #14473

Closed
wants to merge 1 commit into from
Closed
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
8 changes: 4 additions & 4 deletions lib/_tls_common.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,12 @@ exports.translatePeerCertificate = function translatePeerCertificate(c) {
if (!c)
return null;

if (c.issuer) c.issuer = tls.parseCertString(c.issuer);
if (c.issuerCertificate && c.issuerCertificate !== c) {
if (c.issuer != null) c.issuer = tls.parseCertString(c.issuer);
if (c.issuerCertificate != null && c.issuerCertificate !== c) {
c.issuerCertificate = translatePeerCertificate(c.issuerCertificate);
}
if (c.subject) c.subject = tls.parseCertString(c.subject);
if (c.infoAccess) {
if (c.subject != null) c.subject = tls.parseCertString(c.subject);
if (c.infoAccess != null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you put a quick comment that != null  matches both null and undefined? I think that might not be newbie-friendly (also because in every guide about JS it is written to use ===).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll add a comment if you insist but we use the x != null idiom all over the code base. It's not unique to this PR and we don't call it out explicitly anywhere else.

var info = c.infoAccess;
c.infoAccess = {};

Expand Down
55 changes: 55 additions & 0 deletions test/parallel/test-tls-translate-peer-certificate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
'use strict';
const common = require('../common');

if (!common.hasCrypto)
common.skip('missing crypto');

const { strictEqual, deepStrictEqual } = require('assert');
const { translatePeerCertificate } = require('_tls_common');

const certString = 'A=1\nB=2\nC=3';
const certObject = { A: '1', B: '2', C: '3' };

strictEqual(translatePeerCertificate(null), null);
strictEqual(translatePeerCertificate(undefined), null);

strictEqual(translatePeerCertificate(0), null);
strictEqual(translatePeerCertificate(1), 1);

deepStrictEqual(translatePeerCertificate({}), {});

deepStrictEqual(translatePeerCertificate({ issuer: '' }),
{ issuer: {} });
deepStrictEqual(translatePeerCertificate({ issuer: null }),
{ issuer: null });
deepStrictEqual(translatePeerCertificate({ issuer: certString }),
{ issuer: certObject });

deepStrictEqual(translatePeerCertificate({ subject: '' }),
{ subject: {} });
deepStrictEqual(translatePeerCertificate({ subject: null }),
{ subject: null });
deepStrictEqual(translatePeerCertificate({ subject: certString }),
{ subject: certObject });

deepStrictEqual(translatePeerCertificate({ issuerCertificate: '' }),
{ issuerCertificate: null });
deepStrictEqual(translatePeerCertificate({ issuerCertificate: null }),
{ issuerCertificate: null });
deepStrictEqual(
translatePeerCertificate({ issuerCertificate: { subject: certString } }),
{ issuerCertificate: { subject: certObject } });

{
const cert = {};
cert.issuerCertificate = cert;
deepStrictEqual(translatePeerCertificate(cert), { issuerCertificate: cert });
}

deepStrictEqual(translatePeerCertificate({ infoAccess: '' }),
{ infoAccess: {} });
deepStrictEqual(translatePeerCertificate({ infoAccess: null }),
{ infoAccess: null });
deepStrictEqual(
translatePeerCertificate({ infoAccess: 'OCSP - URI:file:///etc/passwd' }),
{ infoAccess: { 'OCSP - URI': ['file:///etc/passwd'] } });