Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 19469a2

Browse files
committed
C#: Re-factor CSV validation into a separate file.
1 parent 8db454a commit 19469a2

7 files changed

Lines changed: 126 additions & 119 deletions

File tree

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/** Provides a query predicate to check the CSV data for validation errors. */
2+
3+
import csharp
4+
private import internal.AccessPathSyntax
5+
private import internal.FlowSummaryImpl::Private::External
6+
private import internal.FlowSummaryImplSpecific
7+
private import ExternalFlow
8+
9+
/** Holds if some row in a CSV-based flow model appears to contain typos. */
10+
query predicate invalidModelRow(string msg) {
11+
exists(
12+
string pred, string namespace, string type, string name, string signature, string ext,
13+
string provenance
14+
|
15+
sourceModel(namespace, type, _, name, signature, ext, _, _, provenance) and pred = "source"
16+
or
17+
sinkModel(namespace, type, _, name, signature, ext, _, _, provenance) and pred = "sink"
18+
or
19+
summaryModel(namespace, type, _, name, signature, ext, _, _, _, provenance) and
20+
pred = "summary"
21+
or
22+
negativeSummaryModel(namespace, type, name, signature, provenance) and
23+
ext = "" and
24+
pred = "nonesummary"
25+
|
26+
not namespace.regexpMatch("[a-zA-Z0-9_\\.]+") and
27+
msg = "Dubious namespace \"" + namespace + "\" in " + pred + " model."
28+
or
29+
not type.regexpMatch("[a-zA-Z0-9_<>,\\+]+") and
30+
msg = "Dubious type \"" + type + "\" in " + pred + " model."
31+
or
32+
not name.regexpMatch("[a-zA-Z0-9_<>,]*") and
33+
msg = "Dubious member name \"" + name + "\" in " + pred + " model."
34+
or
35+
not signature.regexpMatch("|\\([a-zA-Z0-9_<>\\.\\+\\*,\\[\\]]*\\)") and
36+
msg = "Dubious signature \"" + signature + "\" in " + pred + " model."
37+
or
38+
not ext.regexpMatch("|Attribute") and
39+
msg = "Unrecognized extra API graph element \"" + ext + "\" in " + pred + " model."
40+
or
41+
not provenance = ["manual", "generated"] and
42+
msg = "Unrecognized provenance description \"" + provenance + "\" in " + pred + " model."
43+
)
44+
or
45+
exists(string pred, AccessPath input, string part |
46+
sinkModel(_, _, _, _, _, _, input, _, _) and pred = "sink"
47+
or
48+
summaryModel(_, _, _, _, _, _, input, _, _, _) and pred = "summary"
49+
|
50+
(
51+
invalidSpecComponent(input, part) and
52+
not part = "" and
53+
not (part = "Argument" and pred = "sink") and
54+
not parseArg(part, _)
55+
or
56+
part = input.getToken(_) and
57+
parseParam(part, _)
58+
) and
59+
msg = "Unrecognized input specification \"" + part + "\" in " + pred + " model."
60+
)
61+
or
62+
exists(string pred, string output, string part |
63+
sourceModel(_, _, _, _, _, _, output, _, _) and pred = "source"
64+
or
65+
summaryModel(_, _, _, _, _, _, _, output, _, _) and pred = "summary"
66+
|
67+
invalidSpecComponent(output, part) and
68+
not part = "" and
69+
not (part = ["Argument", "Parameter"] and pred = "source") and
70+
msg = "Unrecognized output specification \"" + part + "\" in " + pred + " model."
71+
)
72+
or
73+
exists(string pred, string row, int expect |
74+
sourceModel(row) and expect = 9 and pred = "source"
75+
or
76+
sinkModel(row) and expect = 9 and pred = "sink"
77+
or
78+
summaryModel(row) and expect = 10 and pred = "summary"
79+
|
80+
exists(int cols |
81+
cols = 1 + max(int n | exists(row.splitAt(";", n))) and
82+
cols != expect and
83+
msg =
84+
"Wrong number of columns in " + pred + " model row, expected " + expect + ", got " + cols +
85+
" in " + row + "."
86+
)
87+
or
88+
exists(string b |
89+
b = row.splitAt(";", 2) and
90+
not b = ["true", "false"] and
91+
msg = "Invalid boolean \"" + b + "\" in " + pred + " model."
92+
)
93+
)
94+
or
95+
exists(string row, string kind | summaryModel(row) |
96+
kind = row.splitAt(";", 8) and
97+
not kind = ["taint", "value"] and
98+
msg = "Invalid kind \"" + kind + "\" in summary model."
99+
)
100+
or
101+
exists(string row, string kind | sinkModel(row) |
102+
kind = row.splitAt(";", 7) and
103+
not kind = ["code", "sql", "xss", "remote", "html"] and
104+
msg = "Invalid kind \"" + kind + "\" in sink model."
105+
)
106+
or
107+
exists(string row, string kind | sourceModel(row) |
108+
kind = row.splitAt(";", 7) and
109+
not kind = "local" and
110+
msg = "Invalid kind \"" + kind + "\" in source model."
111+
)
112+
}

csharp/ql/lib/semmle/code/csharp/dataflow/ExternalFlow.qll

Lines changed: 8 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -174,13 +174,17 @@ class NegativeSummaryModelCsv extends Unit {
174174
abstract predicate row(string row);
175175
}
176176

177-
private predicate sourceModel(string row) { any(SourceModelCsv s).row(row) }
177+
/** Holds if `row` is a source model. */
178+
predicate sourceModel(string row) { any(SourceModelCsv s).row(row) }
178179

179-
private predicate sinkModel(string row) { any(SinkModelCsv s).row(row) }
180+
/** Holds if `row` is a sink model. */
181+
predicate sinkModel(string row) { any(SinkModelCsv s).row(row) }
180182

181-
private predicate summaryModel(string row) { any(SummaryModelCsv s).row(row) }
183+
/** Holds if `row` is a summary model. */
184+
predicate summaryModel(string row) { any(SummaryModelCsv s).row(row) }
182185

183-
private predicate negativeSummaryModel(string row) { any(NegativeSummaryModelCsv s).row(row) }
186+
/** Holds if `row` is a negative summary model. */
187+
predicate negativeSummaryModel(string row) { any(NegativeSummaryModelCsv s).row(row) }
184188

185189
/** Holds if a source model exists for the given parameters. */
186190
predicate sourceModel(
@@ -311,115 +315,6 @@ predicate modelCoverage(string namespace, int namespaces, string kind, string pa
311315
)
312316
}
313317

