Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bicep CFG / DF #141

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion ql/lib/bicep.qll
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import codeql.Locations
import codeql.files.FileSystem
import codeql.bicep.AST
import codeql.bicep.Ast
// Resources
import codeql.bicep.microsoft.Compute
import codeql.bicep.microsoft.Storage
Expand Down
12 changes: 10 additions & 2 deletions ql/lib/codeql-pack.lock.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
---
lockVersion: 1.0.0
dependencies:
codeql/controlflow:
version: 0.1.0
codeql/dataflow:
version: 0.1.0
codeql/ssa:
version: 0.2.0
codeql/typetracking:
version: 0.2.0
codeql/util:
version: 0.1.2
version: 0.2.0
codeql/yaml:
version: 0.1.3
version: 0.1.5
compiled: false
6 changes: 5 additions & 1 deletion ql/lib/codeql/bicep/AST.qll
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import codeql.bicep.ast.AstNodes
import codeql.bicep.ast.Ast
import codeql.bicep.ast.Expr
import codeql.bicep.ast.Calls
import codeql.bicep.ast.Loops
import codeql.bicep.ast.Object
import codeql.bicep.ast.Literal
import codeql.bicep.ast.Variables
import codeql.bicep.ast.Resources
8 changes: 8 additions & 0 deletions ql/lib/codeql/bicep/Ast.qll
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import codeql.bicep.ast.Ast
import codeql.bicep.ast.Expr
import codeql.bicep.ast.Calls
import codeql.bicep.ast.Loops
import codeql.bicep.ast.Object
import codeql.bicep.ast.Literal
import codeql.bicep.ast.Variables
import codeql.bicep.ast.Resources
4 changes: 4 additions & 0 deletions ql/lib/codeql/bicep/Cfg.qll
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
private import codeql.bicep.controlflow.internal.Cfg as CfgInternal
import CfgInternal::Completion
import CfgInternal::CfgScope
import CfgInternal::CfgImpl
14 changes: 14 additions & 0 deletions ql/lib/codeql/bicep/ast/Ast.qll
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
private import codeql.bicep.ast.internal.AstNodes
private import codeql.Locations

final class AstNode instanceof BicepAstNode {
AstNode getAChild(string name) { result = super.getAChild(name) }

AstNode getParent() { result.getAChild(_) = this }

string toString() { result = super.toString() }

string getAPrimaryQlClass() { result = super.getAPrimaryQlClass() }

Location getLocation() { result = super.getLocation() }
}
17 changes: 17 additions & 0 deletions ql/lib/codeql/bicep/ast/Calls.qll
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
private import codeql.bicep.ast.Ast
private import codeql.bicep.ast.internal.Calls
private import codeql.bicep.ast.Expr

final class CallExpr extends AstNode instanceof CallExprImpl {
Identifier getFunction() { result = super.getFunction() }

Arguments getArguments() { result = super.getArguments() }
}

final class Arguments extends AstNode instanceof ArgumentsImpl {
Expr getArgument(int i) { result = super.getArgument(i) }

Expr getArguments() { result = super.getArguments() }
}

final class LambdaExpr extends AstNode instanceof LambdaExprImpl { }
139 changes: 33 additions & 106 deletions ql/lib/codeql/bicep/ast/Expr.qll
Original file line number Diff line number Diff line change
@@ -1,122 +1,49 @@
private import codeql.iac.ast.internal.Bicep
private import codeql.bicep.ast.AstNodes
private import codeql.bicep.ast.Ast
private import codeql.bicep.ast.internal.Expr

class Expr extends BicepAstNode, TExpr {
override string getAPrimaryQlClass() { result = "Expr" }
}

class Identifier extends Expr, TIdentifier {
private BICEP::Identifier identifier;

override string getAPrimaryQlClass() { result = "Identifier" }

Identifier() { this = TIdentifier(identifier) }

override string toString() { result = this.getName() }

string getName() { result = identifier.getValue() }
}

class Expression extends Expr, TExpression {
private BICEP::Expression expression;

override string getAPrimaryQlClass() { result = "Expression" }

Expression() { this = TExpression(expression) }
}

class AssignmentExpr extends Expr, TAssignmentExpression {
BICEP::AssignmentExpression aexpr;

override string getAPrimaryQlClass() { result = "AssignmentExpr" }

AssignmentExpr() { this = TAssignmentExpression(aexpr) }
}

class BinaryExpr extends Expr, TBinaryExpression {
BICEP::BinaryExpression bexpr;

override string getAPrimaryQlClass() { result = "BinaryExpr" }

BinaryExpr() { this = TBinaryExpression(bexpr) }
}

class CallExpr extends Expr, TCallExpression {
BICEP::CallExpression cexpr;

override string getAPrimaryQlClass() { result = "CallExpr" }

CallExpr() { this = TCallExpression(cexpr) }
}

class LambdaExpr extends Expr, TLambdaExpression {
BICEP::LambdaExpression lexpr;

override string getAPrimaryQlClass() { result = "LambdaExpr" }

LambdaExpr() { this = TLambdaExpression(lexpr) }
}

class MemberExpr extends Expr, TMemberExpression {
BICEP::MemberExpression mexpr;

override string getAPrimaryQlClass() { result = "MemberExpr" }

MemberExpr() { this = TMemberExpression(mexpr) }

Expr getObject() { toBicepTreeSitter(result) = mexpr.getObject() }

PropertyIdentifier getProperty() { toBicepTreeSitter(result) = mexpr.getProperty() }
}
/**
* A Bicep expression.
*/
final class Expr extends AstNode instanceof ExprImpl { }

class ParenthesizedExpr extends Expr, TParenthesizedExpression {
BICEP::ParenthesizedExpression pexpr;

override string getAPrimaryQlClass() { result = "ParenthesizedExpr" }

ParenthesizedExpr() { this = TParenthesizedExpression(pexpr) }
/**
* A Bicep identifier.
*/
final class Identifier extends Expr instanceof IdentifierImpl {
string getName() { result = super.getName() }
}

class ResourceExpr extends Expr, TResourceExpression {
BICEP::ResourceExpression rexpr;

override string getAPrimaryQlClass() { result = "ResourceExpr" }
/**
* A Bicep expression.
*/
final class Expression extends Expr instanceof ExpressionImpl { }

ResourceExpr() { this = TResourceExpression(rexpr) }
}
/**
* A Binary assignment expression.
*/
final class AssignmentExpr extends Expr instanceof AssignmentExprImpl { }

class SubscriptExpr extends Expr, TSubscriptExpression {
BICEP::SubscriptExpression sexpr;
final class BinaryExpr extends Expr instanceof BinaryExprImpl { }

override string getAPrimaryQlClass() { result = "SubscriptExpr" }
final class MemberExpr extends Expr instanceof MemberExprImpl {
Expr getObject() { result = super.getObject() }

SubscriptExpr() { this = TSubscriptExpression(sexpr) }
PropertyIdentifier getProperty() { result = super.getProperty() }
}

class TerenaryExpr extends Expr, TTernaryExpression {
BICEP::TernaryExpression texpr;

override string getAPrimaryQlClass() { result = "TerenaryExpr" }

TerenaryExpr() { this = TTernaryExpression(texpr) }
}

class UnaryExpr extends Expr, TUnaryExpression {
BICEP::UnaryExpression uexpr;

override string getAPrimaryQlClass() { result = "UnaryExpr" }

UnaryExpr() { this = TUnaryExpression(uexpr) }
}
final class ParenthesizedExpr extends Expr instanceof ParenthesizedExprImpl { }

class PropertyIdentifier extends Expr, TPropertyIdentifier {
BICEP::PropertyIdentifier pidentifier;
final class ResourceExpr extends Expr instanceof ResourceExprImpl { }

override string getAPrimaryQlClass() { result = "PropertyIdentifier" }
final class SubscriptExpr extends Expr instanceof SubscriptExprImpl { }

PropertyIdentifier() { this = TPropertyIdentifier(pidentifier) }
final class TerenaryExpr extends Expr instanceof TerenaryExprImpl { }

override string toString() { result = this.getName() }
final class UnaryExpr extends Expr instanceof UnaryExprImpl { }

string getName() { result = pidentifier.getValue() }
/**
* A Bicept Property Identifier.
*/
final class PropertyIdentifier extends Expr instanceof PropertyIdentifierImpl {
string getName() { result = super.getName() }
}
62 changes: 12 additions & 50 deletions ql/lib/codeql/bicep/ast/Literal.qll
Original file line number Diff line number Diff line change
@@ -1,60 +1,22 @@
private import codeql.iac.ast.internal.Bicep
private import codeql.bicep.ast.AstNodes
private import codeql.bicep.ast.Ast
private import codeql.bicep.ast.internal.Literal

class Literal extends BicepAstNode, TLiteral {
override string getAPrimaryQlClass() { result = "Literal" }
final class Literal extends AstNode instanceof LiteralImpl { }

string getValue() { none() }
final class NullLiteral extends Literal instanceof NullLiteralImpl { }

override string toString() { result = this.getValue() }
final class NumberLiteral extends Literal instanceof NumberLiteralImpl {
int getNumber() { result = super.getNumber() }
}

class NumberLiteral extends Literal, TNumber {
private BICEP::Number literal;

override string getAPrimaryQlClass() { result = "NumberLiteral" }

NumberLiteral() { this = TNumber(literal) }
final class BooleanLiteral extends Literal instanceof BooleanLiteralImpl {
boolean getBool() { result = super.getBool() }
}

class NullLiteral extends Literal, TNull {
private BICEP::Null literal;

override string getAPrimaryQlClass() { result = "NullLiteral" }

NullLiteral() { this = TNull(literal) }
}

class BooleanLiteral extends Literal, TBoolean {
private BICEP::Boolean literal;

override string getAPrimaryQlClass() { result = "BooleanLiteral" }

BooleanLiteral() { this = TBoolean(literal) }

boolean getBool() { result.toString() = literal.getValue() }
final class StringLiteral extends Literal instanceof StringLiteralImpl {
string getValue() { result = super.getValue() }
}

class StringLiteral extends Literal, TString {
private BICEP::String literal;

override string getAPrimaryQlClass() { result = "StringLiteral" }

StringLiteral() { this = TString(literal) }

override string getValue() {
exists(StringContent c | toBicepTreeSitter(c) = literal.getAFieldOrChild() |
result = c.getValue()
)
}
}

class StringContent extends Literal, TStringContent {
private BICEP::StringContent literal;

override string getAPrimaryQlClass() { result = "StringContent" }

StringContent() { this = TStringContent(literal) }

override string getValue() { result = literal.getValue() }
final class StringContent extends Literal instanceof StringContentImpl {
string getValue() { result = super.getValue() }
}
24 changes: 24 additions & 0 deletions ql/lib/codeql/bicep/ast/Loops.qll
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
private import codeql.bicep.ast.Ast
private import codeql.bicep.ast.internal.Loops
private import codeql.bicep.ast.Expr

/**
* A Bicep loop statement.
*/
final class ForStatement extends Expr instanceof ForStatementImpl {
Identifier getInitializer() { result = super.getInitializer() }

Expr getCondition() { result = super.getCondition() }

Expr getBody() { result = super.getBody() }
}

/**
* A Bicep loop enumerator.
*/
final class LoopEnumerator extends Expr instanceof LoopEnumeratorImpl { }

/**
* A Bicep loop variable.
*/
final class LoopVariable extends Expr instanceof LoopVariableImpl { }
Loading
Loading