Merge pull request #3619 from erik-krogh/CWE022-Correctness

Approved by asgerf
This commit is contained in:
semmle-qlci
2020-07-01 20:07:58 +01:00
committed by GitHub
10 changed files with 3027 additions and 2995 deletions

View File

@@ -1,4 +0,0 @@
| normalizedPaths.js:208:38:208:63 | // OK - ... anyway | Spurious alert |
| tainted-string-steps.js:25:43:25:74 | // NOT ... flagged | Missing alert |
| tainted-string-steps.js:26:49:26:74 | // OK - ... flagged | Spurious alert |
| tainted-string-steps.js:28:39:28:70 | // NOT ... flagged | Missing alert |

View File

@@ -1,32 +1,3 @@
import javascript
import semmle.javascript.security.dataflow.TaintedPath::TaintedPath
class Assertion extends LineComment {
boolean shouldHaveAlert;
Assertion() {
if getText().matches("%NOT OK%")
then shouldHaveAlert = true
else (
getText().matches("%OK%") and shouldHaveAlert = false
)
}
predicate shouldHaveAlert() { shouldHaveAlert = true }
predicate hasAlert() {
exists(Configuration cfg, DataFlow::Node src, DataFlow::Node sink, Location loc |
cfg.hasFlow(src, sink) and
loc = sink.getAstNode().getLocation() and
loc.getFile() = getFile() and
loc.getEndLine() = getLocation().getEndLine()
)
}
}
from Assertion assertion, string message
where
assertion.shouldHaveAlert() and not assertion.hasAlert() and message = "Missing alert"
or
not assertion.shouldHaveAlert() and assertion.hasAlert() and message = "Spurious alert"
select assertion, message
import testUtilities.ConsistencyChecking

View File

@@ -14,40 +14,33 @@ var server = http.createServer(function(req, res) {
// BAD: This could still read any file on the file system
res.write(fs.readFileSync("/home/user/" + path));
// BAD: Insufficient sanitisation
if (path.startsWith("/home/user/"))
res.write(fs.readFileSync(path));
res.write(fs.readFileSync(path)); // BAD: Insufficient sanitisation
// BAD: Insufficient sanitisation
if (path.indexOf("secret") == -1)
res.write(fs.readFileSync(path));
res.write(fs.readFileSync(path)); // BAD: Insufficient sanitisation
// BAD: Insufficient sanitisation
if (fs.existsSync(path))
res.write(fs.readFileSync(path));
res.write(fs.readFileSync(path)); // BAD: Insufficient sanitisation
// GOOD: Path is compared to white-list
if (path === 'foo.txt')
res.write(fs.readFileSync(path));
res.write(fs.readFileSync(path)); // GOOD: Path is compared to white-list
// GOOD: Path is compared to white-list
if (path === 'foo.txt' || path === 'bar.txt')
res.write(fs.readFileSync(path));
res.write(fs.readFileSync(path)); // GOOD: Path is compared to white-list
// BAD: Path is incompletely compared to white-list
if (path === 'foo.txt' || path === 'bar.txt' || someOpaqueCondition())
res.write(fs.readFileSync(path));
res.write(fs.readFileSync(path)); // BAD: Path is incompletely compared to white-list
// GOOD: Path is sanitized
path = sanitize(path);
res.write(fs.readFileSync(path));
res.write(fs.readFileSync(path)); // GOOD: Path is sanitized
path = url.parse(req.url, true).query.path;
// BAD: taint is preserved
// GOOD: basename is safe
res.write(fs.readFileSync(pathModule.basename(path)));
// BAD: taint is preserved
res.write(fs.readFileSync(pathModule.dirname(path)));
// BAD: taint is preserved
// GOOD: extname is safe
res.write(fs.readFileSync(pathModule.extname(path)));
// BAD: taint is preserved
res.write(fs.readFileSync(pathModule.join(path)));

View File

@@ -205,7 +205,7 @@ app.get('/join-regression', (req, res) => {
fs.readFileSync(normalizedPath); // NOT OK
if (normalizedPath.startsWith('/home/user/www') || normalizedPath.startsWith('/home/user/public'))
fs.readFileSync(normalizedPath); // OK - but flagged anyway
fs.readFileSync(normalizedPath); // OK - but flagged anyway [INCONSISTENCY]
else
fs.readFileSync(normalizedPath); // NOT OK
});

View File

@@ -7,12 +7,11 @@ var fs = require('fs'),
var server = http.createServer(function(req, res) {
let path = url.parse(req.url, true).query.path;
// BAD: taint is preserved
res.write(fs.readFileSync(['public', path].join('/')));
// BAD: taint is preserved
res.write(fs.readFileSync(['public', path].join('/'))); // BAD - but not flagged because we have no array-steps [INCONSISTENCY]
let parts = ['public', path];
parts = parts.map(x => x.toLowerCase());
res.write(fs.readFileSync(parts.join('/')));
res.write(fs.readFileSync(parts.join('/'))); // BAD - but not flagged because we have no array-steps [INCONSISTENCY]
});
server.listen();

View File

@@ -22,10 +22,10 @@ var server = http.createServer(function(req, res) {
fs.readFileSync(path.split('/')[i]); // NOT OK
fs.readFileSync(path.split(/\//)[i]); // NOT OK
fs.readFileSync(path.split("?")[0]); // NOT OK
fs.readFileSync(path.split(unknown)[i]); // NOT OK -- but not yet flagged
fs.readFileSync(path.split(unknown).whatever); // OK -- but still flagged
fs.readFileSync(path.split(unknown)[i]); // NOT OK -- but not yet flagged [INCONSISTENCY]
fs.readFileSync(path.split(unknown).whatever); // OK -- but still flagged [INCONSISTENCY]
fs.readFileSync(path.split(unknown)); // NOT OK
fs.readFileSync(path.split("?")[i]); // NOT OK -- but not yet flagged
fs.readFileSync(path.split("?")[i]); // NOT OK -- but not yet flagged [INCONSISTENCY]
});
server.listen();