Skip to content

Commit c6f02eb

Browse files
committed
Bug 1745819 - Require origin permission for content scripts in mv3 r=robwu
Differential Revision: https://phabricator.services.mozilla.com/D141557
1 parent 8294004 commit c6f02eb

12 files changed

+214
-64
lines changed

dom/chrome-webidl/WebExtensionContentScript.webidl

+10-7
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,6 @@ interface MozDocumentMatcher {
2020
*/
2121
boolean matchesURI(URI uri);
2222

23-
/**
24-
* Returns true if the the given URI and LoadInfo objects match.
25-
* This should be used to determine whether to begin pre-loading a content
26-
* script based on network events.
27-
*/
28-
boolean matchesLoadInfo(URI uri, LoadInfo loadInfo);
29-
3023
/**
3124
* Returns true if the given window matches. This should be used
3225
* to determine whether to run a script in a window at load time.
@@ -39,6 +32,14 @@ interface MozDocumentMatcher {
3932
[Constant]
4033
readonly attribute boolean allFrames;
4134

35+
/**
36+
* If we can't check extension has permissions to access the URI upfront,
37+
* set the flag to perform the origin check at runtime, upon matching.
38+
* This is always true in MV3, where host permissions are optional.
39+
*/
40+
[Constant]
41+
readonly attribute boolean checkPermissions;
42+
4243
/**
4344
* If true, this (misleadingly-named, but inherited from Chrome) attribute
4445
* causes us to match frames with URLs which inherit a principal that
@@ -102,6 +103,8 @@ interface MozDocumentMatcher {
102103
dictionary MozDocumentMatcherInit {
103104
boolean allFrames = false;
104105

106+
boolean checkPermissions = false;
107+
105108
sequence<OriginAttributesPatternDictionary>? originAttributesPatterns = null;
106109

107110
boolean matchAboutBlank = false;

toolkit/components/extensions/WebExtensionContentScript.h

+2-4
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,6 @@ class MozDocumentMatcher : public nsISupports, public nsWrapperCache {
115115
bool Matches(const DocInfo& aDoc) const;
116116
bool MatchesURI(const URLInfo& aURL) const;
117117

118-
bool MatchesLoadInfo(const URLInfo& aURL, nsILoadInfo* aLoadInfo) const {
119-
return Matches({aURL, aLoadInfo});
120-
}
121-
122118
bool MatchesWindowGlobal(dom::WindowGlobalChild& aWindow) const;
123119

124120
WebExtensionPolicy* GetExtension() { return mExtension; }
@@ -127,6 +123,7 @@ class MozDocumentMatcher : public nsISupports, public nsWrapperCache {
127123
const WebExtensionPolicy* Extension() const { return mExtension; }
128124

129125
bool AllFrames() const { return mAllFrames; }
126+
bool CheckPermissions() const { return mCheckPermissions; }
130127
bool MatchAboutBlank() const { return mMatchAboutBlank; }
131128

132129
MatchPatternSet* Matches() { return mMatches; }
@@ -173,6 +170,7 @@ class MozDocumentMatcher : public nsISupports, public nsWrapperCache {
173170
Nullable<MatchGlobSet> mExcludeGlobs;
174171

175172
bool mAllFrames;
173+
bool mCheckPermissions;
176174
Nullable<uint64_t> mFrameID;
177175
bool mMatchAboutBlank;
178176
Nullable<dom::Sequence<OriginAttributesPattern>> mOriginAttributesPatterns;

toolkit/components/extensions/WebExtensionPolicy.cpp

+14-1
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,7 @@ MozDocumentMatcher::MozDocumentMatcher(GlobalObject& aGlobal,
634634
: mHasActiveTabPermission(aInit.mHasActiveTabPermission),
635635
mRestricted(aRestricted),
636636
mAllFrames(aInit.mAllFrames),
637+
mCheckPermissions(aInit.mCheckPermissions),
637638
mFrameID(aInit.mFrameID),
638639
mMatchAboutBlank(aInit.mMatchAboutBlank) {
639640
MatchPatternOptions options;
@@ -690,6 +691,11 @@ WebExtensionContentScript::WebExtensionContentScript(
690691
mCssPaths.Assign(aInit.mCssPaths);
691692
mJsPaths.Assign(aInit.mJsPaths);
692693
mExtension = &aExtension;
694+
695+
// Origin permissions are optional in mv3, so always check them at runtime.
696+
if (mExtension->ManifestVersion() >= 3) {
697+
mCheckPermissions = true;
698+
}
693699
}
694700

695701
bool MozDocumentMatcher::Matches(const DocInfo& aDoc) const {
@@ -738,7 +744,7 @@ bool MozDocumentMatcher::Matches(const DocInfo& aDoc) const {
738744
return true;
739745
}
740746

741-
if (mRestricted && mExtension->IsRestrictedDoc(aDoc)) {
747+
if (mRestricted && mExtension && mExtension->IsRestrictedDoc(aDoc)) {
742748
return false;
743749
}
744750

@@ -752,6 +758,8 @@ bool MozDocumentMatcher::Matches(const DocInfo& aDoc) const {
752758
}
753759

754760
bool MozDocumentMatcher::MatchesURI(const URLInfo& aURL) const {
761+
MOZ_ASSERT(!mRestricted && !mCheckPermissions || mExtension);
762+
755763
if (!mMatches->Matches(aURL)) {
756764
return false;
757765
}
@@ -772,6 +780,11 @@ bool MozDocumentMatcher::MatchesURI(const URLInfo& aURL) const {
772780
return false;
773781
}
774782

783+
if (mCheckPermissions &&
784+
!mExtension->CanAccessURI(aURL, false, false, true)) {
785+
return false;
786+
}
787+
775788
return true;
776789
}
777790

toolkit/components/extensions/test/mochitest/test_ext_scripting_contentScripts.html

+2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@
3030
// Used in `file_contains_iframe.html`
3131
"*://example.org/",
3232
],
33+
granted_host_permissions: true,
3334
...manifestProps,
3435
},
36+
useAddonManager: "temporary",
3537
...otherProps,
3638
});
3739
};

0 commit comments

Comments
 (0)