You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
contextBridge.exposeInMainWorld does not copy Error details #25596
I agree to follow the Code of Conduct that this project adheres to.
I have searched the issue tracker for an issue that matches the one I want to file, without success.
Issue Details
When calling functions exposed by contextBridge.exposeInMainWorld, Promises rejected with Error objects or directly thrown Errors (and even caught Errors returned in a wrapper) are not copied as the documentation states – all important information except the message is removed, e.g. Common System Error properties like err.code, err.path etc. are gone. Also, the “Uncaught Error: ” message prefix is not very helpful.
Electron Version:
10.1.2
Operating System:
Windows 10 (2004)
Expected Behavior
Instances of the Error class (or inherited from it) coming from the context bridge should have all properties preserved (except err.stack, obviously) and message left intact to allow meaningful error handling. Structured Clone Algorithm otherwise serializes Error objects correctly.
Actual Behavior
The Error object is stripped of important additional properties and has a confusing message saying it’s uncaught even when caught on both sides of the bridge.
@richardfritsch We follow the same strategy for cross-boundarysanitization as chrome. If you try and dispatchEvent with a custom Event object with custom properties those properties will be lost when accessing that event in another world. Errors are tricky because we as a framework don't know what could be in the custom properties on Error objects and don't want to let users shoot themselves in the foot by setting custom properties on error objects that then get leaked into the main world.
IMO in your case of fs usage the renderer should never know why the request failed, just that for some reason it did. Passing details like error code and in particular the filesystem path is counter to the goals of isolated the untrusted renderer from that kind of information.
Thanks for having a look at it. In my case, knowing why a request failed is needed - the app works with files, folders and lockfiles often located on shared network drives etc. where permission and accessibility errors occur – the app will retry or inform the user based on the error details. I understand the security concerns, it’d just be nice if I didn’t have to restructure the code to return the error details inside a plain object and could just catch an exception instead. An option for exposeInMainWorld to allow Errors to pass would be handy.
Reacted by Ondrej Medek, Mikael Finstad, TanShun and Jan Schmutz
I ran into the same problem, errors convey meaning here, including their extra properties (and I'm using instanceof on errors as well). And of course a stack would be nice...
It would be great if this could be the choice of the developer, yes.
In this particular case, I'm using the preload code as sort of a "middleware" which, if existing (i.e., app is run inside Electron and not browser) is called by the frontend with a callback, so the middleware can intercept or inject functionality if required. But, if the inner code (passed as callback) is throwing a certain error, it's expected to stay that error.
Result is :-( if run with window.electron existing, otherwise works. I'd like it to be works in both cases.
In fact, this example is simplified: I'm actually passing SomeSpecialError into the middleware too, so that if the middleware inside Electron wants to throw it, it can do that too - not only the callback inside the frontend code can. (Which of course doesn't work right now.)
This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. If you have any new additional information—in particular, if this is still reproducible in the latest version of Electron or in the beta—please include it with your comment!
This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. If you have any new additional information—in particular, if this is still reproducible in the latest version of Electron or in the beta—please include it with your comment!
This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. If you have any new additional information—in particular, if this is still reproducible in the latest version of Electron or in the beta—please include it with your comment!
This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. If you have any new additional information—in particular, if this is still reproducible in the latest version of Electron or in the beta—please include it with your comment!
This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. If you have any new additional information—in particular, if this is still reproducible in the latest version of Electron or in the beta—please include it with your comment!
This issue has been closed due to inactivity, and will not be monitored. If this is a bug and you can reproduce this issue on a supported version of Electron please open a new issue and include instructions for reproducing the issue.
Activity
MarshallOfSound commentedon Sep 28, 2020
@richardfritsch We follow the same strategy for cross-boundarysanitization as chrome. If you try and
dispatchEventwith a customEventobject with custom properties those properties will be lost when accessing that event in another world. Errors are tricky because we as a framework don't know what could be in the custom properties on Error objects and don't want to let users shoot themselves in the foot by setting custom properties on error objects that then get leaked into the main world.IMO in your case of
fsusage the renderer should never know why the request failed, just that for some reason it did. Passing details like error code and in particular the filesystempathis counter to the goals of isolated the untrusted renderer from that kind of information.rfricz commentedon Sep 29, 2020
Thanks for having a look at it. In my case, knowing why a request failed is needed - the app works with files, folders and lockfiles often located on shared network drives etc. where permission and accessibility errors occur – the app will retry or inform the user based on the error details. I understand the security concerns, it’d just be nice if I didn’t have to restructure the code to return the error details inside a plain object and could just catch an exception instead. An option for
exposeInMainWorldto allow Errors to pass would be handy.CherryDT commentedon Jan 4, 2021
I ran into the same problem, errors convey meaning here, including their extra properties (and I'm using
instanceofon errors as well). And of course a stack would be nice...It would be great if this could be the choice of the developer, yes.
In this particular case, I'm using the preload code as sort of a "middleware" which, if existing (i.e., app is run inside Electron and not browser) is called by the frontend with a callback, so the middleware can intercept or inject functionality if required. But, if the inner code (passed as callback) is throwing a certain error, it's expected to stay that error.
Frontend code:
Preload code:
Result is
:-(if run withwindow.electronexisting, otherwiseworks. I'd like it to beworksin both cases.In fact, this example is simplified: I'm actually passing
SomeSpecialErrorinto the middleware too, so that if the middleware inside Electron wants to throw it, it can do that too - not only the callback inside the frontend code can. (Which of course doesn't work right now.)mifi commentedon Jan 19, 2022
Somewhat related #24427
github-actions commentedon Oct 7, 2022
This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. If you have any new additional information—in particular, if this is still reproducible in the latest version of Electron or in the beta—please include it with your comment!
mifi commentedon Oct 7, 2022
stil an issue
github-actions commentedon Jan 24, 2023
This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. If you have any new additional information—in particular, if this is still reproducible in the latest version of Electron or in the beta—please include it with your comment!
mifi commentedon Jan 24, 2023
Would you be open for a PR of at least removing the «Unchaught error: » prefix? Or maybe also copying simple Primitive props?
20 remaining items
clemp6r commentedon Oct 11, 2024
bump
WorkAttackDev commentedon Oct 18, 2024
bump
electron-issue-triage commentedon Jan 19, 2025
This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. If you have any new additional information—in particular, if this is still reproducible in the latest version of Electron or in the beta—please include it with your comment!
clemp6r commentedon Jan 20, 2025
bump
electron-issue-triage commentedon Apr 21, 2025
This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. If you have any new additional information—in particular, if this is still reproducible in the latest version of Electron or in the beta—please include it with your comment!
clemp6r commentedon Apr 22, 2025
bump
electron-issue-triage commentedon Jul 22, 2025
This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. If you have any new additional information—in particular, if this is still reproducible in the latest version of Electron or in the beta—please include it with your comment!
electron-issue-triage commentedon Aug 22, 2025
This issue has been closed due to inactivity, and will not be monitored. If this is a bug and you can reproduce this issue on a supported version of Electron please open a new issue and include instructions for reproducing the issue.