Bug 1778959 - Don't expose requestMIDIAccess on file scheme. r=bholley.

This is done by adding Navigator::HasMidiSupport that we reference in
the Navigator.webidl `Func` extented attribute for `requestMIDIAccess`.

A test case is added to browser_midi_permission_gated.js to ensure this
works as expected.

Differential Revision: https://phabricator.services.mozilla.com/D157321
This commit is contained in:
nchevobbe 2022-09-30 22:07:19 +00:00
parent 3d1ac4e0b7
commit 5d8ed083ca
6 changed files with 44 additions and 1 deletions

View File

@ -1894,6 +1894,16 @@ bool Navigator::HasShareSupport(JSContext* cx, JSObject* obj) {
#endif
}
/* static */
bool Navigator::HasMidiSupport(JSContext* cx, JSObject* obj) {
nsIPrincipal* principal = nsContentUtils::SubjectPrincipal(cx);
// Enable on secure contexts but exclude file schemes.
return StaticPrefs::dom_webmidi_enabled() &&
IsSecureContextOrObjectIsFromSecureContext(cx, obj) &&
!principal->SchemeIs("file");
}
/* static */
already_AddRefed<nsPIDOMWindowInner> Navigator::GetWindowFromGlobal(
JSObject* aGlobal) {

View File

@ -231,6 +231,8 @@ class Navigator final : public nsISupports, public nsWrapperCache {
JSObject* /* unused */);
static bool HasShareSupport(JSContext* /* unused */, JSObject* /* unused */);
static bool HasMidiSupport(JSContext* /* unused */, JSObject* /* unused */);
nsPIDOMWindowInner* GetParentObject() const { return GetWindow(); }
virtual JSObject* WrapObject(JSContext* cx,

View File

@ -0,0 +1,5 @@
<!DOCTYPE html>
<meta charset="utf8">
<html>
<body></body>
</html>

View File

@ -6,6 +6,9 @@ prefs =
media.navigator.permission.disabled=true
[browser_midi_permission_gated.js]
support-files =
blank.html
[browser_stable_midi_port_ids.js]
run-if = (os != 'android')
support-files =

View File

@ -420,6 +420,29 @@ add_task(async function testRequestMIDIAccessLocalhost() {
);
});
add_task(async function testDisabledRequestMIDIAccessFile() {
let dir = getChromeDir(getResolvedURI(gTestPath));
dir.append("blank.html");
const fileSchemeTestUri = Services.io.newFileURI(dir).spec;
gBrowser.selectedTab = BrowserTestUtils.addTab(gBrowser, fileSchemeTestUri);
await BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser);
info("Check that requestMIDIAccess isn't set on navigator on file scheme");
const isRequestMIDIAccessDefined = await SpecialPowers.spawn(
gBrowser.selectedBrowser,
[],
() => {
return "requestMIDIAccess" in content.wrappedJSObject.navigator;
}
);
is(
isRequestMIDIAccessDefined,
false,
"navigator.requestMIDIAccess is not defined on file scheme"
);
});
async function waitForInstallDialog(id = "addon-webext-permissions") {
let panel = await waitForNotification(id);
return panel.childNodes[0];

View File

@ -240,7 +240,7 @@ partial interface Navigator {
// http://webaudio.github.io/web-midi-api/#requestmidiaccess
partial interface Navigator {
[SecureContext, NewObject, Pref="dom.webmidi.enabled"]
[NewObject, Func="Navigator::HasMidiSupport"]
Promise<MIDIAccess> requestMIDIAccess(optional MIDIOptions options = {});
};