diff --git a/.changeset/fuzzy-onions-smoke.md b/.changeset/fuzzy-onions-smoke.md new file mode 100644 index 00000000000..7cd11e8b9d3 --- /dev/null +++ b/.changeset/fuzzy-onions-smoke.md @@ -0,0 +1,5 @@ +--- +"fast-check": patch +--- + +🐛 Make `stringify` a bit more resilient to poisoning diff --git a/packages/fast-check/src/utils/stringify.ts b/packages/fast-check/src/utils/stringify.ts index 835a5b153c2..df1f8abbf60 100644 --- a/packages/fast-check/src/utils/stringify.ts +++ b/packages/fast-check/src/utils/stringify.ts @@ -7,7 +7,9 @@ import { safePush, safeToISOString, safeToString, + Map, String, + Symbol as StableSymbol, } from './globals'; const safeArrayFrom = Array.from; @@ -249,14 +251,14 @@ export function stringifyInternal( return typeof value === 'string' ? safeJsonStringify(value) : `new String(${safeJsonStringify(value)})`; case '[object Symbol]': { const s = value as unknown as symbol; - if (Symbol.keyFor(s) !== undefined) { - return `Symbol.for(${safeJsonStringify(Symbol.keyFor(s))})`; + if (StableSymbol.keyFor(s) !== undefined) { + return `Symbol.for(${safeJsonStringify(StableSymbol.keyFor(s))})`; } const desc = getSymbolDescription(s); if (desc === null) { return 'Symbol()'; } - const knownSymbol = desc.startsWith('Symbol.') && (Symbol as any)[desc.substring(7)]; + const knownSymbol = desc.startsWith('Symbol.') && (StableSymbol as any)[desc.substring(7)]; return s === knownSymbol ? desc : `Symbol(${safeJsonStringify(desc)})`; } case '[object Promise]': { @@ -353,7 +355,7 @@ export function stringify(value: Ts): string { * @internal */ export function possiblyAsyncStringify(value: Ts): string | Promise { - const stillPendingMarker = Symbol(); + const stillPendingMarker = StableSymbol(); const pendingPromisesForCache: Promise[] = []; const cache = new Map();