Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions javascript/ql/lib/semmle/javascript/frameworks/Express.qll
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,40 @@ module Express {
override RouteHandler getRouteHandler() { result = response.getRouteHandler() }
}

/**
* A call to `res.json()` or `res.jsonp()`.
*
* This sets the `content-type` header.
*/
private class ResponseJsonCall extends DataFlow::MethodCallNode, Http::HeaderDefinition {
private ResponseSource response;

ResponseJsonCall() { this = response.ref().getAMethodCall(["json", "jsonp"]) }

override RouteHandler getRouteHandler() { result = response.getRouteHandler() }

override string getAHeaderName() { result = "content-type" }

override predicate defines(string headerName, string headerValue) {
// Note: for `jsonp` the actual content-type header will be `text/javascript` or similar, but to avoid
// generating a spurious HTML injection sink, we treat it as `application/json` here.
headerName = "content-type" and headerValue = "application/json"
}
}

/**
* An argument passed to the `json` or `json` method of an HTTP response object.
*/
private class ResponseJsonCallArgument extends Http::ResponseSendArgument {
ResponseJsonCall call;

ResponseJsonCallArgument() { this = call.getArgument(0) }

override RouteHandler getRouteHandler() { result = call.getRouteHandler() }

override HeaderDefinition getAnAssociatedHeaderDefinition() { result = call }
}

/**
* An invocation of the `cookie` method on an HTTP response object.
*/
Expand Down
10 changes: 10 additions & 0 deletions javascript/ql/test/library-tests/frameworks/Express/src/json.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const express = require('express');
const app = express();

app.get('/test/json', function(req, res) {
res.json(req.query.data);
});

app.get('/test/jsonp', function(req, res) {
res.jsonp(req.query.data);
});
Loading