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

Ruby: Add Unsafe HMAC Comparison Query. #13825

Merged
merged 19 commits into from
Sep 4, 2023
Merged

Conversation

boveus
Copy link
Contributor

@boveus boveus commented Jul 26, 2023

Hi CodeQL Team,

This adds an experimental query to detect potential Timing Attacks against usages of Ruby HMACs. This rule is partially inspired by the Python one.

The False positive rate seems fairly low on this query, but I did notice a large string of false positives in the Ruby repository after I ran this query against the top 1000 repositories in GitHub. Because I am unsure of why there are so many FP results in that repo, I figured it would be best to have this be an experimental query.

@boveus boveus requested a review from a team as a code owner July 26, 2023 17:31
@github-actions github-actions bot added the Ruby label Jul 26, 2023
@owen-mc owen-mc changed the title Add Unsafe HMAC Comparison Query. Ruby: Add Unsafe HMAC Comparison Query. Jul 26, 2023
@alexrford alexrford self-requested a review July 28, 2023 09:13
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.

Just some style/formatting comments, I haven't reviewed the results much yet but I'll do that next week.

ruby/ql/src/experimental/cwe-208/UnsafeHmacComparison.ql Outdated Show resolved Hide resolved
ruby/ql/src/experimental/cwe-208/UnsafeHmacComparison.ql Outdated Show resolved Hide resolved
}
}

class Configuration extends DataFlow::Configuration {
Copy link
Contributor

Choose a reason for hiding this comment

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

We're moving away from extending DataFlow::Configuration and instead towards implementing the DataFlow::ConfigSig module. This would look something like:

private module UnsafeHmacComparison {
  private module Config implements DataFlow::ConfigSig {
    predicate isSource(DataFlow::Node source) {
      source instanceof OpenSslHmacHexdigest or
      source instanceof OpenSslnewHmac or
      source instanceof OpenSslHmacbase64digest or
      source instanceof OpenSslHmacdigest or
      source instanceof OpenSslHmactos
    }

    // Holds if a given sink is an Equality Operation (== or !=)
    predicate isSink(DataFlow::Node sink) {
      exists(EqualityOperation eqOp |
        eqOp.getLeftOperand() = sink.asExpr().getExpr()
        or
        eqOp.getRightOperand() = sink.asExpr().getExpr()
      )
    }
  }

  import DataFlow::Global<Config>
}

and then

from UnsafeHmacComparison::PathNode source, UnsafeHmacComparison::PathNode sink
where UnsafeHmacComparison::flowPath(source, sink)

in the query itself.

There aren't that many Ruby examples yet, but rb/xpath-injection uses this format.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks - will this pattern end up being deprecated at some point? We currently use this in one of our internal query packs.

ruby/ql/src/experimental/cwe-208/UnsafeHmacComparison.ql Outdated Show resolved Hide resolved
ruby/ql/src/experimental/cwe-208/UnsafeHmacComparison.ql Outdated Show resolved Hide resolved
ruby/ql/src/experimental/cwe-208/UnsafeHmacComparison.ql Outdated Show resolved Hide resolved
@boveus boveus requested a review from alexrford August 9, 2023 18:52
@boveus
Copy link
Contributor Author

boveus commented Aug 9, 2023

@alexrford - I am getting a compilation error with this query. Could you give me a hand getting it resolved? Thanks.

@boveus boveus requested a review from alexrford August 10, 2023 20:14
@alexrford alexrford merged commit 0325c87 into github:main Sep 4, 2023
18 checks passed
@boveus boveus deleted the add-cwe-208 branch September 5, 2023 17:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants