Skip to content

Commit 0560f4f

Browse files
committed
Add classification and type to queries
1 parent 31fdc79 commit 0560f4f

File tree

3 files changed

+75
-16
lines changed

3 files changed

+75
-16
lines changed

extensions/ql-vscode/src/data-extensions-editor/queries/csharp.ts

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,16 @@ class ExternalApi extends CallableMethod {
2222
2323
private Call aUsage(ExternalApi api) { result.getTarget().getUnboundDeclaration() = api }
2424
25-
from ExternalApi api, string apiName, boolean supported, Call usage
25+
from
26+
ExternalApi api, string apiName, boolean supported, Call usage, string type, string classification
2627
where
2728
apiName = api.getApiName() and
2829
supported = isSupported(api) and
29-
usage = aUsage(api)
30-
select usage, apiName, supported.toString(), "supported", api.getFile().getBaseName(), "library"
30+
usage = aUsage(api) and
31+
type = supportedType(api) and
32+
classification = methodClassification(usage)
33+
select usage, apiName, supported.toString(), "supported", api.getFile().getBaseName(), "library",
34+
type, "type", classification, "classification"
3135
`,
3236
frameworkModeQuery: `/**
3337
* @name Public methods
@@ -46,12 +50,13 @@ class PublicMethod extends CallableMethod {
4650
PublicMethod() { this.fromSource() and not this.getFile() instanceof TestFile }
4751
}
4852
49-
from PublicMethod publicMethod, string apiName, boolean supported
53+
from PublicMethod publicMethod, string apiName, boolean supported, string type
5054
where
5155
apiName = publicMethod.getApiName() and
52-
supported = isSupported(publicMethod)
56+
supported = isSupported(publicMethod) and
57+
type = supportedType(publicMethod)
5358
select publicMethod, apiName, supported.toString(), "supported",
54-
publicMethod.getFile().getBaseName(), "library"
59+
publicMethod.getFile().getBaseName(), "library", type, "type", "unknown", "classification"
5560
`,
5661
dependencies: {
5762
"AutomodelVsCode.qll": `/** Provides classes and predicates related to handling APIs for the VS Code extension. */
@@ -66,6 +71,7 @@ private import semmle.code.csharp.dataflow.internal.DataFlowPrivate
6671
private import semmle.code.csharp.dataflow.internal.DataFlowDispatch as DataFlowDispatch
6772
private import semmle.code.csharp.dataflow.internal.FlowSummaryImpl as FlowSummaryImpl
6873
private import semmle.code.csharp.dataflow.internal.TaintTrackingPrivate
74+
private import semmle.code.csharp.frameworks.Test
6975
private import semmle.code.csharp.security.dataflow.flowsources.Remote
7076
7177
pragma[nomagic]
@@ -180,6 +186,25 @@ boolean isSupported(CallableMethod callableMethod) {
180186
result = false
181187
}
182188
189+
string supportedType(CallableMethod method) {
190+
method.isSink() and result = "sink"
191+
or
192+
method.isSource() and result = "source"
193+
or
194+
method.hasSummary() and result = "summary"
195+
or
196+
method.isNeutral() and result = "neutral"
197+
or
198+
not method.isSupported() and result = "none"
199+
}
200+
201+
string methodClassification(Call method) {
202+
method.getFile() instanceof TestFile and result = "test"
203+
or
204+
not method.getFile() instanceof TestFile and
205+
result = "source"
206+
}
207+
183208
/**
184209
* Gets the nested name of the declaration.
185210
*

extensions/ql-vscode/src/data-extensions-editor/queries/java.ts

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,19 @@ class ExternalApi extends CallableMethod {
1616
ExternalApi() { not this.fromSource() }
1717
}
1818
19-
private Call aUsage(ExternalApi api) {
20-
result.getCallee().getSourceDeclaration() = api and
21-
not result.getFile() instanceof GeneratedFile
22-
}
19+
private Call aUsage(ExternalApi api) { result.getCallee().getSourceDeclaration() = api }
2320
24-
from ExternalApi externalApi, string apiName, boolean supported, Call usage
21+
from
22+
ExternalApi externalApi, string apiName, boolean supported, Call usage, string type,
23+
string classification
2524
where
2625
apiName = externalApi.getApiName() and
2726
supported = isSupported(externalApi) and
28-
usage = aUsage(externalApi)
29-
select usage, apiName, supported.toString(), "supported", externalApi.jarContainer(), "library"
27+
usage = aUsage(externalApi) and
28+
type = supportedType(externalApi) and
29+
classification = methodClassification(usage)
30+
select usage, apiName, supported.toString(), "supported", externalApi.jarContainer(), "library",
31+
type, "type", classification, "classification"
3032
`,
3133
frameworkModeQuery: `/**
3234
* @name Public methods
@@ -41,12 +43,14 @@ import AutomodelVsCode
4143
4244
class PublicMethodFromSource extends CallableMethod, ModelApi { }
4345
44-
from PublicMethodFromSource publicMethod, string apiName, boolean supported
46+
from PublicMethodFromSource publicMethod, string apiName, boolean supported, string type
4547
where
4648
apiName = publicMethod.getApiName() and
47-
supported = isSupported(publicMethod)
49+
supported = isSupported(publicMethod) and
50+
type = supportedType(publicMethod)
4851
select publicMethod, apiName, supported.toString(), "supported",
49-
publicMethod.getCompilationUnit().getParentContainer().getBaseName(), "library"
52+
publicMethod.getCompilationUnit().getParentContainer().getBaseName(), "library", type, "type",
53+
"unknown", "classification"
5054
`,
5155
dependencies: {
5256
"AutomodelVsCode.qll": `/** Provides classes and predicates related to handling APIs for the VS Code extension. */
@@ -147,6 +151,28 @@ boolean isSupported(CallableMethod method) {
147151
not method.isSupported() and result = false
148152
}
149153
154+
string supportedType(CallableMethod method) {
155+
method.isSink() and result = "sink"
156+
or
157+
method.isSource() and result = "source"
158+
or
159+
method.hasSummary() and result = "summary"
160+
or
161+
method.isNeutral() and result = "neutral"
162+
or
163+
not method.isSupported() and result = "none"
164+
}
165+
166+
string methodClassification(Call method) {
167+
isInTestFile(method.getLocation().getFile()) and result = "test"
168+
or
169+
method.getFile() instanceof GeneratedFile and result = "generated"
170+
or
171+
not isInTestFile(method.getLocation().getFile()) and
172+
not method.getFile() instanceof GeneratedFile and
173+
result = "source"
174+
}
175+
150176
// The below is a copy of https://github.com/github/codeql/blob/249f9f863db1e94e3c46ca85b49fb0ec32f8ca92/java/ql/lib/semmle/code/java/dataflow/internal/ModelExclusions.qll
151177
// to avoid the use of internal modules.
152178
/** Holds if the given package \`p\` is a test package. */

extensions/ql-vscode/src/data-extensions-editor/queries/query.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ export type Query = {
99
* - "supported": a string literal. This is required to make the query a valid problem query.
1010
* - libraryName: the name of the library that contains the external API. This is a string and usually the basename of a file.
1111
* - "library": a string literal. This is required to make the query a valid problem query.
12+
* - type: the modeled kind of the method, either "sink", "source", "summary", or "neutral"
13+
* - "type": a string literal. This is required to make the query a valid problem query.
14+
* - classification: the classification of the use of the method, either "source", "test", "generated", or "unknown"
15+
* - "classification: a string literal. This is required to make the query a valid problem query.
1216
*/
1317
applicationModeQuery: string;
1418
/**
@@ -22,6 +26,10 @@ export type Query = {
2226
* - "supported": a string literal. This is required to make the query a valid problem query.
2327
* - libraryName: an arbitrary string. This is required to make it match the structure of the application query.
2428
* - "library": a string literal. This is required to make the query a valid problem query.
29+
* - type: the modeled kind of the method, either "sink", "source", "summary", or "neutral"
30+
* - "type": a string literal. This is required to make the query a valid problem query.
31+
* - "unknown": a string literal. This is required to make it match the structure of the application query.
32+
* - "classification: a string literal. This is required to make the query a valid problem query.
2533
*/
2634
frameworkModeQuery: string;
2735
dependencies?: {

0 commit comments

Comments
 (0)