This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

expose mozbrowser only to https/file URLs

  • Loading branch information...
mykmelez committed Aug 23, 2016
1 parent d9ed68d commit 3c531e4380bd18c85270c8990238062696801635
@@ -2118,25 +2118,8 @@ nsDocument::Reset(nsIChannel* aChannel, nsILoadGroup* aLoadGroup)
nsIScriptSecurityManager *securityManager =
nsContentUtils::GetSecurityManager();
if (securityManager) {
// Give loads in top-level docshells the system principal so Positron
// can give chrome privileges to application documents that it loads
// into chrome windows from file: URLs.
//
// TODO: figure out a better way to give those documents this principal.
// https://github.com/mozilla/positron/issues/67
//
nsCOMPtr<nsIDocShell> docShell(mDocumentContainer);
nsCOMPtr<nsIDocShellTreeItem> parentDocShellTreeItem;
if (docShell &&
docShell->ItemType() == nsIDocShellTreeItem::typeChrome &&
NS_SUCCEEDED(docShell->GetParent(getter_AddRefs(parentDocShellTreeItem))) &&
!parentDocShellTreeItem)
{
securityManager->GetSystemPrincipal(getter_AddRefs(principal));
} else {
securityManager->GetChannelResultPrincipal(aChannel,
getter_AddRefs(principal));
}
securityManager->GetChannelResultPrincipal(aChannel,
getter_AddRefs(principal));
}
}

@@ -6012,6 +5995,45 @@ nsDocument::IsWebComponentsEnabled(JSContext* aCx, JSObject* aObject)
return false;
}

bool
nsDocument::IsBrowserElementEnabled(JSContext* aCx, JSObject* aObject)
{
JS::Rooted<JSObject*> obj(aCx, aObject);

if (!Preferences::GetBool("dom.mozBrowserFramesEnabled")) {
return false;
}

if (nsContentUtils::IsCallerChrome()) {
return true;
}

if (!Preferences::GetBool("dom.mozBrowserFramesEnabledForContent")) {
return false;
}

// Check for the browser permission.
JSAutoCompartment ac(aCx, obj);
JS::Rooted<JSObject*> global(aCx, JS_GetGlobalForObject(aCx, obj));
nsCOMPtr<nsPIDOMWindowInner> window =
do_QueryInterface(nsJSUtils::GetStaticScriptGlobal(global));

if (window) {
nsresult rv;
nsCOMPtr<nsIPermissionManager> permMgr =
do_GetService(NS_PERMISSIONMANAGER_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, false);

uint32_t perm;
rv = permMgr->TestPermissionFromWindow(window, "browser", &perm);
NS_ENSURE_SUCCESS(rv, false);

return perm == nsIPermissionManager::ALLOW_ACTION;
}

return false;
}

nsresult
nsDocument::RegisterUnresolvedElement(Element* aElement, nsIAtom* aTypeName)
{
@@ -1540,6 +1540,7 @@ class nsDocument : public nsIDocument,
const nsAString* aTypeExtension) override;

static bool IsWebComponentsEnabled(JSContext* aCx, JSObject* aObject);
static bool IsBrowserElementEnabled(JSContext* aCx, JSObject* aObject);

// The "registry" from the web components spec.
RefPtr<mozilla::dom::Registry> mRegistry;
@@ -30,40 +30,40 @@ BrowserElement implements BrowserElementPrivileged;
interface BrowserElementCommon {
[Throws,
Pref="dom.mozBrowserFramesEnabled",
ChromeOnly]
Func="nsDocument::IsBrowserElementEnabled"]
void setVisible(boolean visible);

[Throws,
Pref="dom.mozBrowserFramesEnabled",
ChromeOnly]
Func="nsDocument::IsBrowserElementEnabled"]
DOMRequest getVisible();

[Throws,
Pref="dom.mozBrowserFramesEnabled",
ChromeOnly]
Func="nsDocument::IsBrowserElementEnabled"]
void setActive(boolean active);

[Throws,
Pref="dom.mozBrowserFramesEnabled",
ChromeOnly]
Func="nsDocument::IsBrowserElementEnabled"]
boolean getActive();

[Throws,
Pref="dom.mozBrowserFramesEnabled",
ChromeOnly]
Func="nsDocument::IsBrowserElementEnabled"]
void addNextPaintListener(BrowserElementNextPaintEventCallback listener);

[Throws,
Pref="dom.mozBrowserFramesEnabled",
ChromeOnly]
Func="nsDocument::IsBrowserElementEnabled"]
void removeNextPaintListener(BrowserElementNextPaintEventCallback listener);
};

