File: permission-manager.js

package info (click to toggle)
conkeror 1.0.3%2Bgit170123-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,988 kB
  • sloc: ansic: 280; sh: 255; xml: 173; makefile: 69
file content (299 lines) | stat: -rw-r--r-- 15,026 bytes parent folder | download
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/**
 * (C) Copyright 2008 Jeremy Maitin-Shepard
 *
 * Use, modification, and distribution are subject to the terms specified in the
 * COPYING file.
**/

/**
 * Interface to nsIPermissionManager, which controls the popup
 * blocking whitelist, among other things.
 */

let permission_manager = Cc["@mozilla.org/permissionmanager;1"].getService(Ci.nsIPermissionManager);

let permission_types = {
    popup : {desc: "specifies a whitelist and blacklist for the popup blocker",
             values: [ ["allow", Ci.nsIPermissionManager.ALLOW_ACTION],
                       ["deny", Ci.nsIPermissionManager.DENY_ACTION] ],
             related_prefs: [
                 [ "dom.disable_open_during_load",
                   "This preference defines the default behavior of whether " +
                   "unrequested popups are allowed for sites not listed in the permission list." ],
                 [ "dom.popup_maximum", "The number of pop-ups to allow from a single non-click event."] ]
            },

    cookie : {desc: "specifies per-host cookie policies",
              values: [
                  ["allow", Ci.nsIPermissionManager.ALLOW_ACTION],
                  ["session", Ci.nsICookiePermission.ACCESS_SESSION, "expire matching cookies when the browser exits"],
                  ["deny", Ci.nsIPermissionManager.DENY_ACTION] ],
              related_prefs: [
                  [ "network.cookie.lifetime.behavior.enabled" ],
                  [ "network.cookie.cookieBehavior",
                    "This preference defines the default cookie behavior for sites not listed in the " +
                    "permission list.  The value 0 means to enable all cookies, 1 means to reject " +
                    "only third-party cookies, and 2 means to reject all cookies." ],
                  [ "network.cookie.lifetime.behavior",
                    "If network.cookie.lifetime.behavior.enabled is set to true, a value of 0 means all " +
                    "cookies expire at the end of the current session,  while a value of 1 means that " +
                    "cookies expire after the number of days specified by network.cookie.lifetime.days."
                  ],
                  [ "network.cookie.lifetime.days" ] ]
             },


    image : {desc: "specifies per-host image automatic loading policies",
              values: [
                  ["allow", Ci.nsIPermissionManager.ALLOW_ACTION],
                  ["deny", Ci.nsIPermissionManager.DENY_ACTION] ],
              related_prefs: [
                  [ "permissions.default.image", "This prefreence defines the default image loading policy "
                                                 + "for sites not listed in the permission list.  The value "
                                                 + "1 means all images should be loaded, 2 means no images "
                                                 + "should be loaded, and 3 means only third-party images "
                                                 + "are blocked." ] ]
            },

    install : {desc: "specifies a whitelist of sites from which XPI files may be opened",
              values: [
                  ["allow", Ci.nsIPermissionManager.ALLOW_ACTION] ]
              }
};

/*
 * cookie
 *
 *
 *
 * popup
 *   dom.popup_maximum  -  The number of pop-ups to allow from a single non-click event
 *
 *   dom.popup_allowed_events
 *
 *   dom.disable_open_during_load - This preference defines the default behavior of whether unrequested popups are allowed for sites not listed in the permission list.
 *
 *
 */

