Merge pull request #11719 from aibaars/alert-suppression-shared

Shared AlertSuppression library
This commit is contained in:
Arthur Baars
2022-12-19 16:04:44 +01:00
committed by GitHub
23 changed files with 290 additions and 541 deletions

View File

@@ -5,76 +5,23 @@
* @id rb/alert-suppression
*/
import codeql.ruby.AST
import codeql.ruby.ast.internal.TreeSitter
private import codeql.suppression.AlertSuppression as AS
private import codeql.ruby.ast.internal.TreeSitter
/**
* An alert suppression comment.
*/
class SuppressionComment extends Ruby::Comment {
string annotation;
SuppressionComment() {
class SingleLineComment extends Ruby::Comment {
SingleLineComment() {
// suppression comments must be single-line
this.getLocation().getStartLine() = this.getLocation().getEndLine() and
exists(string text | text = commentText(this) |
// match `lgtm[...]` anywhere in the comment
annotation = text.regexpFind("(?i)\\blgtm\\s*\\[[^\\]]*\\]", _, _)
or
// match `lgtm` at the start of the comment and after semicolon
annotation = text.regexpFind("(?i)(?<=^|;)\\s*lgtm(?!\\B|\\s*\\[)", _, _).trim()
)
this.getLocation().getStartLine() = this.getLocation().getEndLine()
}
/**
* Gets the text of this suppression comment.
*/
string getText() { result = commentText(this) }
/** Gets the suppression annotation in this comment. */
string getAnnotation() { result = annotation }
/**
* Holds if this comment applies to the range from column `startcolumn` of line `startline`
* to column `endcolumn` of line `endline` in file `filepath`.
*/
predicate covers(string filepath, int startline, int startcolumn, int endline, int endcolumn) {
this.getLocation().hasLocationInfo(filepath, startline, _, endline, endcolumn) and
startcolumn = 1
}
/** Gets the scope of this suppression. */
SuppressionScope getScope() { this = result.getSuppressionComment() }
}
private string commentText(Ruby::Comment comment) { result = comment.getValue().suffix(1) }
/**
* The scope of an alert suppression comment.
*/
class SuppressionScope extends @ruby_token_comment instanceof SuppressionComment {
/** Gets a suppression comment with this scope. */
SuppressionComment getSuppressionComment() { result = this }
/**
* Holds if this element is at the specified location.
* The location spans column `startcolumn` of line `startline` to
* column `endcolumn` of line `endline` in file `filepath`.
* For more information, see
* [Locations](https://codeql.github.com/docs/writing-codeql-queries/providing-locations-in-codeql-queries/).
*/
predicate hasLocationInfo(
string filepath, int startline, int startcolumn, int endline, int endcolumn
) {
super.covers(filepath, startline, startcolumn, endline, endcolumn)
this.getLocation().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
}
/** Gets a textual representation of this element. */
string toString() { result = "suppression range" }
/** Gets the suppression annotation in this comment. */
string getText() { result = this.getValue().suffix(1) }
}
from SuppressionComment c
select c, // suppression comment
c.getText(), // text of suppression comment (excluding delimiters)
c.getAnnotation(), // text of suppression annotation
c.getScope() // scope of suppression
import AS::Make<SingleLineComment>

View File

@@ -8,3 +8,4 @@ defaultSuiteFile: codeql-suites/ruby-code-scanning.qls
dependencies:
codeql/ruby-all: ${workspace}
codeql/suite-helpers: ${workspace}
codeql/util: ${workspace}

View File

@@ -1 +1 @@
TestWindows.java eol=crlf
TestWindows.rb eol=crlf