mongodb/node-mongodb-native · error · MongoCryptAzureKMSRequestError

Malformed response body - unable to parse int from `expires_

Error message

Malformed response body - unable to parse int from `expires_in` field.

What it means

Thrown when expires_in is present but cannot be converted to a finite number (Number('abc')*1000 yields NaN). The driver multiplies expires_in by 1000 to get milliseconds; a non-numeric value means the token expiry cannot be computed, so caching would be unsafe. Note: the code checks Number.isNaN after multiplication, so non-finite strings ('Infinity') and unparseable values are caught, though a numeric string like '3600' works.

Source

Thrown at src/client-side-encryption/providers/azure.ts:97

  if (status !== 200) {
    throw new MongoCryptAzureKMSRequestError('Unable to complete request.', body);
  }

  if (!body.access_token) {
    throw new MongoCryptAzureKMSRequestError(
      'Malformed response body - missing field `access_token`.'
    );
  }

  if (!body.expires_in) {
    throw new MongoCryptAzureKMSRequestError(
      'Malformed response body - missing field `expires_in`.'
    );
  }

  const expiresInMS = Number(body.expires_in) * 1000;
  if (Number.isNaN(expiresInMS)) {
    throw new MongoCryptAzureKMSRequestError(
      'Malformed response body - unable to parse int from `expires_in` field.'
    );
  }

  return {
    accessToken: body.access_token,
    expiresOnTimestamp: Date.now() + expiresInMS
  };
}

/**
 * @internal
 *
 * exposed for CSFLE
 * [prose test 18](https://github.com/mongodb/specifications/tree/master/source/client-side-encryption/tests#azure-imds-credentials)
 */
export interface AzureKMSRequestOptions {
  headers?: Document;

View on GitHub (pinned to 3366c21a63)

Solutions

  1. Ensure expires_in is a numeric value (seconds) in the token response.
  2. If mocking the endpoint, return expires_in: 3600 not '1h' or an ISO date.
  3. Avoid pointing AzureKMSRequestOptions.url at non-conformant token services.

Example fix

// before: { "access_token": "t", "expires_in": "1h" }
// after:  { "access_token": "t", "expires_in": 3600 }
Defensive patterns

Strategy: validation

Validate before calling

// Validate a numeric expires_in when mocking.
function validExpiresIn(v: unknown): v is number {
  return typeof v === 'number' && Number.isFinite(v) || (typeof v === 'string' && Number.isFinite(Number(v)));
}

Prevention

When it happens

Trigger: In parseResponse() when Number(body.expires_in) * 1000 is NaN; reachable via a mock/custom token endpoint returning a non-numeric expires_in.

Common situations: Mock server returning expires_in as an object or arbitrary string; a token service returning a date string instead of a seconds count; proxy mangling the field.

Related errors


AI-assisted analysis of mongodb/node-mongodb-native@3366c21a63 (2026-08-04). Data as JSON: /data/errors/700a80e06d42a3ee.json. Report an issue: GitHub.