|
| 1 | +/** |
| 2 | + * Provides modeling for the `Response` component of the `Rack` library. |
| 3 | + */ |
| 4 | + |
| 5 | +private import codeql.ruby.AST |
| 6 | +private import codeql.ruby.ApiGraphs |
| 7 | +private import codeql.ruby.Concepts |
| 8 | +private import codeql.ruby.controlflow.CfgNodes::ExprNodes |
| 9 | +private import codeql.ruby.DataFlow |
| 10 | +private import codeql.ruby.typetracking.TypeTracker |
| 11 | +private import App as A |
| 12 | + |
| 13 | +/** Contains implementation details for modeling `Rack::Response`. */ |
| 14 | +module Private { |
| 15 | + /** A `DataFlow::Node` that may be a rack response. This is detected heuristically, if something "looks like" a rack response syntactically then we consider it to be a potential response node. */ |
| 16 | + class PotentialResponseNode extends DataFlow::ArrayLiteralNode { |
| 17 | + // [status, headers, body] |
| 18 | + PotentialResponseNode() { this.getNumberOfArguments() = 3 } |
| 19 | + |
| 20 | + /** Gets the headers returned with this response. */ |
| 21 | + DataFlow::Node getHeaders() { result = this.getElement(1) } |
| 22 | + |
| 23 | + /** Gets the body of this response. */ |
| 24 | + DataFlow::Node getBody() { result = this.getElement(2) } |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +/** |
| 29 | + * Provides modeling for the `Response` component of the `Rack` library. |
| 30 | + */ |
| 31 | +module Public { |
| 32 | + bindingset[headerName] |
| 33 | + private DataFlow::Node getHeaderValue(ResponseNode resp, string headerName) { |
| 34 | + exists(DataFlow::Node headers | headers = resp.getHeaders() | |
| 35 | + // set via `headers.<header_name>=` |
| 36 | + exists( |
| 37 | + DataFlow::CallNode contentTypeAssignment, Assignment assignment, |
| 38 | + DataFlow::PostUpdateNode postUpdateHeaders |
| 39 | + | |
| 40 | + contentTypeAssignment.getMethodName() = headerName.replaceAll("-", "_").toLowerCase() + "=" and |
| 41 | + assignment = |
| 42 | + contentTypeAssignment.getArgument(0).(DataFlow::OperationNode).asOperationAstNode() and |
| 43 | + postUpdateHeaders.(DataFlow::LocalSourceNode).flowsTo(headers) and |
| 44 | + postUpdateHeaders.getPreUpdateNode() = contentTypeAssignment.getReceiver() |
| 45 | + | |
| 46 | + result.asExpr().getExpr() = assignment.getRightOperand() |
| 47 | + ) |
| 48 | + or |
| 49 | + // set within a hash |
| 50 | + exists(DataFlow::HashLiteralNode headersHash | headersHash.flowsTo(headers) | |
| 51 | + result = |
| 52 | + headersHash |
| 53 | + .getElementFromKey(any(ConstantValue v | |
| 54 | + v.getStringlikeValue().toLowerCase() = headerName.toLowerCase() |
| 55 | + )) |
| 56 | + ) |
| 57 | + ) |
| 58 | + } |
| 59 | + |
| 60 | + /** A `DataFlow::Node` returned from a rack request. */ |
| 61 | + class ResponseNode extends Private::PotentialResponseNode, Http::Server::HttpResponse::Range { |
| 62 | + ResponseNode() { this = any(A::App::AppCandidate app).getResponse() } |
| 63 | + |
| 64 | + override DataFlow::Node getBody() { result = this.getElement(2) } |
| 65 | + |
| 66 | + override DataFlow::Node getMimetypeOrContentTypeArg() { |
| 67 | + result = getHeaderValue(this, "content-type") |
| 68 | + } |
| 69 | + |
| 70 | + // TODO: is there a sensible value for this? |
| 71 | + override string getMimetypeDefault() { none() } |
| 72 | + } |
| 73 | + |
| 74 | + /** A `DataFlow::Node` returned from a rack request that has a redirect HTTP status code. */ |
| 75 | + class RedirectResponse extends ResponseNode, Http::Server::HttpRedirectResponse::Range { |
| 76 | + private DataFlow::Node redirectLocation; |
| 77 | + |
| 78 | + RedirectResponse() { redirectLocation = getHeaderValue(this, "location") } |
| 79 | + |
| 80 | + override DataFlow::Node getRedirectLocation() { result = redirectLocation } |
| 81 | + } |
| 82 | +} |
0 commit comments