Skip to content

Commit

Permalink
Properly encode greasemonkey-script URI's paths.
Browse files Browse the repository at this point in the history
This ensures URIs containing reserved characters for URI components
(like "#") are handled correctly. Previously, any modification to
such an URI's hash would lead to mis-parsing of the resource name.

One example where we don't fully control the URI's hash are favicons,
which have their icon size appended to the hash.

Fixes greasemonkey#1955.
  • Loading branch information
Ventero committed Sep 7, 2014
1 parent dccd743 commit ccf3641
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
9 changes: 7 additions & 2 deletions components/scriptProtocol.js
Expand Up @@ -71,7 +71,11 @@ ScriptProtocol.prototype.newURI = function(aSpec, aCharset, aBaseUri) {

// nsIProtocolHandler
ScriptProtocol.prototype.newChannel = function(aUri) {
var m = aUri.spec.match(/greasemonkey-script:([-0-9a-f]+)\/(.*)/);
// The URI's path is in the following format:
// greasemonkey-script:<scriptUuid>/<encodedResourceName>
// The location's hash ("ref") has to be ignored, as it may contain data we
// don't control for certain URIs (e.g. the image size for favicons, #1955).
var m = aUri.specIgnoringRef.match(/greasemonkey-script:([-0-9a-f]+)\/(.*)/);

// Incomplete URI, send a 404.
if (!m) return new DummyChannel(aUri);
Expand All @@ -81,8 +85,9 @@ ScriptProtocol.prototype.newChannel = function(aUri) {
})[0];

if (script) {
var name = decodeURIComponent(m[2]);
for (var i = 0, resource = null; resource = script.resources[i]; i++) {
if (resource.name == m[2]) {
if (resource.name == name) {
return ioService.newChannelFromURI(
GM_util.getUriFromFile(resource.file));
}
Expand Down
3 changes: 2 additions & 1 deletion modules/miscapis.js
Expand Up @@ -203,7 +203,8 @@ function GM_Resources(script){
}

GM_Resources.prototype.getResourceURL = function(aScript, name) {
return ['greasemonkey-script:', aScript.uuid, '/', name].join('');
var encodedName = encodeURIComponent(name);
return ['greasemonkey-script:', aScript.uuid, '/', encodedName].join('');
};

GM_Resources.prototype.getResourceText = function(name) {
Expand Down

0 comments on commit ccf3641

Please sign in to comment.