11/**
2- * Provides a utility classes and predicates for queries reasoning about Kernel.open and related methods.
2+ * Provides utility classes and predicates for reasoning about ` Kernel.open` and related methods.
33 */
44
55private import codeql.ruby.AST
@@ -9,36 +9,28 @@ private import codeql.ruby.ApiGraphs
99private import codeql.ruby.frameworks.core.Kernel:: Kernel
1010
1111/** A call to a method that might access a file or start a process. */
12- abstract class AmbiguousPathCall extends DataFlow:: CallNode {
13- /** Gets the name for the method being called. */
14- abstract string getName ( ) ;
15-
16- /** Gets the name for a safer method that can be used instead. */
17- abstract string getReplacement ( ) ;
18-
19- /** Gets the argument that specifies the path to be accessed. */
20- abstract DataFlow:: Node getPathArgument ( ) ;
21- }
22-
23- private class KernelOpenCall extends KernelMethodCall , AmbiguousPathCall {
24- KernelOpenCall ( ) { this .getMethodName ( ) = "open" }
25-
26- override string getName ( ) { result = "Kernel.open" }
12+ class AmbiguousPathCall extends DataFlow:: CallNode {
13+ string name ;
2714
28- override string getReplacement ( ) { result = "File.open" }
29-
30- override DataFlow:: Node getPathArgument ( ) { result = this .getArgument ( 0 ) }
31- }
32-
33- private class IOReadCall extends DataFlow:: CallNode , AmbiguousPathCall {
34- IOReadCall ( ) {
15+ AmbiguousPathCall ( ) {
16+ this .( KernelMethodCall ) .getMethodName ( ) = "open" and
17+ name = "Kernel.open"
18+ or
3519 this = API:: getTopLevelMember ( "IO" ) .getAMethodCall ( "read" ) and
36- not this = API:: getTopLevelMember ( "File" ) .getAMethodCall ( "read" ) // needed in e.g. opal/opal, where some calls have both paths, but I'm not sure why
20+ not this = API:: getTopLevelMember ( "File" ) .getAMethodCall ( "read" ) and // needed in e.g. opal/opal, where some calls have both paths, but I'm not sure why
21+ name = "IO.read"
3722 }
3823
39- override string getName ( ) { result = "IO.read" }
24+ /** Gets the name for the method being called. */
25+ string getName ( ) { result = name }
4026
41- override string getReplacement ( ) { result = "File.read" }
27+ /** Gets the name for a safer method that can be used instead. */
28+ string getReplacement ( ) {
29+ result = "File.read" and name = "IO.read"
30+ or
31+ result = "File.open" and name = "Kernel.open"
32+ }
4233
43- override DataFlow:: Node getPathArgument ( ) { result = this .getArgument ( 0 ) }
34+ /** Gets the argument that specifies the path to be accessed. */
35+ DataFlow:: Node getPathArgument ( ) { result = this .getArgument ( 0 ) }
4436}
0 commit comments