Closed Bug 1155898 Opened 10 years ago Closed 10 years ago

Expose fetch on JS sandbox

Categories

(Core :: XPConnect, defect)

defect
Not set
normal

Tracking

()

RESOLVED FIXED
mozilla41
Tracking Status
firefox40 --- affected
firefox41 --- fixed

People

(Reporter: mt, Assigned: mt)

References

Details

(Keywords: dev-doc-complete)

Attachments

(3 files, 1 obsolete file)

It's unfair that we only get to use the crappy XMLHttpRequest API in JS sandboxes. Enabling fetch doesn't seem to be too intrusive. I only wish that it were easier to test using xpcshell. I need to work up a more rigorous test than what I have currently. At least the WebRTC stuff works with the changes I've made.
Attached file MozReview Request: bz://1155898/mt (obsolete) —
/r/7229 - Bug 1155898 - Fetch support for running outside of window/worker /r/7231 - Bug 1155898 - Expose fetch on JS sandbox /r/7233 - Bug 1155898 - Use fetch instead of XHR for IdP Pull down these commits: hg pull -r fc9821779ff0b8de8d3274a0df8da278945fb6f9 https://reviewboard-hg.mozilla.org/gecko/
Comment on attachment 8594219 [details] MozReview Request: bz://1155898/mt Do you think that I need anyone else to look at this more carefully?
Attachment #8594219 - Flags: feedback?(nsm.nikhil)
Attachment #8594219 - Flags: feedback?(gkrizsanits)
Can't this be done with Exposed= in the WebIDL?
If you know how to do that, I'd be happy to learn. Is it as simple as [Exposed=System] ?
(In reply to Martin Thomson [:mt] from comment #5) > If you know how to do that, I'd be happy to learn. Is it as simple as > [Exposed=System] ? Oh hm, I guess we'd talked about it, but that would require bug 1137772 first. So nevermind, I guess.
https://reviewboard.mozilla.org/r/7229/#review6031 Patches looks great! I have only a few nits, and I would like someone who is an expert in webidl to take a look at the second patch before we ship them... ::: dom/fetch/Fetch.cpp (Diff revision 1) > + // and the principal is not null or the system principal. or nsExpandedPrincipal... basically if it has a meaningfull URI, note that system princiapl also returns null for the URI, but nullprincipal does not. So it's enough checking if it's not nullprincipal and has a non-null URI.
https://reviewboard.mozilla.org/r/7231/#review6025 ::: js/xpconnect/tests/unit/test_sandbox_fetch.js (Diff revision 1) > + dump('(' + testFunc.toSource() + ')(' + propertyName + ');'); I don't think this dump should stay... ::: js/xpconnect/src/Sandbox.cpp (Diff revision 1) > + const MutableHandleValue& arg0) Arg0 does not make much sence here as the name. ::: js/xpconnect/src/Sandbox.cpp (Diff revision 1) > + return false; In the false case what error does TrySetToRequest return? ::: js/xpconnect/src/Sandbox.cpp (Diff revision 1) > + if (arg0.isObject() && !requestHolder.TrySetToRequest(cx, arg0, noMatch, false)) { RequestOrUSVStringArgument seems like something only the code generator should use... I would really like some webidl expert to take a look at this. ::: js/xpconnect/src/Sandbox.cpp (Diff revision 1) > + JS_ReportError(cx, "fetch requires a string or Request in argument 1"); I would prefer this function to just return false in case of any error and then its consumer to report the error. The 1st argument makes more sense in that context anyway then here. We could then also rename this function to something more generic, like SetRequestFromValue or SetFetchRequestFromValue. ::: js/xpconnect/tests/unit/test_sandbox_fetch.js (Diff revision 1) > + var interfaceExists = o => ok(o); Since these patches not just exposing the fetch to sandboxes but also enabling them to be used without a window/document, it would be great if we could add some more meaningfull test than just checking the existance of these properties. (So we could guard against someone breaking the sandboxed version in the future) Could you add some trivial test that checks functionality? Like adding a chrome test that creates a sandbox and runs http://mxr.mozilla.org/mozilla-central/source/dom/tests/mochitest/fetch/test_fetch_basic.js so basically adding a new driver function to fetch_test_framework.js and use it from a chrome mochi test?
https://reviewboard.mozilla.org/r/7233/#review6029 This looks good to me but please remove the old code instead of commenting it out. ::: dom/media/tests/mochitest/identity/idp.js (Diff revision 1) > +/* var xhr = new XMLHttpRequest(); Is there a reason not to remove this code?
Comment on attachment 8594219 [details] MozReview Request: bz://1155898/mt (In reply to Martin Thomson [:mt] from comment #3) > Do you think that I need anyone else to look at this more carefully? I guess that someone is Bobby, who already did that :) For the second patch I would like either him or Boris or someone else who is an expert in webidl to take a closer look at. I'm not sure if we should use some of these types/methods you are using outside of the codegen... so better ask than sorry :)
Attachment #8594219 - Flags: feedback?(gkrizsanits)
(In reply to Gabor Krizsanits [:krizsa :gabor] from comment #8) > > + if (arg0.isObject() && !requestHolder.TrySetToRequest(cx, arg0, noMatch, false)) { > > RequestOrUSVStringArgument seems like something only the code generator > should use... I would really like some webidl expert to take a look at this. I will ask. I'm really only cargo-culting some of this, so actual expert input will be needed. Honestly, I'm surprised that something this hacktastic even worked. > ::: js/xpconnect/src/Sandbox.cpp > (Diff revision 1) > > + JS_ReportError(cx, "fetch requires a string or Request in argument 1"); > > I would prefer this function to just return false in case of any error and > then its consumer to report the error. The 1st argument makes more sense in > that context anyway then here. We could then also rename this function to > something more generic, like SetRequestFromValue or SetFetchRequestFromValue. Yes, I have moved that to the calling scope; and the name suggestion is great, thanks. > ::: js/xpconnect/tests/unit/test_sandbox_fetch.js > (Diff revision 1) > > + var interfaceExists = o => ok(o); > > Since these patches not just exposing the fetch to sandboxes but also > enabling them to be used without a window/document, it would be great if we > could add some more meaningfull test than just checking the existance of > these properties. > (So we could guard against someone breaking the sandboxed version in the > future) Yes, I have that test, and will upload it. It's a regular mochitest. I'll probably remove the stubbed test at the same time. (In reply to Gabor Krizsanits [:krizsa :gabor] from comment #9) > https://reviewboard.mozilla.org/r/7233/#review6029 > > This looks good to me but please remove the old code instead of commenting > it out. Yeah, that's just a mistake; I was switching back and forth, looking at how XHR and fetch differ in practice. > (In reply to Martin Thomson [:mt] from comment #3) > > Do you think that I need anyone else to look at this more carefully? > > I guess that someone is Bobby, who already did that :) For the second patch > I would like either him or Boris or someone else who is an expert in webidl > to take a closer look at. I'm not sure if we should use some of these > types/methods you are using outside of the codegen... so better ask than > sorry :) I will do. (BTW, I notice that your RB comments only ever reference a single line. Did you know that you can select more lines of context by dragging the mouse over the lines, rather than clicking?)
Comment on attachment 8594219 [details] MozReview Request: bz://1155898/mt /r/7229 - Bug 1155898 - Fetch support for running outside of window/worker /r/7231 - Bug 1155898 - Expose fetch on JS sandbox /r/7233 - Bug 1155898 - Use fetch instead of XHR for IdP Pull down these commits: hg pull -r 602ffb301e2fe3870f403ddb2d61306049ca4061 https://reviewboard-hg.mozilla.org/gecko/
Attachment #8594219 - Flags: feedback?(nsm.nikhil)
nsm, do you think that this is worth opening a separate bug for? I've commented out the line in test_sandbox_fetch.html that triggers this dump, it's the blob test here: https://dxr.mozilla.org/mozilla-central/source/dom/tests/mochitest/fetch/test_fetch_basic.js#38 |<----------------Class--------------->|<-----Bytes------>|<----Objects---->| Per-Inst Leaked Total Rem 0 TOTAL 30 768 1074118 16 136 DataOwner 48 48 1 1 175 FileImpl 24 48 2 2 176 FileImplMemory 120 120 1 1 290 MultipartFileImpl 128 128 1 1 493 WeakReference<MessageListener> 16 16 265 1 597 nsAuthURLParser 24 24 2 1 607 nsBasePrincipal 24 24 311 1 950 nsPrincipal 64 64 311 1 1012 nsStandardURL 248 248 7108 1 1022 nsStringBuffer 8 24 64550 3 1064 nsTArray_base 8 24 204039 3 Adding a call to Cu.nukeSandbox() doesn't fix the problem, sadly. I see the leak of a weak reference to a MessageListener all the time, so you can probably ignore that one.
Flags: needinfo?(nsm.nikhil)
Attachment #8594219 - Flags: feedback?(nsm.nikhil)
https://reviewboard.mozilla.org/r/7229/#review6091 ::: dom/fetch/Fetch.cpp:239 (Diff revision 2) > + loadGroup = do_CreateInstance(NS_LOADGROUP_CONTRACTID); The loadgroup should ideally be associated with the principal. You can obtain one by using https://dxr.mozilla.org/mozilla-central/source/netwerk/base/nsNetUtil.h#830 ::: dom/fetch/FetchDriver.cpp (Diff revision 2) > - MOZ_ASSERT(mLoadGroup); why was this assertion removed?
Comment on attachment 8594219 [details] MozReview Request: bz://1155898/mt it seems like reviewboard does not integrate with feedback? flag yet.
Attachment #8594219 - Flags: feedback?(nsm.nikhil)
Inside the fetch handler, can you try calling URL.revokeObjectURL(url) and see if it stops leaking? That way we'll know where the problem is. In windows+documents, the document is aware of such urls backed by blobs and releases them https://dxr.mozilla.org/mozilla-central/source/dom/base/URL.cpp#175. You'd have to add similar functionality to sandbox, or prohibit the sandbox from using createObjectURL for now.
Flags: needinfo?(nsm.nikhil)
(In reply to Martin Thomson [:mt] from comment #11) > (BTW, I notice that your RB comments only ever reference a single line. Did > you know that you can select more lines of context by dragging the mouse > over the lines, rather than clicking?) Nope, I was not aware of it, in fact it kind of bothered me, so thanks a lot! :)
(In reply to Nikhil Marathe [:nsm] (needinfo? please) from comment #18) > Inside the fetch handler, can you try calling URL.revokeObjectURL(url) and > see if it stops leaking? That way we'll know where the problem is. In > windows+documents, the document is aware of such urls backed by blobs and > releases them > https://dxr.mozilla.org/mozilla-central/source/dom/base/URL.cpp#175. You'd > have to add similar functionality to sandbox, or prohibit the sandbox from > using createObjectURL for now. It looks like that is the problem. I'm going to open a second bug for that. It should be easy enough to attach something like that to SandboxPrivate, but I'm concerned that we're just punting the problem if we do that: won't workers have the same problem?
Flags: needinfo?(nsm.nikhil)
See Also: → 1156875
Attachment #8594219 - Flags: review?(peterv)
Attachment #8594219 - Flags: review?(nsm.nikhil)
Attachment #8594219 - Flags: review?(jib)
Attachment #8594219 - Flags: review?(gkrizsanits)
Comment on attachment 8594219 [details] MozReview Request: bz://1155898/mt /r/7229 - Bug 1155898 - Fetch support for running outside of window/worker /r/7231 - Bug 1155898 - Expose fetch on JS sandbox /r/7233 - Bug 1155898 - Use fetch instead of XHR for IdP Pull down these commits: hg pull -r 5ebab881fbc2a387c633c9b0177a290cb4c377c8 https://reviewboard-hg.mozilla.org/gecko/
Attachment #8594219 - Flags: review?(jib) → review+
(In reply to Martin Thomson [:mt] from comment #20) > (In reply to Nikhil Marathe [:nsm] (needinfo? please) from comment #18) > > Inside the fetch handler, can you try calling URL.revokeObjectURL(url) and > > see if it stops leaking? That way we'll know where the problem is. In > > windows+documents, the document is aware of such urls backed by blobs and > > releases them > > https://dxr.mozilla.org/mozilla-central/source/dom/base/URL.cpp#175. You'd > > have to add similar functionality to sandbox, or prohibit the sandbox from > > using createObjectURL for now. > > It looks like that is the problem. I'm going to open a second bug for that. > It should be easy enough to attach something like that to SandboxPrivate, > but I'm concerned that we're just punting the problem if we do that: won't > workers have the same problem? workers also seem to have hooks like documents - https://dxr.mozilla.org/mozilla-central/source/dom/workers/WorkerPrivate.cpp#4110
Flags: needinfo?(nsm.nikhil)
Comment on attachment 8594219 [details] MozReview Request: bz://1155898/mt https://reviewboard.mozilla.org/r/7227/#review6169 Ship It!
Attachment #8594219 - Flags: review?(nsm.nikhil) → review+
https://reviewboard.mozilla.org/r/7231/#review6247 ::: js/xpconnect/tests/mochitest/test_sandbox_fetch.html:51 (Diff revision 3) > + SpecialPowers.Cu.nukeSandbox(sb); Why do we need to nuke the sandbox here? ::: js/xpconnect/src/Sandbox.cpp:275 (Diff revision 3) > + FetchRequest(global, Constify(request), Constify(options), rv); > + rv.WouldReportJSException(); > + if (rv.Failed()) { > + return ThrowMethodFailedWithDetails(cx, rv, "Sandbox", "fetch"); > + } Not sure if we should use ThrowMethodFailedWithDetails here... Probably not... but then again I let Peter sort out what API of these binging internals should or should not be used externally. I wish there were an easier way to export functions to sandboxes like there is for ctors (::GetConstructorObject).
Comment on attachment 8594219 [details] MozReview Request: bz://1155898/mt https://reviewboard.mozilla.org/r/7227/#review6249 Ship It!
Attachment #8594219 - Flags: review?(gkrizsanits) → review+
(In reply to Gabor Krizsanits [:krizsa :gabor] from comment #27) > https://reviewboard.mozilla.org/r/7231/#review6247 > > ::: js/xpconnect/tests/mochitest/test_sandbox_fetch.html:51 > (Diff revision 3) > > + SpecialPowers.Cu.nukeSandbox(sb); > > Why do we need to nuke the sandbox here? I added this when investigating the leak that this test exposed. It's not really needed. I'll remove it. > ::: js/xpconnect/src/Sandbox.cpp:275 > (Diff revision 3) > > + FetchRequest(global, Constify(request), Constify(options), rv); > > + rv.WouldReportJSException(); > > + if (rv.Failed()) { > > + return ThrowMethodFailedWithDetails(cx, rv, "Sandbox", "fetch"); > > + } > > Not sure if we should use ThrowMethodFailedWithDetails here... Probably > not... but then again I let Peter sort out what API of these binging > internals should or should not be used externally. I wish there were an > easier way to export functions to sandboxes like there is for ctors > (::GetConstructorObject). I couldn't find one. But that's why I'm asking Peter for review. I really don't know this code well enough.
https://reviewboard.mozilla.org/r/7231/#review7579 ::: js/xpconnect/src/Sandbox.cpp:233 (Diff revision 3) > + const MutableHandleValue& requestOrUrl) Fix up whitespace.
Attachment #8594219 - Flags: review?(peterv)
Attachment #8594219 - Flags: review+
Comment on attachment 8594219 [details] MozReview Request: bz://1155898/mt /r/7229 - Bug 1155898 - Fetch support for running outside of window/worker, r=nsm /r/7231 - Bug 1155898 - Expose fetch on JS sandbox, r=gabor,peterv /r/7233 - Bug 1155898 - Use fetch instead of XHR for IdP, r=jib Pull down these commits: hg pull -r 65c1b06068e7e1a2c4bdd27a521459247bbff83b https://reviewboard-hg.mozilla.org/gecko/
Comment on attachment 8594219 [details] MozReview Request: bz://1155898/mt Carrying r+
Attachment #8594219 - Flags: review+
Keywords: checkin-needed
Backed out for test_sandbox_fetch.html failures. https://treeherder.mozilla.org/logviewer.html#?job_id=10002262&repo=mozilla-inbound This looks relevant: 08:30:34 INFO - JavaScript error: http://mochi.test:8888/tests/js/xpconnect/tests/mochitest/test_sandbox_fetch.html, line 28: ReferenceError: resolvePromise is not defined
Comment on attachment 8594219 [details] MozReview Request: bz://1155898/mt /r/7229 - Bug 1155898 - Fetch support for running outside of window/worker, r=nsm /r/7231 - Bug 1155898 - Expose fetch on JS sandbox, r=gabor,peterv /r/7233 - Bug 1155898 - Use fetch instead of XHR for IdP, r=jib Pull down these commits: hg pull -r cfdc83a79f6ad2c0b389385a3eabc4a3d4a0e6e3 https://reviewboard-hg.mozilla.org/gecko/
Attachment #8594219 - Flags: review+
Comment on attachment 8594219 [details] MozReview Request: bz://1155898/mt https://reviewboard.mozilla.org/r/7227/#review7789 Ship It!
Attachment #8594219 - Flags: review+
Keywords: checkin-needed
Assignee: nobody → martin.thomson
Attachment #8594219 - Attachment is obsolete: true
Attachment #8620089 - Flags: review+
Attachment #8620090 - Flags: review+
Attachment #8620091 - Flags: review+
See Also: → 1137772
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: