-
Notifications
You must be signed in to change notification settings - Fork 1.7k
[JS]: Adding express-validator support #18252
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
GeekMasher
wants to merge
15
commits into
github:main
Choose a base branch
from
GeekMasher:js-express-validator
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
c88d239
feat(js): Add initial express-validator support
GeekMasher 3aff553
feat(js): Add express-validator tests
GeekMasher 064f35d
feat(js): Add Reflective XSS test and sanitizer
GeekMasher 5688cb1
docs(js): Add Change Notes
GeekMasher 616841d
style: Update formatting
GeekMasher d9d16a9
fix(js): Small updates to fix warnings
GeekMasher e8d8a86
fix(js): Use Routing + small docs change
GeekMasher d6f539a
feat(js): Remove consistency tests and rename test to Validators
GeekMasher 6ce9a83
style(js): Update formatting
GeekMasher 67efbd6
feat(js): Add Validator code
GeekMasher 19ae407
fix(js): Update tests
GeekMasher e53cf18
fix(js): Reverting getRouteHandler and fix typo in class name
GeekMasher cdd4ba0
feat(js): Add Reflected XSS Sanitizer
GeekMasher 9f7974d
style(js): Format QL
GeekMasher f5163bf
feat(js): Update tests to use Inline Expectations
GeekMasher File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
javascript/ql/lib/change-notes/2024-12-09-express-validator-support.md
This file contains hidden or 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,4 @@ | ||
--- | ||
category: feature | ||
--- | ||
* Added support for `express-validator`, a middleware for Express.js that provides a way to validate incoming requests. |
This file contains hidden or 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
117 changes: 117 additions & 0 deletions
117
javascript/ql/lib/semmle/javascript/frameworks/ExpressValidator.qll
This file contains hidden or 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,117 @@ | ||
/** | ||
* Models of npm modules that are used with Express servers. | ||
*/ | ||
|
||
import javascript | ||
private import semmle.javascript.frameworks.HTTP | ||
private import semmle.javascript.frameworks.Express | ||
private import semmle.javascript.security.dataflow.Xss | ||
|
||
/** | ||
* Models of the express-validator npm module. | ||
*/ | ||
module ExpressValidator { | ||
/** | ||
* The middleware instance that is used to validate the request. | ||
*/ | ||
class MiddlewareInstance extends DataFlow::InvokeNode { | ||
private string validator; | ||
|
||
MiddlewareInstance() { | ||
exists(DataFlow::SourceNode middleware | | ||
( | ||
// query('search').notEmpty().escape() | ||
middleware = DataFlow::moduleMember("express-validator", ["query", "param"]).getACall() and | ||
validator = "parameter" | ||
or | ||
// body('search').notEmpty().escape() | ||
middleware = DataFlow::moduleMember("express-validator", "body").getACall() and | ||
validator = "body" | ||
or | ||
// cookie('search').notEmpty().escape() | ||
middleware = DataFlow::moduleMember("express-validator", "cookie").getACall() and | ||
validator = "cookie" | ||
or | ||
// header('search').notEmpty().escape() | ||
middleware = DataFlow::moduleMember("express-validator", "header").getACall() and | ||
validator = "header" | ||
) and | ||
isSafe(middleware) | ||
| | ||
this = middleware | ||
) | ||
} | ||
|
||
/** | ||
* Gets the name iof the parameter that is safe. | ||
*/ | ||
string getSafeParameterName() { this.getArgument(0).mayHaveStringValue(result) } | ||
|
||
/** | ||
* Gets the type of the validator (parameter, body, etc) | ||
*/ | ||
string getValidatorType() { result = validator } | ||
|
||
/** | ||
* Gets the route handler that is validated. | ||
*/ | ||
Express::RouteHandler getRouteHandler() { | ||
exists(Express::RouteSetup route | | ||
this.getAstNode().getParent*() = route.getARouteHandlerNode().getAstNode() | ||
| | ||
result = route.getLastRouteHandlerNode() | ||
) | ||
} | ||
|
||
/** | ||
* Gets the parameter that is validated and is secure | ||
*/ | ||
DataFlow::Node getSecureRequestInputAccess() { | ||
exists(Express::RequestInputAccess node | | ||
// Hook up to the parameter that is validated | ||
this.getRouteHandler() = node.getRouteHandler() and | ||
// Check the kind of the parameter is the same as the safely escaped parameter | ||
node.getKind() = this.getValidatorType() and | ||
// Check if the PropertyAccess is getSafeParameterName() | ||
node.(DataFlow::PropRead).getPropertyName() = this.getSafeParameterName() and | ||
node.isUserControlledObject() | ||
| | ||
result = node | ||
) | ||
} | ||
} | ||
|
||
/** | ||
* If the `query/body/cookie/header` functions are called, we want to check if one of the | ||
* chaining method calls is a sanitizer. | ||
* | ||
* If a non-sanitizing functions is called, we want to recursively check if the parent is safe | ||
*/ | ||
private predicate isSafe(DataFlow::SourceNode node) { | ||
// Sanitizers | ||
exists(node.getAChainedMethodCall(["escape", "isEmail", "isIn", "isInt"])) | ||
or | ||
// Non-sanitizing chained calls | ||
exists(DataFlow::SourceNode builder | | ||
builder = | ||
node.getAChainedMethodCall([ | ||
"notEmpty", "exists", "isArray", "isObject", "isString", "isULID", | ||
]) and | ||
isSafe(builder) | ||
) | ||
} | ||
|
||
/** | ||
* The sanitizers for `express-validator` validated requests. | ||
* | ||
* These are a list of source nodes that are automatically sanitized by the | ||
* express-validator library. | ||
*/ | ||
class ExpressValidatorSanitizer extends DataFlow::Node { | ||
ExpressValidatorSanitizer() { | ||
exists(ExpressValidator::MiddlewareInstance middleware | | ||
this = middleware.getSecureRequestInputAccess() | ||
) | ||
} | ||
} | ||
} |
This file contains hidden or 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
Empty file.
25 changes: 25 additions & 0 deletions
25
javascript/ql/test/library-tests/frameworks/ExpressValidator/Validators.ql
This file contains hidden or 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,25 @@ | ||
import javascript | ||
import testUtilities.InlineExpectationsTest | ||
|
||
module TestConfig implements TestSig { | ||
string getARelevantTag() { result = ["middleware", "secure"] } | ||
Check warningCode scanning / CodeQL Dead code Warning test
This code is never used, and it's not publicly exported.
|
||
|
||
predicate hasActualResult(Location location, string element, string tag, string value) { | ||
Check warningCode scanning / CodeQL Dead code Warning test
This code is never used, and it's not publicly exported.
|
||
element = "" and | ||
tag = "middleware" and | ||
exists(ExpressValidator::MiddlewareInstance middleware | | ||
location = middleware.getAstNode().getLocation() and | ||
value = middleware.getValidatorType() + "::" + middleware.getSafeParameterName() | ||
) | ||
or | ||
element = "" and | ||
tag = "secure" and | ||
exists(DataFlow::Node safe, ExpressValidator::MiddlewareInstance middleware | | ||
safe = middleware.getSecureRequestInputAccess() and | ||
location = safe.getAstNode().getLocation() and | ||
value = safe.toString() | ||
) | ||
} | ||
} | ||
|
||
import MakeTest<TestConfig> |
30 changes: 30 additions & 0 deletions
30
javascript/ql/test/library-tests/frameworks/ExpressValidator/XSS.expected
This file contains hidden or 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,30 @@ | ||
nodes | ||
| src/validator.js:38:21:38:62 | `<h1>Se ... !</h1>` | | ||
| src/validator.js:38:21:38:62 | `<h1>Se ... !</h1>` | | ||
| src/validator.js:38:39:38:54 | req.query.search | | ||
| src/validator.js:38:39:38:54 | req.query.search | | ||
| src/validator.js:41:21:41:60 | `<h1>Se ... !</h1>` | | ||
| src/validator.js:41:21:41:60 | `<h1>Se ... !</h1>` | | ||
| src/validator.js:41:39:41:52 | req.query.name | | ||
| src/validator.js:41:39:41:52 | req.query.name | | ||
| src/validator.js:44:21:44:60 | `<h1>Se ... !</h1>` | | ||
| src/validator.js:44:21:44:60 | `<h1>Se ... !</h1>` | | ||
| src/validator.js:44:39:44:52 | req.query.name | | ||
| src/validator.js:44:39:44:52 | req.query.name | | ||
edges | ||
| src/validator.js:38:39:38:54 | req.query.search | src/validator.js:38:21:38:62 | `<h1>Se ... !</h1>` | | ||
| src/validator.js:38:39:38:54 | req.query.search | src/validator.js:38:21:38:62 | `<h1>Se ... !</h1>` | | ||
| src/validator.js:38:39:38:54 | req.query.search | src/validator.js:38:21:38:62 | `<h1>Se ... !</h1>` | | ||
| src/validator.js:38:39:38:54 | req.query.search | src/validator.js:38:21:38:62 | `<h1>Se ... !</h1>` | | ||
| src/validator.js:41:39:41:52 | req.query.name | src/validator.js:41:21:41:60 | `<h1>Se ... !</h1>` | | ||
| src/validator.js:41:39:41:52 | req.query.name | src/validator.js:41:21:41:60 | `<h1>Se ... !</h1>` | | ||
| src/validator.js:41:39:41:52 | req.query.name | src/validator.js:41:21:41:60 | `<h1>Se ... !</h1>` | | ||
| src/validator.js:41:39:41:52 | req.query.name | src/validator.js:41:21:41:60 | `<h1>Se ... !</h1>` | | ||
| src/validator.js:44:39:44:52 | req.query.name | src/validator.js:44:21:44:60 | `<h1>Se ... !</h1>` | | ||
| src/validator.js:44:39:44:52 | req.query.name | src/validator.js:44:21:44:60 | `<h1>Se ... !</h1>` | | ||
| src/validator.js:44:39:44:52 | req.query.name | src/validator.js:44:21:44:60 | `<h1>Se ... !</h1>` | | ||
| src/validator.js:44:39:44:52 | req.query.name | src/validator.js:44:21:44:60 | `<h1>Se ... !</h1>` | | ||
#select | ||
| src/validator.js:38:21:38:62 | `<h1>Se ... !</h1>` | src/validator.js:38:39:38:54 | req.query.search | src/validator.js:38:21:38:62 | `<h1>Se ... !</h1>` | Cross-site scripting vulnerability due to a $@. | src/validator.js:38:39:38:54 | req.query.search | user-provided value | | ||
| src/validator.js:41:21:41:60 | `<h1>Se ... !</h1>` | src/validator.js:41:39:41:52 | req.query.name | src/validator.js:41:21:41:60 | `<h1>Se ... !</h1>` | Cross-site scripting vulnerability due to a $@. | src/validator.js:41:39:41:52 | req.query.name | user-provided value | | ||
| src/validator.js:44:21:44:60 | `<h1>Se ... !</h1>` | src/validator.js:44:39:44:52 | req.query.name | src/validator.js:44:21:44:60 | `<h1>Se ... !</h1>` | Cross-site scripting vulnerability due to a $@. | src/validator.js:44:39:44:52 | req.query.name | user-provided value | |
1 change: 1 addition & 0 deletions
1
javascript/ql/test/library-tests/frameworks/ExpressValidator/XSS.qlref
This file contains hidden or 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 @@ | ||
Security/CWE-079/ReflectedXss.ql |
50 changes: 50 additions & 0 deletions
50
javascript/ql/test/library-tests/frameworks/ExpressValidator/src/validator.js
This file contains hidden or 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,50 @@ | ||
|
||
const express = require('express') | ||
const { query, body } = require('express-validator'); | ||
|
||
const app = express(); | ||
|
||
app.get('/secure/1', query('search').escape(), (req, res) => { // $ middleware=parameter::search | ||
return res.send(`<h1>Searching: ${req.query.search}!</h1>`); // $ secure=req.query.search | ||
}) | ||
app.get('/secure/2', query('type').isInt(), (req, res) => { // $ middleware=parameter::type | ||
return res.send(`<h1>Type: ${req.query.type}!</h1>`); // $ secure=req.query.type | ||
}) | ||
app.get('/secure/3', query('email').isEmail(), (req, res) => { // $ middleware=parameter::email | ||
return res.send(`<h1>Email: ${req.query.email}!</h1>`); // $ secure=req.query.email | ||
}) | ||
app.get('/secure/4', query('type').isIn(["this", "or", "that"]), (req, res) => { // $ middleware=parameter::type | ||
return res.send(`<h1>Type: ${req.query.type}!</h1>`); // $ secure=req.query.type | ||
}) | ||
app.get('/secure/5', query('search').notEmpty().isString().escape(), (req, res) => { // $ middleware=parameter::search | ||
return res.send(`<h1>Searching: ${req.query.search}!</h1>`); // $ secure=req.query.search | ||
}) | ||
|
||
app.get( | ||
'/multichains/1', | ||
[ | ||
query('search').escape(), // $ middleware=parameter::search | ||
query('type').isInt(), // $ middleware=parameter::type | ||
query('email').isEmail() // $ middleware=parameter::email | ||
], | ||
(req, res) => { | ||
return res.send( | ||
`<h1>Searching: ${req.query.search} - ${req.query.type}</h1><h2>User: ${req.query.email}</h2>` // $ secure=req.query.search secure=req.query.type secure=req.query.email | ||
); | ||
} | ||
) | ||
|
||
app.get('/insecure/1', (req, res) => { | ||
return res.send(`<h1>Searching: ${req.query.search}!</h1>`); | ||
}) | ||
app.get('/insecure/2', query('search').escape(), (req, res) => { // $ middleware=parameter::search | ||
return res.send(`<h1>Searching: ${req.query.name}!</h1>`); | ||
}) | ||
app.get('/insecure/3', body('name').escape(), (req, res) => { // $ middleware=body::name | ||
return res.send(`<h1>Searching: ${req.query.name}!</h1>`); | ||
}) | ||
|
||
|
||
app.listen(8000, () => { | ||
console.log(`Example app listening on port 8000`) | ||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.