generated from abhijithvijayan/web-extension-starter
/
index.ts
297 lines (266 loc) · 7.66 KB
/
index.ts
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
/*
* Zed Attack Proxy (ZAP) and its related source files.
*
* ZAP is an HTTP/HTTPS proxy for assessing web application security.
*
* Copyright 2023 The ZAP Development Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import Browser, {Runtime} from 'webextension-polyfill';
import {
ReportedElement,
ReportedObject,
ReportedStorage,
ReportedEvent,
} from '../types/ReportedModel';
import Recorder from './recorder';
import {
LOCAL_STORAGE,
LOCAL_ZAP_ENABLE,
LOCAL_ZAP_URL,
REPORT_EVENT,
REPORT_OBJECT,
SESSION_STORAGE,
ZAP_START_RECORDING,
ZAP_STOP_RECORDING,
} from '../utils/constants';
const reportedObjects = new Set<string>();
const reportedEvents: {[key: string]: ReportedEvent} = {};
const recorder = new Recorder();
function reportStorage(
name: string,
storage: Storage,
fn: (re: ReportedStorage) => void
): void {
const url = window.location.href;
for (const key of Object.keys(storage)) {
fn(new ReportedStorage(name, '', key, '', storage.getItem(key), url));
}
}
async function sendEventToZAP(obj: ReportedEvent): Promise<number> {
return Browser.runtime.sendMessage({
type: REPORT_EVENT,
data: obj.toString(),
});
}
async function sendObjectToZAP(obj: ReportedObject): Promise<number> {
return Browser.runtime.sendMessage({
type: REPORT_OBJECT,
data: obj.toString(),
});
}
function reportObject(repObj: ReportedObject): void {
const repObjStr = repObj.toShortString();
if (!reportedObjects.has(repObjStr)) {
sendObjectToZAP(repObj);
reportedObjects.add(repObjStr);
}
}
function reportAllStorage(): void {
reportStorage(LOCAL_STORAGE, localStorage, reportObject);
reportStorage(SESSION_STORAGE, sessionStorage, reportObject);
}
function withZapEnableSetting(fn: () => void): void {
Browser.storage.sync.get({zapenable: true}).then((items) => {
if (items.zapenable) {
fn();
}
});
}
function withZapRecordingActive(fn: () => void): void {
Browser.storage.sync.get({zaprecordingactive: false}).then((items) => {
if (items.zaprecordingactive) {
fn();
}
});
}
function reportPageUnloaded(): void {
withZapEnableSetting(() => {
Browser.runtime.sendMessage({
type: REPORT_EVENT,
data: new ReportedEvent('pageUnload').toString(),
});
for (const value of Object.values(reportedEvents)) {
sendEventToZAP(value);
}
reportAllStorage();
});
}
function reportEvent(event: ReportedEvent): void {
let existingEvent: ReportedEvent;
existingEvent = reportedEvents[event.eventName];
if (!existingEvent) {
existingEvent = new ReportedEvent(event.eventName);
reportedEvents[event.eventName] = event;
sendEventToZAP(existingEvent);
} else if (existingEvent.url !== window.location.href) {
// The fragment has changed - report the old one and start a new count
sendEventToZAP(existingEvent);
existingEvent = new ReportedEvent(event.eventName);
reportedEvents[event.eventName] = event;
sendEventToZAP(existingEvent);
} else {
// eslint-disable-next-line no-plusplus
existingEvent.count++;
}
}
function reportPageForms(
doc: Document,
fn: (re: ReportedObject) => void
): void {
const url = window.location.href;
Array.prototype.forEach.call(doc.forms, (form: HTMLFormElement) => {
fn(new ReportedElement(form, url));
});
}
function reportPageLinks(
doc: Document,
fn: (re: ReportedObject) => void
): void {
const url = window.location.href;
Array.prototype.forEach.call(
doc.links,
(link: HTMLAnchorElement | HTMLAreaElement) => {
fn(new ReportedElement(link, url));
}
);
}
function reportElements(
collection: HTMLCollection,
fn: (re: ReportedObject) => void
): void {
const url = window.location.href;
Array.prototype.forEach.call(collection, (element: Element) => {
fn(new ReportedElement(element, url));
});
}
function reportNodeElements(
node: Node,
tagName: string,
fn: (re: ReportedObject) => void
): void {
if (node.nodeType === Node.ELEMENT_NODE) {
reportElements((node as Element).getElementsByTagName(tagName), fn);
}
}
function reportPageLoaded(
doc: Document,
fn: (re: ReportedObject) => void
): void {
const url = window.location.href;
if (url.indexOf('/zapCallBackUrl/') > 0) {
// The Browser has been launched from ZAP - use this URL for comms
Browser.runtime.sendMessage({
type: 'zapDetails',
data: {zapurl: url, zapkey: ''},
});
return;
}
Browser.runtime.sendMessage({
type: REPORT_EVENT,
data: new ReportedEvent('pageLoad').toString(),
});
reportPageLinks(doc, fn);
reportPageForms(doc, fn);
reportElements(doc.getElementsByTagName('input'), fn);
reportElements(doc.getElementsByTagName('button'), fn);
reportStorage(LOCAL_STORAGE, localStorage, fn);
reportStorage(SESSION_STORAGE, sessionStorage, fn);
}
const domMutated = function domMutation(
mutationList: MutationRecord[],
_obs: MutationObserver
): void {
withZapEnableSetting(() => {
reportEvent(new ReportedEvent('domMutation'));
reportPageLinks(document, reportObject);
reportPageForms(document, reportObject);
for (const mutation of mutationList) {
if (mutation.type === 'childList') {
reportNodeElements(mutation.target, 'input', reportObject);
reportNodeElements(mutation.target, 'button', reportObject);
}
}
});
};
function onLoadEventListener(): void {
Browser.storage.sync.set({zaprecordingactive: false});
withZapEnableSetting(() => {
reportPageLoaded(document, reportObject);
});
}
function enableExtension(): void {
window.addEventListener('load', onLoadEventListener, false);
window.onbeforeunload = reportPageUnloaded;
const observer = new MutationObserver(domMutated);
observer.observe(document, {
attributes: false,
childList: true,
subtree: true,
});
setInterval(() => {
// Have to poll to pickup storage changes in a timely fashion
reportAllStorage();
}, 500);
// This is needed for more traditional apps
reportPageLoaded(document, reportObject);
}
function configureExtension(): void {
const localzapurl = localStorage.getItem(LOCAL_ZAP_URL);
const localzapenable = localStorage.getItem(LOCAL_ZAP_ENABLE) || true;
if (localzapurl) {
Browser.storage.sync.set({
zapurl: localzapurl,
zapenable: localzapenable !== 'false',
});
}
}
function injectScript(): Promise<boolean> {
return new Promise((resolve) => {
configureExtension();
withZapRecordingActive(() => {
recorder.recordUserInteractions();
});
withZapEnableSetting(() => {
enableExtension();
resolve(true);
});
resolve(false);
});
}
injectScript();
Browser.runtime.onMessage.addListener(
(message: MessageEvent, _sender: Runtime.MessageSender) => {
if (message.type === ZAP_START_RECORDING) {
configureExtension();
recorder.recordUserInteractions();
recorder.initializationScript();
} else if (message.type === ZAP_STOP_RECORDING) {
recorder.stopRecordingUserInteractions();
}
}
);
export {
reportPageLinks,
reportPageLoaded,
reportPageForms,
reportNodeElements,
reportStorage,
ReportedElement,
ReportedObject,
ReportedStorage,
ReportedEvent,
injectScript,
enableExtension,
};