Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
express().get('/news', (req, res) => {
let topic = req.query.topic;
res.send(`<h1>${topic}</h1>`);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
let path = require('path');

express().get('/data', (req, res) => {
let file = path.join(HOME_DIR, 'public', req.query.file);
res.sendFile(file);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>Using unsanitized untrusted data in an external API can cause a variety of security issues. This query reports
all external APIs that are used with untrusted data, along with how frequently the API is used, and how many
unique sources of untrusted data flow to this API. This query is designed primarily to help identify which APIs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
unique sources of untrusted data flow to this API. This query is designed primarily to help identify which APIs
unique sources of untrusted data flow to this API. This query is primarily designed to help identify the APIs

may be relevant for security analysis of this application.</p>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also I am not sure I understand what you mean here, so let me know if my suggestion doesn't make any sense

Suggested change
may be relevant for security analysis of this application.</p>
that may be relevant for the analysis of security within this application.</p>


<p>An external API is defined as a call to a function that is not defined in the source code, not overridden
in the source code, and is not modeled as a taint step in the default taint library. External APIs may be from the
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
in the source code, and is not modeled as a taint step in the default taint library. External APIs may be from the
in the source code, and not modeled as a taint step in the default taint library. External APIs may come from the

third party dependencies or from internal dependencies. The query will report the external package name, followed
by an access path leading to the function, followed by <code>[param x]</code> where <code>x</code>
indicates the position of the parameter receiving the untrusted data.</p>

</overview>
<recommendation>

<p>For each result:</p>

<ul>
<li>If the result highlights a known sink, no action is required.</li>
<li>If the result highlights an unknown sink for a problem, then add modeling for the sink to the relevant query.</li>
<li>If the result represents a call to an external API which transfers taint, add the appropriate modeling, and
re-run the query to determine what new results have appeared due to this additional modeling.</li>
</ul>

<p>Otherwise, the result is likely uninteresting. Custom versions of this query can extend the <code>SafeExternalAPIMethod</code>
class to exclude known safe external APIs from future analysis.</p>

</recommendation>
<example>

<p>If the query were to return the API <code>express().get.[callback].[param 'res'].send() [param 0]</code>,
this could correspond to the <code>X</code> in <code>express().get('/foo', (req, res) => res.send(X))</code>.
First we should consider whether this a security relevant sink. In this case, this is writing to a HTTP response, so we should
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
First we should consider whether this a security relevant sink. In this case, this is writing to a HTTP response, so we should
First we should consider whether this a security-relevant sink. In this case, this is writing to a HTTP response, so we should

consider whether this is an XSS sink. If it is, we should confirm that it is handled by the reflected XSS query.</p>

<p>If the query were to return the API <code>url.parse java.lang.StringBuilder.append(java.lang.String) [param 0]</code>, then this should be
reviewed as a possible taint step, because tainted data would flow from the 0th argument to the qualifier of the call.</p>

<p>Note that both examples are correctly handled by the standard taint tracking library and XSS query.</p>
</example>
<references>

</references>
</qhelp>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* @name Frequency counts for external APIs that are used with untrusted data
* @description This reports the external APIs that are used with untrusted data, along with how
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This description doesn't follow the format specified in https://github.com/github/codeql/blob/master/docs/query-metadata-style-guide.md#query-descriptions-description.
Alternatively, what do you think of something like "Use this query to return the external APIs...."?

* frequently the API is called, and how many unique sources of untrusted data flow
* to it.
* @id js/count-untrusted-data-external-api
* @kind table
* @tags security external/cwe/cwe-20
*/

import javascript
import semmle.javascript.security.dataflow.ExternalAPIUsedWithUntrustedData::ExternalAPIUsedWithUntrustedData

from ExternalAPIUsedWithUntrustedData externalAPI
select externalAPI, count(externalAPI.getUntrustedDataNode()) as numberOfUses,
externalAPI.getNumberOfUntrustedSources() as numberOfUntrustedSources order by
numberOfUntrustedSources desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>Using unsanitized untrusted data in an external API can cause a variety of security issues. This query reports
external APIs that use untrusted data. The results are not filtered so that you can audit all examples. The query provides data for security reviews of the application and you can also use it to identify external APIs that should be modeled as either taint steps, or sinks for specific problems.</p>

<p>An external API is defined as a method call to a method that is not defined in the source code, not overridden
in the source code, and is not modeled as a taint step in the default taint library. External APIs may be from the
third-party dependencies or from internal dependencies. The query reports uses of
untrusted data one of the arguments of external API call or in the return value from a callback passed to an external API.</p>

</overview>
<recommendation>

<p>For each result:</p>

<ul>
<li>If the result highlights a known sink, confirm that the result is reported by the relevant query, or
that the result is a false positive because this data is sanitized.</li>
<li>If the result highlights an unknown sink for a problem, then add modeling for the sink to the relevant query,
and confirm that the result is either found, or is safe due to appropriate sanitization.</li>
<li>If the result represents a call to an external API that transfers taint, add the appropriate modeling, and
re-run the query to determine what new results have appeared due to this additional modeling.</li>
</ul>

<p>Otherwise, the result is likely uninteresting. Custom versions of this query can extend the <code>SafeExternalAPIMethod</code>
class to exclude known safe external APIs from future analysis.</p>

</recommendation>
<example>

<p>In this first example, a query parameter is read from the <code>req</code> parameter and then ultimately used in a call to the
<code>res.send</code> external API:</p>

<sample src="ExternalAPISinkExample.js" />

<p>This is a reflected XSS sink. The XSS query should therefore be reviewed to confirm that this sink is appropriately modeled,
and if it is, to confirm that the query reports this particular result, or that the result is a false positive due to
some existing sanitization.</p>

<p>In this second example, again a query parameter is read from <code>req</code>.</p>

<sample src="ExternalAPITaintStepExample.js" />

<p>If the query reported the call to <code>path.join</code> on line 4, this would suggest that this external API is
not currently modeled as a taint step in the taint tracking library. The next step would be to model this as a taint step, then
re-run the query to determine what additional results might be found. In this example, it seems the result of the
<code>path.join</code> will be used as a file path, leading to a path traversal vulnerability.</p>

<p>Note that both examples are correctly handled by the standard taint tracking library and security queries.</p>
</example>
<references>

</references>
</qhelp>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @name Untrusted data passed to external API
* @description Data provided remotely is used in this external API without sanitization, which could be a security risk.
* @id js/untrusted-data-to-external-api
* @kind path-problem
* @precision low
* @problem.severity error
* @tags security external/cwe/cwe-20
*/

import javascript
import semmle.javascript.security.dataflow.ExternalAPIUsedWithUntrustedData::ExternalAPIUsedWithUntrustedData
import DataFlow::PathGraph

from Configuration config, DataFlow::PathNode source, DataFlow::PathNode sink
where config.hasFlowPath(source, sink)
select sink, source, sink,
"Call to " + sink.getNode().(Sink).getApiName() + " with untrusted data from $@.", source,
source.toString()
5 changes: 5 additions & 0 deletions javascript/ql/src/semmle/javascript/dataflow/Nodes.qll
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,11 @@ class FunctionNode extends DataFlow::ValueNode, DataFlow::SourceNode {
*/
class ObjectLiteralNode extends DataFlow::ValueNode, DataFlow::SourceNode {
override ObjectExpr astNode;

/** Gets the value of a spread property of this object literal, such as `x` in `{...x}` */
DataFlow::Node getASpreadProperty() {
result = astNode.getAProperty().(SpreadProperty).getInit().(SpreadElement).getOperand().flow()
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/**
* Provides a taint tracking configuration for reasoning about untrusted
* data flowing to an external API call.
*
* Note, for performance reasons: only import this file if
* `ExternalAPIUsedWithUntrustedData::Configuration` is needed, otherwise
* `ExternalAPIUsedWithUntrustedDataCustomizations` should be imported instead.
*/

import javascript

/**
* Provides a taint tracking configuration for reasoning about untrusted
* data flowing to an external API call.
*/
module ExternalAPIUsedWithUntrustedData {
import ExternalAPIUsedWithUntrustedDataCustomizations::ExternalAPIUsedWithUntrustedData

/** Flow label for objects from which a tainted value is reachable. */
private class ObjectWrapperFlowLabel extends DataFlow::FlowLabel {
ObjectWrapperFlowLabel() { this = "object-wrapper" }
}

/**
* A taint tracking configuration for untrusted data flowing to an external API.
*/
class Configuration extends TaintTracking::Configuration {
Configuration() { this = "ExternalAPIUsedWithUntrustedData" }

override predicate isSource(DataFlow::Node source) { source instanceof Source }

override predicate isSink(DataFlow::Node sink, DataFlow::FlowLabel lbl) {
sink instanceof Sink and
(lbl.isTaint() or lbl instanceof ObjectWrapperFlowLabel)
}

override predicate isSanitizer(DataFlow::Node node) {
super.isSanitizer(node) or
node instanceof Sanitizer
}

override predicate isAdditionalFlowStep(
DataFlow::Node pred, DataFlow::Node succ, DataFlow::FlowLabel predLbl,
DataFlow::FlowLabel succLbl
) {
// Step into an object and switch to the 'object-wrapper' label.
exists(DataFlow::PropWrite write |
pred = write.getRhs() and
succ = write.getBase().getALocalSource() and
(predLbl.isTaint() or predLbl instanceof ObjectWrapperFlowLabel) and
succLbl instanceof ObjectWrapperFlowLabel
)
}

override predicate isSanitizerEdge(DataFlow::Node pred, DataFlow::Node succ) {
// Block flow from the location to its properties, as the relevant properties (hash and search) are taint sources of their own.
// The location source is only used for propagating through API calls like `new URL(location)` and into external APIs where
// the whole location object escapes.
exists(DataFlow::PropRead read |
read = DOM::locationRef().getAPropertyRead() and
pred = read.getBase() and
succ = read
)
}
}

/** A node representing data being passed to an external API. */
class ExternalAPIDataNode extends DataFlow::Node {
ExternalAPIDataNode() { this instanceof Sink }
}

/** A node representing untrusted data being passed to an external API. */
class UntrustedExternalAPIDataNode extends ExternalAPIDataNode {
UntrustedExternalAPIDataNode() { any(Configuration c).hasFlow(_, this) }

/** Gets a source of untrusted data which is passed to this external API data node. */
DataFlow::Node getAnUntrustedSource() { any(Configuration c).hasFlow(result, this) }
}

/**
* Name of an external API sink, boxed in a newtype for consistency with other languages.
*/
private newtype TExternalApi =
MkExternalApiNode(string name) {
exists(Sink sink |
any(Configuration c).hasFlow(_, sink) and
name = sink.getApiName()
)
}

/** An external API which is used with untrusted data. */
class ExternalAPIUsedWithUntrustedData extends TExternalApi {
/** Gets a possibly untrusted use of this external API. */
UntrustedExternalAPIDataNode getUntrustedDataNode() {
this = MkExternalApiNode(result.(Sink).getApiName())
}

/** Gets the number of untrusted sources used with this external API. */
int getNumberOfUntrustedSources() {
result = count(getUntrustedDataNode().getAnUntrustedSource())
}

/** Gets a textual representation of this element. */
string toString() { this = MkExternalApiNode(result) }
}
}
Loading