Skip to content

Commit 65249da

Browse files
committed
Ruby: add warning for wrong number of columns in CSV row
1 parent f28acbf commit 65249da

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

ruby/ql/lib/codeql/ruby/frameworks/data/internal/ApiGraphModels.qll

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,4 +420,26 @@ module ModelOutput {
420420
result = getNodeFromPath(package2, type2, path)
421421
)
422422
}
423+
424+
/**
425+
* Gets an error message relating to an invalid CSV row in a model.
426+
*/
427+
string getAWarning() {
428+
// Check number of columns
429+
exists(string row, string kind, int expectedArity, int actualArity |
430+
any(SourceModelCsv csv).row(row) and kind = "source" and expectedArity = 4
431+
or
432+
any(SinkModelCsv csv).row(row) and kind = "sink" and expectedArity = 4
433+
or
434+
any(SummaryModelCsv csv).row(row) and kind = "summary" and expectedArity = 6
435+
or
436+
any(TypeModelCsv csv).row(row) and kind = "type" and expectedArity = 5
437+
|
438+
actualArity = count(row.indexOf(";")) + 1 and
439+
actualArity != expectedArity and
440+
result =
441+
"CSV " + kind + " row should have " + expectedArity + " columns but has " + actualArity +
442+
": " + row
443+
)
444+
}
423445
}

ruby/ql/test/library-tests/dataflow/summaries/Summaries.expected

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,6 @@ invalidOutputSpecComponent
8181
| summaries.rb:37:36:37:42 | tainted | summaries.rb:1:20:1:26 | "taint" : | summaries.rb:37:36:37:42 | tainted | $@ | summaries.rb:1:20:1:26 | "taint" : | "taint" : |
8282
| summaries.rb:41:8:41:25 | call to matchedByName | summaries.rb:40:7:40:13 | "taint" : | summaries.rb:41:8:41:25 | call to matchedByName | $@ | summaries.rb:40:7:40:13 | "taint" : | "taint" : |
8383
| summaries.rb:42:8:42:25 | call to matchedByName | summaries.rb:40:7:40:13 | "taint" : | summaries.rb:42:8:42:25 | call to matchedByName | $@ | summaries.rb:40:7:40:13 | "taint" : | "taint" : |
84+
warning
85+
| CSV type row should have 5 columns but has 2: test;TooFewColumns |
86+
| CSV type row should have 5 columns but has 8: test;TooManyColumns;;;Member[Foo].Instance;too;many;columns |

ruby/ql/test/library-tests/dataflow/summaries/Summaries.ql

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ query predicate invalidSpecComponent(SummarizedCallable sc, string s, string c)
1515
Private::External::invalidSpecComponent(s, c)
1616
}
1717

18+
query predicate warning = ModelOutput::getAWarning/0;
19+
1820
query predicate invalidOutputSpecComponent(SummarizedCallable sc, AccessPath s, AccessPathToken c) {
1921
sc.propagatesFlowExt(_, s, _) and
2022
c = s.getToken(_) and
@@ -89,6 +91,16 @@ private class TypeFromModel extends ModelInput::TypeModelCsv {
8991
}
9092
}
9193

94+
private class InvalidTypeModel extends ModelInput::TypeModelCsv {
95+
override predicate row(string row) {
96+
row =
97+
[
98+
"test;TooManyColumns;;;Member[Foo].Instance;too;many;columns", //
99+
"test;TooFewColumns", //
100+
]
101+
}
102+
}
103+
92104
private class SinkFromModel extends ModelInput::SinkModelCsv {
93105
override predicate row(string row) { row = "test;FooOrBar;Method[method].Argument[0];test-sink" }
94106
}

0 commit comments

Comments
 (0)