-
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.
+231
−0
Open
Changes from 6 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
114 changes: 114 additions & 0 deletions
114
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,114 @@ | ||
/** | ||
* 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` function is called and it then uses `.escape()`. | ||
*/ | ||
private predicate isSafe(DataFlow::SourceNode node) { | ||
// Sanitizers | ||
exists(node.getAChainedMethodCall(["escape", "isEmail", "isIn", "isInt"])) | ||
or | ||
// If the `query` function is called and it then uses `.notEmpty()` or `.toString()` or `.isInt()` | ||
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 ExpressValidationSanitizer extends Shared::Sanitizer { | ||
ExpressValidationSanitizer() { | ||
exists(ExpressValidator::MiddlewareInstance middleware | | ||
this = middleware.getSecureRequestInputAccess() | ||
) | ||
} | ||
} | ||
} |
Empty file.
2 changes: 2 additions & 0 deletions
2
javascript/ql/test/library-tests/frameworks/ExpressValidator/Consistency.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,2 @@ | ||
import testUtilities.ConsistencyChecking | ||
import semmle.javascript.security.dataflow.ReflectedXssQuery as ReflectedXss |
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/validators.js:35:21:35:62 | `<h1>Se ... !</h1>` | | ||
| src/validators.js:35:21:35:62 | `<h1>Se ... !</h1>` | | ||
| src/validators.js:35:39:35:54 | req.query.search | | ||
| src/validators.js:35:39:35:54 | req.query.search | | ||
| src/validators.js:38:21:38:60 | `<h1>Se ... !</h1>` | | ||
| src/validators.js:38:21:38:60 | `<h1>Se ... !</h1>` | | ||
| src/validators.js:38:39:38:52 | req.query.name | | ||
| src/validators.js:38:39:38:52 | req.query.name | | ||
| src/validators.js:41:21:41:60 | `<h1>Se ... !</h1>` | | ||
| src/validators.js:41:21:41:60 | `<h1>Se ... !</h1>` | | ||
| src/validators.js:41:39:41:52 | req.query.name | | ||
| src/validators.js:41:39:41:52 | req.query.name | | ||
edges | ||
| src/validators.js:35:39:35:54 | req.query.search | src/validators.js:35:21:35:62 | `<h1>Se ... !</h1>` | | ||
| src/validators.js:35:39:35:54 | req.query.search | src/validators.js:35:21:35:62 | `<h1>Se ... !</h1>` | | ||
| src/validators.js:35:39:35:54 | req.query.search | src/validators.js:35:21:35:62 | `<h1>Se ... !</h1>` | | ||
| src/validators.js:35:39:35:54 | req.query.search | src/validators.js:35:21:35:62 | `<h1>Se ... !</h1>` | | ||
| src/validators.js:38:39:38:52 | req.query.name | src/validators.js:38:21:38:60 | `<h1>Se ... !</h1>` | | ||
| src/validators.js:38:39:38:52 | req.query.name | src/validators.js:38:21:38:60 | `<h1>Se ... !</h1>` | | ||
| src/validators.js:38:39:38:52 | req.query.name | src/validators.js:38:21:38:60 | `<h1>Se ... !</h1>` | | ||
| src/validators.js:38:39:38:52 | req.query.name | src/validators.js:38:21:38:60 | `<h1>Se ... !</h1>` | | ||
| src/validators.js:41:39:41:52 | req.query.name | src/validators.js:41:21:41:60 | `<h1>Se ... !</h1>` | | ||
| src/validators.js:41:39:41:52 | req.query.name | src/validators.js:41:21:41:60 | `<h1>Se ... !</h1>` | | ||
| src/validators.js:41:39:41:52 | req.query.name | src/validators.js:41:21:41:60 | `<h1>Se ... !</h1>` | | ||
| src/validators.js:41:39:41:52 | req.query.name | src/validators.js:41:21:41:60 | `<h1>Se ... !</h1>` | | ||
#select | ||
| src/validators.js:35:21:35:62 | `<h1>Se ... !</h1>` | src/validators.js:35:39:35:54 | req.query.search | src/validators.js:35:21:35:62 | `<h1>Se ... !</h1>` | Cross-site scripting vulnerability due to a $@. | src/validators.js:35:39:35:54 | req.query.search | user-provided value | | ||
| src/validators.js:38:21:38:60 | `<h1>Se ... !</h1>` | src/validators.js:38:39:38:52 | req.query.name | src/validators.js:38:21:38:60 | `<h1>Se ... !</h1>` | Cross-site scripting vulnerability due to a $@. | src/validators.js:38:39:38:52 | req.query.name | user-provided value | | ||
| src/validators.js:41:21:41:60 | `<h1>Se ... !</h1>` | src/validators.js:41:39:41:52 | req.query.name | src/validators.js:41:21:41:60 | `<h1>Se ... !</h1>` | Cross-site scripting vulnerability due to a $@. | src/validators.js:41:39:41: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 |
Empty file.
10 changes: 10 additions & 0 deletions
10
javascript/ql/test/library-tests/frameworks/ExpressValidator/test.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,10 @@ | ||
| src/validators.js:7:22:7:36 | query('search') | parameter | search | | ||
| src/validators.js:10:22:10:34 | query('type') | parameter | type | | ||
| src/validators.js:13:22:13:35 | query('email') | parameter | email | | ||
| src/validators.js:16:22:16:34 | query('type') | parameter | type | | ||
| src/validators.js:19:22:19:36 | query('search') | parameter | search | | ||
| src/validators.js:23:28:23:42 | query('search') | parameter | search | | ||
| src/validators.js:23:54:23:66 | query('type') | parameter | type | | ||
| src/validators.js:23:77:23:90 | query('email') | parameter | email | | ||
| src/validators.js:30:24:30:38 | query('search') | parameter | search | | ||
| src/validators.js:33:24:33:35 | body('name') | body | name | |
8 changes: 8 additions & 0 deletions
8
javascript/ql/test/library-tests/frameworks/ExpressValidator/test.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,8 @@ | ||
import javascript | ||
|
||
query predicate test_middleweare( | ||
ExpressValidator::MiddlewareInstance middleware, string param_type, string param | ||
) { | ||
param = middleware.getSafeParameterName() and | ||
param_type = middleware.getValidatorType() | ||
} |
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.