Skip to content
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

Experimental CodeInjection query for JavaScript doesn't seem to work #18619

Open
yonajix opened this issue Jan 29, 2025 · 2 comments
Open

Experimental CodeInjection query for JavaScript doesn't seem to work #18619

yonajix opened this issue Jan 29, 2025 · 2 comments
Labels
question Further information is requested

Comments

@yonajix
Copy link

yonajix commented Jan 29, 2025

I noticed that the sources and sinks for some of the code injection CVEs in JavaScript were spread out among different queries making it difficult or impossible to find the issues in some cases. I also saw that there is an experimental suite of Javascript Security queries that add additional sources.

I tested the experimental query for code injection located here on some simple js code that contains a use of JSON.stringify as source and eval as sink. I know this should be covered because there is a private class for JSON.stringify in the AdditionalSources.qll that CodeInjection.ql imports.

/**
 * A use of `JSON.stringify`, viewed as a source for command-line injections
 * since it does not properly escape single quotes and dollar symbols.
 */
private class JsonStringifyAsCommandInjectionSource extends HeuristicSource,
  CommandInjection::Source instanceof JsonStringifyCall
{
  override string getSourceType() { result = "a string from JSON.stringify" }
}

When I run it on this code, I get 0 results:

import fs from 'fs';


function encodeStatements(statements) {

    eval(JSON.stringify(statements))

}

module.exports = encodeStatements;


I tested with this query to ensure that the sink was recognized which it was.


import javascript
import semmle.javascript.security.dataflow.CodeInjectionQuery
import CodeInjectionFlow::PathGraph
import semmle.javascript.heuristics.AdditionalSources

from DataFlow::Node sink
where CodeInjectionConfig::isSink(sink)
select sink, "Sink found"

When I tried the same thing for sources I got received 0 results in the source code. However I ran this query which confirmed that JSON.stringify is part of the sources added as HeuristicSource, so I'm very confused as to why this doesn't work.

import javascript
import semmle.javascript.security.dataflow.CodeInjectionQuery
import CodeInjectionFlow::PathGraph
import semmle.javascript.heuristics.AdditionalSources



from DataFlow::Node source
where source instanceof HeuristicSource
select source, "User input"

Not sure where the error here lies but I could use some help diagnosing it. Thanks!

@yonajix yonajix added the question Further information is requested label Jan 29, 2025
@rvermeulen
Copy link
Contributor

rvermeulen commented Jan 29, 2025

Hi @yonajix,

Thanks for your question. I agree this is confusing, but the result is indeed empty.

The JSON.stringify source is not a active threat-model source as expected by the CodeInjection source.

We can add one, however a second problem will prevent any results.
Namely the JsonStringlifySanitizer that sanitizes the result of that call. The opposite of what we want.

I will contact the author to determine if this heuristic source was intended to be supported. Keep you posted.

@rvermeulen
Copy link
Contributor

Hi @yonajix,

It was indeed intended to support this additional heuristic source, however the extensions cannot influence the barrier that prevents it.
Improving experimental queries are not a product priority, but I will create a tracking issue in the case we can pick up this FN.

To resolve this for your use, I would suggest to copy the configuration and modify the barrier to exclude heuristic source.

Let me know if you have any further questions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants