mirror of
https://github.com/github/codeql.git
synced 2025-12-21 03:06:31 +01:00
Initial support for capturing sink models
This commit is contained in:
37
java/ql/src/utils/model-generator/CaptureSinkModels.ql
Normal file
37
java/ql/src/utils/model-generator/CaptureSinkModels.ql
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
import java
|
||||||
|
import Telemetry.ExternalAPI
|
||||||
|
import semmle.code.java.dataflow.DataFlow
|
||||||
|
import semmle.code.java.dataflow.TaintTracking
|
||||||
|
import semmle.code.java.dataflow.ExternalFlow
|
||||||
|
import ModelGeneratorUtils
|
||||||
|
|
||||||
|
class Configuration extends TaintTracking::Configuration {
|
||||||
|
Configuration() { this = "public methods calling sinks" }
|
||||||
|
|
||||||
|
override predicate isSource(DataFlow::Node source) {
|
||||||
|
exists(MethodAccess ma |
|
||||||
|
ma = source.asExpr() and
|
||||||
|
ma.getAnEnclosingStmt().getEnclosingCallable().isPublic() and
|
||||||
|
ma.getAnEnclosingStmt().getEnclosingCallable().fromSource()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
override predicate isSink(DataFlow::Node sink) { sinkNode(sink, _) }
|
||||||
|
}
|
||||||
|
|
||||||
|
string asInputArgument(Expr source) { result = "Argument[" + source.(Argument).getPosition() + "]" }
|
||||||
|
|
||||||
|
string captureSink(Callable api) {
|
||||||
|
exists(DataFlow::Node src, DataFlow::Node sink, Configuration config, string kind |
|
||||||
|
config.hasFlow(src, sink) and
|
||||||
|
sinkNode(sink, kind) and
|
||||||
|
api = src.asExpr().getEnclosingCallable() and
|
||||||
|
result = asSinkModel(api, asInputArgument(src.asExpr()), kind)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
from Callable api, string sink
|
||||||
|
where
|
||||||
|
sink = captureSink(api) and
|
||||||
|
not api.getCompilationUnit().getFile().getAbsolutePath().matches("%src/test/%")
|
||||||
|
select sink order by sink
|
||||||
@@ -16,6 +16,23 @@ string asValueModel(Callable api, string input, string output) {
|
|||||||
|
|
||||||
bindingset[input, output, kind]
|
bindingset[input, output, kind]
|
||||||
string asSummaryModel(Callable api, string input, string output, string kind) {
|
string asSummaryModel(Callable api, string input, string output, string kind) {
|
||||||
|
result =
|
||||||
|
asPartialModel(api) + input + ";" //
|
||||||
|
+ output + ";" //
|
||||||
|
+ kind + ";" //
|
||||||
|
}
|
||||||
|
|
||||||
|
bindingset[input, kind]
|
||||||
|
string asSinkModel(Callable api, string input, string kind) {
|
||||||
|
result =
|
||||||
|
asPartialModel(api) + input + ";" //
|
||||||
|
+ kind + ";" //
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Computes the first 6 columns for CSV rows.
|
||||||
|
*/
|
||||||
|
private string asPartialModel(Callable api) {
|
||||||
result =
|
result =
|
||||||
api.getCompilationUnit().getPackage().getName() + ";" //
|
api.getCompilationUnit().getPackage().getName() + ";" //
|
||||||
+ api.getDeclaringType().nestedName() + ";" //
|
+ api.getDeclaringType().nestedName() + ";" //
|
||||||
@@ -23,9 +40,6 @@ string asSummaryModel(Callable api, string input, string output, string kind) {
|
|||||||
+ api.getName() + ";" //
|
+ api.getName() + ";" //
|
||||||
+ paramsString(api) + ";" //
|
+ paramsString(api) + ";" //
|
||||||
+ /* ext + */ ";" //
|
+ /* ext + */ ";" //
|
||||||
+ input + ";" //
|
|
||||||
+ output + ";" //
|
|
||||||
+ kind + ";" //
|
|
||||||
}
|
}
|
||||||
|
|
||||||
string parameterAccess(Parameter p) {
|
string parameterAccess(Parameter p) {
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
| p;Sinks;true;copyFileToDirectory;(Path,Path,CopyOption[]);;Argument[1];create-file; |
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
utils/model-generator/CaptureSinkModels.ql
|
||||||
25
java/ql/test/utils/model-generator/p/Sinks.java
Normal file
25
java/ql/test/utils/model-generator/p/Sinks.java
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
package p;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.nio.file.CopyOption;
|
||||||
|
import java.nio.charset.Charset;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
|
||||||
|
public class Sinks {
|
||||||
|
|
||||||
|
public Path copyFileToDirectory(final Path sourceFile, final Path targetDirectory, final CopyOption... copyOptions) throws IOException {
|
||||||
|
return Files.copy(sourceFile, targetDirectory.resolve(sourceFile.getFileName()), copyOptions);
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: not detected
|
||||||
|
public String readUrl(final URL url, Charset encoding) throws IOException {
|
||||||
|
try (InputStream in = url.openStream()) {
|
||||||
|
byte[] bytes = in.readAllBytes();
|
||||||
|
return new String(bytes, encoding);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user