Skip to content

Commit

Permalink
Merge pull request #16314 from github/nickrolfe/rb-sensitive
Browse files Browse the repository at this point in the history
Ruby: do fewer regexp matches in SensitiveActions
  • Loading branch information
nickrolfe authored Apr 25, 2024
2 parents 290b0fc + 8f2e51f commit 116873c
Showing 1 changed file with 37 additions and 18 deletions.
55 changes: 37 additions & 18 deletions ruby/ql/lib/codeql/ruby/security/SensitiveActions.qll
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,37 @@ abstract class SensitiveNode extends DataFlow::Node {
}

/** A method call that might produce sensitive data. */
class SensitiveCall extends SensitiveNode instanceof DataFlow::CallNode {
abstract class SensitiveCall extends SensitiveNode { }

private class SensitiveDataMethodNameCall extends SensitiveCall instanceof DataFlow::CallNode {
SensitiveDataClassification classification;

SensitiveCall() {
SensitiveDataMethodNameCall() {
classification = this.getMethodName().(SensitiveDataMethodName).getClassification()
or
// This is particularly to pick up methods with an argument like "password", which
// may indicate a lookup.
exists(string s | super.getArgument(_).asExpr().getConstantValue().isStringlikeValue(s) |
nameIndicatesSensitiveData(s, classification)
)
}

override string describe() { result = "a call to " + super.getMethodName() }

override SensitiveDataClassification getClassification() { result = classification }
}

private class SensitiveArgumentCall extends SensitiveCall instanceof DataFlow::CallNode {
string argName;

SensitiveArgumentCall() {
// This is particularly to pick up methods with an argument like "password", which may indicate
// a lookup.
super.getArgument(_).asExpr().getConstantValue().isStringlikeValue(argName) and
nameIndicatesSensitiveData(argName)
}

override string describe() { result = "a call to " + super.getMethodName() }

override SensitiveDataClassification getClassification() {
nameIndicatesSensitiveData(argName, result)
}
}

/** An access to a variable or hash value that might contain sensitive data. */
abstract class SensitiveVariableAccess extends SensitiveNode {
string name;
Expand Down Expand Up @@ -93,7 +106,7 @@ private string unprefixedVariableName(string name) { result = name.regexpReplace

/** A write to a variable or property that might contain sensitive data. */
private class BasicSensitiveWrite extends SensitiveWrite {
SensitiveDataClassification classification;
string unprefixedName;

BasicSensitiveWrite() {
exists(string name |
Expand All @@ -111,23 +124,29 @@ private class BasicSensitiveWrite extends SensitiveWrite {
*/

writesProperty(this, name) and
nameIndicatesSensitiveData(unprefixedVariableName(name), classification)
unprefixedName = unprefixedVariableName(name) and
nameIndicatesSensitiveData(unprefixedName)
)
}

/** Gets a classification of the kind of sensitive data the write might handle. */
SensitiveDataClassification getClassification() { result = classification }
SensitiveDataClassification getClassification() {
nameIndicatesSensitiveData(unprefixedName, result)
}
}

/** An access to a variable or hash value that might contain sensitive data. */
private class BasicSensitiveVariableAccess extends SensitiveVariableAccess {
SensitiveDataClassification classification;
string unprefixedName;

BasicSensitiveVariableAccess() {
nameIndicatesSensitiveData(unprefixedVariableName(name), classification)
unprefixedName = unprefixedVariableName(name) and
nameIndicatesSensitiveData(unprefixedName)
}

override SensitiveDataClassification getClassification() { result = classification }
override SensitiveDataClassification getClassification() {
nameIndicatesSensitiveData(unprefixedName, result)
}
}

/** A method name that suggests it may be sensitive. */
Expand All @@ -143,11 +162,11 @@ abstract class SensitiveDataMethodName extends SensitiveMethodName {

/** A method name that might return sensitive credential data. */
class CredentialsMethodName extends SensitiveDataMethodName {
SensitiveDataClassification classification;

CredentialsMethodName() { nameIndicatesSensitiveData(this, classification) }
CredentialsMethodName() { nameIndicatesSensitiveData(this) }

override SensitiveDataClassification getClassification() { result = classification }
override SensitiveDataClassification getClassification() {
nameIndicatesSensitiveData(this, result)
}
}

/**
Expand Down

0 comments on commit 116873c

Please sign in to comment.