[NoInterfaceObject]
interface BrowserElementPrivileged {
[Throws,
Pref="dom.mozBrowserFramesEnabled",
ChromeOnly]
Func="nsDocument::IsBrowserElementEnabled"]
void sendMouseEvent(DOMString type,
unsigned long x,
unsigned long y,
@@ -74,7 +74,7 @@ interface BrowserElementPrivileged {
[Throws,
Pref="dom.mozBrowserFramesEnabled",
Func="TouchEvent::PrefEnabled",
ChromeOnly]
Func="nsDocument::IsBrowserElementEnabled"]
void sendTouchEvent(DOMString type,
sequence<unsigned long> identifiers,
sequence<long> x,
@@ -88,96 +88,96 @@ interface BrowserElementPrivileged {

[Throws,
Pref="dom.mozBrowserFramesEnabled",
ChromeOnly]
Func="nsDocument::IsBrowserElementEnabled"]
void goBack();

[Throws,
Pref="dom.mozBrowserFramesEnabled",
ChromeOnly]
Func="nsDocument::IsBrowserElementEnabled"]
void goForward();

[Throws,
Pref="dom.mozBrowserFramesEnabled",
ChromeOnly]
Func="nsDocument::IsBrowserElementEnabled"]
void reload(optional boolean hardReload = false);

[Throws,
Pref="dom.mozBrowserFramesEnabled",
ChromeOnly]
Func="nsDocument::IsBrowserElementEnabled"]
void stop();

[Throws,
Pref="dom.mozBrowserFramesEnabled",
ChromeOnly]
Func="nsDocument::IsBrowserElementEnabled"]
DOMRequest download(DOMString url,
optional BrowserElementDownloadOptions options);

[Throws,
Pref="dom.mozBrowserFramesEnabled",
ChromeOnly]
Func="nsDocument::IsBrowserElementEnabled"]
DOMRequest purgeHistory();

[Throws,
Pref="dom.mozBrowserFramesEnabled",
ChromeOnly]
Func="nsDocument::IsBrowserElementEnabled"]
DOMRequest getScreenshot([EnforceRange] unsigned long width,
[EnforceRange] unsigned long height,
optional DOMString mimeType="");

[Throws,
Pref="dom.mozBrowserFramesEnabled",
ChromeOnly]
Func="nsDocument::IsBrowserElementEnabled"]
void zoom(float zoom);

[Throws,
Pref="dom.mozBrowserFramesEnabled",
ChromeOnly]
Func="nsDocument::IsBrowserElementEnabled"]
DOMRequest getCanGoBack();

[Throws,
Pref="dom.mozBrowserFramesEnabled",
ChromeOnly]
Func="nsDocument::IsBrowserElementEnabled"]
DOMRequest getCanGoForward();

[Throws,
Pref="dom.mozBrowserFramesEnabled",
ChromeOnly]
Func="nsDocument::IsBrowserElementEnabled"]
DOMRequest getContentDimensions();

[Throws,
Pref="dom.mozBrowserFramesEnabled",
ChromeOnly]
Func="nsDocument::IsBrowserElementEnabled"]
DOMRequest setInputMethodActive(boolean isActive);

[Throws,
Pref="dom.mozBrowserFramesEnabled",
ChromeOnly]
Func="nsDocument::IsBrowserElementEnabled"]
void setNFCFocus(boolean isFocus);

[Throws,
Pref="dom.mozBrowserFramesEnabled",
ChromeOnly]
Func="nsDocument::IsBrowserElementEnabled"]
void findAll(DOMString searchString, BrowserFindCaseSensitivity caseSensitivity);

[Throws,
Pref="dom.mozBrowserFramesEnabled",
ChromeOnly]
Func="nsDocument::IsBrowserElementEnabled"]
void findNext(BrowserFindDirection direction);

[Throws,
Pref="dom.mozBrowserFramesEnabled",
ChromeOnly]
Func="nsDocument::IsBrowserElementEnabled"]
void clearMatch();

[Throws,
Pref="dom.mozBrowserFramesEnabled",
ChromeOnly]
Func="nsDocument::IsBrowserElementEnabled"]
DOMRequest executeScript(DOMString script,
optional BrowserElementExecuteScriptOptions options);

[Throws,
Pref="dom.mozBrowserFramesEnabled",
ChromeOnly]
Func="nsDocument::IsBrowserElementEnabled"]
DOMRequest getWebManifest();

};
@@ -54,7 +54,7 @@ partial interface HTMLIFrameElement {

partial interface HTMLIFrameElement {
// nsIDOMMozBrowserFrame
[ChromeOnly,SetterThrows]
[Func="nsDocument::IsBrowserElementEnabled",SetterThrows]
attribute boolean mozbrowser;
};

@@ -23,6 +23,7 @@ this.Runtime = {
"titlebar=no",
];

this._maybeEnableBrowserElementForURI(uri);
let window = Services.ww.openWindow(null, uri.spec, "_blank", features.join(","), null);

window.addEventListener("mozContentEvent", function(event) {
@@ -48,4 +49,19 @@ this.Runtime = {
}
}, false, true);
},

_maybeEnableBrowserElementForURI: function(uri) {
if (uri.scheme !== 'https' && uri.scheme !== 'file') {
console.warn(`not enabling mozbrowser for non-https/file URL ${uri.spec}`);
return;
}

console.log(`enabling mozbrowser for https/file URL ${uri.spec}`);
Services.perms.add(uri, "browser", Services.perms.ALLOW_ACTION);

// TODO: remove permission once BrowserWindow is closed?
// Otherwise, if the app gets updated to a version that no longer opens
// a BrowserWindow to the URL, the permission will persist unnecessarily.
},

};

0 comments on commit 3c531e4

Please sign in to comment.