qtil
advanced-security/qtil-ruby 0.0.2
Search

Class API::Node

A node in the API graph, representing a value that has crossed the boundary between this codebase and an external library (or in general, any external codebase).

Basic usage

API graphs are typically used to identify “API calls”, that is, calls to an external function whose implementation is not necessarily part of the current codebase.

The most basic use of API graphs is typically as follows:

  1. Start with API::getTopLevelMember for the relevant library.
  2. Follow up with a chain of accessors such as getMethod describing how to get to the relevant API function.
  3. Map the resulting API graph nodes to data-flow nodes, using asSource or asSink.

For example, a simplified way to get arguments to Foo.bar would be

API::getTopLevelMember("Foo").getMethod("bar").getParameter(0).asSink()

The most commonly used accessors are getMember, getMethod, getParameter, and getReturn.

API graph nodes

There are two kinds of nodes in the API graphs, distinguished by who is “holding” the value:

  • Use-nodes represent values held by the current codebase, which came from an external library. (The current codebase is “using” a value that came from the library).
  • Def-nodes represent values held by the external library, which came from this codebase. (The current codebase “defines” the value seen by the library).

API graph nodes are associated with data-flow nodes in the current codebase. (Since external libraries are not part of the database, there is no way to associate with concrete data-flow nodes from the external library).

  • Use-nodes are associated with data-flow nodes where a value enters the current codebase, such as the return value of a call to an external function.
  • Def-nodes are associated with data-flow nodes where a value leaves the current codebase, such as an argument passed in a call to an external function.

Access paths and edge labels

Nodes in the API graph are associated with a set of access paths, describing a series of operations that may be performed to obtain that value.

For example, the access path API::getTopLevelMember("Foo").getMethod("bar") represents the action of reading the top-level constant Foo and then accessing the method bar on the resulting object. It would be associated with a call such as Foo.bar().

Each edge in the graph is labelled by such an “operation”. For an edge A->B, the type of the A node determines who is performing the operation, and the type of the B node determines who ends up holding the result:

  • An edge starting from a use-node describes what the current codebase is doing to a value that came from a library.
  • An edge starting from a def-node describes what the external library might do to a value that came from the current codebase.
  • An edge ending in a use-node means the result ends up in the current codebase (at its associated data-flow node).
  • An edge ending in a def-node means the result ends up in external code (its associated data-flow node is the place where it was “last seen” in the current codebase before flowing out)

Because the implementation of the external library is not visible, it is not known exactly what operations it will perform on values that flow there. Instead, the edges starting from a def-node are operations that would lead to an observable effect within the current codebase; without knowing for certain if the library will actually perform those operations. (When constructing these edges, we assume the library is somewhat well-behaved).

For example, given this snippet:

Foo.bar(->(x) { doSomething(x) })

A callback is passed to the external function Foo.bar. We can’t know if Foo.bar will actually invoke this callback. But if the library should decide to invoke the callback, then a value will flow into the current codebase via the x parameter. For that reason, an edge is generated representing the argument-passing operation that might be performed by Foo.bar. This edge is going from the def-node associated with the callback to the use-node associated with the parameter x of the lambda.

Import path

import codeql.ruby.ApiGraphs

Direct supertypes

Known direct subtypes

Predicates

asSink

Gets a data-flow node where this value leaves the current codebase and flows into an external library (or in general, any external codebase).

asSource

Gets a data-flow node where this value enters the current codebase.

getAMember

Gets a node representing a member of this API component where the name of the member may or may not be known statically.

getAMethodCall

Gets a call to a method on the receiver represented by this API component.

getAPredecessor

Gets a node such that there is an edge in the API graph between this node and the other one.

getAPredecessor

Gets a node such that there is an edge in the API graph between that other node and this one, and that edge is labeled with lbl

getARhs

DEPRECATED. This predicate has been renamed to asSink().

getASubclass

Gets a node representing a (direct or indirect) subclass of the class represented by this node. rb class A; end class B < A; end class C < B; end In the example above, getMember("A").getASubclass() will return uses of A, B and C.

getASuccessor

Gets a node such that there is an edge in the API graph between that other node and this one.

getASuccessor

Gets a node such that there is an edge in the API graph between this node and the other one, and that edge is labeled with lbl.

getAUse

DEPRECATED. This predicate has been renamed to getAValueReachableFromSource().

getAValueReachableFromSource

Gets a data-flow node where this value may flow after entering the current codebase.

getAValueReachingRhs

DEPRECATED. This predicate has been renamed to getAValueReachingSink().

getAValueReachingSink

Get a data-flow node that transitively flows to an external library (or in general, any external codebase).

getAnElement

Gets a node representing an element of this collection (known or unknown).

getAnImmediateSubclass

Gets a node representing a direct subclass of the class represented by this node. rb class A; end class B < A; end class C < B; end In the example above, getMember("A").getAnImmediateSubclass() will return uses of B only.

getAnImmediateUse

DEPRECATED. This predicate has been renamed to asSource().

getAnInstantiation

Gets a new call to the function represented by this API component.

getBlock

Gets an API node representing the block parameter.

getContent

Gets a node representing the content stored on the base object.

getContents

Gets a node representing the contents stored on the base object.

getDepth

Gets the shortest distance from the root to this node in the API graph.

getField

Gets a node representing the instance field of the given name, which must include the @ character.

getInducingNode

Gets the data-flow node that gives rise to this node, if any.

getInstance

Gets a node representing an instance of this API component, that is, an object whose constructor is the function represented by this node.

getKeywordParameter

Gets an API node representing the given keyword parameter.

getLocation

Gets the location of this node.

getMember

Gets a node representing member m of this API component.

getMethod

Gets a node representing a call to method on the receiver represented by this node.

getParameter

Gets an API node representing the nth positional parameter.

getPath

Gets a string representation of the lexicographically least among all shortest access paths from the root to this node.

getReturn

Gets a node representing the result of this call.

getReturn

Gets a node representing the result of calling a method on the receiver represented by this node.

toString

Gets a textual representation of this element.