-
Notifications
You must be signed in to change notification settings - Fork 5k
Fix null deref in forEachProperty when Proxy getPrototype throws #30457
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
robobun
wants to merge
3
commits into
main
from
farm/8b83d80e/fix-foreachproperty-proxy-getprototype
+30
−3
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟣 Heads up: the same vulnerable pattern this PR fixes still exists at
src/jsc/bindings/napi.cpp:1837(current_object->getPrototype(globalObject).getObject()), reachable vianapi_get_all_property_nameswithnapi_key_include_prototypeswhen the chain contains a Proxy with a throwinggetPrototypeOforgetOwnPropertyDescriptortrap. This is pre-existing and not touched by this PR — just flagging since it's the identical root cause and may be worth the same one-line guard as a follow-up.Extended reasoning...
Summary
This PR correctly fixes a null deref in
JSC__JSValue__forEachPropertyImplwhereiterating->getPrototype(globalObject).getObject()is called on a potentially-emptyJSValue. However, the identical pattern remains unfixed atsrc/jsc/bindings/napi.cpp:1837insidenapi_get_all_property_names:There is no exception check or empty-
JSValueguard between thegetOwnPropertyDescriptorcall and the.getObject()call on the next line.How it manifests
As the PR description explains, when
getPrototype()is called on aProxyObjectand thegetPrototypeOftrap throws (or there is already a pending exception, causingProxyObject::performGetPrototypeto early-return{}viaRETURN_IF_EXCEPTION), it returns an emptyJSValue. An emptyJSValuereportsisCell() == truebutasCell() == nullptr, so.getObject()callsJSCell::getObject()onnullptrand segfaults — exactly the crash this PR fixes inbindings.cpp.Why the existing guard at line 1823 doesn't prevent it
allPropertyKeys()at line 1818 also walks the prototype chain, andNAPI_RETURN_IF_EXCEPTION(env)at line 1823 would catch an unconditionally-throwinggetPrototypeOftrap on that first walk. However, the descriptor-filtering loop at lines 1833–1842 walks the chain again per key, and is reachable past line 1823 in at least two ways:getOwnPropertyDescriptortrap. Line 1836 callsgetOwnPropertyDescriptoron the Proxy. If that trap throws, it returnsfalse(loop body entered) and leaves a pending exception. Line 1837 then callsgetPrototype()on the same Proxy with the exception pending → emptyJSValue→ null deref. TheownKeys/getPrototypeOftraps used byallPropertyKeysneed not throw for this path.getPrototypeOftrap. A trap that succeeds duringallPropertyKeysbut throws on a subsequent invocation (e.g. throws on the Nth call) reaches line 1837 directly.Step-by-step proof (path 1)
napi_get_all_property_names(env, obj, napi_key_include_prototypes, napi_key_writable, ...)whereobj's prototype isnew Proxy(target, { getOwnPropertyDescriptor() { throw new Error('x') } }).allPropertyKeyswalks the chain viagetPrototype/ownKeys— nogetOwnPropertyDescriptortrap invoked, no throw. Line 1823 passes.key_filter & filter_by_any_descriptoris truthy (napi_key_writable), so we enter the per-key filter loop.getOwnPropertyDescriptoronobj→ not own → loop iterates,current_objectadvances to the Proxy.getOwnPropertyDescriptoron the Proxy invokes the trap → throws → returnsfalse, pending exception set.current_object->getPrototype(globalObject)on the Proxy with a pending exception returnsJSValue{}..getObject()on the empty value:isCell()→ true,asCell()→nullptr,nullptr->getObject()→ segfault.Impact
A native addon calling
napi_get_all_property_nameswithnapi_key_include_prototypesand any ofnapi_key_enumerable | napi_key_writable | napi_key_configurableon user-supplied objects can be crashed by a hostile Proxy in the prototype chain. Same severity class as the bug this PR fixes (process crash from JS-observable input), just gated behind N-API rather thanBun.inspect.Suggested fix
Apply the same guard the PR uses in
bindings.cpp:(and ideally also check for an exception after
getOwnPropertyDescriptorat line 1836).Severity
Pre-existing. This PR does not touch
napi.cpp, add callers to it, or change its behavior in any way. Flagging only because it is the identical root cause and the fix is the same one-liner — fine to defer to a follow-up.