-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14846 from github/max-schaefer/js/path-injection
Update qhelp for js/path-injection.
- Loading branch information
Showing
6 changed files
with
253 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 9 additions & 10 deletions
19
javascript/ql/src/Security/CWE-022/examples/TaintedPath.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,12 @@ | ||
var fs = require('fs'), | ||
http = require('http'), | ||
url = require('url'); | ||
const fs = require('fs'), | ||
http = require('http'), | ||
url = require('url'); | ||
|
||
var server = http.createServer(function(req, res) { | ||
let path = url.parse(req.url, true).query.path; | ||
const ROOT = "/var/www/"; | ||
|
||
// BAD: This could read any file on the file system | ||
res.write(fs.readFileSync(path)); | ||
var server = http.createServer(function(req, res) { | ||
let filePath = url.parse(req.url, true).query.path; | ||
|
||
// BAD: This could still read any file on the file system | ||
res.write(fs.readFileSync("/home/user/" + path)); | ||
}); | ||
// BAD: This function uses unsanitized input that can read any file on the file system. | ||
res.write(fs.readFileSync(ROOT + filePath, 'utf8')); | ||
}); |
19 changes: 19 additions & 0 deletions
19
javascript/ql/src/Security/CWE-022/examples/TaintedPathGood.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
const fs = require('fs'), | ||
http = require('http'), | ||
path = require('path'), | ||
url = require('url'); | ||
|
||
const ROOT = "/var/www/"; | ||
|
||
var server = http.createServer(function(req, res) { | ||
let filePath = url.parse(req.url, true).query.path; | ||
|
||
// GOOD: Verify that the file path is under the root directory | ||
filePath = fs.realpathSync(path.resolve(ROOT, filePath)); | ||
if (!filePath.startsWith(ROOT)) { | ||
res.statusCode = 403; | ||
res.end(); | ||
return; | ||
} | ||
res.write(fs.readFileSync(filePath, 'utf8')); | ||
}); |
Oops, something went wrong.