-
Notifications
You must be signed in to change notification settings - Fork 5
/
popup.js
347 lines (288 loc) · 13.3 KB
/
popup.js
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
"use strict";
if (typeof chrome.runtime === "undefined") chrome = browser;
document.addEventListener('DOMContentLoaded', function() {
{
const lnkVersion = document.getElementById("lblVersion");
lnkVersion.textContent = "v"+chrome.runtime.getManifest().version;
lnkVersion.addEventListener("click", function() { chrome.runtime.openOptionsPage(); }, false);
const lnkCopyForBug = document.getElementById("lnkCopyForBug");
lnkCopyForBug.addEventListener("click", function() { copyForBug(); }, false);
const lnkUnmark = document.getElementById("lnkUnmark");
lnkUnmark.addEventListener("click", function() {
lnkUnmark.textContent = "";
chrome.tabs.executeScript(null, {code:"{const u = document.querySelectorAll('.moarTLSUnsecure');for (let i = 0; i < u.length; i++) u[i].classList.remove('moarTLSUnsecure');}", allFrames: true, runAt:"document_idle"}, null);
}, false);
}
chrome.tabs.query({active: true, currentWindow: true }, function(activeTabs) {
if (activeTabs.length < 1) return; // impossible?
const activeTab = activeTabs[0];
const oUri = document.createElement("a");
oUri.href = activeTab.url;
const sOrigin = "https://" + oUri.host +"/";
const sProt = oUri.protocol.toLowerCase();
if ((sProt === "http:") || (sProt === "ftp:"))
{
document.getElementById("lnkUnmark").style.display="inline";
}
if (sProt.indexOf("chrome") == 0) {
if (oUri.host === "newtab") {
// injecting into newtab is permitted
document.getElementById("lnkDomain").style.display = "none";
}
else {
// otherwise, bail out.
document.getElementById("txtStatus").textContent = "Unfortunately, Chrome's internal pages cannot be analyzed.";
return;
}
}
// http://stackoverflow.com/questions/11613371/chrome-extension-content-script-on-https-chrome-google-com-webstore
if (oUri.href.toLowerCase().indexOf("https://chrome.google.com/webstore/") == 0)
{
document.getElementById("txtStatus").textContent = "Unfortunately, Chrome's Web Store pages cannot be analyzed.";
// Bail out.
return;
}
const lnkDomain = document.getElementById("lnkDomain");
lnkDomain.href = "https://dev.ssllabs.com/ssltest/analyze.html?d=" + escape(oUri.hostname);
lnkDomain.textContent = (((sProt == "http:") || (sProt =="ftp:")) ? (sProt.slice(0,-1)+"/") : "") + oUri.hostname;
{
document.getElementById("txtStatus").textContent = "Analyzing top-level page";
// Mark top-level Page
if (sProt == "https:")
{
document.getElementById("lnkDomain").classList.add("pageIsHTTPS");
}
// If HTTP/HTTPS, use XHR to check for HSTS
if ((sProt == "http:") || (sProt == "https:"))
{
// TODO: Switch XHR over to fetch() so that we're fully buzzword compliant.
const oReq = new XMLHttpRequest();
oReq.addEventListener("load", function() {
const sHSTS = oReq.getResponseHeader("Strict-Transport-Security");
const bHSTS = (sHSTS && sHSTS.includes("max-age=") && !sHSTS.includes("max-age=0"));
const l = document.getElementById("lnkDomain");
if (sProt != "https:") { l.classList.add("pageCanUpgrade"); }
if (bHSTS) { l.classList.add("pageIsHSTS"); l.classList.remove("pageIsHTTPS"); }
const arrLI = htLinks[sOrigin];
markLIs(arrLI, true, bHSTS);
}, false);
const fnErr = function() {
document.getElementById("lnkDomain").classList.add("pageCannotUpgrade");
const arrLI = htLinks[sOrigin];
markLIs(arrLI, false, false);
};
oReq.addEventListener("error", fnErr, false);
oReq.addEventListener("timeout", fnErr, false);
oReq.open("HEAD", sOrigin, true);
oReq.setRequestHeader("Cache-Control", "no-cache");
oReq.timeout = 5000;
oReq.send();
}
}
document.getElementById("txtStatus").textContent = "Analyzing page elements";
chrome.scripting.insertCSS({
target: {tabId: activeTab.id, allFrames: true}, files: ["injected.css"]
});
/* TODO REstore error handling after we learn how
// If you try to inject into an extensions page or the webstore/NTP you'll get an error
if (chrome.runtime.lastError) {
console.log('moarTLS error injecting css : \n' + chrome.runtime.lastError.message);
chrome.runtime.sendMessage(null, {"error": chrome.runtime.lastError.message, "context": "insertCSS"});
}
});*/
// https://developer.chrome.com/docs/extensions/mv3/intro/mv3-migration/#cs-static-file
chrome.scripting.executeScript({
target: {tabId: activeTab.id, allFrames: true}, files: ['injected.js']
});
/* TODO: Restore error handling after we learn how
chrome.tabs.executeScript(null, {file:"injected.js", allFrames: true, runAt:"document_idle"}, function() {
// If you try to inject into an extensions page or the webstore/NTP you'll get an error
if (chrome.runtime.lastError) {
console.log('moarTLS error injecting script : \n' + chrome.runtime.lastError.message);
chrome.runtime.sendMessage(null, {"error": chrome.runtime.lastError.message, "context": "executeScript"});
}
});*/
});
}, false);
function copyForBug()
{
const copyFrom = document.createElement("textarea");
// TODO: Generate a proper report
copyFrom.textContent = document.body.textContent;
document.body.appendChild(copyFrom);
copyFrom.focus();
copyFrom.select();
document.execCommand('Copy', false, null);
copyFrom.remove();
const lnkCopyForBug = document.getElementById("lnkCopyForBug");
lnkCopyForBug.textContent = "copied!";
setTimeout(function() { lnkCopyForBug.innerHTML = "Copy"; }, 450);
}
function computeLinksDisplayString(cInsecure, cTotal)
{
if (cTotal < 1) return "This page does not contain any links.";
if (cInsecure < 1) {
if (cTotal == 1) return "The only link on this page is secure.";
if (cTotal == 2) return "Both links on this page are secure.";
return "All " + cTotal + " links on this page are secure.";
}
if (cInsecure == cTotal) {
if (cTotal == 1) return "The only link on this page is non-secure.";
if (cTotal == 2) return "Both links on this page are non-secure.";
return "All " + cTotal + " links on this page are non-secure.";
}
let sResult = ("\n"+cInsecure + " of " + cTotal + " links " + ((cInsecure == 1) ? "is" : "are") + " non-secure.");
return sResult;
}
function computeImagesDisplayString(cInsecureImages)
{
if (cInsecureImages < 1) return ""; //"This page does not contain any images.";
let sResult = ((cInsecureImages == 1) ? "One image is" : (cInsecureImages + " images are")) + " non-secure.";
return sResult;
}
// Update list UI based on HTTPS/HSTS availability
function markLIs(arrLI, bHTTPS, bHSTS)
{
// No links yet
if (!arrLI) { return; }
for (let i=0; i < arrLI.length; i++) {
if (arrLI[i].textContent.substring(0, 11) == "[Checking] ") {
arrLI[i].textContent = arrLI[i].textContent.substring(11);
}
if (bHTTPS) {
arrLI[i].classList.add("isHTTPSyes");
if (bHSTS) arrLI[i].classList.add("isHSTS");
arrLI[i].title = "This URL is available via HTTPS" + ((bHSTS) ? " + HSTS!" : ".");
}
else {
arrLI[i].classList.add("isHTTPSno");
arrLI[i].title = "This URL is NOT available by simply changing the protocol to HTTPS.";
}
}
}
// Check a target for HTTPS/HSTS availability using XHR
// TODO: Switch to FETCH and handle cases of redirections
function checkForHTTPS(lnk)
{
if ((lnk.title.substring(0,11) == "This URL is") ||
(lnk.title.substring(0,11) == "[Checking] ")) return;
const oUri = document.createElement("a");
oUri.href = lnk.textContent;
// Wipe path entirely to prevent cases where e.g. a HEAD example.com/buy
// isn't idempotent // if (oUri.pathname.includes("logout"))
// TODO if we ever remove this: ensure proper Path encoding when calling oReq.open
const sOrigin = "https://" + oUri.host +"/";
const arrLI = htLinks[sOrigin];
for (let i=0; i < arrLI.length; i++)
{
arrLI[i].textContent = "[Checking] " + arrLI[i].textContent;
arrLI[i].title = "[Checking] Using XmlHttpRequest to check for a HTTPS version of this url...";
}
const oReq = new XMLHttpRequest();
oReq.addEventListener("load", function() {
const sHSTS = oReq.getResponseHeader("Strict-Transport-Security");
const bHSTS = (sHSTS && sHSTS.includes("max-age=") && !sHSTS.includes("max-age=0"));
markLIs(arrLI, true, bHSTS);
}, false);
const fnErr = function() { markLIs(arrLI, false, false); };
oReq.addEventListener("error", fnErr, false);
oReq.addEventListener("timeout", fnErr, false);
oReq.open("HEAD", sOrigin, true);
oReq.setRequestHeader("Cache-Control", "no-cache");
oReq.timeout = 5000;
oReq.send();
}
// Total number of elements evaluated in the page
var cTotalLinks = 0;
// Total number of non-secure elements in the page
var cLinksUnsecure = 0;
// Total number of non-secure images in the page
var cImagesUnsecure = 0;
// Hashtable mapping Origin->ListItem[]
var htLinks = {};
chrome.runtime.onMessage.addListener( function(request, sender, sendResponse) {
if (request.error)
{
let divError = document.createElement("div");
divError.className = "scanErrorMessage";
divError.textContent = "Error in " + request.context + ": " + request.error;
document.body.appendChild(divError);
return;
}
cTotalLinks += request.LinkCount || 0;
cLinksUnsecure += (request.unsecure) ? request.unsecure.length : 0;
cImagesUnsecure += (request.NonSecureImages) ? request.NonSecureImages.length : 0;
const bAnyInsecure = (cLinksUnsecure + cImagesUnsecure > 0);
document.getElementById("txtStatus").textContent = computeLinksDisplayString(cLinksUnsecure, cTotalLinks, cImagesUnsecure);
document.getElementById("txtStatus2").textContent = computeImagesDisplayString(cImagesUnsecure);
if (bAnyInsecure) {
document.body.style.backgroundColor = "#FFFF40";
document.getElementById("lnkUnmark").style.display="inline";
document.getElementById("lnkCopyForBug").style.display="inline";
document.getElementById("lnkTips").style.display="inline";
}
else
{
if (document.getElementById("lnkDomain").classList.contains("pageIsHTTPS") ||
document.getElementById("lnkDomain").classList.contains("pageIsHSTS"))
{
document.body.style.backgroundColor = "#68FF68";
}
}
let listUnsecure = document.getElementById("olUnsecureList");
if (!listUnsecure && bAnyInsecure)
{
listUnsecure = document.createElement("ol");
listUnsecure.id = "olUnsecureList";
document.getElementById("divUnsecureList").appendChild(listUnsecure);
}
for (let i=0; i < request.unsecure.length; i++) {
const listItem = document.createElement("li");
const text = document.createTextNode(request.unsecure[i]);
listItem.appendChild(text);
const oUri = document.createElement("a");
oUri.href = request.unsecure[i];
const sOrigin = "https://" + oUri.host +"/";
if (undefined === htLinks[sOrigin])
{
htLinks[sOrigin] = [];
}
htLinks[sOrigin].push(listItem);
listItem.addEventListener('click', function(e) {
if ((e.altKey || e.ctrlKey) || (1 == e.button))
{
document.getElementById("lnkTips").style.display = "none";
checkForHTTPS(this);
return;
}
}, false);
listUnsecure.appendChild(listItem);
}
for (let i=0; i < request.NonSecureImages.length; i++) {
const listItem = document.createElement("li");
const text = document.createTextNode(request.NonSecureImages[i]);
listItem.appendChild(text);
const oUri = document.createElement("a");
oUri.href = request.NonSecureImages[i];
const sOrigin = "https://" + oUri.host +"/";
if (undefined === htLinks[sOrigin])
{
htLinks[sOrigin] = [];
}
htLinks[sOrigin].push(listItem);
listItem.addEventListener('click', function(e) {
if ((e.altKey || e.ctrlKey) || (1 == e.button))
{
document.getElementById("lnkTips").style.display = "none";
checkForHTTPS(this);
return;
}
}, false);
listUnsecure.appendChild(listItem);
}
});
window.addEventListener('click', function(e) {
if ((e.target.nodeName == "A") && (e.target.href !== undefined)) {
chrome.tabs.create({ url: e.target.href });
}
});