Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 436cc60

Browse files
committed
Ruby: update some uses of getConstantValue()
1 parent 156964b commit 436cc60

8 files changed

Lines changed: 16 additions & 29 deletions

File tree

ruby/ql/lib/codeql/ruby/Concepts.qll

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -271,10 +271,7 @@ module Http {
271271

272272
/** Gets the URL pattern for this route, if it can be statically determined. */
273273
string getUrlPattern() {
274-
exists(CfgNodes::ExprNodes::StringlikeLiteralCfgNode strNode |
275-
this.getUrlPatternArg().getALocalSource() = DataFlow::exprNode(strNode) and
276-
result = strNode.getExpr().getConstantValue().getStringlikeValue()
277-
)
274+
result = this.getUrlPatternArg().getALocalSource().getConstantValue().getStringlikeValue()
278275
}
279276

280277
/**
@@ -538,10 +535,12 @@ module Http {
538535

539536
/** Gets the mimetype of this HTTP response, if it can be statically determined. */
540537
string getMimetype() {
541-
exists(CfgNodes::ExprNodes::StringlikeLiteralCfgNode strNode |
542-
this.getMimetypeOrContentTypeArg().getALocalSource() = DataFlow::exprNode(strNode) and
543-
result = strNode.getExpr().getConstantValue().getStringlikeValue().splitAt(";", 0)
544-
)
538+
result =
539+
this.getMimetypeOrContentTypeArg()
540+
.getALocalSource()
541+
.getConstantValue()
542+
.getStringlikeValue()
543+
.splitAt(";", 0)
545544
or
546545
not exists(this.getMimetypeOrContentTypeArg()) and
547546
result = this.getMimetypeDefault()

ruby/ql/lib/codeql/ruby/frameworks/ActionController.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ private module Request {
234234
// Request headers are prefixed with `HTTP_` to distinguish them from
235235
// "headers" supplied by Rack middleware.
236236
this.getMethodName() = ["get_header", "fetch_header"] and
237-
this.getArgument(0).asExpr().getExpr().getConstantValue().getString().regexpMatch("^HTTP_.+")
237+
this.getArgument(0).getConstantValue().getString().regexpMatch("^HTTP_.+")
238238
}
239239

240240
override Http::Server::RequestInputKind getKind() { result = Http::Server::headerInputKind() }
@@ -292,7 +292,7 @@ private module Request {
292292
EnvHttpAccess() {
293293
any(EnvCall c).(DataFlow::LocalSourceNode).flowsTo(this.getReceiver()) and
294294
this.getMethodName() = "[]" and
295-
this.getArgument(0).asExpr().getExpr().getConstantValue().getString().regexpMatch("^HTTP_.+")
295+
this.getArgument(0).getConstantValue().getString().regexpMatch("^HTTP_.+")
296296
}
297297

298298
override Http::Server::RequestInputKind getKind() { result = Http::Server::headerInputKind() }

ruby/ql/lib/codeql/ruby/frameworks/ActiveRecord.qll

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -571,9 +571,7 @@ class ActiveRecordAssociation extends DataFlow::CallNode {
571571
* For example, in `has_many :posts`, this is `post`.
572572
*/
573573
string getTargetModelName() {
574-
exists(string s |
575-
s = this.getArgument(0).asExpr().getExpr().getConstantValue().getStringlikeValue()
576-
|
574+
exists(string s | s = this.getArgument(0).getConstantValue().getStringlikeValue() |
577575
// has_one :profile
578576
// belongs_to :user
579577
this.isSingular() and

ruby/ql/lib/codeql/ruby/frameworks/Rails.qll

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -212,12 +212,7 @@ private module Settings {
212212
private class LiteralSetting extends Setting {
213213
ConstantValue value;
214214

215-
LiteralSetting() {
216-
exists(DataFlow::LocalSourceNode lsn |
217-
lsn.asExpr().getConstantValue() = value and
218-
lsn.flowsTo(this.getArgument(0))
219-
)
220-
}
215+
LiteralSetting() { value = this.getArgument(0).getALocalSource().getConstantValue() }
221216

222217
string getValueText() { result = value.toString() }
223218

ruby/ql/lib/codeql/ruby/security/UnsafeDeserializationCustomizations.qll

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,7 @@ module UnsafeDeserialization {
8888

8989
private predicate isOjModePair(CfgNodes::ExprNodes::PairCfgNode p, string modeValue) {
9090
p.getKey().getConstantValue().isStringlikeValue("mode") and
91-
exists(DataFlow::LocalSourceNode symbolLiteral, DataFlow::Node value |
92-
symbolLiteral.asExpr().getExpr().getConstantValue().isSymbol(modeValue) and
93-
symbolLiteral.flowsTo(value) and
94-
value.asExpr() = p.getValue()
95-
)
91+
DataFlow::exprNode(p.getValue()).getALocalSource().getConstantValue().isSymbol(modeValue)
9692
}
9793

9894
/**

ruby/ql/lib/codeql/ruby/security/XSS.qll

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,11 +180,10 @@ private module Shared {
180180
private predicate isFlowFromLocals0(
181181
CfgNodes::ExprNodes::ElementReferenceCfgNode refNode, string hashKey, ErbFile erb
182182
) {
183-
exists(DataFlow::Node argNode, CfgNodes::ExprNodes::StringlikeLiteralCfgNode strNode |
183+
exists(DataFlow::Node argNode |
184184
argNode.asExpr() = refNode.getArgument(0) and
185185
refNode.getReceiver().getExpr().(MethodCall).getMethodName() = "local_assigns" and
186-
argNode.getALocalSource() = DataFlow::exprNode(strNode) and
187-
strNode.getExpr().getConstantValue().isStringlikeValue(hashKey) and
186+
argNode.getALocalSource().getConstantValue().isStringlikeValue(hashKey) and
188187
erb = refNode.getFile()
189188
)
190189
}

ruby/ql/src/queries/security/cwe-078/NonConstantKernelOpen.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import codeql.ruby.ast.Literal
2020
from AmbiguousPathCall call
2121
where
2222
// there is not a constant string argument
23-
not exists(call.getPathArgument().asExpr().getExpr().getConstantValue()) and
23+
not exists(call.getPathArgument().getConstantValue()) and
2424
// if it's a format string, then the first argument is not a constant string
2525
not call.getPathArgument().getALocalSource().asExpr().getExpr().(StringLiteral).getComponent(0)
2626
instanceof StringTextComponent

ruby/ql/test/library-tests/dataflow/summaries/Summaries.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ private class TypeFromCodeQL extends ModelInput::TypeModel {
112112
override DataFlow::Node getASource(string package, string type) {
113113
package = "test" and
114114
type = "FooOrBar" and
115-
result.asExpr().getExpr().getConstantValue().getString() = "magic_string"
115+
result.getConstantValue().getString() = "magic_string"
116116
}
117117

118118
override API::Node getAnApiNode(string package, string type) {

0 commit comments

Comments
 (0)