Selaa lähdekoodia

PROD-1221: Use encodeURIComponent, extract magic number into constant

Blake Schneider 5 vuotta sitten
vanhempi
commit
5317b1f0c9
1 muutettua tiedostoa jossa 6 lisäystä ja 3 poistoa
  1. 6 3
      modules/certificate.js

+ 6 - 3
modules/certificate.js

@@ -1,8 +1,9 @@
 'use strict';
 
-const { unescape } = require('querystring');
 const { pki, md, asn1 } = require('node-forge');
 
+const NUMBER_OF_CHARACTERS_IN_10_COLON_DELIMITED_BYTES = 29;
+
 /*
   escapedPemCertificate comes from nginx, it's the client cert in PEM format.
   This function calculates the 10-byte fingerprint required by OpenADR.
@@ -11,7 +12,7 @@ const { pki, md, asn1 } = require('node-forge');
 function calculatePartialFingerprintOfEscapedPemCertificate(
   escapedPemCertificate,
 ) {
-  const pemCertificate = unescape(escapedPemCertificate);
+  const pemCertificate = decodeURIComponent(escapedPemCertificate);
   const parsedCertificate = pki.certificateFromPem(pemCertificate);
   const asn1Encoded = pki.certificateToAsn1(parsedCertificate);
   const derEncoded = asn1.toDer(asn1Encoded).getBytes();
@@ -22,7 +23,9 @@ function calculatePartialFingerprintOfEscapedPemCertificate(
     .toHex()
     .match(/.{2}/g)
     .join(':');
-  return fullFingerprintDelimited.slice(-29).toUpperCase();
+  return fullFingerprintDelimited
+    .slice(-NUMBER_OF_CHARACTERS_IN_10_COLON_DELIMITED_BYTES)
+    .toUpperCase();
 }
 
 module.exports = {