Skip to content

@smithy/core version 3.24.0 "ReferenceError: crypto is not defined" in serde UUID v4 fallback on Node 18 #2022

Description

@wpessers

Noticed this in CI of opentelemetry-js-contrib only for node 18:

ReferenceError: crypto is not defined
      at Object.v4 (/home/runner/work/opentelemetry-js-contrib/opentelemetry-js-contrib/node_modules/@smithy/core/dist-cjs/submodules/serde/index.js:445:5)
      at /home/runner/work/opentelemetry-js-contrib/opentelemetry-js-contrib/node_modules/@smithy/core/dist-cjs/submodules/retry/index.js:165:63
      at async /home/runner/work/opentelemetry-js-contrib/opentelemetry-js-contrib/node_modules/@aws-sdk/middleware-sdk-sqs/dist-cjs/index.js:88:18
      at async /home/runner/work/opentelemetry-js-contrib/opentelemetry-js-contrib/node_modules/@aws-sdk/middleware-logger/dist-cjs/index.js:5:26
      at async Context.<anonymous> (test/aws-sdk-v3-sqs.test.ts:70:22)

Did some digging and came to following conclusion:

The v4 uuid generator in serde/uuid/v4.ts has always had a fallback using the global crypto without a guard, but on node it use to be dead code: randomUUID.ts used import * as crypto from "node:crypto" so randomUUID was always defined and v4 always took the fast path. #2009 replaced the node-specific import with a runtime typeof crypto check:

export const randomUUID =
typeof crypto !== "undefined" && typeof crypto.randomUUID === "function"
? (crypto.randomUUID.bind(crypto) as typeof crypto.randomUUID)
: undefined;

So that means the fallback will be hit whenever globalThis.crypto isn't on the global object, which is the case in node 18 (where it sits behind a feature flag --experimental-global-webcrypto). node 18 is currently the package's minimum engine version:

"engines": {
"node": ">=18.0.0"
},

But this breaks every node 18 consumer of any AWS SDK call that needs an idempotency token.

Metadata

Metadata

Assignees

Labels

bugThis issue is a bug.closing-soonThis issue will automatically close in 2 days unless further comments are made.releasedThe change has been released and there is a comment with the release version.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions