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

Skip to content

Commit 9b16192

Browse files
committed
Swift: Re-factor CsvValidation into a separate file.
1 parent 54e85ff commit 9b16192

2 files changed

Lines changed: 120 additions & 88 deletions

File tree

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

swift/ql/lib/codeql/swift/dataflow/ExternalFlow.qll

Lines changed: 6 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,14 @@ class SummaryModelCsv extends Unit {
111111
abstract predicate row(string row);
112112
}
113113

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

116-
private predicate sinkModel(string row) { any(SinkModelCsv s).row(row) }
117+
/** Holds if `row` is a sink model. */
118+
predicate sinkModel(string row) { any(SinkModelCsv s).row(row) }
117119

118-
private predicate summaryModel(string row) { any(SummaryModelCsv s).row(row) }
120+
/** Holds if `row` is a summary model. */
121+
predicate summaryModel(string row) { any(SummaryModelCsv s).row(row) }
119122

120123
/** Holds if a source model exists for the given parameters. */
121124
predicate sourceModel(
@@ -232,91 +235,6 @@ predicate modelCoverage(string namespace, int namespaces, string kind, string pa
232235
)
233236
}
234237

235-
/** Provides a query predicate to check the CSV data for validation errors. */
236-
module CsvValidation {
237-
/** Holds if some row in a CSV-based flow model appears to contain typos. */
238-
query predicate invalidModelRow(string msg) {
239-
exists(string pred, string namespace, string type, string name, string signature, string ext |
240-
sourceModel(namespace, type, _, name, signature, ext, _, _, _) and pred = "source"
241-
or
242-
sinkModel(namespace, type, _, name, signature, ext, _, _, _) and pred = "sink"
243-
or
244-
summaryModel(namespace, type, _, name, signature, ext, _, _, _, _) and pred = "summary"
245-
|
246-
not namespace.regexpMatch("[a-zA-Z0-9_\\.]+") and
247-
msg = "Dubious namespace \"" + namespace + "\" in " + pred + " model."
248-
or
249-
not type.regexpMatch("[a-zA-Z0-9_<>,\\+]+") and
250-
msg = "Dubious type \"" + type + "\" in " + pred + " model."
251-
or
252-
not name.regexpMatch("[a-zA-Z0-9_<>,]*") and
253-
msg = "Dubious member name \"" + name + "\" in " + pred + " model."
254-
or
255-
not signature.regexpMatch("|\\([a-zA-Z0-9_<>\\.\\+\\*,\\[\\]]*\\)") and
256-
msg = "Dubious signature \"" + signature + "\" in " + pred + " model."
257-
or
258-
not ext.regexpMatch("|Attribute") and
259-
msg = "Unrecognized extra API graph element \"" + ext + "\" in " + pred + " model."
260-
)
261-
or
262-
exists(string pred, AccessPath input, string part |
263-
sinkModel(_, _, _, _, _, _, input, _, _) and pred = "sink"
264-
or
265-
summaryModel(_, _, _, _, _, _, input, _, _, _) and pred = "summary"
266-
|
267-
(
268-
invalidSpecComponent(input, part) and
269-
not part = "" and
270-
not (part = "Argument" and pred = "sink") and
271-
not parseArg(part, _)
272-
or
273-
part = input.getToken(_) and
274-
parseParam(part, _)
275-
) and
276-
msg = "Unrecognized input specification \"" + part + "\" in " + pred + " model."
277-
)
278-
or
279-
exists(string pred, string output, string part |
280-
sourceModel(_, _, _, _, _, _, output, _, _) and pred = "source"
281-
or
282-
summaryModel(_, _, _, _, _, _, _, output, _, _) and pred = "summary"
283-
|
284-
invalidSpecComponent(output, part) and
285-
not part = "" and
286-
not (part = ["Argument", "Parameter"] and pred = "source") and
287-
msg = "Unrecognized output specification \"" + part + "\" in " + pred + " model."
288-
)
289-
or
290-
exists(string pred, string row, int expect |
291-
sourceModel(row) and expect = 8 and pred = "source"
292-
or
293-
sinkModel(row) and expect = 8 and pred = "sink"
294-
or
295-
summaryModel(row) and expect = 9 and pred = "summary"
296-
|
297-
exists(int cols |
298-
cols = 1 + max(int n | exists(row.splitAt(";", n))) and
299-
cols != expect and
300-
msg =
301-
"Wrong number of columns in " + pred + " model row, expected " + expect + ", got " + cols +
302-
"."
303-
)
304-
or
305-
exists(string b |
306-
b = row.splitAt(";", 2) and
307-
not b = ["true", "false"] and
308-
msg = "Invalid boolean \"" + b + "\" in " + pred + " model."
309-
)
310-
)
311-
or
312-
exists(string row, string kind | summaryModel(row) |
313-
kind = row.splitAt(";", 8) and
314-
not kind = ["taint", "value"] and
315-
msg = "Invalid kind \"" + kind + "\" in summary model."
316-
)
317-
}
318-
}
319-
320238
private predicate elementSpec(
321239
string namespace, string type, boolean subtypes, string name, string signature, string ext
322240
) {

0 commit comments

Comments
 (0)