-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Ruby: Make array inclusion barrier more sensitive #11534
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,19 +13,18 @@ cached | |
private predicate stringConstCompare(CfgNodes::AstCfgNode guard, CfgNode testedNode, boolean branch) { | ||
exists(CfgNodes::ExprNodes::ComparisonOperationCfgNode c | | ||
c = guard and | ||
exists(CfgNodes::ExprNodes::StringLiteralCfgNode strLitNode | | ||
// Only consider strings without any interpolations | ||
not strLitNode.getExpr().getComponent(_) instanceof StringInterpolationComponent and | ||
exists(ExprCfgNode strNode | | ||
strNode.getConstantValue().isStringlikeValue(_) and | ||
c.getExpr() instanceof EqExpr and | ||
branch = true | ||
or | ||
c.getExpr() instanceof CaseEqExpr and branch = true | ||
or | ||
c.getExpr() instanceof NEExpr and branch = false | ||
| | ||
c.getLeftOperand() = strLitNode and c.getRightOperand() = testedNode | ||
c.getLeftOperand() = strNode and c.getRightOperand() = testedNode | ||
or | ||
c.getLeftOperand() = testedNode and c.getRightOperand() = strLitNode | ||
c.getLeftOperand() = testedNode and c.getRightOperand() = strNode | ||
) | ||
) | ||
or | ||
|
@@ -116,7 +115,7 @@ private predicate stringConstArrayInclusionCall( | |
isArrayConstant(t.getContainerNode().asExpr(), arr) | ||
| | ||
forall(ExprCfgNode elem | elem = arr.getAnArgument() | | ||
elem instanceof ExprNodes::StringLiteralCfgNode | ||
elem.getConstantValue().isStringlikeValue(_) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider replacing all occurrences of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems to make sense, I think it would allow us to cover guards like: guardvar = "safe"
case input
when guardvar
# input is validated
else
# input is not validated
end |
||
) | ||
) | ||
) | ||
|
@@ -193,39 +192,45 @@ private predicate stringConstCaseCompare( | |
guard = | ||
any(CfgNodes::ExprNodes::WhenClauseCfgNode branchNode | | ||
branchNode = case.getBranch(_) and | ||
// For simplicity, consider patterns that contain only string literals or arrays of string literals | ||
// For simplicity, consider patterns that contain only string literals, string-valued variables, or arrays of the same. | ||
forall(ExprCfgNode pattern | pattern = branchNode.getPattern(_) | | ||
// foo = "foo" | ||
// | ||
// when foo | ||
// when foo, bar | ||
// when "foo" | ||
// when "foo", "bar" | ||
pattern instanceof ExprNodes::StringLiteralCfgNode | ||
pattern.getConstantValue().isStringlikeValue(_) | ||
or | ||
pattern = | ||
any(CfgNodes::ExprNodes::SplatExprCfgNode splat | | ||
// when *["foo", "bar"] | ||
forex(ExprCfgNode elem | | ||
elem = splat.getOperand().(ExprNodes::ArrayLiteralCfgNode).getAnArgument() | ||
| | ||
elem instanceof ExprNodes::StringLiteralCfgNode | ||
elem.getConstantValue().isStringlikeValue(_) | ||
) | ||
or | ||
// when *some_var | ||
// when *SOME_CONST | ||
exists(ExprNodes::ArrayLiteralCfgNode arr | | ||
isArrayConstant(splat.getOperand(), arr) and | ||
forall(ExprCfgNode elem | elem = arr.getAnArgument() | | ||
elem instanceof ExprNodes::StringLiteralCfgNode | ||
elem.getConstantValue().isStringlikeValue(_) | ||
) | ||
) | ||
) | ||
) | ||
) | ||
or | ||
// foo = "foo" | ||
// | ||
// in foo | ||
// in "foo" | ||
exists( | ||
CfgNodes::ExprNodes::InClauseCfgNode branchNode, ExprNodes::StringLiteralCfgNode pattern | ||
| | ||
exists(CfgNodes::ExprNodes::InClauseCfgNode branchNode, ExprCfgNode pattern | | ||
branchNode = case.getBranch(_) and | ||
pattern = branchNode.getPattern() and | ||
pattern.getConstantValue().isStringlikeValue(_) and | ||
guard = pattern | ||
) | ||
) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
strnode is not restricted in this disjunct and the one below
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you! I missed this comment until recently. A fix for this is here: #12052