314-
/** Provides a query predicate to check the CSV data for validation errors. */
315-
module CsvValidation {
316-
/** Holds if some row in a CSV-based flow model appears to contain typos. */
317-
query predicate invalidModelRow(string msg) {
318-
exists(
319-
string pred, string namespace, string type, string name, string signature, string ext,
320-
string provenance
321-
|
322-
sourceModel(namespace, type, _, name, signature, ext, _, _, provenance) and pred = "source"
323-
or
324-
sinkModel(namespace, type, _, name, signature, ext, _, _, provenance) and pred = "sink"
325-
or
326-
summaryModel(namespace, type, _, name, signature, ext, _, _, _, provenance) and
327-
pred = "summary"
328-
or
329-
negativeSummaryModel(namespace, type, name, signature, provenance) and
330-
ext = "" and
331-
pred = "nonesummary"
332-
|
333-
not namespace.regexpMatch("[a-zA-Z0-9_\\.]+") and
334-
msg = "Dubious namespace \"" + namespace + "\" in " + pred + " model."
335-
or
336-
not type.regexpMatch("[a-zA-Z0-9_<>,\\+]+") and
337-
msg = "Dubious type \"" + type + "\" in " + pred + " model."
338-
or
339-
not name.regexpMatch("[a-zA-Z0-9_<>,]*") and
340-
msg = "Dubious member name \"" + name + "\" in " + pred + " model."
341-
or
342-
not signature.regexpMatch("|\\([a-zA-Z0-9_<>\\.\\+\\*,\\[\\]]*\\)") and
343-
msg = "Dubious signature \"" + signature + "\" in " + pred + " model."
344-
or
345-
not ext.regexpMatch("|Attribute") and
346-
msg = "Unrecognized extra API graph element \"" + ext + "\" in " + pred + " model."
347-
or
348-
not provenance = ["manual", "generated"] and
349-
msg = "Unrecognized provenance description \"" + provenance + "\" in " + pred + " model."
350-
)
351-
or
352-
exists(string pred, AccessPath input, string part |
353-
sinkModel(_, _, _, _, _, _, input, _, _) and pred = "sink"
354-
or
355-
summaryModel(_, _, _, _, _, _, input, _, _, _) and pred = "summary"
356-
|
357-
(
358-
invalidSpecComponent(input, part) and
359-
not part = "" and
360-
not (part = "Argument" and pred = "sink") and
361-
not parseArg(part, _)
362-
or
363-
part = input.getToken(_) and
364-
parseParam(part, _)
365-
) and
366-
msg = "Unrecognized input specification \"" + part + "\" in " + pred + " model."
367-
)
368-
or
369-
exists(string pred, string output, string part |
370-
sourceModel(_, _, _, _, _, _, output, _, _) and pred = "source"
371-
or
372-
summaryModel(_, _, _, _, _, _, _, output, _, _) and pred = "summary"
373-
|
374-
invalidSpecComponent(output, part) and
375-
not part = "" and
376-
not (part = ["Argument", "Parameter"] and pred = "source") and
377-
msg = "Unrecognized output specification \"" + part + "\" in " + pred + " model."
378-
)
379-
or
380-
exists(string pred, string row, int expect |
381-
sourceModel(row) and expect = 9 and pred = "source"
382-
or
383-
sinkModel(row) and expect = 9 and pred = "sink"
384-
or
385-
summaryModel(row) and expect = 10 and pred = "summary"
386-
|
387-
exists(int cols |
388-
cols = 1 + max(int n | exists(row.splitAt(";", n))) and
389-
cols != expect and
390-
msg =
391-
"Wrong number of columns in " + pred + " model row, expected " + expect + ", got " + cols +
392-
" in " + row + "."
393-
)
394-
or
395-
exists(string b |
396-
b = row.splitAt(";", 2) and
397-
not b = ["true", "false"] and
398-
msg = "Invalid boolean \"" + b + "\" in " + pred + " model."
399-
)
400-
)
401-
or
402-
exists(string row, string kind | summaryModel(row) |
403-
kind = row.splitAt(";", 8) and
404-
not kind = ["taint", "value"] and
405-
msg = "Invalid kind \"" + kind + "\" in summary model."
406-
)
407-
or
408-
exists(string row, string kind | sinkModel(row) |
409-
kind = row.splitAt(";", 7) and
410-
not kind = ["code", "sql", "xss", "remote", "html"] and
411-
not kind.matches("encryption-%") and
412-
msg = "Invalid kind \"" + kind + "\" in sink model."
413-
)
414-
or
415-
exists(string row, string kind | sourceModel(row) |
416-
kind = row.splitAt(";", 7) and
417-
not kind = ["local", "file"] and
418-
msg = "Invalid kind \"" + kind + "\" in source model."
419-
)
420-
}
421-
}
422-
423318
private predicate elementSpec(
424319
string namespace, string type, boolean subtypes, string name, string signature, string ext
425320
) {

csharp/ql/test/library-tests/dataflow/external-models/ExternalFlow.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
invalidModelRow
12
edges
23
| ExternalFlow.cs:9:27:9:38 | object creation of type Object : Object | ExternalFlow.cs:10:29:10:32 | access to local variable arg1 : Object |
34
| ExternalFlow.cs:10:29:10:32 | access to local variable arg1 : Object | ExternalFlow.cs:10:18:10:33 | call to method StepArgRes |
@@ -151,7 +152,6 @@ nodes
151152
| ExternalFlow.cs:196:18:196:40 | call to method MixedFlowArgs | semmle.label | call to method MixedFlowArgs |
152153
| ExternalFlow.cs:196:38:196:39 | access to local variable o2 : Object | semmle.label | access to local variable o2 : Object |
153154
subpaths
154-
invalidModelRow
155155
#select
156156
| ExternalFlow.cs:10:18:10:33 | call to method StepArgRes | ExternalFlow.cs:9:27:9:38 | object creation of type Object : Object | ExternalFlow.cs:10:18:10:33 | call to method StepArgRes | $@ | ExternalFlow.cs:9:27:9:38 | object creation of type Object : Object | object creation of type Object : Object |
157157
| ExternalFlow.cs:18:18:18:24 | access to local variable argOut1 | ExternalFlow.cs:15:29:15:40 | object creation of type Object : Object | ExternalFlow.cs:18:18:18:24 | access to local variable argOut1 | $@ | ExternalFlow.cs:15:29:15:40 | object creation of type Object : Object | object creation of type Object : Object |

csharp/ql/test/library-tests/dataflow/external-models/ExternalFlow.ql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
*/
44

55
import csharp
6-
import semmle.code.csharp.dataflow.ExternalFlow
76
import DataFlow::PathGraph
8-
import CsvValidation
7+
import semmle.code.csharp.dataflow.CsvValidation
8+
import semmle.code.csharp.dataflow.ExternalFlow
99

1010
class SummaryModelTest extends SummaryModelCsv {
1111
override predicate row(string row) {

csharp/ql/test/library-tests/dataflow/external-models/sinks.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import csharp
22
import DataFlow
3+
import semmle.code.csharp.dataflow.CsvValidation
34
import semmle.code.csharp.dataflow.ExternalFlow
45
import semmle.code.csharp.dataflow.internal.FlowSummaryImpl as FlowSummaryImpl
5-
import CsvValidation
66

77
class SinkModelTest extends SinkModelCsv {
88
override predicate row(string row) {

csharp/ql/test/library-tests/dataflow/external-models/srcs.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import csharp
22
import DataFlow
3+
import semmle.code.csharp.dataflow.CsvValidation
34
import semmle.code.csharp.dataflow.ExternalFlow
45
import semmle.code.csharp.dataflow.internal.FlowSummaryImpl as FlowSummaryImpl
5-
import CsvValidation
66

77
class SourceModelTest extends SourceModelCsv {
88
override predicate row(string row) {

csharp/ql/test/library-tests/dataflow/external-models/steps.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import csharp
22
import DataFlow
3+
import semmle.code.csharp.dataflow.CsvValidation
34
import semmle.code.csharp.dataflow.ExternalFlow
45
import semmle.code.csharp.dataflow.FlowSummary
56
import semmle.code.csharp.dataflow.internal.DataFlowDispatch as DataFlowDispatch
67
import semmle.code.csharp.dataflow.internal.FlowSummaryImpl as FlowSummaryImpl
7-
import CsvValidation
88

99
private class SummaryModelTest extends SummaryModelCsv {
1010
override predicate row(string row) {

0 commit comments

Comments
 (0)