
Cloud Vulnerability DB
A community-led vulnerabilities database
napi_throw_error| Field | Value |
|---|---|
| Package | napi-rs (Rust / Node-API bindings, source on GitHub, pkg:github/napi-rs/napi-rs; affected code in crates/napi) |
| Repository | https://github.com/napi-rs/napi-rs |
| Component | napi::bindgen_runtime::async_iterator — generator_next / generator_return / generator_throw callbacks |
| Vulnerability class | Out-of-bounds read (heap over-read leading to information disclosure into a JS-visible error) |
| CWE | CWE-125 |
| Severity (reporter assessment) | High |
| Verified against | Commit cda88ea649a2e182e0b6f7ec9cb8a6db7758b47d only. Other commits and released versions were not verified. |
| API surface | Public documented API: a class implementing napi::bindgen_prelude::AsyncGenerator, exposed through the napi-rs iterator attribute (the verified reproduction used #[napi(async_iterator)]) |
| Affected in default configuration | Yes. Any exported AsyncGenerator with a typed Next/Return conversion is affected. No non-standard usage or configuration is needed. The reproduction enabled the crate features async and napi4. |
On argument-conversion failure, and on promise-spawn failure, the AsyncGenerator callbacks in async_iterator.rs pass format!("{}", e.status).as_ptr(), e.status.as_ref().as_ptr() and e.reason.as_ptr() directly to sys::napi_throw_error. The Node-API declaration of that function (crates/sys/src/functions.rs:323) takes *const c_char NUL-terminated C strings. Rust String and &str buffers are not NUL-terminated, so Node's strlen reads past the end of the heap allocation. The bytes it reads beyond the end become part of the thrown error's message and code.
On the consumer side, any JavaScript caller can trigger this by passing a wrongly-typed argument to next() or return() on an exported async iterator. For example, passing a string where the addon declares type Next = i32 makes FromNapiValue fail before any addon generator logic runs.
Affected throw sites in async_iterator.rs at cda88ea:
generator_next: Next conversion error -> non-NUL status and reason pointers, lines 336-340generator_return: Return conversion error -> non-NUL status and reason pointers, lines 396-400generator_return: promise-spawn error branch -> status.as_ref() (&'static str) and reason String, lines 422-426generator_throw: promise-spawn error branch -> same pattern, lines 497-501
For comparison, direct throws elsewhere in the crate that are C-string-safe pass C-string literals. (Specific sibling line references were not collected.) The synchronous iterator support has a similar direct-throw pattern. That is out of scope here: async_iterator.rs has its own direct throw calls and needs its own fix.A remote JavaScript caller (any code that can call the iterator's methods) causes a native heap out-of-bounds read inside napi_throw_error. In the verified reproduction, adjacent heap bytes (glibc malloc chunk metadata) showed up in the JS-visible e.message. There was also a second over-read while setting e.code. If the read crosses into an unmapped page, the Node process can crash.
Without this bug, an invalid argument gives a bounded JavaScript exception or rejected promise whose text matches the Rust error. With it, the same routine invalid call reads native memory past the string's length and exposes it to JavaScript. What gets disclosed depends on the allocator and the heap layout.
Scoring note: we did not compute a CVSS vector. Exploitability, and the value of the disclosed bytes, depend on whether the consuming addon exposes an AsyncGenerator to untrusted JavaScript input and on heap layout. Addons that do not implement AsyncGenerator, or whose async iterators are never reachable with attacker-chosen arguments, are not affected by this path.
napi::bindgen_prelude::AsyncGenerator through the public napi-rs iterator support.Next and/or Return (for example type Next = i32). This is the standard, documented usage pattern, not a non-default option.next() or return() on the iterator with an arbitrary JavaScript value, such as a string where a number is expected.T::Next::from_napi_value / T::Return::from_napi_value returns Err (the reproduction got status NumberExpected). Alternatively, the spawn-failure branches in generator_return / generator_throw are reached.
No non-standard usage, monkey-patching or unusual configuration is needed. AsyncGenerator is a public bindgen iterator surface. We found no documentation saying callers must pass only trusted, valid JS input, or permitting native C-string over-reads.argv[0] (async_iterator.rs:332, async_iterator.rs:392).T::Next::from_napi_value / T::Return::from_napi_value rejects the wrong-typed value and returns a napi::Error whose status and reason are Rust strings (async_iterator.rs:332-334, async_iterator.rs:392-394).sys::napi_throw_error(env, format!("{}", e.status).as_ptr().cast(), e.reason.as_ptr().cast()) (async_iterator.rs:336-340, async_iterator.rs:396-400). The spawn-error variants using e.status.as_ref().as_ptr() are at async_iterator.rs:422-426 and async_iterator.rs:497-501. The callee expects *const c_char (functions.rs:323).it[Symbol.asyncIterator]().next(x) / .return(x) or await it.next(x) (async_iterator.rs:332-501).crates/napi/src/bindgen_runtime/async_iterator.rs, conversion-error branch (generator_next, around lines 336-340; the same shape is at 396-400) at cda88ea649a2e182e0b6f7ec9cb8a6db7758b47d:
Err(e) => {
unsafe {
sys::napi_throw_error(
env,
format!("{}", e.status).as_ptr().cast(), // SINK: String bytes, no NUL terminator
e.reason.as_ptr().cast(), // SINK: String bytes, no NUL terminator
)
};
None
}The spawn-error branches at lines 422-426 and 497-501 follow the same pattern, passing e.status.as_ref().as_ptr() (a &'static str, also not NUL-terminated) in place of the format! result.
Verified dynamically at commit cda88ea649a2e182e0b6f7ec9cb8a6db7758b47d with Node 24.22.1 on Linux/glibc, using a minimal cdylib addon built against crates/napi (features async, napi4).
// Minimal addon: an AsyncGenerator with a typed Next.
// Trait signature taken from the finding; exact form may need adjusting to the commit under test.
use napi::bindgen_prelude::*;
use napi_derive::napi;
#[napi(async_iterator)]
pub struct AsyncNumbers;
#[napi]
impl AsyncGenerator for AsyncNumbers {
type Yield = u32;
type Next = i32;
type Return = ();
fn next(&mut self, _: Option<i32>) -> impl std::future::Future<Output = Option<u32>> + Send {
async { Some(1) }
}
}// Trigger: pass a wrongly-typed value to next().
const addon = require('./index.node');
(async () => {
const it = new addon.AsyncNumbers()[Symbol.asyncIterator]();
try { await it.next('x'); } catch (e) { console.log(e.code, e.message, e.message.length); }
})();
// Run under: valgrind --tool=memcheck node repro.jsObserved:
e.message was 57 characters long, while the Rust reason string was 56 bytes. The extra trailing byte was 0x21 / 0x51 (glibc malloc chunk-size metadata of the adjacent chunk): heap bytes leaked into the JS-visible error.Invalid read of size 1 at strlen by napi_throw_error <- generator_next_fn (async_iterator.rs:336) ... Address is 0 bytes after a block of size 56 alloc'd (the reason String).set_error_code: 0 bytes after a block of size 14 alloc'd (the format!-ed status code NumberExpected).
The generator_return conversion path and the spawn-error branches have the same code pattern by inspection. Only the generator_next conversion path was exercised dynamically.message and code exactly match the Rust reason and status, with no native reads past the string lengths.strlen reads past the end of both heap buffers, and the bytes it over-reads appear in the thrown error's message.Never pass a Rust String/&str pointer where Node-API expects a C string. Convert to an owned CString (handling embedded NULs), or use the crate's existing length-bounded JS error construction.
Err(e) => {
// Strip interior NULs so CString::new cannot fail, then pass NUL-terminated buffers.
let code = std::ffi::CString::new(format!("{}", e.status).replace('\0', ""))
.expect("NULs removed");
let msg = std::ffi::CString::new(e.reason.replace('\0', ""))
.expect("NULs removed");
unsafe { sys::napi_throw_error(env, code.as_ptr(), msg.as_ptr()) };
None
}A second form, matching the crate's own idiom, is to turn the napi::Error into a JS error through the crate's existing error-to-JS conversion, which builds JS strings with explicit lengths. That avoids C strings entirely. (We have not verified the exact helper name at this commit.)
Notes:
e.status.as_ref() is a &'static str and is equally unterminated.napi::Error as a JS error") would keep the four branches consistent.type Next = i32, call next('x'), and assert that e.code === 'NumberExpected' and that e.message equals the expected Rust reason exactly (same length, no trailing bytes). Also run the case under valgrind or ASan and assert there are no invalid reads. Add the equivalent case for return('x').main (fb1c6d46). Fix merged in commit 95ee412973f32a45a24211b64df059b83fc5c7b6 ("Merge commit from fork", 2026-11-22) and shipped in napi 3.16.3 (2026-11-23). All 7 raw sys::napi_throw_error sites in async_iterator.rs and iterator.rs now route through JsError::from(e).throw_into(env) (length-bounded napi_create_string_utf8), conversion and set_generator_value error paths return early via Result<sys::napi_value>, and pending exception identity is preserved while closing [[GeneratorState]] in generator_throw_fn.AsyncGenerator (crates/napi/src/bindgen_runtime/async_iterator.rs, 4 sites): napi >= 3.0.0-alpha.25, <= 3.16.2 (introduced in 98cb7671 / PR #2418).Generator / ScopedGenerator (crates/napi/src/bindgen_runtime/iterator.rs, 3 identical sibling sites): napi >= 2.4.0, <= 3.16.2 (introduced in a3356264 / PR #1166).sys::napi_throw_error calls across both async_iterator.rs and iterator.rs with JsError::from(e).throw_into(env) (length-bounded napi_create_string_utf8, avoiding heap / .rodata over-reads, 0x1 empty-reason SIGSEGV, and interior-NUL truncation).set_generator_value error paths in generator_next / generator_return / generator_throw to return early via Result<sys::napi_value> instead of falling through to invoke g.next(None) or Node-API with a pending exception.[[GeneratorState]] in iterator.rs::generator_throw_fn.Source: NVD
Free Vulnerability Assessment
Evaluate your cloud security practices across 9 security domains to benchmark your risk level and identify gaps in your defenses.
Get a personalized demo
"Best User Experience I have ever seen, provides full visibility to cloud workloads."
"Wiz provides a single pane of glass to see what is going on in our cloud environments."
"We know that if Wiz identifies something as critical, it actually is."