-
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 #13771 from github/max-schaefer/server-side-url-re…
…direct-help JavaScript: Improve query help for `js/server-side-unvalidated-url-redirection`.
- Loading branch information
Showing
8 changed files
with
95 additions
and
5 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
4 changes: 2 additions & 2 deletions
4
javascript/ql/src/Security/CWE-601/examples/ServerSideUrlRedirect.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,6 +1,6 @@ | ||
const app = require("express")(); | ||
|
||
app.get('/some/path', function(req, res) { | ||
app.get("/redirect", function (req, res) { | ||
// BAD: a request parameter is incorporated without validation into a URL redirect | ||
res.redirect(req.param("target")); | ||
res.redirect(req.query["target"]); | ||
}); |
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
22 changes: 22 additions & 0 deletions
22
javascript/ql/src/Security/CWE-601/examples/ServerSideUrlRedirectGood2.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,22 @@ | ||
const app = require("express")(); | ||
|
||
function isLocalUrl(path) { | ||
try { | ||
return ( | ||
// TODO: consider substituting your own domain for example.com | ||
new URL(path, "https://example.com").origin === "https://example.com" | ||
); | ||
} catch (e) { | ||
return false; | ||
} | ||
} | ||
|
||
app.get("/redirect", function (req, res) { | ||
// GOOD: check that we don't redirect to a different host | ||
let target = req.query["target"]; | ||
if (isLocalUrl(target)) { | ||
res.redirect(target); | ||
} else { | ||
res.redirect("/"); | ||
} | ||
}); |
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
6 changes: 6 additions & 0 deletions
6
...cript/ql/test/query-tests/Security/CWE-601/ServerSideUrlRedirect/ServerSideUrlRedirect.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,6 @@ | ||
const app = require("express")(); | ||
|
||
app.get("/redirect", function (req, res) { | ||
// BAD: a request parameter is incorporated without validation into a URL redirect | ||
res.redirect(req.query["target"]); | ||
}); |
13 changes: 13 additions & 0 deletions
13
...t/ql/test/query-tests/Security/CWE-601/ServerSideUrlRedirect/ServerSideUrlRedirectGood.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,13 @@ | ||
const app = require("express")(); | ||
|
||
const VALID_REDIRECT = "http://cwe.mitre.org/data/definitions/601.html"; | ||
|
||
app.get("/redirect", function (req, res) { | ||
// GOOD: the request parameter is validated against a known fixed string | ||
let target = req.query["target"]; | ||
if (VALID_REDIRECT === target) { | ||
res.redirect(target); | ||
} else { | ||
res.redirect("/"); | ||
} | ||
}); |
22 changes: 22 additions & 0 deletions
22
.../ql/test/query-tests/Security/CWE-601/ServerSideUrlRedirect/ServerSideUrlRedirectGood2.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,22 @@ | ||
const app = require("express")(); | ||
|
||
function isLocalUrl(path) { | ||
try { | ||
return ( | ||
// TODO: consider substituting your own domain for example.com | ||
new URL(path, "https://example.com").origin === "https://example.com" | ||
); | ||
} catch (e) { | ||
return false; | ||
} | ||
} | ||
|
||
app.get("/redirect", function (req, res) { | ||
// GOOD: check that we don't redirect to a different host | ||
let target = req.query["target"]; | ||
if (isLocalUrl(target)) { | ||
res.redirect(target); | ||
} else { | ||
res.redirect("/"); | ||
} | ||
}); |