-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathframe.js
More file actions
171 lines (148 loc) · 5.24 KB
/
frame.js
File metadata and controls
171 lines (148 loc) · 5.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
* Multiprocess Firefox requires splitting scripts that run in the browser
* chrome from the document frame.
*
* This script is the document frame script.
*/
var CommandRunInit = {
onPageLoad: function(event) {
var win = event.originalTarget.defaultView.wrappedJSObject;
//win.CommandRun = new CommandRunHandler();
win.CommandRun = Components.utils.cloneInto(new CommandRunHandler(),win,{cloneFunctions: true});
win.CommandRun.page = win.document.location.href;
Object.freeze(win.CommandRun);
}
};
addEventListener("DOMContentLoaded", function(event) {
CommandRunInit.onPageLoad(event);
});
var CommandRunHandler = function() {
/* see https://blog.mozilla.org/addons/2012/08/20/exposing-objects-to-content-safely/ */
this.__exposedProps__ = {
"run" : "r"};
this.page = null;
this.run = function(command,args) {
/* check whether command is allowed */
if (!Components.utils.waiveXrays(this).isCommandAllowed(command,this.page)) {
var alertText = "CommandRun: Blocked '" + this.page +
"' from running '" + command + "'";
sendAsyncMessage("commandrun-alert", {alertText: alertText});
return 1;
}
var file = Components.classes["@mozilla.org/file/local;1"].
createInstance(Components.interfaces.nsILocalFile);
try {
file.initWithPath(command);
} catch (e) {
var alertText = "CommandRun: command not found: '" + command + "'";
sendAsyncMessage("commandrun-alert", {alertText: alertText});
return 1;
}
var blocking = true;
var process = Components.classes["@mozilla.org/process/util;1"].
createInstance(Components.interfaces.nsIProcess);
try {
process.init(file);
} catch (e) {
var alertText = "CommandRun: command not found: '" + command + "'";
sendAsyncMessage("commandrun-alert", {alertText: alertText});
return 1;
}
process.run(blocking, args, args.length);
result = process.exitValue;
return result;
};
this.isCommandAllowed = function(command,page) {
/* get the root preferences branch */
var prefs = Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefBranch);
/* get the allowed commands preference */
try {
var allowedCommandsPref =
prefs.getComplexValue(
"extensions.commandrun.allowedcommands",
Components.interfaces.nsISupportsString).data;
} catch (NS_ERROR_UNEXPECTED) {
/* ignore */
}
/* evaluate the json text */
if (allowedCommandsPref) {
var allowedCommands = JSON.parse(allowedCommandsPref);
if (Components.utils.waiveXrays(this).contains(command,allowedCommands)) return true;
}
/* continue with allowed commands per host preference */
try {
var allowedCommandsPerHostPref =
prefs.getComplexValue(
"extensions.commandrun.allowedcommandsperhost",
Components.interfaces.nsISupportsString).data;
} catch (NS_ERROR_UNEXPECTED) {
/* ignore */
}
if (allowedCommandsPerHostPref) {
var obj = JSON.parse(allowedCommandsPerHostPref);
/* keys of the object are prefixes for pages,
values are lists of commands */
try {
for (prefix in obj) {
if (Components.utils.waiveXrays(this).isPrefix(page,prefix)) {
/* get allowed commands for this prefix */
var allowedCommands = obj[prefix];
if (Components.utils.waiveXrays(this).contains(command,allowedCommands)) {
return true;
}
}
}
} catch (e) {
var alertText = "CommandRun: Failed to parse extensions.commandrun.allowedcommandsperhost: " + e;
sendAsyncMessage("commandrun-alert", {alertText: alertText});
}
}
return false;
};
this.contains = function(element,array) {
var i;
for (i=0; i<array.length; i++) {
if (array[i] === element) {
return true;
}
}
return false;
};
/**
* Checks whether the given prefix is a prefix of the
* given string.
*/
this.isPrefix = function(string,prefix) {
return (prefix === string.substring(0,prefix.length));
}
/**
* Reads a file from a directory and returns its contents.
* Useful for reading command output.
*
* The variable dir should have one of the values listed here:
* https://developer.mozilla.org/en-US/Add-ons/Code_snippets/File_I_O
* e.g. "Home" or "TmpD".
*
* WARNING: Do not use on very large files, as your browser may freeze.
*/
this.readFile = function (dir, filename, encoding = "UTF-8") {
Components.utils.import("resource://gre/modules/FileUtils.jsm");
var file = FileUtils.getFile(dir, [filename]);
var data = "";
var fstream = Components.classes["@mozilla.org/network/file-input-stream;1"].
createInstance(Components.interfaces.nsIFileInputStream);
var cstream = Components.classes["@mozilla.org/intl/converter-input-stream;1"].
createInstance(Components.interfaces.nsIConverterInputStream);
fstream.init(file, -1, 0, 0);
cstream.init(fstream, encoding, 0, 0);
let str = {};
let read = 0;
do {
read = cstream.readString(0xffffffff, str);
data += str.value;
} while (read != 0);
cstream.close();
return data;
}
}