From 40f2b0ea90f608899343895ff48ae3a223e379e0 Mon Sep 17 00:00:00 2001 From: Alexander Cerutti Date: Sun, 3 Oct 2021 18:20:51 +0200 Subject: [PATCH] Locked certificates under a symbol with a setter to validate them first --- src/PKPass.ts | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/src/PKPass.ts b/src/PKPass.ts index 3ad867b..33c0c41 100644 --- a/src/PKPass.ts +++ b/src/PKPass.ts @@ -18,9 +18,10 @@ const importMetadataSymbol = Symbol("import.pass.metadata"); const createManifestSymbol = Symbol("pass.manifest"); const closePassSymbol = Symbol("pass.close"); const passTypeSymbol = Symbol("pass.type"); +const certificatesSymbol = Symbol("pass.certificates"); export default class PKPass extends Bundle { - private certificates: Schemas.CertificatesSchema; + private [certificatesSymbol]: Schemas.CertificatesSchema; private [fieldKeysPoolSymbol] = new Set(); private [propsSymbol]: Schemas.PassProps = {}; private [localizationSymbol]: { @@ -56,7 +57,7 @@ export default class PKPass extends Bundle { if (source instanceof PKPass) { /** Cloning is happening here */ - certificates = source.certificates; + certificates = source[certificatesSymbol]; buffers = {}; const buffersEntries = Object.entries(source[filesSymbol]); @@ -167,10 +168,26 @@ export default class PKPass extends Bundle { ); Object.assign(this[propsSymbol], overridesValidation); - this.certificates = certificates; } + /** + * Allows changing the certificates, if needed. + * They are actually expected to be received in + * the constructor, but they can get overridden + * here for whatever purpose. + * + * @param certs + */ + + public set certificates(certs: Schemas.CertificatesSchema) { + if (!Schemas.isValid(certs, Schemas.CertificatesSchema)) { + throw new TypeError("Cannot set certificates: invalid"); + } + + this[certificatesSymbol] = certs; + } + /** * Allows getting an image of the props * that are composing your pass instance. @@ -585,7 +602,7 @@ export default class PKPass extends Bundle { const signatureBuffer = Signature.create( manifestBuffer, - this.certificates, + this[certificatesSymbol], ); super.addBuffer("signature", signatureBuffer); }