Skip to content

fix(schema): recognize cross-realm arrays with Array.isArray - #1646

Merged
ssalbdivad merged 4 commits into
arktypeio:mainfrom
edenbuilds:fix/cross-realm-array-isarray
Sep 17, 2026
Merged

ssalbdivad merged 4 commits into
arktypeio:mainfrom
edenbuilds:fix/cross-realm-array-isarray

Conversation

@edenbuilds

Copy link
Copy Markdown
Contributor

Summary

  • ProtoNode validated arrays with instanceof Array (and the same in the JIT compiledCondition), which fails for arrays from another window/vm realm.
  • Use Array.isArray for the Array proto; early-return "Array" from objectKindOf the same way so error text stays accurate.

Fixes #1597

Test plan

  • Runtime probe: vm foreign [1,2,3] → allows false on main, true with fix; T(foreign) returns [1,2,3]
  • Mocha: accepts arrays from another realm in ark/type/__tests__/arrays/array.test.ts
  • Confirmed the new test fails when proto.ts / objectKinds.ts are stashed

Made with Cursor

ProtoNode and objectKindOf used instanceof Array, which rejects arrays
from other windows/vm contexts. Use Array.isArray (and the compiled
equivalent) so number[] accepts foreign-realm arrays (fixes arktypeio#1597).

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Important

The fix itself is correct, but the newly-added test fails as written, so the suite is red. attest(T(foreign)).equals([1, 2, 3]) at array.test.ts:294 throws Objects did not have the same constructor because attest's equals rejects mismatched constructors, and a cross-realm array's constructor differs from a local [1, 2, 3] literal.

Reviewed changes

  • proto.ts — ProtoNode now routes the Array proto through Array.isArray in both traverseAllows and compiledCondition, so arrays from other realms validate.
  • objectKinds.ts — objectKindOf early-returns "Array" for any Array.isArray value, keeping kind/error descriptions accurate across realms.
  • array.test.ts — adds a vm-realm cross-realm array test.

I verified the runtime behavior directly: with the fix, type("number[]").allows returns true for a vm-realm [1, 2, 3] (and reverting proto.ts to instanceof Array breaks it), so the fix is sound. The only problem is the test's final assertion, which fails regardless of the fix and must be corrected before this can merge.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using DeepSeek Flash (free via Pullfrog for OSS) | 𝕏

Comment thread ark/type/__tests__/arrays/array.test.ts Resolved
attest().equals treats foreign-realm arrays as unequal to literals
because their constructors differ. Assert identity + element values.
Unblocks arktype CI prChecks (prettier --check on proto.ts).
@edenbuilds

Copy link
Copy Markdown
Contributor Author

@pullfrog addressed: the test no longer uses `attest(...).equals` against a foreign-realm array (constructor identity fails). It asserts identity + element values instead. CI `core` / `prChecks` / compatibility are green on the latest head.

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No new issues found.

Confirmed the fix: array.test.ts no longer calls attest(...).equals against the foreign-realm array (whose constructor differs, which attest treats as unequal). It now asserts realm-safe facts — Array.isArray(foreign) is true, T.allows(foreign) is true, T(foreign) returns the same object (result === foreign) — and compares element values via a local spread [...(result as number[])].

pnpm mocha ark/type/__tests__/arrays/array.test.ts passes accepts arrays from another realm. (The benchTemplate/snapTemplate module-not-found output in that run comes from the ark/attest dev build not being present locally and is unrelated to this PR.) The earlier review blocker is resolved.

Pullfrog  | View workflow run | Using DeepSeek Flash (free via Pullfrog for OSS) | 𝕏

@ssalbdivad

Copy link
Copy Markdown
Member

@pullfrog based on the intent of this fix, are there other prototypes with similar built-in methods for validating instances cross-realm that should also be included as part of this for consistency?

@pullfrog

pullfrog Bot commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

Good question. I dug through the builtin constructors (ecmascriptConstructors + platformConstructors + typedArrayConstructors in ark/util/objectKinds.ts) and checked how each one is validated. The only prototype with a directly analogous, dedicated realm-safe static predicate is Array itself (Array.isArray), which this PR already covers.

Here's the breakdown:

Other prototypes with a built-in realm-safe check:

  • Typed arrays — ArrayBuffer.isView(value) is realm-safe and matches all 11 typed array kinds. Caveat: it also returns true for DataView, and it can't distinguish one typed-array kind from another (e.g. Int8Array vs Float64Array), so it can't cleanly substitute for per-kind instanceof Int8Array. For per-kind cross-realm validation you'd need the Object.prototype.toString.call brand check ([object Int8Array]).
  • Function — typeof value === "function" is realm-safe (functions from any realm report "function"), so it's a correct drop-in for value instanceof Function.

Prototypes with no dedicated static predicate (Date, Map, Set, WeakMap, WeakSet, RegExp, Promise, Error, and boxed String/Number/Boolean): there is no Array.isArray-style method. The only realm-safe brand check for these is Object.prototype.toString.call(value) (e.g. [object Date], [object Map]), which the codebase currently doesn't use anywhere (I searched — no Object.prototype.toString/toString.call usage).

One consistency gap worth flagging: the objectKindOf while-loop in objectKinds.ts still resolves array-adjacent kinds via data instanceof builtinConstructors[name]. The Array.isArray early-return patches the Array case, but the same cross-realm bug remains for every other builtin — e.g. objectKindOf on a foreign-realm Date/Map/Set walks past its prototype and returns undefined, so error text falls back to "object" instead of "a Date"/"a Map". Same for ProtoNode.traverseAllows/compiledCondition for those protos.

So my recommendation, if we want consistency rather than a one-off:

  • Cheap, targeted follow-ups with built-in predicates: Function (typeof) and typed arrays (ArrayBuffer.isView, accepting its coarse granularity).
  • The fully consistent approach is a single realm-safe brand-check helper built on Object.prototype.toString.call, used by both objectKindOf and ProtoNode.traverseAllows/compiledCondition. That fixes every builtin in one place instead of special-casing prototypes one at a time — though it's a broader refactor than this narrow fix.

I'd lean toward the general Object.prototype.toString.call helper (it subsumes the Array fix too, and Array.isArray remains the most efficient for arrays specifically). Happy to sketch it if you want to take it further.

Pullfrog  | View workflow run | via Pullfrog | Using DeepSeek Flash (free via Pullfrog for OSS) | 𝕏

@ssalbdivad

Copy link
Copy Markdown
Member

@pullfrog implement an alternate version of this that universally uses the Object.prototype.toString.call... I assume this can't be tricked by just naming a particular prototype directly and it will only return true for the built in?

If so, build this version on another branch and simplify as much as possible so instanceof works consistently across all builtins cross-realm.

@pullfrog

pullfrog Bot commented Sep 9, 2026 •

Copy link
Copy Markdown
Contributor

Run failed. View the logs →

Pullfrog  | Rerun failed job ➔ | View workflow run | via Pullfrog | Using DeepSeek Flash (free via Pullfrog for OSS) | 𝕏

@edenbuilds

Copy link
Copy Markdown
Contributor Author

Re: the Object.prototype.toString.call alternative — worth flagging before that's implemented: it's actually less safe here, not more. toString.call is fooled by a spoofed Symbol.toStringTag, while Array.isArray isn't:

const spoofed = { [Symbol.toStringTag]: "Array", length: 3 };
Object.prototype.toString.call(spoofed); // "[object Array]" — false positive
Array.isArray(spoofed);                  // false — correct

const foreign = vm.runInContext("[1,2,3]", vm.createContext());
Object.prototype.toString.call(foreign); // "[object Array]" — correct
Array.isArray(foreign);                  // true — also correct

Array.isArray is defined by spec to test the internal array exotic-object slot directly (unaffected by Symbol.toStringTag or prototype tricks), which is exactly why it's the standard cross-realm-safe check and is already what this PR uses for both traverseAllows and compiledCondition. Switching to toString.call would reintroduce the toStringTag-spoofing gap this PR's own test (spoofed toStringTag array case) is meant to catch.

Separately: the pullfrog self-review run timed out after 1h mid-investigation (agent run timed out after 1h, then a 401 on posting results) rather than reporting an actual finding — all three real CI jobs (core, compatibility, prChecks) are green.

@ssalbdivad

Copy link
Copy Markdown
Member

@edenbuilds I figured that may be the case and probably should have googled it, just trying to inflate pullfrog usage numbers as per usual 😆

I'm not sure about this. I guess it makes sense to give arrays some special status as they are uniquely represented as string[] in the type system, but it feels a bit unintuitive to have a built-in workaround for realms for arrays specifically but it would fail for every other instanceof category by default like Date without much recourse.

@edenbuilds

Copy link
Copy Markdown
Contributor Author

Fair question. The reason Array gets special-cased rather than a general `instanceof`-is-unsafe-across-realms fix: it's not just Array that's spoofable via `Symbol.toStringTag` — that trick works on any object, including a real `Date` (`realDate[Symbol.toStringTag] = "X"` makes `Object.prototype.toString.call(realDate)` return `"[object X]"` too, spec-wise the tag lookup happens before the internal-slot fallback). So there's no `toString.call`-based fix for the general case either — it has the same weakness for every built-in, not just Array.

What actually differs for Array is that the language shipped a dedicated, unspoofable primitive for exactly this problem (`Array.isArray`, added specifically because `instanceof Array` breaks across iframes/vm/workers), and nothing equivalent exists for Date/Map/Error/etc. Node has `util.types.isDate()` / `isMap()` / `isNativeError()` that do it correctly, but that's Node-only — no browser/Deno-portable equivalent, so it'd conflict with arktype being runtime-agnostic. That's why I scoped this to the one category the platform actually gives you a portable fix for, rather than reaching for `toString.call` everywhere and getting the same problem back through a different door.

If cross-realm `Date`/`Map`/etc. is something you want covered too, happy to look at what's feasible (probably an opt-in Node-only path, or documenting it as a known limitation) — but wanted to flag that it's a materially different problem, not a copy-paste of this fix, so I'd treat it as a separate issue/PR rather than scope creep on this one.

@ssalbdivad

Copy link
Copy Markdown
Member

@edenbuilds this makes sense, I buy the existence of a language feature being sufficient justification for having this special-cased.

@pullfrog add a note to the documentation for instanceof that Array is the only constructor that will work cross-realm.

@pullfrog

pullfrog Bot commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor

Added the requested note to the instanceof docs: constructors are compared by identity, so Array (special-cased via Array.isArray) is the only built-in constructor that matches values from another realm. Mirrored the change into the generated llms.txt and pushed to the existing PR (#1646).

Pullfrog  | View workflow run | via Pullfrog | Using DeepSeek Flash (free via Pullfrog for OSS) | 𝕏

@ssalbdivad
ssalbdivad merged commit a046ebe into arktypeio:main Sep 17, 2026
6 checks passed
@github-project-automation github-project-automation Bot moved this from To do to Done (merged or closed) in arktypeio Sep 17, 2026
@ssalbdivad

Copy link
Copy Markdown
Member

@edenbuilds thanks again for your help :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done (merged or closed)

Development

Successfully merging this pull request may close these issues.

Array from a different window object not recognized as array

2 participants