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
|
// http://lists.w3.org/Archives/Public/www-archive/2009Sep/att-0051/draft-hodges-strict-transport-sec-05.plain.html
const STS = {
enabled: false,
get db() {
delete this.db;
return this.initPersistentDB();
},
initPersistentDB: function() {
return this.db = new STSDB(STSPersistence);
},
processRequest: function(chan) {
if (this.enabled) {
var uri = chan.URI;
if (uri.schemeIs("https")) {
try {
this.db.processHeader(uri.asciiHost, chan.getResponseHeader("Strict-Transport-Security"));
} catch (e) {}
}
}
},
isSTSURI: function(uri) {
return this.enabled && this.db.matches(uri.asciiHost);
},
enterPrivateBrowsing: function() {
try {
this.db.save();
} catch(e) {}
this.db = new STSDB(this.db);
},
exitPrivateBrowsing: function() {
this.initPersistentDB();
},
eraseDB: function() {
this.db.reset();
STSPersistence.save(this.db);
},
patchErrorPage: function(docShell, errorURI) {
// see #errors-in-secure-transport-establishment
if (!this.enabled) return;
if (!(/^about:certerror?/.test(errorURI.spec) &&
this.isSTSURI(docShell.currentURI))
) return;
Thread.delay(function() {
docShell.document.getElementById("expertContent").style.display = "none";
}, 100);
},
dispose: function() {
this.db.save();
}
};
function STSDB(source) {
this._entries = {};
if (source && source._entries) { // clone
var entries = source._entries;
for (var p in entries) {
this._entries[p] = entries[p];
}
} else {
if (source && source.load) {
this._persistence = source;
this.load();
}
}
}
STSDB.prototype = {
_persistence: null,
_dirty: false,
_saveTimer: null,
processHeader: function(host, header) {
if (DNS.isIP(host)) return;
var m = header.match(/^\s*max-age\s*=\s*(\d+)\s*(;\s*includeSubDomains)?/i);
if (!m) return;
var maxAge = parseInt(m[1]);
var includeSubDomains = !!m[2];
var expiration = Math.round(Date.now() / 1000) + maxAge;
if (host in this._entries) {
var e = this._entries[host];
if (e.expiration == expiration && e.includeSubDomains == includeSubDomains)
return;
e.expiration = expiration;
e.includeSubDomains = includeSubDomains;
} else {
this.add(new STSEntry(host, expiration, includeSubDomains));
}
this.saveDeferred();
},
add: function(entry) {
this._entries[entry.host] = entry;
},
matches: function(host, asSuperDomain) {
if (host in this._entries) {
var e = this._entries[host];
if (e.expiration >= Date.now() / 1000) {
if ((!asSuperDomain || e.includeSubDomains))
return true;
} else {
delete this._entries[host];
}
}
var dotPos = host.indexOf(".");
var lastDotPos = host.lastIndexOf(".");
if (dotPos == lastDotPos)
return false;
return this.matches(host.substring(dotPos + 1), true);
},
serialize: function() {
var lines = [], ee = this._entries;
var e;
for (var h in ee) {
e = ee[h];
lines.push([e.host, e.expiration, e.includeSubDomains ? "*" : ""].join(";"));
}
return lines.join("\n");
},
restore: function(s) {
s.split(/\s+/).forEach(function(line) {
if (line) {
var args = line.split(";");
if (args.length > 1)
this.add(new STSEntry(args[0], parseInt(args[1]), !!args[2]));
}
}, this);
},
load: function() {
if (this._persistence) {
this._persistence.load(this);
this.purgeExpired();
}
},
save: function() {
if (this._dirty && this._persistence) {
this.purgeExpired();
this._persistence.save(this);
this._dirty = false;
}
},
saveDeferred: function() {
if (this._dirty || !this._persistence) return;
this._dirty = true;
if (!this._timer) this._timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
this._timer.initWithCallback(this, 10000, Ci.nsITimer.TYPE_ONE_SHOT);
},
notify: function(timer) {
this.save();
},
purgeExpired: function() {
var now = Math.round(Date.now() / 1000);
for (var h in this._entries) {
if (this._entries[h].expiration < now) delete this._entries[h];
}
},
reset: function() {
this._entries = {};
this._dirty = false;
}
};
function STSEntry(host, expiration, includeSubDomains) {
this.host = host;
this.expiration = expiration;
if (includeSubDomains) this.includeSubDomains = includeSubDomains;
}
STSEntry.prototype = {
includeSubDomains: false
};
const STSPersistence = {
get _file() {
delete this._file;
var f = Cc["@mozilla.org/file/directory_service;1"].getService(
Ci.nsIProperties).get("ProfD", Ci.nsIFile);
f.append("NoScriptSTS.db");
return this._file = f;
},
load: function(db) {
var f = this._file;
try {
if (f.exists()) db.restore(IO.readFile(f));
} catch (e) {
dump("STS: Error loading db from " + f.path + "!" + e + "\n");
return false;
}
return true;
},
save: function(db) {
var f = this._file;
try {
IO.safeWriteFile(f, db.serialize());
} catch(e) {
dump("STS: Error saving db to " + f.path + "!" + e + "\n");
return false;
}
return true;
}
};
|