Bug 1487478 - Move MatchPattern normalization into classifyPermission r=aswan

Differential Revision: https://phabricator.services.mozilla.com/D4707
This commit is contained in:
Rob Wu committed 2018-09-27 11:27:54 +02:00
1 parent e0de415fb7
commit 122583206f
3 files changed
+28 -43

No files matched your search

+27 -24
View File
@@ -123,6 +123,7 @@ const CHILD_SHUTDOWN_TIMEOUT_MS = 8000;
* as a host/origin permission, an api permission, or a regular permission.
*
* @param {string} perm The permission string to classify
* @param {boolean} restrictSchemes
*
* @returns {object}
* An object with exactly one of the following properties:
@@ -131,10 +132,15 @@ const CHILD_SHUTDOWN_TIMEOUT_MS = 8000;
* (as used for webextensions experiments).
* "permission" to indicate this is a regular permission.
*/
function classifyPermission(perm) {
function classifyPermission(perm, restrictSchemes) {
let match = /^(\w+)(?:\.(\w+)(?:\.\w+)*)?$/.exec(perm);
if (!match) {
return {origin: perm};
try {
let {pattern} = new MatchPattern(perm, {restrictSchemes, ignorePath: true});
return {origin: pattern};
} catch (e) {
return {originInvalid: perm};
}
} else if (match[1] == "experiments" && match[2]) {
return {api: match[2]};
}
@@ -470,11 +476,12 @@ class ExtensionData {
let permissions = new Set();
let origins = new Set();
let restrictSchemes = !this.hasPermission("mozillaAddons");
for (let perm of this.manifest.permissions || []) {
let type = classifyPermission(perm);
let type = classifyPermission(perm, restrictSchemes);
if (type.origin) {
origins.add(perm);
} else if (!type.api) {
} else if (type.permission) {
permissions.add(perm);
}
}
@@ -506,7 +513,10 @@ class ExtensionData {
}
let result = {
origins: this.whiteListedHosts.patterns.map(matcher => matcher.pattern),
origins: this.whiteListedHosts.patterns.map(matcher => matcher.pattern)
// moz-extension://id/* is always added to whiteListedHosts, but it
// is not a valid host permission in the API. So, remove it.
.filter(pattern => !pattern.startsWith("moz-extension:")),
apis: [...this.apiNames],
};
@@ -628,19 +638,15 @@ class ExtensionData {
}
}
let type = classifyPermission(perm);
let type = classifyPermission(perm, restrictSchemes);
if (type.origin) {
try {
let matcher = new MatchPattern(perm, {restrictSchemes, ignorePath: true});
perm = matcher.pattern;
originPermissions.add(perm);
} catch (e) {
this.manifestWarning(`Invalid host permission: ${perm}`);
continue;
}
perm = type.origin;
originPermissions.add(perm);
} else if (type.api) {
apiNames.add(type.api);
} else if (type.originInvalid) {
this.manifestWarning(`Invalid host permission: ${perm}`);
continue;
}
permissions.add(perm);
@@ -1069,15 +1075,11 @@ class ExtensionData {
allUrls = true;
break;
}
if (permission.startsWith("moz-extension:")) {
continue;
}
let match = /^[a-z*]+:\/\/([^/]+)\//.exec(permission);
let match = /^[a-z*]+:\/\/([^/]*)\//.exec(permission);
if (!match) {
Cu.reportError(`Unparseable host permission ${permission}`);
continue;
throw new Error(`Unparseable host permission ${permission}`);
}
if (match[1] == "*") {
if (!match[1] || match[1] == "*") {
allUrls = true;
} else if (match[1].startsWith("*.")) {
wildcards.add(match[1].slice(2));
@@ -1993,8 +1995,9 @@ class Extension extends ExtensionData {
get optionalOrigins() {
if (this._optionalOrigins == null) {
let origins = this.manifest.optional_permissions.filter(perm => classifyPermission(perm).origin);
this._optionalOrigins = new MatchPatternSet(origins, {restrictSchemes: !this.hasPermission("mozillaAddons"), ignorePath: true});
let restrictSchemes = !this.hasPermission("mozillaAddons");
let origins = this.manifest.optional_permissions.filter(perm => classifyPermission(perm, restrictSchemes).origin);
this._optionalOrigins = new MatchPatternSet(origins, {restrictSchemes, ignorePath: true});
}
return this._optionalOrigins;
}
@@ -489,24 +489,6 @@
}
]
},
{
"id": "MatchPatternInternal",
"description": "Same as MatchPattern above, but includes moz-extension protocol",
"choices": [
{
"type": "string",
"enum": ["<all_urls>"]
},
{
"type": "string",
"pattern": "^(https?|wss?|file|ftp|moz-extension|\\*)://(\\*|\\*\\.[^*/]+|[^*/]+)/.*$"
},
{
"type": "string",
"pattern": "^file:///.*$"
}
]
},
{
"id": "ContentScript",
"type": "object",
@@ -33,7 +33,7 @@
},
"origins": {
"type": "array",
"items": { "$ref": "manifest.MatchPatternInternal" },
"items": { "$ref": "manifest.MatchPattern" },
"optional": true,
"default": []
}