-
Notifications
You must be signed in to change notification settings - Fork 12
/
background.js
173 lines (149 loc) · 6.5 KB
/
background.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
var versionIconMap = new Map();
versionIconMap.set('TLSv1.3', 'icons/tlsv13.png');
versionIconMap.set('TLSv1.2', 'icons/tlsv12.png');
versionIconMap.set('TLSv1.1', 'icons/tlsv11.png');
versionIconMap.set('TLSv1', 'icons/tlsv10.png');
versionIconMap.set('unknown', 'icons/tlsunknown.png');
var versionIconWarningMap = new Map();
versionIconWarningMap.set('TLSv1.3', 'icons/tlsv13_warning.png');
versionIconWarningMap.set('TLSv1.2', 'icons/tlsv12_warning.png');
versionIconWarningMap.set('TLSv1.1', 'icons/tlsv11_warning.png');
versionIconWarningMap.set('TLSv1', 'icons/tlsv10_warning.png');
var versionComparisonMap = new Map();
versionComparisonMap.set('TLSv1.3', 13);
versionComparisonMap.set('TLSv1.2', 12);
versionComparisonMap.set('TLSv1.1', 11);
versionComparisonMap.set('TLSv1', 10);
versionComparisonMap.set('unknown', 0);
var tabMainProtocolMap = new Map();
var tabMainDowngradedMap = new Map();
var tabSubresourceProtocolMap = new Map();
var translationElementMap = new Map();
translationElementMap.set("popupTitleResources", "popup-button-resources-text");
async function detectTheme() {
var themeInfo = await browser.theme.getCurrent();
if (themeInfo.colors && themeInfo.colors.icons === "rgb(249, 249, 250, 0.7)") {
versionIconMap.set('TLSv1.3', 'icons/tlsv13_dark.png');
versionIconWarningMap.set('TLSv1.3', 'icons/tlsv13_dark_warning.png');
} else {
versionIconMap.set('TLSv1.3', 'icons/tlsv13.png');
versionIconWarningMap.set('TLSv1.3', 'icons/tlsv13_warning.png');
}
}
detectTheme();
async function updateIcon(tabId, protocolVersion, warning) {
if (!protocolVersion) {
if (tabId >= 0) {
browser.pageAction.setTitle({ tabId: tabId, title: browser.i18n.getMessage('clearCache') });
}
} else {
if (warning) {
browser.pageAction.setIcon({
tabId: tabId, path: versionIconWarningMap.get(protocolVersion)
});
} else {
browser.pageAction.setIcon({
tabId: tabId, path: versionIconMap.get(protocolVersion)
});
}
browser.pageAction.setTitle({ tabId: tabId, title: protocolVersion});
browser.pageAction.setPopup({tabId: tabId, popup: "/popup/popup.html"});
}
}
async function loadSavedSecurityInfoAndUpdateIcon(details) {
const securityInfo = tabMainProtocolMap.get(details.tabId);
cached_version = securityInfo ? securityInfo.protocolVersion : undefined;
if (typeof cached_version !== "undefined" && cached_version !== "unknown") {
if (tabMainDowngradedMap.has(details.tabId)) {
await updateIcon(details.tabId, cached_version, tabMainDowngradedMap.get(details.tabId));
} else {
await updateIcon(details.tabId, cached_version, false);
}
} else {
await updateIcon(details.tabId, undefined, false);
}
}
function getSubresourceMap(tabId) {
if (!tabSubresourceProtocolMap.has(tabId)) {
tabSubresourceProtocolMap.set(tabId, new Map());
}
var subresourceMap = tabSubresourceProtocolMap.get(tabId);
return subresourceMap;
}
async function processSecurityInfo(details) {
try {
const securityInfo = await browser.webRequest.getSecurityInfo(details.requestId,{});
if (typeof securityInfo === "undefined") {
return;
}
// save the security info for the current tab and update the page action icon
if (details.type === 'main_frame') {
tabMainProtocolMap.set(details.tabId, securityInfo);
tabMainDowngradedMap.set(details.tabId, false);
await updateIcon(details.tabId, securityInfo.protocolVersion, false);
}
// save the security info for third party hosts that were loaded within
// the current tab
const host = (new URL(details.url)).host;
var subresourceMap = getSubresourceMap(details.tabId);
subresourceMap.set(host, securityInfo);
tabSubresourceProtocolMap.set(details.tabId, subresourceMap);
var mainProtocolVersion = versionComparisonMap.get(tabMainProtocolMap.get(details.tabId).protocolVersion);
for (const securityInfo of subresourceMap.values()) {
if (versionComparisonMap.get(securityInfo.protocolVersion) < mainProtocolVersion) {
tabMainDowngradedMap.set(details.tabId, true);
await updateIcon(details.tabId, tabMainProtocolMap.get(details.tabId).protocolVersion, true);
break;
}
}
} catch(error) {
console.error(error);
}
}
// clear security info when navigating to a different URL
function handleNavigation(details) {
tabSubresourceProtocolMap.set(details.tabId, new Map());
}
// extension internal event handling to pass information from
// background to page action ("foreground")
function handleMessage(request, sender, sendResponse) {
var response;
try {
switch (request.type) {
case 'request':
const is_undefined = typeof request.key === 'undefined';
if (request.resource === 'tabSubresourceProtocolMap') {
response = {
requested_info: is_undefined ? tabSubresourceProtocolMap : tabSubresourceProtocolMap.get(request.key)
};
} else if (request.resource === 'tabMainProtocolMap') {
response = {
requested_info: is_undefined ? tabMainProtocolMap : tabMainProtocolMap.get(request.key)
};
} else if (request.resource === 'versionComparisonMap') {
response = {
requested_info: is_undefined ? versionComparisonMap : versionComparisonMap.get(request.key)
};
} else {
response = new Error(browser.i18n.getMessage('invalidResourceRequest'));
}
break;
default:
response = new Error(browser.i18n.getMessage('invalidMessageRequest'));
}
} catch (error) {
response = error;
}
sendResponse(response);
}
browser.webRequest.onHeadersReceived.addListener(processSecurityInfo,
{urls: ["https://*/*"]}, ["blocking", "responseHeaders"]
);
browser.webRequest.onCompleted.addListener(loadSavedSecurityInfoAndUpdateIcon,
{urls: ["https://*/*"]}
);
browser.webNavigation.onBeforeNavigate.addListener(handleNavigation,
{url: [{schemes: ["https"]} ]}
);
browser.runtime.onMessage.addListener(handleMessage);
browser.theme.onUpdated.addListener(detectTheme);