forked from github/codeql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver-crash.js
More file actions
166 lines (153 loc) · 4.25 KB
/
server-crash.js
File metadata and controls
166 lines (153 loc) · 4.25 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
const express = require("express");
const app = express();
const fs = require("fs");
const EventEmitter = require("events");
const http = require("http");
const port = 12000;
let server = app.listen(port, () =>
// for manual testing of the fickle node.js runtime
console.log(`Example app listening on port ${port}!`)
);
function indirection1() {
fs.readFile("/foo", (err, x) => {
throw err; // $ Alert[js/server-crash]
}); // $ Sink[js/server-crash]
}
function indirection2() {
throw 42; // $ Alert[js/server-crash]
}
function indirection3() {
try {
fs.readFile("/foo", (err, x) => {
throw err; // $ Alert[js/server-crash]
}); // $ Sink[js/server-crash]
} catch (e) {}
}
function indirection4() {
throw 42; // OK - guarded caller
}
function indirection5() {
indirection6();
}
function indirection6() {
fs.readFile("/foo", (err, x) => {
throw err; // $ Alert[js/server-crash]
}); // $ Sink[js/server-crash]
}
app.get("/async-throw", (req, res) => {
fs.readFile("/foo", (err, x) => {
throw err; // $ Alert[js/server-crash]
}); // $ Sink[js/server-crash]
fs.readFile("/foo", (err, x) => {
try {
throw err; // OK - guarded throw
} catch (e) {}
});
fs.readFile("/foo", (err, x) => {
res.setHeader("reflected", req.query.header); // $ MISSING: Alert
});
fs.readFile("/foo", (err, x) => {
try {
res.setHeader("reflected", req.query.header); // OK - guarded call
} catch (e) {}
});
indirection1();
fs.readFile("/foo", (err, x) => {
indirection2();
}); // $ Sink[js/server-crash]
indirection3();
try {
indirection4();
} catch (e) {}
indirection5();
fs.readFile("/foo", (err, x) => {
req.query.foo;
});
fs.readFile("/foo", (err, x) => {
req.query.foo.toString();
});
fs.readFile("/foo", (err, x) => {
req.query.foo.bar; // $ MISSING: Alert - need to add property reads as sinks
});
fs.readFile("/foo", (err, x) => {
res.setHeader("reflected", unknown);
});
try {
indirection7();
} catch (e) {}
});
function indirection7() {
fs.readFile("/foo", (err, x) => {
throw err; // $ Alert[js/server-crash]
}); // $ Sink[js/server-crash]
}
app.get("/async-throw-again", (req, res) => {
fs.readFile("foo", () => {
throw "e"; // $ Alert[js/server-crash]
}); // $ Sink[js/server-crash]
fs.readFileSync("foo", () => {
throw "e"; // OK - does not take callbacks at all
});
// can nest async calls (and only warns about the inner one)
fs.readFile("foo", () => {
fs.readFile("bar", () => {
throw "e"; // $ Alert[js/server-crash]
}); // $ Sink[js/server-crash]
});
fs.readFile("foo", () => {
// can not catch async exceptions
try {
fs.readFile("bar", () => {
throw "e"; // $ Alert[js/server-crash]
}); // $ Sink[js/server-crash]
} catch (e) {}
});
// can mix sync/async calls
fs.readFile("foo", () => {
(() =>
fs.readFile("bar", () => {
throw "e"; // $ Alert[js/server-crash]
}))(); // $ Sink[js/server-crash]
});
});
app.get("/throw-in-promise-1", async (req, res) => {
async function fun() {
throw new Error(); // OK - requires `node --unhandled-rejections=strict ...` to terminate the process
}
await fun();
});
app.get("/throw-in-promise-2", async (req, res) => {
async function fun() {
fs.readFile("/foo", (err, x) => {
throw err; // $ Alert[js/server-crash]
}); // $ Sink[js/server-crash]
}
await fun();
});
app.get("/throw-in-promise-3", async (req, res) => {
fs.readFile("/foo", async (err, x) => {
throw err; // OK - requires `node --unhandled-rejections=strict ...` to terminate the process
});
});
app.get("/throw-in-event-emitter", async (req, res) => {
class MyEmitter extends EventEmitter {}
const myEmitter = new MyEmitter();
myEmitter.on("event", () => {
throw new Error(); // OK - requires `node --unhandled-rejections=strict ...` to terminate the process
});
myEmitter.emit("event");
});
app.get("/throw-with-ambiguous-paths", (req, res) => {
function throwError() {
throw new Error(); // $ Alert[js/server-crash]
}
function cb() {
throwError(); // on path
} // $ Sink[js/server-crash]
function withAsync() {
throwError(); // not on path
fs.stat(X, cb);
}
throwError(); // not on path
withAsync();
});