interactive("permission-manager", "View or edit the host-specific "
            + "permission list.\nThis list is used for the popup"
            + "blocker whitelist, among other things.",
            function (I) {
                I.minibuffer.message("Save the file and close the editor when done editing permissions.");

                let existing_perms = {};

                var file_buf =
                    "# -*- conf -*-\n" +
                    "# Permission list\n\n";


                {
                    let e = permission_manager.enumerator;
                    let arr = [];
                    let max_host_len = 0;
                    let max_type_len = 0;
                    while (e.hasMoreElements()) {
                        let p = e.getNext().QueryInterface(Ci.nsIPermission);
                        let host;
                        if (version_compare(get_mozilla_version(), "42.0") >= 0)
                            host = p.principal.origin;
                        else
                            host = p.host;
                        let type = p.type;
                        let cap = p.capability;
                        if (max_host_len < host.length)
                            max_host_len = host.length;
                        if (max_type_len < type.length)
                            max_type_len = type.length;
                        arr.push([host, type, cap]);
                        existing_perms[""+[host, type]] = cap;
                    }
                    ++max_host_len;
                    ++max_type_len;
                    let max_host = get_spaces(max_host_len);
                    let max_type = get_spaces(max_type_len);
                    for (let i = 0; i < arr.length; ++i) {
                        let [host, type, cap] = arr[i];
                        if (permission_types.hasOwnProperty(type)) {
                            let values = permission_types[type].values;
                            for (let j = 0; j < values.length; ++j) {
                                if (cap == values[j][1]) {
                                    cap = values[j][0];
                                    break;
                                }
                            }
                        }
                        file_buf += host + max_host.substr(host.length) + type + max_type.substr(type.length) + cap + "\n";
                    }

                    if (arr.length == 0)
                        file_buf += "\n";
                }

                let example = (version_compare(get_mozilla_version(), "42.0") >= 0) ?
                    "https://google.com" : "google.com";
                let url_type = (version_compare(get_mozilla_version(), "42.0") >= 0) ?
                    "protocol://domain" : "domain";
                file_buf += "\n" +
                    "# entry syntax (one per line): <" + url_type + "> <type> <permission>\n\n" +
                    "# example: " + example + " popup allow\n\n" +

                    word_wrap("The <domain> must be a valid domain name.  Depending on the <type>, only exact " +
                              "matches may be used, or alternatively it may match any sub-domain if a more " +
                              "specific entry is not found.", 80, "# ") + "\n" +
                    "# The possible values for the permission <type> include:\n";
                for (let type in permission_types) {
                    let data = permission_types[type];
                    file_buf += "#   " + type + " - " + data.desc + "\n\n";
                    file_buf += "#     Supported <permission> values:\n";
                    for (let i = 0; i < data.values.length; ++i) {
                        let x = data.values[i];
                        file_buf += "#       " + x[0] + " (" + x[1] + ")";
                        if (x[3])
                            file_buf += " - " + x[3];
                        file_buf += "\n";
                    }
                    if (data.related_prefs && data.related_prefs.length > 0) {
                        file_buf += "\n#     Related Mozilla preferences:\n";
                        for (let i = 0; i < data.related_prefs.length; ++i) {
                            let x = data.related_prefs[i];
                            file_buf += "#       " + x[0] + " = " + get_pref(x[0]) + "\n";
                            if (x.length > 1) {
                                file_buf += word_wrap(x[1], 80, "#         ", "#");
                            }
                            file_buf += "\n";
                        }
                    }
                }
                var file = null;
                try {
                    file = get_temporary_file("permissions.txt");
                    let line = 4;

                    outer: while (1) {

                        write_text_file(file, file_buf);
                        yield open_file_with_external_editor(file, $line = line);

                        let new_buf = read_text_file(file);
                        if (new_buf == file_buf) {
                            I.minibuffer.message("No permission changes made.");
                            break;
                        }

                        // Parse
                        let lines = new_buf.split("\n");
                        if (lines[lines.length - 1].length == 0) // Remove extra line at end
                            lines.length = lines.length - 1;
                        let arr = [];
                        let prev_entries = {};
                        for (let i = 0; i < lines.length; ++i) {
                            // Parse each line, checking for syntax errors
                            let x = lines[i];
                            let idx = x.indexOf('#');
                            if (idx != -1)
                                x = x.substr(0, idx);
                            let parts = x.split(/\s+/);
                            if (parts.length == 1 && parts[0].length == 0)
                                continue; // ignore blank line
                            try {
                                let host = parts[0];
                                if (!/[a-zA-Z0-9-_]+(\.[a-zA-Z0-9-_]+)*/.test(host))
                                    throw "invalid host name: " + host;
                                if (parts.length < 2)
                                    throw "missing permission type";
                                let type = parts[1];
                                if (parts.length < 3)
                                    throw "missing permission value";
                                let cap = parts[2];
                                if (permission_types.hasOwnProperty(type)) {
                                    let values = permission_types[type].values;
                                    for (let i = 0; i < values.length; ++i) {
                                        if (cap == values[i][0]) {
                                            cap = values[i][1];
                                            break;
                                        }
                                    }
                                }
                                if (!/([0-9]+)/.test(cap))
                                    throw "invalid permission value: " + cap;
                                cap = parseInt(cap);
                                if (parts.length > 3)
                                    throw "too many terms";
                                if (prev_entries[""+[host,type]])
                                    throw "duplicate entry";
                                prev_entries[""+[host,type]] = true;
                                arr.push([host,type,cap]);
                            } catch (syntax_err) {
                                line = i + 1;
                                lines.splice(i+1, 0, "# ERROR on previous line: " + syntax_err, "");
                                file_buf = lines.join("\n") + "\n";
                                I.minibuffer.message("Correct the syntax error in the permissions list, " +
                                                     "or close the editor to abort.");
                                continue outer;
                            }
                        }
                        let num_added = 0;
                        let num_changed = 0;
                        for (let i = 0; i < arr.length; ++i) {
                            let [host,type,cap] = arr[i];
                            let x = existing_perms[""+[host,type]];
                            let add = false;
                            if (x === undefined) {
                                ++num_added;
                                add = true;
                            } else {
                                if (x != cap) {
                                    ++num_changed;
                                    add = true;
                                }
                                delete existing_perms[""+[host,type]];
                            }
                            if (add) {
                                if (version_compare(get_mozilla_version(), "42.0") >= 0) {
                                    permission_manager.add(make_uri(host), type, cap);
                                } else {
                                    permission_manager.add(make_uri("http://" + host), type, cap);
                                }
                            }
                        }
                        let num_removed = 0;
                        for (let [k,v] in Iterator(existing_perms)) {
                            let [host,type] = k.split(",",2);
                            ++num_removed;
                            if (version_compare(get_mozilla_version(), "42.0") >= 0) {
                                permission_manager.remove(make_uri(host),type);
                            } else {
                                permission_manager.remove(host,type);
                            }
                        }
                        let msg;
                        if (num_added == 0 && num_changed == 0 && num_removed == 0)
                            msg = "No permission changes made.";
                        else {
                            msg = "Updated permissions list: " +
                                [["added", num_added],
                                 ["changed", num_changed],
                                 ["removed", num_removed]].
                                filter(function ([caption, count]) count > 0).
                                map(function ([caption, count]) {
                                        if (count == 1)
                                             return "1 entry " + caption;
                                        return count + " entries " + caption;
                                    }).join(", ") +
                                ".";
                        }
                        I.minibuffer.message(msg);
                        break;
                    }
                } catch (e) {
                    dump_error(e);
                    throw interactive_error("Failed to edit permissions list in external editor.");
                } finally {
                    file.remove(false);
                }
            });

provide("permission-manager");