Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
529 lines (458 loc) · 19.7 KB
/
Copy pathapp.js
File metadata and controls
529 lines (458 loc) · 19.7 KB
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
/*
* Northwind Retail Analytics: application logic.
*
* This file is deliberately vulnerable. Every flaw in it is listed in
* catalog.js, and every flaw sits inside a feature that has an independent
* reason to exist: a saved view you can deep link, a formula you can define, a
* theme that persists, a widget from another team. None of them are named after
* their sink, because real DOM-XSS is never labelled either.
*
* Beside most of them is a "safe twin": the neighbouring bit of the same
* feature written correctly, with textContent or an escaped template. Those are
* load bearing. A demo where the tool lights up on everything proves nothing.
*
* Do not copy anything out of this file.
*/
(function () {
"use strict";
/* ---------------------------------------------------------------- data -- */
var STORES = [
{ name: "Bridgeport Central", region: "Northeast", revenue: 412900, orders: 5120 },
{ name: "Cedar Hills", region: "Midwest", revenue: 388400, orders: 4870 },
{ name: "Marlowe Street", region: "Northeast", revenue: 351200, orders: 4402 },
{ name: "Halton Quay", region: "South", revenue: 297650, orders: 3980 },
{ name: "Ridgeway Park", region: "West", revenue: 265300, orders: 3411 }
];
var FIGURES = { revenue: 4182900, orders: 51038, returns: 0.047, visits: 613480 };
var BUILTIN_METRICS = {
"Average order value": function (f) { return f.revenue / f.orders; },
"Revenue per visit": function (f) { return f.revenue / f.visits; },
"Orders per visit": function (f) { return f.orders / f.visits; },
"Return rate": function (f) { return f.returns; },
"Revenue per store": function (f) { return f.revenue / 5; }
};
var API_BASE = "https://api.northwind.example/v3/";
var PARTNER_TAG = "https://tags.northwind.example/first-party.js";
/* ------------------------------------------------------------- helpers -- */
function el(id) { return document.getElementById(id); }
// Parse the fragment into key/value pairs. The fragment is how this app deep
// links a view, which is the honest reason it ends up in so many features.
function hashParams() {
var out = {};
var raw = location.hash.slice(1);
if (!raw) { return out; }
raw.split("&").forEach(function (pair) {
var i = pair.indexOf("=");
if (i === -1) { out[pair] = ""; return; }
out[pair.slice(0, i)] = decodeURIComponent(pair.slice(i + 1));
});
return out;
}
function queryParam(name) {
var raw = location.search.slice(1);
var found = null;
raw.split("&").forEach(function (pair) {
var i = pair.indexOf("=");
if (i > 0 && pair.slice(0, i) === name) {
found = decodeURIComponent(pair.slice(i + 1).replace(/\+/g, " "));
}
});
return found;
}
function money(n) {
return n.toLocaleString("en-US", { maximumFractionDigits: 0 });
}
function log(what) {
if (window.console && console.info) { console.info("[northwind] " + what); }
}
var P = hashParams();
/* --------------------------------------------------------------- seeds -- */
/*
* Three features take their input from browser storage rather than from the
* URL: the saved theme, the resume strip and the draft note. To exercise
* those from a single URL the app can seed the store and then reload, so that
* on the second load the value genuinely arrives from storage.
*
* A fragment-only change does not re-run the page, so the reload is explicit.
* The replace target is a constant: building it out of location.pathname
* would put a second, uninteresting flow in every trace.
*/
function seedAndReload(key) {
if (P["seed-theme"] !== undefined && key === "theme") {
localStorage.setItem("nw.theme", P["seed-theme"]);
location.replace("index.html#theme");
location.reload();
return true;
}
if (P["seed-resume"] !== undefined && key === "resume") {
// VULNERABLE (document.cookie sink): the resume marker is attacker
// controlled and is written straight into a cookie.
document.cookie = "nw_resume=" + encodeURIComponent(P["seed-resume"]) + "; path=/";
// SAFE TWIN: the session id is generated here, not taken from input.
document.cookie = "nw_sid=" + Math.floor(Math.random() * 1e9) + "; path=/";
location.replace("index.html#resume");
location.reload();
return true;
}
if (P["seed-draft"] !== undefined && key === "draft") {
sessionStorage.setItem("nw.draft", P["seed-draft"]);
sessionStorage.setItem("nw.draftCount", "1");
location.replace("index.html#draft");
location.reload();
return true;
}
return false;
}
if (seedAndReload("theme") || seedAndReload("resume") || seedAndReload("draft")) {
return; // the reload takes it from here
}
/* ---------------------------------------------------------- base render -- */
function renderStores() {
var body = el("store-rows");
body.textContent = "";
STORES.forEach(function (s) {
var tr = document.createElement("tr");
[s.name, s.region, money(s.revenue), money(s.orders)].forEach(function (v) {
var td = document.createElement("td");
td.textContent = v; // SAFE: store data is never markup
tr.appendChild(td);
});
body.appendChild(tr);
});
}
// Referenced by name from the auto refresh feature below, so it has to be a
// global. That is also why a string handler is tempting there in the first
// place.
window.refreshTiles = function refreshTiles() {
el("kpi-revenue").textContent = money(FIGURES.revenue);
el("kpi-orders").textContent = money(FIGURES.orders);
el("kpi-returns").textContent = (FIGURES.returns * 100).toFixed(1) + "%";
log("tiles refreshed");
};
renderStores();
window.refreshTiles();
/* ---------------------------------------------- 1. deep linked saved view -- */
(function savedView() {
if (P.view === undefined) { return; }
// VULNERABLE (Element.innerHTML): a saved view label is rendered as markup
// so a team can emphasise a word in the name of a shared view.
el("view-label").innerHTML = P.view;
// SAFE TWIN: the description under it does the same job with textContent.
el("view-note").textContent = "Saved view: " + P.view + ", rolling 28 days.";
log("saved view applied");
}());
/* -------------------------------------------------- 2. search results head -- */
(function search() {
var q = queryParam("q");
if (q === null) { return; }
el("search-panel").hidden = false;
// VULNERABLE (Element.insertAdjacentHTML): the header echoes the search
// term with the matched part wrapped in <mark>.
el("search-head").insertAdjacentHTML(
"beforeend",
"<h2>Results for <mark>" + q + "</mark></h2>"
);
// SAFE TWIN: the result rows are built from the same term with textContent.
var list = el("search-results");
STORES.slice(0, 3).forEach(function (s) {
var li = document.createElement("li");
li.textContent = s.name + ", matched on “" + q + "”";
list.appendChild(li);
});
log("search rendered");
}());
/* --------------------------------------------- 3. custom computed metric -- */
(function computedMetric() {
if (P.metric === undefined) {
// SAFE TWIN: the shipped metrics are looked up, never evaluated.
var fn = BUILTIN_METRICS["Average order value"];
el("computed-value").textContent = fn(FIGURES).toFixed(2);
return;
}
var formula = P.metric;
var revenue = FIGURES.revenue, orders = FIGURES.orders,
returns = FIGURES.returns, visits = FIGURES.visits;
var value;
try {
// VULNERABLE (eval): a custom metric is a formula over the figures above,
// and the quickest way to support that is to evaluate it.
value = eval(formula);
} catch (e) {
value = "error";
}
el("computed-label").textContent = "Custom metric";
el("computed-formula").textContent = formula;
el("computed-value").textContent =
(typeof value === "number") ? value.toFixed(2) : String(value);
log("custom metric evaluated");
}());
/* ------------------------------------------------- 4. auto refresh timer -- */
(function autoRefresh() {
if (P.refresh === undefined) {
// SAFE TWIN: the default refresh passes the function itself.
setTimeout(window.refreshTiles, 60000);
return;
}
// VULNERABLE (setTimeout with a string): the saved view carries its own
// refresh action, and it is scheduled as source text.
setTimeout(P.refresh, 4000);
el("refresh-note").textContent = "Auto refresh: " + P.refresh;
log("refresh handler scheduled");
}());
/* -------------------------------------------------- 5. custom tick handler -- */
(function tickHandler() {
if (P.tick === undefined) { return; }
try {
// VULNERABLE (new Function): the same idea one layer down, compiling a
// handler body instead of scheduling it.
var handler = new Function(P.tick);
handler();
} catch (e) {
log("tick handler failed");
}
}());
/* --------------------------------------------- 6. embedded report widget -- */
(function reportWidget() {
var frame = el("widget-frame");
var payload = (P.widget !== undefined) ? P.widget : "Regional summary is up to date.";
// The widget is configured through its URL, the way an embedded third party
// report normally is.
frame.src = "widget.html#msg=" + encodeURIComponent(payload);
window.addEventListener("message", function (e) {
// No origin check, and the body is trusted as markup. This is the flaw:
// the host page treats anything the widget says as its own HTML.
var data = e.data;
if (!data || typeof data !== "object") { return; }
// VULNERABLE (Element.innerHTML from postMessage).
el("widget-body").innerHTML = data.body;
// SAFE TWIN: the title arrives in the same message and is set as text.
el("widget-title").textContent = data.title || "Partner report";
log("widget message rendered");
});
}());
/* --------------------------------------------- 7. partner analytics tag -- */
(function partnerTag() {
// SAFE TWIN: the first party tag URL is a constant.
var own = document.createElement("script");
own.src = PARTNER_TAG;
own.async = true;
document.head.appendChild(own);
if (P.partner === undefined) { return; }
// VULNERABLE (HTMLScriptElement.src): a partner tag can be switched on per
// view, and the URL comes with the link.
var tag = document.createElement("script");
tag.src = P.partner;
tag.async = true;
document.head.appendChild(tag);
log("partner tag injected");
}());
/* ------------------------------------------------ 8. staging API override -- */
(function apiOverride() {
var base = (P.api !== undefined) ? P.api : API_BASE;
// VULNERABLE when overridden (fetch): the base URL for the summary call.
fetch(base + "summary.json", { mode: "cors" })
.then(function () { log("summary loaded"); })
.catch(function () { log("summary request failed, as expected offline"); });
// VULNERABLE when overridden (XMLHttpRequest.open): the older export path
// still uses XHR, so the same override reaches a second sink.
try {
var xhr = new XMLHttpRequest();
xhr.open("GET", base + "export.csv", true);
xhr.onerror = function () { log("export request failed, as expected offline"); };
xhr.send();
} catch (e) {
log("xhr blocked");
}
if (P.api !== undefined) { log("api base overridden"); }
}());
/* --------------------------------------------------- 9. live updates feed -- */
(function liveFeed() {
if (P.live !== undefined) {
try {
// VULNERABLE (WebSocket): the live endpoint is taken from the link.
var ws = new WebSocket(P.live);
ws.onerror = function () { log("live socket failed, as expected offline"); };
el("live-note").textContent = "Live updates: " + P.live;
} catch (e) {
log("socket rejected");
}
}
if (P.sse !== undefined) {
try {
// VULNERABLE (EventSource): the fallback endpoint, same shape.
var es = new EventSource(P.sse);
es.onerror = function () { es.close(); };
el("live-note").textContent = "Live updates (fallback): " + P.sse;
} catch (e) {
log("eventsource rejected");
}
}
}());
/* ------------------------------------------------- 10. saved theme (CSS) -- */
(function savedTheme() {
var theme = localStorage.getItem("nw.theme");
if (!theme) { return; }
// VULNERABLE (CSSStyleDeclaration.cssText): a saved theme is stored as a
// declaration list and applied to the page header.
document.querySelector(".topbar").style.cssText = theme;
log("saved theme applied");
}());
/* -------------------------------------------------------- 11. user avatar -- */
(function avatar() {
if (P.avatar === undefined) { return; }
// VULNERABLE (Element.setAttribute): the avatar URL comes from the link,
// and setAttribute will happily set an event handler attribute too.
el("avatar").setAttribute("src", P.avatar);
el("avatar").setAttribute("alt", "Profile picture");
log("avatar set");
}());
/* ---------------------------------------- 12. continue where you left off -- */
(function resumeStrip() {
var marker = null;
document.cookie.split(";").forEach(function (part) {
var kv = part.split("=");
if (kv[0].trim() === "nw_resume") {
marker = decodeURIComponent(kv.slice(1).join("="));
}
});
if (marker === null) { return; }
el("resume-strip").hidden = false;
// VULNERABLE (Element.innerHTML from document.cookie).
el("resume-body").innerHTML = marker;
// SAFE TWIN: the timestamp beside it is formatted and set as text.
el("resume-time").textContent = "saved earlier today";
log("resume strip rendered");
}());
/* ------------------------------------------------- 13. share dialog handoff -- */
(function shareHandoff() {
el("btn-share").addEventListener("click", function () {
location.href = "share.html";
});
if (P.share === undefined) { return; }
var note = window.name;
if (!note) { return; }
var box = document.createElement("div");
box.className = "share-note";
document.querySelector(".content").insertBefore(
box, document.querySelector(".page-head").nextSibling
);
// VULNERABLE (Element.innerHTML from window.name): the note parked by the
// share dialog is rendered as markup.
box.innerHTML = "Shared with a note: " + note;
window.name = "";
log("share note rendered");
}());
/* ------------------------------------------------ 14. draft comment restore -- */
(function draftRestore() {
var draft = sessionStorage.getItem("nw.draft");
if (!draft) { return; }
// SAFE TWIN: the count is read from the same store and set as text.
el("draft-count").textContent =
(sessionStorage.getItem("nw.draftCount") || "0") + " saved draft";
// VULNERABLE (Element.outerHTML): the placeholder node is replaced wholesale
// by the saved draft, which is why the feature reaches for outerHTML.
el("draft-slot").outerHTML =
'<div class="draft-slot" id="draft-slot">' + draft + "</div>";
log("draft restored");
}());
/* ---------------------------------------------------- 15. export preview -- */
(function exportPreview() {
el("btn-export").addEventListener("click", function () {
location.href = "index.html#print=Quarterly%20summary";
location.reload();
});
if (P.print === undefined) { return; }
el("export-panel").hidden = false;
var header = "<h1>" + P.print + "</h1><p>Northwind Retail Analytics</p>";
// VULNERABLE (DOMParser.parseFromString): the report header is parsed so a
// title can carry basic formatting.
var doc = new DOMParser().parseFromString(header, "text/html");
el("export-head").appendChild(doc.body.firstChild.cloneNode(true));
// VULNERABLE (Document.write): the print frame is composed by writing into
// it, which is what an export preview normally does.
var frame = el("print-frame");
var fdoc = frame.contentDocument || frame.contentWindow.document;
fdoc.open();
fdoc.write("<!doctype html><html><head>");
// SAFE TWIN: the print stylesheet beside it is a constant.
fdoc.write('<link rel="stylesheet" href="styles.css">');
fdoc.write("</head><body>" + header + "</body></html>");
fdoc.close();
log("export preview built");
}());
/* -------------------------------------------------- 16. charting component -- */
(function chartTitle() {
var chart = el("chart");
if (!chart || !chart.setTitle) { return; }
if (P.chart !== undefined) {
// VULNERABLE (ShadowRoot.innerHTML): see components.js. The chart caption
// is rendered as markup inside the component's shadow root.
chart.setTitle(P.chart);
log("chart caption set");
} else {
chart.setTitle(null);
}
}());
/* ---------------------------------------------- known non-detections ------ */
/*
* The three features below are just as exploitable as everything above, and
* the shipped es-chromium build does NOT report them. They are here on
* purpose. A demo that only shows what a tool catches is marketing; the
* catalogue marks these as expected non-detections so the harness re-checks
* them on every future build and says so if one starts firing.
*
* What they have in common: each rebuilds the string character by character
* or through an encoder, and the taint does not survive the rebuild.
*/
(function regionFilter() {
if (P.region === undefined) { return; }
// Region codes are upper-cased before display. Case folding drops the
// taint, so this reaches innerHTML unreported.
var region = P.region.toUpperCase();
var box = document.createElement("div");
box.className = "strip";
document.querySelector(".content").insertBefore(
box, document.querySelector(".tiles")
);
box.innerHTML = "Filtered to region " + region;
log("region filter applied");
}());
(function savedLayout() {
if (P.layout === undefined) { return; }
// Saved layouts are passed around base64 encoded. atob rebuilds the string
// from scratch and the taint does not come with it.
var decoded;
try { decoded = atob(P.layout); } catch (e) { return; }
var box = document.createElement("div");
box.className = "strip";
document.querySelector(".content").insertBefore(
box, document.querySelector(".tiles")
);
box.innerHTML = "Layout: " + decoded;
log("saved layout applied");
}());
(function labelSlug() {
if (P.slug === undefined) { return; }
// A per character pass, the shape a hand rolled sanitiser usually takes.
// Splitting to characters and joining back loses the taint. Note that
// splitting on a real separator does not.
var slug = P.slug.split("").map(function (c) { return c; }).join("");
var box = document.createElement("div");
box.className = "strip";
document.querySelector(".content").insertBefore(
box, document.querySelector(".tiles")
);
box.innerHTML = "Label: " + slug;
log("label slug applied");
}());
/* ------------------------------------------ 17. back to where you came from -- */
(function backLink() {
if (P.back === undefined) { return; }
var ref = document.referrer;
if (!ref) { log("no referrer to go back to"); return; }
log("navigating back to the referrer");
// VULNERABLE (Location.assign from document.referrer): the app sends the
// user back to whatever page linked here, with no check on where that is.
location.assign(ref);
}());
}());