File tree 5 files changed +47
-0
lines changed
test/library-tests/frameworks/active_support
5 files changed +47
-0
lines changed Original file line number Diff line number Diff line change
1
+ ---
2
+ category : minorAnalysis
3
+ ---
4
+ * The ` ActiveSupport ` extensions ` Object#try ` and ` Object#try! ` are now recognised as code executions.
Original file line number Diff line number Diff line change @@ -81,6 +81,29 @@ module ActiveSupport {
81
81
preservesValue = true
82
82
}
83
83
}
84
+
85
+ /**
86
+ * A call to `Object#try`, which may execute its first argument as a Ruby
87
+ * method call.
88
+ * ```rb
89
+ * x = "abc"
90
+ * x.try(:upcase) # => "ABC"
91
+ * y = nil
92
+ * y.try(:upcase) # => nil
93
+ * ```
94
+ * `Object#try!` behaves similarly but raises `NoMethodError` if the
95
+ * receiver is not `nil` and does not respond to the method.
96
+ */
97
+ class TryCallCodeExecution extends CodeExecution:: Range , DataFlow:: CallNode {
98
+ TryCallCodeExecution ( ) {
99
+ this .asExpr ( ) .getExpr ( ) instanceof UnknownMethodCall and
100
+ this .getMethodName ( ) = [ "try" , "try!" ]
101
+ }
102
+
103
+ override DataFlow:: Node getCode ( ) { result = this .getArgument ( 0 ) }
104
+
105
+ override predicate runsArbitraryCode ( ) { none ( ) }
106
+ }
84
107
}
85
108
86
109
/**
Original file line number Diff line number Diff line change @@ -5,3 +5,12 @@ constantizeCalls
5
5
loggerInstantiations
6
6
| active_support.rb:6:1:6:33 | call to new |
7
7
| active_support.rb:7:1:7:40 | call to new |
8
+ codeExecutions
9
+ | active_support.rb:1:1:1:22 | call to constantize |
10
+ | active_support.rb:3:1:3:13 | call to constantize |
11
+ | active_support.rb:4:1:4:18 | call to safe_constantize |
12
+ | active_support.rb:296:5:296:18 | call to try |
13
+ | active_support.rb:297:5:297:17 | call to try |
14
+ | active_support.rb:298:5:298:19 | call to try! |
15
+ | active_support.rb:298:5:298:35 | call to try! |
16
+ | active_support.rb:299:5:299:18 | call to try! |
Original file line number Diff line number Diff line change 1
1
import codeql.ruby.frameworks.ActiveSupport
2
2
import codeql.ruby.DataFlow
3
3
import codeql.ruby.frameworks.stdlib.Logger
4
+ import codeql.ruby.Concepts
4
5
5
6
query DataFlow:: Node constantizeCalls ( ActiveSupport:: CoreExtensions:: String:: Constantize c ) {
6
7
result = c .getCode ( )
7
8
}
8
9
9
10
query predicate loggerInstantiations ( Logger:: LoggerInstantiation l ) { any ( ) }
11
+
12
+ query predicate codeExecutions ( CodeExecution c ) { any ( ) }
Original file line number Diff line number Diff line change @@ -290,3 +290,11 @@ def m_deep_dup
290
290
x = source "a"
291
291
sink x . deep_dup # $hasValueFlow=a
292
292
end
293
+
294
+ def m_try ( method )
295
+ x = "abc"
296
+ x . try ( :upcase )
297
+ x . try ( method )
298
+ x . try! ( :upcase ) . try! ( :downcase )
299
+ x . try! ( method )
300
+ end
You can’t perform that action at this time.
0 commit comments