Skip to content

Validate JavaScript native wrapper identity before Rust access - #175

Merged
leileizhang (lei9444) merged 1 commit into
mainfrom
lei9444-native-wrapper-validation
Sep 20, 2026
Merged

leileizhang (lei9444) merged 1 commit into
mainfrom
lei9444-native-wrapper-validation

Conversation

@lei9444

Copy link
Copy Markdown
Contributor

Summary

Fix native-class identity validation at the shared JavaScript binding boundary, rather than validating collection values after interpreting another class's Rust storage.

  • Pin the published, matched napi 3.12.0, napi-derive 3.6.1, and backend 6.1.0; enable N-API 8 explicitly. The lockfile resolves napi-sys 3.3.2.
  • Apply the supported upstream type_tag attribute to native class declarations with a thin native_class! declaration macro. A deterministic SHA-256 salt covers sorted native binding/core/contract sources, manifests/lockfile, target/features/build configuration and compiler version. Upstream still incorporates the actual Rust module and class identity.
  • Replace prototype-based descriptor/COM carrier checks with native tags. Keep handwritten references inside synchronous, higher-ranked bridge closures, returning owned copies/references where needed.
  • Preserve the separate Win32 carrier tags and ownership/state checks; there is no double stamping, ABI/ownership inference, Python change, DLL-loading change or generated WinRT API change.
  • Add production subprocess regressions and explicitly isolated test-hooks controls, and document the compatibility boundary.

Root cause and dependency choice

The previous generated FromNapiRef/FromNapiMutRef conversions called napi_unwrap and cast the returned pointer without verifying its Rust class. This also affected nested collection parameters, metadata/signature arguments and handwritten conversion bridges. Ordinary JavaScript receiver checks are not an adequate replacement for creation-time native identity.

The selected published pairing includes the complete class-identity implementation from napi-rs/napi-rs#3405, verified against published source and its release commit 58bd87fa524a837a7c962ab4103e5588557ccd81:

Path Protection
Constructors, factories, new_instance / ToNapiValue Stamp the tag for the actual Rust T
Generated shared/mutable references, including nested inputs Check the native tag before creating typed Rust references
ClassInstance, Reference, method and accessor receivers Check the tag before typed conversion/access
Handwritten bridges Use the same checked conversions, with bounded synchronous use

Some upstream paths first retrieve an opaque pointer via napi_unwrap; the tag is checked before its typed Rust cast/reference/dereference.

The backend is pinned separately so its compatible version range cannot select mismatched later conversion glue. Subsequent upstream borrow/unload policy changes reject existing reentrant delegate release/once behavior and require separate compatibility work. This PR adopts the complete class-identity fix, not that separate migration, and does not claim general Rust aliasing or addon-unload safety.

Before / after

The coordinator's retained pre-fix production ARM64 evidence at 34eb6246627c671113a501c00edc8e3317fc8b3d records a genuine owned process resource passed as a vector element terminating with 0xC0000005; valid/null/plain-object and resource-close controls completed normally. The known crash was not rerun.

The production regression obtains an owned resource through the existing Win32 runtime's OpenProcess call for the current process. The same wrong input now raises a JS exception; GetProcessId still succeeds afterward, the resource closes once, and the process exits naturally.

Validation

Executed on Node 24.15.0 native ARM64 unless noted:

  • Production native identity suite: 8 subprocess cases, 51 expected rejections, no skips; includes vector/map keys/values/types, signature/descriptor classes, getters, manual/callback bridges, genuine objects/nulls, public prototype controls, and four worker environments.
  • Existing coupled lifecycle/marshalling/Win32/import suites: 190 passed, 2 existing optional fixture skips. Includes once, both nested/reentrant delegate-release paths, IOCP/worker teardown, and genuine Win32 COM inputs.
  • Generated SDK collection factory roundtrips: 11 passed.
  • All existing TypeScript check groups passed. Final production declaration tokens exactly match the pre-change baseline.
  • Production PE/import gates passed, including all six runtime entrypoints. The retained final addon is a production build, not a test-hooks PE.
  • Isolated test-hooks identity test: 14 rejections, zero rejected calls reaching native bodies, and three layout-identical control payloads finalized exactly once. Covers untagged/other-brand objects, shared/mutable/aliased/owned references, accessors/setters and duplicate tagging cleanup.
  • x64 binding Rust tests, executed under Windows ARM64 emulation: 128 passed, 1 existing ignored, plus 3 branding tests. i686 compile check passed.
  • Core Rust tests: 466 passed, 1 existing ignored, plus the core WinRT regression. Codegen snapshots: 6 passed.

Limits: Node 18 was not installed/executed here; its N-API 8 capability was checked, not represented as runtime execution. The Node >=18 requirement is unchanged; source builds now require Rust >=1.88. Existing full ARM64 raw-union tests remain blocked by their pre-existing by-value union support gate; this PR does not expand that support. Build branding is a conservative compatibility boundary, not a formal cross-library Rust ABI guarantee.

Pin the matched upstream type-tag implementation, brand native classes by deterministic build inputs, and cover collection, descriptor, callback, lifetime, and ownership boundaries.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@github-actions

Copy link
Copy Markdown

Mixed-language test coverage

Workflow status: ✅ Passed

Layer Lines Functions Branches/regions
Rust, including native .pyd/.node 86.06% 81.27% 85.66% regions
Python aggregate 71.06% n/a 37.9% branches
Python runtime 99.12% n/a 97.37% branches
Generated Python WinRT projections 70.05% n/a 27.07% branches
Generated Python WinRT implementations 72.29% n/a 47.04% branches
JavaScript aggregate 22.14% 25.22% 57.85% branches
JavaScript runtime 44.27% 45.76% 78.99% branches
Generated WinRT projections 22.96% 18.72% 54.88% branches
Generated WinRT implementations 48.66% 59.76% 61.14% branches
Generated Classic COM projections 11.92% 23.97% 54.2% branches

View workflow run and download full HTML/LCOV/XML reports

@lei9444
leileizhang (lei9444) merged commit f446adb into main Sep 20, 2026
33 checks passed
@lei9444
leileizhang (lei9444) deleted the lei9444-native-wrapper-validation branch September 20, 2026 08:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants