mirror of
https://github.com/github/codeql.git
synced 2026-05-01 03:35:13 +02:00
JS: Extract RegExp ASTs from string literals
This commit is contained in:
@@ -59,6 +59,11 @@ public class Literal extends Expression implements ITypeExpression {
|
||||
return tokenType == TokenType.regexp;
|
||||
}
|
||||
|
||||
/** Is this a string literal? */
|
||||
public boolean isStringLiteral() {
|
||||
return tokenType == TokenType.string;
|
||||
}
|
||||
|
||||
/** The value of this literal expressed as a string. */
|
||||
public String getStringValue() {
|
||||
// regular expressions may have a null value; use the raw value instead
|
||||
|
||||
@@ -516,7 +516,11 @@ public class ASTExtractor {
|
||||
String valueString = nd.getStringValue();
|
||||
|
||||
trapwriter.addTuple("literals", valueString, source, key);
|
||||
if (nd.isRegExp()) regexpExtractor.extract(source.substring(1, source.lastIndexOf('/')), nd);
|
||||
if (nd.isRegExp()) {
|
||||
regexpExtractor.extract(source.substring(1, source.lastIndexOf('/')), nd, false);
|
||||
} else if (nd.isStringLiteral()) {
|
||||
regexpExtractor.extract(valueString, nd, true);
|
||||
}
|
||||
return key;
|
||||
}
|
||||
|
||||
|
||||
@@ -341,9 +341,14 @@ public class RegExpExtractor {
|
||||
}
|
||||
}
|
||||
|
||||
public void extract(String src, Node parent) {
|
||||
this.literalStart = parent.getLoc().getStart();
|
||||
public void extract(String src, Node parent, boolean isSpeculativeParsing) {
|
||||
Result res = parser.parse(src);
|
||||
|
||||
if (isSpeculativeParsing && res.getErrors().size() > 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.literalStart = parent.getLoc().getStart();
|
||||
RegExpTerm ast = res.getAST();
|
||||
new V().visit(ast, trapwriter.localID(parent), 0);
|
||||
|
||||
|
||||
@@ -815,7 +815,7 @@ regexpterm (unique int id: @regexpterm,
|
||||
int idx: int ref,
|
||||
varchar(900) tostring: string ref);
|
||||
|
||||
@regexpparent = @regexpterm | @regexpliteral;
|
||||
@regexpparent = @regexpterm | @regexpliteral | @stringliteral;
|
||||
|
||||
case @regexpterm.kind of
|
||||
0 = @regexp_alt
|
||||
|
||||
Reference in New Issue
Block a user