-
Notifications
You must be signed in to change notification settings - Fork 3
/
vulnerable.patch
712 lines (636 loc) · 26.2 KB
/
vulnerable.patch
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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
--- lib/db-tables.js 2000-01-01 12:00:00.000000000 -0500
+++ vulnerable/lib/db-tables.js 2000-01-01 12:00:00.000000000 -0500
@@ -23,7 +23,8 @@
* utilities to restore to known state for tests.
*/
-const safesql = require('safesql');
+//const safesql = require('safesql');
+const safesql = { pg: String.raw }; // eslint-disable-line id-length
const initStmts = safesql.pg`
@@ -151,7 +152,7 @@
const dropStmts = (() => {
- const createSql = initStmts.content;
+ const createSql = initStmts;
const tableNamePattern = /^CREATE (TABLE|INDEX) IF NOT EXISTS (\w+)/mg;
let dropSql = null;
--- lib/dbi.js 2000-01-01 12:00:00.000000000 -0500
+++ vulnerable/lib/dbi.js 2000-01-01 12:00:00.000000000 -0500
@@ -24,29 +24,32 @@
// SENSITIVE - Responsible for not leaking personally identifying information.
-require('module-keys/cjs').polyfill(module, require);
+//require('module-keys/cjs').polyfill(module, require);
-const { box, isPublicKey } = require.moduleKeys;
+//const { box, isPublicKey } = require.moduleKeys;
-const safesql = require('safesql');
+//const safesql = require('safesql');
+const safesql = { pg: String.raw }; // eslint-disable-line id-length
const sessionTimeoutMs = safesql.pg`(interval '1 hour')`;
const linkify = require('./poorly-written-linkifier.js');
const { sanitize } = require('./safe/html.js');
-const { publicKey: accountPubKey } = require('../lib/handlers/account.pug');
+//const { publicKey: accountPubKey } = require('../lib/handlers/account.pug');
+const { escape } = require('pug-runtime');
const defaultLimit = 10; // eslint-disable-line no-magic-numbers
/**
* True when the principal identified by the given public key
* may read publicly identifying information?
- */
+ *
function mayReadPii(readerPubKey) { // eslint-disable-line no-unused-vars
return isPublicKey(readerPubKey) && readerPubKey() && readerPubKey === accountPubKey;
}
+*/
function fallbackToPlainText(plainText, untrustedHtml, fallback) {
- return (untrustedHtml ? sanitize(untrustedHtml) : plainText) || fallback;
+ return (untrustedHtml ? sanitize(untrustedHtml) : escape(plainText)) || fallback;
}
/**
@@ -63,7 +66,7 @@
(friendResults) => {
fclient.release();
const friendList = friendResults.rows.map(({ friend }) => friend);
- resolve(require.moduleKeys.box(friendList, () => true));
+ resolve(friendList);
},
(exc) => {
fclient.release();
@@ -81,11 +84,11 @@
client.query(
safesql.pg`
-- Remove any timed out sessions
-DELETE FROM Sessions WHERE sessionnonce=${ sessionNonce } AND created < (NOW() - ${ sessionTimeoutMs });
+DELETE FROM Sessions WHERE sessionnonce='${ sessionNonce }' AND created < (NOW() - ${ sessionTimeoutMs });
-- Create a session entry if none, otherwise reset timeout countdown.
INSERT INTO Sessions (sessionnonce, aid)
- VALUES (${ sessionNonce }, NULL)
+ VALUES ('${ sessionNonce }', NULL)
ON CONFLICT (sessionnonce) DO UPDATE SET created=NOW();
-- Get the user info we need.
@@ -94,7 +97,7 @@
FROM Sessions
LEFT JOIN Accounts ON Sessions.aid = Accounts.aid
LEFT JOIN PersonalInfo ON Sessions.aid = PersonalInfo.aid
- WHERE Sessions.sessionnonce = ${ sessionNonce }
+ WHERE Sessions.sessionnonce = '${ sessionNonce }'
`)
.then(
(resultSet) => {
@@ -118,9 +121,10 @@
resolve({
aid,
displayName: fallbackToPlainText(displayname, displaynamehtml, 'Anonymous'),
+ displayNameIsHtml: Boolean(displaynamehtml),
publicUrl: publicurl,
- realName: box(realname, mayReadPii),
- email: box(email, mayReadPii),
+ realName: realname,
+ email,
get friends() {
const friendsPromise = makeFriendsPromise(aid);
delete this.friends;
@@ -146,7 +150,7 @@
// The NULL author authors nothing even if we end up with NULLs
// in the author column perhaps due to some mistake by a dbadmin.
const isAuthor = aid ? safesql.pg`Posts.author = ${ aid }` : false;
- const authorFriendly = friends && friends.length ? safesql.pg`Posts.author IN (${ friends })` : false;
+ const authorFriendly = friends && friends.length ? safesql.pg`Posts.author IN (${ friends.join(', ') })` : false;
// Intentionally large attack surface.
// This code is not only relying on access control checks in SQL, but SQL that
@@ -196,7 +200,7 @@
pidToPost.set(pid, post);
}
return {
- sql: safesql.pg`SELECT pid, urlpath FROM PostResources WHERE pid IN ( ${ pids } ) ORDER BY rid;`,
+ sql: safesql.pg`SELECT pid, urlpath FROM PostResources WHERE pid IN ( ${ pids.join(', ') } ) ORDER BY rid;`,
pidToPost,
};
}
@@ -210,10 +214,7 @@
return new Promise((resolve, reject) => {
const friendsPromise = (viewer && viewer.friends) || Promise.resolve(null);
Promise.all([ database.connect(), friendsPromise ]).then(
- ([ client, friendsBox ]) => {
- // Avoid errors by double checking that the friend list came from getCurrentAccount.
- const friends = require.moduleKeys.unbox(
- friendsBox, (key) => key === require.moduleKeys.publicKey && key(), []);
+ ([ client, friends ]) => {
function releaseAndReject(exc) {
client.release();
--- lib/handlers/account.js 2000-01-01 12:00:00.000000000 -0500
+++ vulnerable/lib/handlers/account.js 2000-01-01 12:00:00.000000000 -0500
@@ -22,11 +22,12 @@
* Displays recent posts visible to the current user.
*/
-require('module-keys/cjs').polyfill(module, require);
+//require('module-keys/cjs').polyfill(module, require);
const { URL } = require('url');
const template = require('./account.pug');
-const safesql = require('safesql');
+//const safesql = require('safesql');
+const safesql = { pg: String.raw }; // eslint-disable-line id-length
// eslint-disable-next-line no-magic-numbers
const STATUS_TEMPORARY_REDIRECT = 302;
@@ -106,14 +107,14 @@
function commitAccountChanges() {
const sql = safesql.pg`
UPDATE Accounts
- SET displayname=${ displayname },
- displaynamehtml=${ displaynamehtml },
- publicurl=${ publicurl }
+ SET displayname='${ displayname }',
+ displaynamehtml='${ displaynamehtml }',
+ publicurl='${ publicurl }'
WHERE aid=${ aid };
UPDATE PersonalInfo
- SET realname=${ realname },
- email=${ email }
+ SET realname='${ realname }',
+ email='${ email }'
WHERE aid=${ aid };`;
client.query(sql).then(
success,
@@ -126,7 +127,8 @@
}
function serveForm() {
+ const displayNameIsHtml = Boolean(('displayNameIsHtml' in params ? params : currentAccount).displayNameIsHtml);
res.statusCode = 200;
- res.end(template(Object.assign({}, bundle, { cont })));
+ res.end(template(Object.assign({}, bundle, { cont, displayNameIsHtml })));
}
};
--- lib/handlers/account.pug 2000-01-01 12:00:00.000000000 -0500
+++ vulnerable/lib/handlers/account.pug 2000-01-01 12:00:00.000000000 -0500
@@ -7,6 +7,7 @@
mixin body()
- let displayName = currentAccount.displayName || ''
form(id="account" method="post" action="/account")
+ input(name="_csrf" type="hidden" value=_csrf)
table.formatting
tr
td(colspan=2)
@@ -16,7 +17,7 @@
label(for= "displayName") Display Name
td
input(name="displayName" value=displayName)
- input(name="displayNameIsHtml" checked=(typeof currentAccount.displayName === 'object') type="checkbox")
+ input(name="displayNameIsHtml" checked=displayNameIsHtml type="checkbox")
label.assocleft(for= "displayNameIsHtml") HTML
tr
td
@@ -30,12 +31,12 @@
td
label(for= "realName") Real Name
td
- input(name="realName" value=require.moduleKeys.unbox(currentAccount.realName, () => true))
+ input(name="realName" value=currentAccount.realName)
tr
td
label(for= "email") Email
td
- input(name="email" value=require.moduleKeys.unbox(currentAccount.email, () => true))
+ input(name="email" value=currentAccount.email)
input(name="cont" type="hidden" value=cont)
button(form="account" type="submit") Submit
|
--- lib/handlers/echo.js 2000-01-01 12:00:00.000000000 -0500
+++ vulnerable/lib/handlers/echo.js 2000-01-01 12:00:00.000000000 -0500
@@ -28,7 +28,8 @@
*/
const template = require('./echo.pug');
-const safesql = require('safesql');
+//const safesql = require('safesql');
+const safesql = { pg: String.raw }; // eslint-disable-line id-length
exports.handle = (bundle, handleError) => {
const { database, reqUrl, res } = bundle;
@@ -50,7 +51,7 @@
database.connect().then(
(client) => {
const query = safesql.pg`SELECT ${
- inputs.map(([ name, value ]) => safesql.pg`${ value } AS "${ name }"`) }`;
+ inputs.map(([ name, value ]) => safesql.pg`'${ value }' AS "${ name }"`) }`;
// eslint-disable-next-line no-console
console.log(`echo sending ${ query }`);
client.query(query).then(
--- lib/handlers/error.js 2000-01-01 12:00:00.000000000 -0500
+++ vulnerable/lib/handlers/error.js 2000-01-01 12:00:00.000000000 -0500
@@ -22,7 +22,7 @@
* Dumps an error trace to the output.
*/
-const { isProduction } = require('../framework/is-prod.js');
+const isProduction = true;
const template = require('./error.pug');
function handleError(bundle, exc) {
--- lib/handlers/includes/envelope.pug 2000-01-01 12:00:00.000000000 -0500
+++ vulnerable/lib/handlers/includes/envelope.pug 2000-01-01 12:00:00.000000000 -0500
@@ -3,14 +3,14 @@
head
title
+title()
- script(src='/common.js')
- link(rel='stylesheet' href='/styles.css')
+ script(src='/common.js' nonce=cspNonce)
+ link(rel='stylesheet' href='/styles.css' nonce=cspNonce)
body
if reqUrl.pathname !== '/login'
.userfloat
if currentAccount
span.user.name
- = currentAccount.displayName
+ != currentAccount.displayName
if reqUrl.pathname !== '/account'
a.nounder(href='/account') ▼
if reqUrl.pathname !== '/logout'
@@ -18,6 +18,7 @@
action=`/logout?cont=${encodeURIComponent(reqUrl.pathname + reqUrl.search)}`
method="POST"
name="logout")
+ input(name="_csrf" type="hidden" value=_csrf)
button.logoutlink(type="submit") logout
else
a.loginlink(href="/login") login
--- lib/handlers/includes/post-common.pug 2000-01-01 12:00:00.000000000 -0500
+++ vulnerable/lib/handlers/includes/post-common.pug 2000-01-01 12:00:00.000000000 -0500
@@ -3,13 +3,13 @@
span.author.name
if post.authorUrl
a(href=post.authorUrl)
- = post.authorName
+ != post.authorName
else
- = post.authorName
+ != post.authorName
if post.created
span.created
= fmtDate(post.created)
- div.body=(post.body || '')
+ div.body!=(post.body || '')
if post.images && post.images.length
div.images
each image in post.images
@@ -21,6 +21,7 @@
p.error
=error
form(id="post-form" action="/post" enctype="multipart/form-data" method="POST")
+ input(name="_csrf" type="hidden" value=_csrf)
textarea(name="body" cols="40" rows="5")= ((preview && (preview.unsanitizedBody || preview.body)) || '')
div
label(for="public") Public
--- lib/handlers/login.js 2000-01-01 12:00:00.000000000 -0500
+++ vulnerable/lib/handlers/login.js 2000-01-01 12:00:00.000000000 -0500
@@ -22,11 +22,12 @@
* Associates the current session with an account.
*/
-require('module-keys/cjs').polyfill(module, require);
+//require('module-keys/cjs').polyfill(module, require);
const { URL } = require('url');
-const safesql = require('safesql');
+// const safesql = require('safesql');
+const safesql = { pg: String.raw }; // eslint-disable-line id-length
const bodyParser = require('body-parser');
const template = require('./login.pug');
@@ -107,7 +108,7 @@
client.query(safesql.pg`INSERT INTO Accounts DEFAULT VALUES RETURNING *`).then(
(resultSet) => {
const [ { aid } ] = resultSet.rows;
- client.query(safesql.pg`INSERT INTO PersonalInfo (aid, email) VALUES (${ aid }, ${ email })`)
+ client.query(safesql.pg`INSERT INTO PersonalInfo (aid, email) VALUES (${ aid }, '${ email }')`)
.then(
() => {
// eslint-disable-next-line no-use-before-define
@@ -124,8 +125,8 @@
function associateAccountWithSessionNonce(resultSet) {
if (resultSet.rowCount === 1) {
const [ { aid } ] = resultSet.rows;
- const sessionNonceValue = require.moduleKeys.unbox(sessionNonce, () => true);
- client.query(safesql.pg`UPDATE SESSIONS SET aid=${ aid } WHERE sessionnonce=${ sessionNonceValue }`)
+ //const sessionNonceValue = require.moduleKeys.unbox(sessionNonce, () => true);
+ client.query(safesql.pg`UPDATE SESSIONS SET aid=${ aid } WHERE sessionnonce='${ sessionNonce }'`)
.then(
(updates) => {
client.release();
@@ -140,7 +141,7 @@
createNewAccount();
}
}
- client.query(safesql.pg`SELECT aid FROM PersonalInfo WHERE email=${ email }`).then(
+ client.query(safesql.pg`SELECT aid FROM PersonalInfo WHERE email='${ email }'`).then(
associateAccountWithSessionNonce,
releaseAndReject);
},
--- lib/handlers/login.pug 2000-01-01 12:00:00.000000000 -0500
+++ vulnerable/lib/handlers/login.pug 2000-01-01 12:00:00.000000000 -0500
@@ -9,6 +9,7 @@
| Email is a required field.
form(method="POST" action=reqUrl.pathname id="login")
+ input(name="_csrf" type="hidden" value=_csrf)
label(for="email") Email
input(name="email" value=email)
input(name="cont" type="hidden" value=cont)
--- lib/handlers/logout.js 2000-01-01 12:00:00.000000000 -0500
+++ vulnerable/lib/handlers/logout.js 2000-01-01 12:00:00.000000000 -0500
@@ -22,11 +22,12 @@
* Remove the association between the current session and any account.
*/
-require('module-keys/cjs').polyfill(module, require);
+//require('module-keys/cjs').polyfill(module, require);
const { URL } = require('url');
-const safesql = require('safesql');
+//const safesql = require('safesql');
+const safesql = { pg: String.raw }; // eslint-disable-line id-length
const template = require('./logout.pug');
// eslint-disable-next-line no-magic-numbers
@@ -52,11 +53,11 @@
return;
}
- const sessionNonceValue = require.moduleKeys.unbox(sessionNonce, () => true);
+ //const sessionNonceValue = require.moduleKeys.unbox(sessionNonce, () => true);
database.connect().then(
(client) => {
- client.query(safesql.pg`UPDATE SESSIONS SET aid=NULL WHERE sessionnonce=${ sessionNonceValue }`)
+ client.query(safesql.pg`UPDATE SESSIONS SET aid=NULL WHERE sessionnonce='${ sessionNonce }'`)
.then(
() => {
client.release();
--- lib/handlers/logout.pug 2000-01-01 12:00:00.000000000 -0500
+++ vulnerable/lib/handlers/logout.pug 2000-01-01 12:00:00.000000000 -0500
@@ -7,6 +7,7 @@
center
form(method="POST" action="/logout" id="logout")
+ input(name="_csrf" type="hidden" value=_csrf)
input(type="hidden" name="cont" value=cont)
button(type="submit" form="logout") Logout
|
--- lib/handlers/post.js 2000-01-01 12:00:00.000000000 -0500
+++ vulnerable/lib/handlers/post.js 2000-01-01 12:00:00.000000000 -0500
@@ -25,15 +25,27 @@
const path = require('path');
const template = require('./post.pug');
-const childProcess = require('../safe/child_process.js');
+const childProcess = require('child_process');
const { sanitize } = require('../safe/html.js');
const relativeDate = require('tiny-relative-date');
const mime = require('mime-types');
-const safesql = require('safesql');
-const { sh } = require('sh-template-tag');
+//const safesql = require('safesql');
+const safesql = { pg: String.raw }; // eslint-disable-line id-length
+//const { sh } = require('sh-template-tag');
+const sh = String.raw; // eslint-disable-line id-length
const { URL } = require('url');
+function toISOString(x) {
+ const year = x.getFullYear();
+ const month = x.getMonth() + 1;
+ const day = x.getDate();
+ const hour = x.getHours();
+ const minute = x.getMinutes();
+ const second = x.getSeconds();
+ return `${ year }-${ month }-${ day } ${ hour }:${ minute }:${ second }`;
+}
+
exports.handle = (bundle, handleError) => {
const {
req, res, reqUrl, database, currentAccount,
@@ -92,7 +104,7 @@
const mimeType = mime.lookup(basename);
if (typeof mimeType === 'string' && /^image\//.test(mimeType)) {
const imagePath = path.join(userUploadsDir, basename);
- const mvCommand = sh`mv -n -- ${ tempPath } ${ imagePath }`;
+ const mvCommand = sh`mv -n -- '${ tempPath }' '${ imagePath }'`;
cmd = cmd ? sh`${ cmd } && ${ mvCommand }` : mvCommand;
uploadedImagePaths.push(imagePath);
} else {
@@ -200,7 +212,7 @@
INSERT INTO Posts
( author, public, bodyhtml, created )
VALUES
- ( ${ authorId }, ${ isPublic }, ${ body }, ${ now } )
+ ( ${ authorId }, ${ isPublic }, '${ body.replace(/'/g, '\'\'') }', '${ toISOString(now) }' )
RETURNING pid`;
}
@@ -208,8 +220,8 @@
if (!images.length) {
return Promise.resolve(null);
}
- const imageTuples = images.map((relurl) => safesql.pg`( ${ pid }, ${ relurl } )`);
- const imageSql = safesql.pg`INSERT INTO PostResources (pid, urlpath) VALUES ${ imageTuples };`;
+ const imageTuples = images.map((relurl) => safesql.pg`( ${ pid }, '${ relurl }' )`);
+ const imageSql = safesql.pg`INSERT INTO PostResources (pid, urlpath) VALUES ${ imageTuples.join(', ') };`;
return client.query(imageSql);
}
--- lib/safe/child_process.js 2000-01-01 12:00:00.000000000 -0500
+++ vulnerable/lib/safe/child_process.js 2000-01-01 12:00:00.000000000 -0500
@@ -31,6 +31,8 @@
'use strict'; // eslint-disable-line filenames/match-regex
+/*
+
const childProcess = require('child_process');
const { ShFragment } = require('sh-template-tag');
const { Mintable } = require('node-sec-patterns');
@@ -80,3 +82,5 @@
}
return childProcess.exec(command, options);
};
+
+*/
--- lib/safe/html.js 2000-01-01 12:00:00.000000000 -0500
+++ vulnerable/lib/safe/html.js 2000-01-01 12:00:00.000000000 -0500
@@ -40,15 +40,15 @@
'use strict';
-require('module-keys/cjs').polyfill(module, require);
+//require('module-keys/cjs').polyfill(module, require);
-const { TrustedHTML } = require('web-contract-types');
-const { Mintable } = require('node-sec-patterns');
+//const { TrustedHTML } = require('web-contract-types');
+//const { Mintable } = require('node-sec-patterns');
const createDOMPurify = require('dompurify');
const { JSDOM } = require('jsdom');
-const mintTrustedHTML = require.moduleKeys.unbox(
- Mintable.minterFor(TrustedHTML), () => true, (x) => x);
+//const mintTrustedHTML = require.moduleKeys.unbox(
+// Mintable.minterFor(TrustedHTML), () => true, (x) => x);
const { window } = (new JSDOM(''));
const DOMPurify = createDOMPurify(window);
const sanitize = DOMPurify.sanitize.bind(DOMPurify);
@@ -62,9 +62,4 @@
* Otherwises coerces to string and sanitizes.
* @return {TrustedHTML}
*/
-module.exports.sanitize = (html) => {
- if (TrustedHTML.is(html)) {
- return html;
- }
- return mintTrustedHTML(sanitize(`${ html }`));
-};
+module.exports.sanitize = sanitize;
--- lib/safe/pg.js 2000-01-01 12:00:00.000000000 -0500
+++ vulnerable/lib/safe/pg.js 2000-01-01 12:00:00.000000000 -0500
@@ -38,6 +38,8 @@
// but which only issues messages to the database whose content is part of a
// SqlFragment minted by an authorized minter.
+return;
+
const { apply } = Reflect;
// eslint-disable-next-line id-length
const pg = require('pg');
--- lib/server.js 2000-01-01 12:00:00.000000000 -0500
+++ vulnerable/lib/server.js 2000-01-01 12:00:00.000000000 -0500
@@ -38,7 +38,7 @@
// GUARANTEE - One user cannot get another user's session nonce.
-require('module-keys/cjs').polyfill(module, require);
+//require('module-keys/cjs').polyfill(module, require);
const crypto = require('crypto');
const http = require('http');
@@ -64,7 +64,7 @@
const csrfCrypto = require('csrf-crypto');
const multiparty = require('multiparty');
const serveStatic = require('serve-static');
-const { TrustedHTML } = require('web-contract-types');
+//const { TrustedHTML } = require('web-contract-types');
const { handle: handleError } = require('./handlers/error.js');
const { getCurrentAccount } = require('./dbi.js');
@@ -94,6 +94,7 @@
},
});
+/*
const sessionKeyWhitelist = new Set([
// eslint-disable-next-line global-require
require('./handlers/login.js').publicKey, require('./handlers/logout.js').publicKey,
@@ -101,6 +102,7 @@
function sessionNonceGuard(key) {
return require.moduleKeys.isPublicKey(key) && key() && sessionKeyWhitelist.has(key);
}
+*/
function start(
{ hostName, port, rootDir, database, writeToPlaybackLog },
@@ -157,12 +159,12 @@
});
return nonce;
}
-
+ /*
function requireTrustedHTML(...chunks) {
const html = TrustedHTML.concat(...(chunks.map(TrustedHTML.escape)));
return TrustedHTML.is(html) ? html.content : ' ';
}
-
+ */
function monkeypatch(req, res) {
const cookies = [];
@@ -177,12 +179,14 @@
};
// Ensure that the response body is trusted HTML chunks.
+ /*
const end = res.end.bind(res);
const write = res.write.bind(res);
res.end =
(html, ...rest) =>
(html || rest.length ? end(requireTrustedHTML(html), ...rest) : end());
res.write = (html, ...rest) => write(requireTrustedHTML(html), ...rest);
+ */
}
// Handles and end()s HTTP requests.
@@ -231,7 +235,7 @@
},
database,
cspNonce,
- sessionNonce: require.moduleKeys.box(sessionNonce, sessionNonceGuard),
+ sessionNonce/*: require.moduleKeys.box(sessionNonce, sessionNonceGuard)*/,
/* eslint-disable no-underscore-dangle */
get _csrf() {
// Thread through from crypto-csrf above to Pug automagic.
--- main.js 2000-01-01 12:00:00.000000000 -0500
+++ vulnerable/main.js 2000-01-01 12:00:00.000000000 -0500
@@ -34,10 +34,10 @@
const isMain = require.main === module;
-require('./lib/framework/bootstrap-secure.js')(path.resolve(__dirname), isMain);
+//require('./lib/framework/bootstrap-secure.js')(path.resolve(__dirname), isMain);
const { start } = require('./lib/server.js');
-const safepg = require('./lib/safe/pg.js');
+const safepg = require('pg');
const { initializeTablesWithTestData } = require('./lib/db-tables');
const { flock } = require('fs-ext');
@@ -65,7 +65,7 @@
if (isMain) {
// Fail fast if run via `node main.js` instead of as a script with the flags from #! above.
- let evalWorks = true;
+ let evalWorks = false; // Disable check in vulnerable server.
try {
eval(String(Math.random())); // eslint-disable-line no-eval
} catch (evalFailed) {
--- package.json 2000-01-01 12:00:00.000000000 -0500
+++ vulnerable/package.json 2000-01-01 12:00:00.000000000 -0500
@@ -154,16 +154,12 @@
"jsdom": "^12.2.0",
"mime-types": "^2.1.20",
"multiparty": "^4.2.1",
- "node-sec-patterns": "^3.0.2",
"pg": "^7.5.0",
"pug": "^2.0.3",
"pug-require": "^2.0.2",
- "safesql": "^2.0.2",
"serve-static": "^1.13.2",
- "sh-template-tag": "^4.0.2",
"tiny-relative-date": "^1.3.0",
- "tlds": "^1.203.1",
- "web-contract-types": "^2.0.2"
+ "tlds": "^1.203.1"
},
"devDependencies": {
"capture-console": "^1.0.1",
--- scripts/run-locally.js 2000-01-01 12:00:00.000000000 -0500
+++ vulnerable/scripts/run-locally.js 2000-01-01 12:00:00.000000000 -0500
@@ -1,4 +1,4 @@
-#!./bin/node
+#!/usr/bin/env node
/**
* @license
@@ -212,14 +212,19 @@
if (require.main === module) {
+ let nodeExec = path.join(rootDir, '..', 'bin', 'node');
+ try {
+ nodeExec = x('which', 'node').replace(/\n$/, '');
+ } catch (exc) {
+ // Ok as long as we have a built node runtime
+ }
+
spinUpDatabase(
({ pgPort, pgSocksDir }) => {
// Run the server main file
const serverProcess = childProcess.spawn(
- path.join(rootDir, 'bin', 'node'),
+ nodeExec,
[ /* eslint-disable array-element-newline */
- '--cjs-loader', './lib/framework/init-hooks.js',
- '--disallow_code_generation_from_strings',
path.join(rootDir, 'main.js'),
...(process.argv.slice(2)),
], /* eslint-enable array-element-newline */
--- node_modules/pug-require/index.js 2000-01-01 12:00:00.000000000 -0500
+++ vulnerable/node_modules/pug-require/index.js 2000-01-01 12:00:00.000000000 -0500
@@ -7,7 +7,7 @@
const { assign, create, hasOwnProperty } = Object;
const { apply } = Reflect;
-const ttPlugin = require('pug-plugin-trusted-types');
+//const ttPlugin = require('pug-plugin-trusted-types');
let config = null;
@@ -24,7 +24,7 @@
}
// Pack the prologue onto one line to preserve line numbers as apparent to pug-codegen.
- return `'use strict'; require('module-keys/cjs').polyfill(module, require); ${ body }
+ return `'use strict'; ${ body }
module.exports = ${ name };`;
}
@@ -39,6 +39,7 @@
const givenPlugins = options.plugins;
const plugins = isArray(givenPlugins) ? [ ...givenPlugins ] : [];
+ /*
let addTtPlugin = true;
for (let i = 0, len = plugins.length; i < len; ++i) {
if (plugins[i] === ttPlugin) {
@@ -49,6 +50,7 @@
if (addTtPlugin) {
plugins[plugins.length] = ttPlugin;
}
+ */
config = assign(create(null), options, { plugins });
}