From 84884956ca88d01163193da1d9703b206b91658d Mon Sep 17 00:00:00 2001 From: Vishal Habib Date: Fri, 13 Nov 2026 13:49:02 -0500 Subject: [PATCH] Use the global fetch for OCSP requests checkOCSPStatus relied on two node-fetch v2-only APIs: the timeout option and response.buffer(). On runtimes where node-fetch resolves to a polyfill over the platform fetch (Cloudflare Workers with nodejs_compat), the OCSP request fails before it is sent, so online checks always return RETRYABLE_VERIFICATION_FAILURE. Use the global fetch with AbortSignal.timeout(30000) and read the body with arrayBuffer(). Behavior on Node is unchanged; the API client still uses node-fetch. Fixes #476 --- jws_verification.ts | 9 +++------ tests/unit-tests/jws_verification.test.ts | 10 ++++++---- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/jws_verification.ts b/jws_verification.ts index ad706aa..15174fa 100644 --- a/jws_verification.ts +++ b/jws_verification.ts @@ -5,7 +5,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'; import { ResponseBodyV2DecodedPayload, ResponseBodyV2DecodedPayloadValidator } from './models/ResponseBodyV2DecodedPayload'; @@ -313,16 +312,14 @@ export class SignedDataVerifier { throw new VerificationException(VerificationStatus.INVALID_CERTIFICATE) } const request = new KJUR.asn1.ocsp.OCSPRequest({reqList: [{issuerCert: issuer.toString(), subjectCert: cert.toString() , alg: "sha256"}]}) - const headers = new Headers() - headers.append('Content-Type', 'application/ocsp-request') let response try { response = await fetch(matchResult[1], { - headers: headers, + headers: { 'Content-Type': 'application/ocsp-request' }, method: 'POST', body: Buffer.from(request.getEncodedHex(), 'hex'), - timeout: 30000 + signal: AbortSignal.timeout(30000) }) if (!response.ok) { @@ -336,7 +333,7 @@ export class SignedDataVerifier { throw new VerificationException(VerificationStatus.RETRYABLE_VERIFICATION_FAILURE, error instanceof Error ? error : undefined) } - const responseBuffer = await response.buffer() + const responseBuffer = Buffer.from(await response.arrayBuffer()) const parsedResponse = new (KJUR.asn1.ocsp as any).OCSPParser().getOCSPResponse(responseBuffer.toString('hex')) // The issuer could also be the signer const jsrassignX509Issuer = new X509() diff --git a/tests/unit-tests/jws_verification.test.ts b/tests/unit-tests/jws_verification.test.ts index 6fa65c4..07f0e33 100644 --- a/tests/unit-tests/jws_verification.test.ts +++ b/tests/unit-tests/jws_verification.test.ts @@ -2,10 +2,8 @@ import assert = require("assert"); import jsonwebtoken = require('jsonwebtoken'); -import nodeFetch = require('node-fetch'); import { KeyObject, X509Certificate, createHash, createPublicKey, generateKeyPairSync } from "crypto"; import { KJUR, X509 } from "jsrsasign"; -import { Response } from "node-fetch"; import { SignedDataVerifier, VerificationException, VerificationStatus } from "../../jws_verification"; import { Environment } from "../../models/Environment"; import { readFile, readBytes, getSignedPayloadVerifier, getSignedPayloadVerifierWithDefaultAppAppleId, getDefaultSignedPayloadVerifier } from "../util"; @@ -623,7 +621,7 @@ describe("OCSP Checks", () => { } function mockOCSPResponse(body: Buffer, status: number = 200) { - return jest.spyOn(nodeFetch, 'default').mockImplementation(() => Promise.resolve(new Response(body, { status: status }))) + return jest.spyOn(global, 'fetch').mockImplementation(() => Promise.resolve(new Response(new Uint8Array(body), { status: status }))) } async function expectOCSPFailure(status: VerificationStatus) { @@ -645,6 +643,10 @@ describe("OCSP Checks", () => { await getVerifier().testCheckOCSPStatus(leaf, intermediate) expect(spy).toHaveBeenCalledTimes(1) expect(spy.mock.calls[0][0]).toBe(OCSP_URL) + const init = spy.mock.calls[0][1] as RequestInit + expect(init.method).toBe('POST') + expect(init.headers).toEqual({ 'Content-Type': 'application/ocsp-request' }) + expect(init.signal).toBeInstanceOf(AbortSignal) }) it('should accept a good OCSP response signed directly by the issuer', async () => { @@ -721,7 +723,7 @@ describe("OCSP Checks", () => { }) it('should return a retryable failure when the OCSP responder cannot be reached', async () => { - jest.spyOn(nodeFetch, 'default').mockImplementation(() => Promise.reject(new Error("Network error"))) + jest.spyOn(global, 'fetch').mockImplementation(() => Promise.reject(new Error("Network error"))) await expectOCSPFailure(VerificationStatus.RETRYABLE_VERIFICATION_FAILURE) }) });