Skip to content

Commit 718f6eb

Browse files
committed
JS: update and prettify examples
1 parent 5a6e692 commit 718f6eb

File tree

3 files changed

+20
-20
lines changed

3 files changed

+20
-20
lines changed
Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
const express = require("express"),
2-
fs = require("fs");
2+
fs = require("fs");
33

4-
function save(rootDir, path, content){
5-
if (!isValidPath(rootDir, req.query.filePath)) {
6-
throw new Error(`Invalid filePath: ${req.query.filePath}`); // BAD crashes the server
7-
}
8-
// write content to disk
4+
function save(rootDir, path, content) {
5+
if (!isValidPath(rootDir, req.query.filePath)) {
6+
throw new Error(`Invalid filePath: ${req.query.filePath}`); // BAD crashes the server
7+
}
8+
// write content to disk
99
}
1010
express().post("/save", (req, res) => {
11-
fs.access(rootDir, (err) => {
12-
if (err) {
13-
console.error(`Server setup is corrupted, ${rootDir} does not exist!`);
14-
res.status(500);
15-
res.end();
16-
}
17-
save(rootDir, req.query.path, req.body);
18-
res.status(200);
19-
res.end();
20-
});
11+
fs.exists(rootDir, (exists) => {
12+
if (!exists) {
13+
console.error(`Server setup is corrupted, ${rootDir} does not exist!`);
14+
res.status(500);
15+
res.end();
16+
return;
17+
}
18+
save(rootDir, req.query.path, req.body);
19+
res.status(200);
20+
res.end();
21+
});
2122
});

javascript/ql/src/Security/CWE-730/examples/server-crash.GOOD-A.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// ...
22
express().post("/save", (req, res) => {
3-
fs.access(rootDir, (err) => {
3+
fs.exists(rootDir, (exists) => {
44
// ...
55
try {
66
save(rootDir, req.query.path, req.body); // GOOD no uncaught exception

javascript/ql/src/Security/CWE-730/examples/server-crash.GOOD-B.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
// ...
22
express().post("/save", async (req, res) => {
3-
try {
4-
await fs.access(rootDir);
5-
} catch (e) {
3+
if (await fs.promises.exists(rootDir)) {
64
console.error(`Server setup is corrupted, ${rootDir} does not exist!`);
75
res.status(500);
86
res.end();
7+
return;
98
}
109
save(rootDir, req.query.path, req.body); // MAYBE BAD, depends on the commandline options
1110
res.status(200);

0 commit comments

Comments
 (0)