diff --git a/jsrsasign_loader.ts b/jsrsasign_loader.ts new file mode 100644 index 0000000..27f617d --- /dev/null +++ b/jsrsasign_loader.ts @@ -0,0 +1,12 @@ +// Copyright (c) 2023 Apple Inc. Licensed under MIT License. + +type Jsrsasign = typeof import('jsrsasign') + +let jsrsasign: Jsrsasign | undefined + +export function getJsrsasign(): Jsrsasign { + if (jsrsasign === undefined) { + jsrsasign = require('jsrsasign') as Jsrsasign + } + return jsrsasign +} diff --git a/jws_verification.ts b/jws_verification.ts index ad706aa..85093ed 100644 --- a/jws_verification.ts +++ b/jws_verification.ts @@ -4,7 +4,6 @@ import jsonwebtoken = require('jsonwebtoken'); import base64url from 'base64url'; import { KeyObject, X509Certificate, createHash, verify } from 'crypto'; -import { KJUR, X509, ASN1HEX } from 'jsrsasign'; import fetch, { Headers } from 'node-fetch'; import { Environment } from './models/Environment'; import { JWSTransactionDecodedPayload, JWSTransactionDecodedPayloadValidator } from './models/JWSTransactionDecodedPayload'; @@ -14,6 +13,7 @@ import { DecodedRealtimeRequestBody, DecodedRealtimeRequestBodyValidator } from import { Validator } from './models/Validator'; import { DecodedSignedData } from './models/DecodedSignedData'; import { AppTransaction, AppTransactionValidator } from './models/AppTransaction'; +import { getJsrsasign } from './jsrsasign_loader'; const MAX_SKEW = 60000 @@ -278,6 +278,7 @@ export class SignedDataVerifier { } protected async verifyCertificateChainWithoutCaching(trustedRoots: X509Certificate[], leaf: X509Certificate, intermediate: X509Certificate, effectiveDate: Date): Promise { + const { X509 } = getJsrsasign() let validity = false let rootCert for (const root of trustedRoots) { @@ -307,6 +308,7 @@ export class SignedDataVerifier { return leaf.publicKey } protected async checkOCSPStatus(cert: X509Certificate, issuer: X509Certificate): Promise { + const { ASN1HEX, KJUR, X509 } = getJsrsasign() const authorityRex = /^OCSP - URI:(.*)$/m const matchResult = cert.infoAccess ? authorityRex.exec(cert.infoAccess) : "" if (matchResult === null || matchResult.length !== 2) { @@ -341,7 +343,7 @@ export class SignedDataVerifier { // The issuer could also be the signer const jsrassignX509Issuer = new X509() jsrassignX509Issuer.readCertHex(issuer.raw.toString('hex')) - const allCerts: X509[] = [jsrassignX509Issuer] + const allCerts = [jsrassignX509Issuer] for (const certHex of parsedResponse.certs) { const cert = new X509() cert.readCertHex(certHex) @@ -457,4 +459,4 @@ export class VerificationException extends Error { this.status = status this.cause = cause } -} \ No newline at end of file +} diff --git a/receipt_utility.ts b/receipt_utility.ts index d2df402..4ed09fb 100644 --- a/receipt_utility.ts +++ b/receipt_utility.ts @@ -1,6 +1,6 @@ // Copyright (c) 2023 Apple Inc. Licensed under MIT License. -import { ASN1HEX } from 'jsrsasign'; +import { getJsrsasign } from './jsrsasign_loader' const IN_APP_TYPE_ID = 17; const TRANSACTION_IDENTIFIER_TYPE_ID = 1703; @@ -96,6 +96,7 @@ export class ReceiptUtility { * @throws Error If the receipt is not valid base64, is not a PKCS#7 app receipt, or is otherwise not in the expected format */ extractTransactionIdFromAppReceipt(appReceipt: string): string | null { + const { ASN1HEX } = getJsrsasign() const receipt = decodeBase64(appReceipt).toString('hex') // Xcode receipts use indefinite length encoding, not supported by all parsers // Indefinite length encoding is only entered, but never left during parsing for receipts diff --git a/tests/unit-tests/jsrsasign_loading.test.ts b/tests/unit-tests/jsrsasign_loading.test.ts new file mode 100644 index 0000000..a8605c9 --- /dev/null +++ b/tests/unit-tests/jsrsasign_loading.test.ts @@ -0,0 +1,13 @@ +// Copyright (c) 2023 Apple Inc. Licensed under MIT License. + +describe('jsrsasign loading', () => { + it('does not load jsrsasign while importing the public library', () => { + jest.isolateModules(() => { + jest.doMock('jsrsasign', () => { + throw new Error('jsrsasign was loaded during module initialization') + }, { virtual: true }) + + expect(() => require('../../index')).not.toThrow() + }) + }) +})