Skip to content

JS: Add query for unsafe construction of code from library input #5841

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

Merged
merged 17 commits into from
Feb 10, 2022

Conversation

erik-krogh
Copy link
Contributor

@erik-krogh erik-krogh commented May 5, 2021

CVE-2017-5954: TP/FP (conditional unsafety)
CVE-2019-5413: TP/TN
CVE-2020-28502: TP/TN
CVE-2020-7712: TP/TN
CVE-2021-23406: TP/TN
CVE-2021-23337: TP
CVE-2021-32831: TP
CVE-2021-23358: TP
CVE-2020-7640: TP/TN

A test run shows a lot of results, many of which just looks to be (very) bad style.
So I've set the severity to warning.

An evaluation looks good.. (The baseline is main + a where none() edition of the query).
The new results look reasonable to me, even though they're basically just bad style.
(I suppose there are performance reasons for implementing it in that style).

@erik-krogh erik-krogh added the Awaiting evaluation Do not merge yet, this PR is waiting for an evaluation to finish label May 5, 2021
@erik-krogh erik-krogh force-pushed the libCode branch 2 times, most recently from ed24fe9 to 9f2f022 Compare May 6, 2021 12:29
@erik-krogh erik-krogh removed the Awaiting evaluation Do not merge yet, this PR is waiting for an evaluation to finish label May 6, 2021
@erik-krogh erik-krogh marked this pull request as ready for review May 6, 2021 19:51
@erik-krogh erik-krogh requested a review from a team as a code owner May 6, 2021 19:51
Copy link
Contributor

@esbena esbena left a comment

Choose a reason for hiding this comment

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

LGTM, but lets merge #5769 first, and then apply the teachings from that PR here.


<overview>
<p>
Dynamically constructing code with inputs from exported functions
Copy link
Contributor

Choose a reason for hiding this comment

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

Same formulation concerns as in #5769

Comment on lines 42 to 45
override predicate hasFlowPath(DataFlow::SourcePathNode source, DataFlow::SinkPathNode sink) {
super.hasFlowPath(source, sink) and
exists(DataFlow::MidPathNode mid |
source.getASuccessor*() = mid and
sink = mid.getASuccessor() and
mid.getPathSummary().hasReturn() = false
)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Rebase on top of #5769 , and reuse the similar predicate from that PR

class ExternalInputSource extends Source, DataFlow::ParameterNode {
ExternalInputSource() {
this = Exports::getALibraryInputParameter() and
not this.getName() = "code"
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
not this.getName() = "code"
// permit parameters that clearly are intended to contain executable code.
not this.getName() = "code"

Copy link
Contributor

Choose a reason for hiding this comment

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

(go home diff, you are drunk :) )

@asgerf
Copy link
Contributor

asgerf commented Oct 1, 2021

Sorry for leaving this PR hanging for so long. Looking at the evaluation results, it looks like all of them are FPs:

  • The Angular results are in internal API. I've put up a PR to fix it here: JS: Restrict what PackageExports considers to be public #6789
  • The result in Underscore is a template compiler, which by design can evaluate JS code. It's not bad style or a performance trick; these templates can contain JS code which should be evaluated as-is.

I'd say templating engines should be considered a well-known counter example, and an allow-list should be used to exclude methods with names like template or compile or perhaps even render.

@erik-krogh
Copy link
Contributor Author

I've rebased on top of that PR, and the angular results disappeared in a new evaluation.

  • The result in Underscore is a template compiler, which by design can evaluate JS code. It's not bad style or a performance trick; these templates can contain JS code which should be evaluated as-is.

Yes, the text argument in the underscore template compiler is unsafe by design.
But the kind of code-injection that this query flags can happen in a template compiler (see CVE-2021-23337 and CVE-2021-23358, both of which we get a TP for).
So I think the noise in Underscore is something we can live with.

@esbena
Copy link
Contributor

esbena commented Feb 7, 2022

I'd say templating engines should be considered a well-known counter example, and an allow-list should be used to exclude methods with names like template or compile or perhaps even render.

I agree with this. Could we use a whitelist to avoid intentional APIs like those and otherwise merge this twice-stalled PR as is? We can then separately address the undesirable FNs the whitelist causes.

@erik-krogh
Copy link
Contributor Author

erik-krogh commented Feb 7, 2022

I agree with this. Could we use a whitelist to avoid intentional APIs like those and otherwise merge this twice-stalled PR as is? We can then separately address the undesirable FNs the whitelist causes.

I took a look at the method-names in the CVEs currently flagged by this query:

CVE method-name
CVE-2017-5954 deserialize
CVE-2019-5413 compile
CVE-2020-28502 send
CVE-2020-7712 parseLookup
CVE-2021-23406 compile
CVE-2021-23337 template
CVE-2021-32831 set
CVE-2021-23358 template
CVE-2020-7640 create

Adding a allow-list based on names that are template-engine-like would remove some of those TPs.
I would prefer we lower the precision of the query without an allow-list.

(I rebased the PR on main, and updated to the new change-note format).

@esbena esbena added the ready-for-doc-review This PR requires and is ready for review from the GitHub docs team. label Feb 7, 2022
Co-authored-by: Esben Sparre Andreasen <esbena@github.com>
ethanpalm
ethanpalm previously approved these changes Feb 9, 2022
Copy link
Contributor

@ethanpalm ethanpalm left a comment

Choose a reason for hiding this comment

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

This looks good from a docs point of view. I am just making one small change to move the explanation of why the first example is a bad coding practice to the paragraph before the example rather than after the example ⚡

@codeql-ci codeql-ci merged commit 1a91a79 into github:main Feb 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation JS ready-for-doc-review This PR requires and is ready for review from the GitHub docs team.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants