Skip to content

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

Merged
merged 4 commits into from
Jan 9, 2023

Conversation

hmac
Copy link
Contributor

@hmac hmac commented Dec 2, 2022

This change means we recognise barriers where a variable is tested for inclusion in an array which contains variables, rather than just string literals. The only requirement is that the variables have a stringlike ConstantValue.

@github-actions github-actions bot added the Ruby label Dec 2, 2022
@hmac hmac force-pushed the array-inclusion-barrier-guard-constant branch 2 times, most recently from bb20ac9 to 99c06ad Compare December 11, 2022 21:56
@hmac hmac marked this pull request as ready for review December 11, 2022 23:36
@hmac hmac requested a review from a team as a code owner December 11, 2022 23:36
@hmac hmac added the no-change-note-required This PR does not need a change note label Dec 11, 2022
@@ -116,7 +116,7 @@ private predicate stringConstArrayInclusionCall(
isArrayConstant(t.getContainerNode().asExpr(), arr)
|
forall(ExprCfgNode elem | elem = arr.getAnArgument() |
elem instanceof ExprNodes::StringLiteralCfgNode
elem.getConstantValue().isStringlikeValue(_)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider replacing all occurrences of ExprNodes::StringLiteralCfgNode in this file.

Copy link
Contributor

@alexrford alexrford Jan 3, 2023

Choose a reason for hiding this comment

The 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

Copy link
Contributor

@alexrford alexrford left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment, otherwise LGTM

hmac added 4 commits January 4, 2023 11:45
This demonstrates that we are missing a guard when a case branch
compares against a string-valued variable rather than a string literal.
This increases the sensitivity of our barrier guards.
@hmac hmac force-pushed the array-inclusion-barrier-guard-constant branch from 448131e to 4d228bc Compare January 3, 2023 22:45
Copy link
Contributor

@alexrford alexrford left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DCA looks fine to me 👍

@hmac hmac merged commit 5b11708 into github:main Jan 9, 2023
// 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
Copy link
Contributor

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

Copy link
Contributor Author

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

@hmac hmac deleted the array-inclusion-barrier-guard-constant branch April 29, 2023 03:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
no-change-note-required This PR does not need a change note Ruby
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants