Skip to content

Commit d679b63

Browse files
committed
Ruby: Ignore results inside here-docs in inline expectations
1 parent 101dc04 commit d679b63

3 files changed

Lines changed: 40 additions & 9 deletions

File tree

ruby/ql/lib/utils/test/InlineExpectationsTestQuery.ql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,12 @@ private module Input implements T::TestPostProcessing::InputSig<Impl> {
1818
f.getRelativePath() + ":" + startline + ":" + startcolumn + ":" + endline + ":" + endcolumn
1919
)
2020
}
21+
22+
predicate ignoreResult(string filePath, int startLine, int startColumn, int endLine, int endColumn) {
23+
exists(Ast::HereDoc doc |
24+
doc.getAChild*()
25+
.getLocation()
26+
.hasLocationInfo(filePath, startLine, startColumn, endLine, endColumn)
27+
)
28+
}
2129
}

ruby/ql/test/query-tests/security/cwe-094/UnsafeCodeConstruction/UnsafeCodeConstruction.expected

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,3 @@ nodes
7373
| impl/unsafeCode.rb:63:30:63:30 | y | semmle.label | y |
7474
| impl/unsafeCode.rb:64:10:64:13 | arr2 | semmle.label | arr2 |
7575
subpaths
76-
testFailures
77-
| impl/unsafeCode.rb:49:9:49:12 | #{...} | Unexpected result: Alert |

shared/util/codeql/util/test/InlineExpectationsTest.qll

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,19 @@ module TestPostProcessing {
635635

636636
signature module InputSig<InlineExpectationsTestSig Input> {
637637
string getRelativeUrl(Input::Location location);
638+
639+
/**
640+
* Holds if results at the given location should be ignored.
641+
*
642+
* This is useful when it is impossible to insert expectation comments, for example
643+
* inside Ruby here-docs.
644+
*/
645+
bindingset[filePath, startLine, startColumn, endLine, endColumn]
646+
default predicate ignoreResult(
647+
string filePath, int startLine, int startColumn, int endLine, int endColumn
648+
) {
649+
none()
650+
}
638651
}
639652

640653
module Make<InlineExpectationsTestSig Input, InputSig<Input> Input2> {
@@ -744,7 +757,11 @@ module TestPostProcessing {
744757

745758
pragma[nomagic]
746759
private predicate mainQueryResult(int row, int column, TestLocation loc) {
747-
queryResults(mainResultSet(), row, column, loc.getRelativeUrl())
760+
queryResults(mainResultSet(), row, column, loc.getRelativeUrl()) and
761+
not exists(string filePath, int startLine, int startColumn, int endLine, int endColumn |
762+
loc.hasLocationInfo(filePath, startLine, startColumn, endLine, endColumn) and
763+
Input2::ignoreResult(filePath, startLine, startColumn, endLine, endColumn)
764+
)
748765
}
749766

750767
/**
@@ -755,10 +772,14 @@ module TestPostProcessing {
755772
*/
756773
private string getSourceTag(int row) {
757774
getQueryKind() = "path-problem" and
758-
exists(TestLocation sourceLoc, TestLocation selectLoc |
759-
mainQueryResult(row, 0, selectLoc) and
775+
exists(TestLocation sourceLoc |
760776
mainQueryResult(row, 2, sourceLoc) and
761-
if sameLineInfo(selectLoc, sourceLoc) then result = "Alert" else result = "Source"
777+
if
778+
exists(TestLocation selectLoc |
779+
mainQueryResult(row, 0, selectLoc) and sameLineInfo(selectLoc, sourceLoc)
780+
)
781+
then result = "Alert"
782+
else result = "Source"
762783
)
763784
}
764785

@@ -770,10 +791,14 @@ module TestPostProcessing {
770791
*/
771792
private string getSinkTag(int row) {
772793
getQueryKind() = "path-problem" and
773-
exists(TestLocation sinkLoc, TestLocation selectLoc |
774-
mainQueryResult(row, 0, selectLoc) and
794+
exists(TestLocation sinkLoc |
775795
mainQueryResult(row, 4, sinkLoc) and
776-
if sameLineInfo(selectLoc, sinkLoc) then result = "Alert" else result = "Sink"
796+
if
797+
exists(TestLocation selectLoc |
798+
mainQueryResult(row, 0, selectLoc) and sameLineInfo(selectLoc, sinkLoc)
799+
)
800+
then result = "Alert"
801+
else result = "Sink"
777802
)
778803
}
779804

0 commit comments

Comments
 (0)