query-primitives


License

License

GroupId

GroupId

io.shiftleft
ArtifactId

ArtifactId

query-primitives
Last Version

Last Version

0.9.322
Release Date

Release Date

Type

Type

jar
Description

Description

query-primitives
query-primitives
Project URL

Project URL

https://github.com/ShiftLeftSecurity/codepropertygraph/
Project Organization

Project Organization

io.shiftleft
Source Code Management

Source Code Management

https://github.com/ShiftLeftSecurity/codepropertygraph

Download query-primitives

How to add to project

<!-- https://jarcasting.com/artifacts/io.shiftleft/query-primitives/ -->
<dependency>
    <groupId>io.shiftleft</groupId>
    <artifactId>query-primitives</artifactId>
    <version>0.9.322</version>
</dependency>
// https://jarcasting.com/artifacts/io.shiftleft/query-primitives/
implementation 'io.shiftleft:query-primitives:0.9.322'
// https://jarcasting.com/artifacts/io.shiftleft/query-primitives/
implementation ("io.shiftleft:query-primitives:0.9.322")
'io.shiftleft:query-primitives:jar:0.9.322'
<dependency org="io.shiftleft" name="query-primitives" rev="0.9.322">
  <artifact name="query-primitives" type="jar" />
</dependency>
@Grapes(
@Grab(group='io.shiftleft', module='query-primitives', version='0.9.322')
)
libraryDependencies += "io.shiftleft" % "query-primitives" % "0.9.322"
[io.shiftleft/query-primitives "0.9.322"]

Dependencies

compile (4)

Group / Artifact Type Version
org.scala-lang : scala-library jar 2.12.8
io.shiftleft : codepropertygraph jar 0.9.322
org.json4s : json4s-native_2.12 jar 3.5.2
com.massisframework : j-text-utils jar 0.3.4

test (1)

Group / Artifact Type Version
org.scalatest : scalatest_2.12 jar 3.0.3

Project Modules

There are no modules declared in this project.

Build Status bintray Maven Central

Code Property Graph - Specification and Tooling

Note: for first-time users, we recommend building "joern" at https://github.com/ShiftLeftSecurity/joern/ instead. It combines this repo with a C/C++ language frontend to construct a complete code analysis platform.

A Code Property Graph (CPG) is an extensible and language-agnostic representation of program code designed for incremental and distributed code analysis. This repository hosts the base specification together with a build process that generates data structure definitions for accessing the graph with different programming languages.

We are publishing the Code Property Graph specification as a suggestion for an open standard for the exchange of code in intermediate representations along with analysis results. With this goal in mind, the specification consists of a minimal base schema that can be augmented via extension schemas to enable storage of application-specific data.

Usage as a dependency

build.sbt:

libraryDependencies += "io.shiftleft" %% "codepropertygraph" % "x.y.z"

Building the code

Note: for first-time users, we recommend building "joern" at https://github.com/ShiftLeftSecurity/joern/ instead. It contains a code property graph generator for C/C++, a component for querying the code property graph, as well as a few helpful examples to get started.

The build process has been verified on Linux and it should be possible to build on OS X and BSD systems as well. The build process requires the following prerequisites:

Some binary files required for testing are managed through git-lfs. If you haven't cloned this repository yet, simply run git lfs install. If you have cloned it already, additionally run git lfs pull (from within the repository).

Additional build-time dependencies are automatically downloaded as part of the build process. To build and install into your local Maven cache, issue the command ./publishLocal.sh.

This command will install the following artifacts:

  • codepropertygraph-VERSION.jar: Java and Scala classes to be used in combination with the ShiftLeft Tinkergraph [3].

  • codepropertygraph-protos-VERSION.jar: Java bindings for Google's Protocol Buffer definitions

Creating Protocol Buffer bindings for different languages

The codepropertygraph-VERSION.jar artifact contains a Protocol Buffer definition file cpg.proto that you can use to generate your own language-specific bindings. For instance, to create C++ and Python bindings, issue the following series of commands:

sbt package
cd codepropertygraph/target
mkdir cpp python
protoc --cpp_out=cpp --python_out=python cpg.proto

Base schema for the Code Property Graph

You can find the code property graph specification in base.json. A high level description is present in

https://docs.shiftleft.io/core-concepts/code-property-graph

Loading a codepropertygraph

Here's how you can load a cpg into ShiftLeft Tinkergraph [3] in the sbt console - the next section will list some queries you can interactively run from there.

There are some sample cpgs in this repository in the resources/testcode/cpgs directory. You can run ./regenerate-test-cpgs.sh to update them, but this requires the proprietary java2cpg installed locally.

Tinkergraph (in memory reference db)

sbt semanticcpg/console
import io.shiftleft.codepropertygraph.Cpg
import io.shiftleft.semanticcpg.language._
val cpg = io.shiftleft.codepropertygraph.cpgloading.CpgLoader.load("./resources/testcode/cpgs/hello-shiftleft-0.0.5/cpg.bin.zip")

Querying the cpg

Once you've loaded a cpg you can run queries, which are provided by the query-primitives subproject. Note that if you're in the sbt shell you can play with it interactively: TAB completion is your friend. Otherwise your IDE will assist.

Here are some simple traversals to get all the base nodes. Running all of these without errors is a good test to ensure that your cpg is valid:

cpg.literal.toList
cpg.file.toList
cpg.namespace.toList
cpg.types.toList
cpg.methodReturn.toList
cpg.parameter.toList
cpg.member.toList
cpg.call.toList
cpg.local.toList
cpg.identifier.toList
cpg.argument.toList
cpg.typeDecl.toList
cpg.method.toList

From here you can traverse through the cpg. The query-primitives DSL ensures that only valid steps are available - anything else will result in a compile error:

cpg.method.name("getAccountList").parameter.toList
/* List(
 *   MethodParameterIn(Some(v[7054781587948444580]),this,0,this,BY_SHARING,io.shiftleft.controller.AccountController,Some(28),None,None,None),
 *   MethodParameterIn(Some(v[7054781587948444584]),request,2,request,BY_SHARING,javax.servlet.http.HttpServletRequest,Some(28),None,None,None),
 *   MethodParameterIn(Some(v[7054781587948444582]),response,1,response,BY_SHARING,javax.servlet.http.HttpServletResponse,Some(28),None,None,None)
 *   )
 **/

cpg.method.name("getAccountList").definingTypeDecl.toList.head
// TypeDecl(Some(v[464]),AccountController,io.shiftleft.controller.AccountController,false,List(java.lang.Object))

Base Schema Specification

The base schema provides the minimum requirements all valid CPGs must satisfy. The base specification is concerned with three aspects of the program:

  • Program structure
  • Type declarations
  • Method declarations

where a declaration comprises a formal signature, along with defining content such as a method body for methods or a literal value in the definition of a variable.

Property graphs alone are comparable in generality to hash tables and linked lists. To tailor them towards storing, transmitting, and analyzing code, the main challenge is to specify a suitable graph schema. In particular, a schema must define the valid node and edge types, node and edge keys, together with each of their domains. Finally, a schema puts constraints on the edges that may connect nodes, depending on their type.

The base schema of the CPG is specified in the JSON file base.json. The file contains a JSON object with the following members:

  • nodeKeys/edgeKeys. List of all valid node/edge attributes. Each list element is an object specifying the attribute's ID, name, its type, and a comment.

  • nodeTypes/edgeTypes. List of all node/edge types (i.e. edge labels in the original property graph definition), where each node/edge type is given by an object that specifies an ID, name,keys, comment, and, for node types (each note is required to have a mandatory note type, represented by a node attribute), valid outgoing edge types.

There are 19 node types across five categories:

Category Names
Program structure FILE, NAMESPACE_BLOCK
Type declarations TYPE_DECL, TYPE_PARAMETER, MEMBER, TYPE, TYPE_ARGUMENT
Method header METHOD, METHOD_PARAMETER_IN, METHOD_RETURN, MODIFIER
Method body LITERAL, IDENTIFIER, CALL, RETURN, METHOD_REF, LOCAL, BLOCK
Meta data META_DATA

There are eight edge types:

Name Usage
AST Syntax tree edge - structure
CFG Control flow edge - execution order and conditions
REF Reference edge - references to type/method/identifier declarations
EVAL_TYPE Type edge - attach known types to expressions
CALL Method invocation edge - caller/callee relationship
VTABLE Virtual method table edge - represents vtables
INHERITS_FROM Type inheritance edge - models OOP inheritance
BINDS_TO Binding edge - provides type parameters

There are 17 node keys across three categories:

Category Names
Declarations NAME, FULL_NAME, IS_EXTERNAL
Method header SIGNATURE, MODIFIER_TYPE
Method body PARSER_TYPE_NAME, ORDER, CODE, DISPATCH_TYPE, EVALUATION_STRATEGY,LINE_NUMBER, LINE_NUMBER_END, COLUMN_NUMBER,COLUMN_NUMBER_END, ARGUMENT_INDEX
Meta data LANGUAGE, VERSION

There are zero edge keys in the base specification.

Notice the deviation from the JSON standard by allowing inline comments. Any line for which the first two non-whitespace characters are equal to / are treated as comments, and need to be stripped prior to passing the definitions to standard JSON parsers.

Program Structure

Node types: FILE, NAMESPACE_BLOCK

Program structure is concerned with the organization of programs into files, namespaces, and packages. A program is composed of zero or more files (type FILE), each of which contains one or more namespace blocks (type NAMESPACE_BLOCK). Namespace blocks contain type and method declarations (type TYPE_DECL and METHOD). Abstract syntax tree (AST) edges must exist from files to namespace blocks. Structural elements below namespace blocks are not connected to their AST parents by an edge. Instead the AST_PARENT_TYPE and AST_PARENT_FULL_NAME properties are used to inform the backend about the AST relation of methods (type METHOD) and type declarations (type TYPE_DECL) to their parents. The property FULL_NAME thereby must be a unique identifier for the three node types METHOD, TYPE_DECL and NAMESPACE_BLOCK. This figure shows how a Java class definition is represented in a CPG.

Program Structure

The concept of namespace blocks correspond to the equivalent concepts in the C++ programming language, where namespace blocks are used to place declarations into a namespace. Other languages, e.g., Java or Python, do not provide the same type of namespace blocks. However, they allow package declarations at the start of source files that serve the purpose of placing all remaining declarations of the source file into a namespace. Package declarations are translated into corresponding namespace blocks for these languages. So the name of a namespace block is the complete namespace of all the elements within the block and the full name of a namespace block is a unique identifier for a specific block. For Java, it is enough to prefix the file name to the namespace because there is only one namespace describing package statement per file.

Type Declarations

Node types: TYPE_DECL, TYPE_PARAMETER, MEMBER, TYPE, TYPE_ARGUMENT

Language constructs are expressed that declare types via type declarations. Examples of these constructs include classes, interfaces, structures, and enumerations. A type declaration consists of a name, an optional list for type parameters, member variables and methods. Inheritance relations with other types may be encoded in type declarations.

Program Structure

In the CPG, each type declaration is represented by a designated type-declaration node (type TYPE_DECL), with at least a full-name attribute. Member variables (type MEMBER), method declarations (type METHOD), and type parameters (type TYPE_PARAMETER) are connected to the type declaration via AST edges, originating at the type declaration. Inheritance relations are expressed via INHERITS_FROM edges to zero or more other type declarations (type TYPE_DECL), which indicate that the source type declaration inherits from the destination declaration.

Usage of a type, for example in the declaration of a variable, is indicated by a type node (type TYPE). The type node is connected to the corresponding type declaration via a reference edge (type REF), and to type arguments through AST edges (type AST). Type-argument nodes are connected to type parameters by binding edges (type BINDS_TO).

Method Declarations

A method declaration consists of a method header and a method body, where the declaration is a CPG representation of the method's input and output parameters, and the method body contains the instructions/statements of the method.

Method Header

Node types: METHOD, METHOD_PARAMETER_IN, METHOD_RETURN, LOCAL, BLOCK, MODIFIER

The term method is used in object-oriented programming languages to refer to a procedure that is associated with a class. The term is used here in a broader sense to refer to any named block of code. This code may or may not be defined to be associated with a type. The method consists of a method header and a method body. The method header is given by a name, a formal return parameter and a list of formal input parameters and corresponding output parameters. The method body is simply a block of statements.

Method Header

In the CPG, each method is represented by a designated method node (type METHOD) that contains the method name in particular. Methods are connected to their method input parameters (type METHOD_PARAMETER_IN), return parameter (type METHOD_RETURN), modifiers (type MODIFIER) and locals (type LOCAL) through AST edges. The method node is connected to a block node (type BLOCK), which represents the method body.

Method Body

Node types: LITERAL, IDENTIFIER, CALL, RETURN, METHOD_REF

Method bodies contain the method implementation, given by the operations the method carries out. Method bodies are represented as Control Flow graphs over method invocations, a representation used to provide a common ground for the instruction, statement, and expression concepts used across machine-level and high-level programming languages. The core elements of the method body representation are thus method invocations ("calls") and control flow edges.

In the CPG, a method invocation is represented by a designated call node (type CALL). Arguments are either identifier (type IDENTIFIER), literals (type LITERAL), other calls (type CALL) or method references (type METHOD_REF). Each argument has an argument index property (type ARGUMENT_INDEX) to indicate which parameter it is associated with. Calls are connected to their arguments through outgoing AST edges, and are associated to the called method via their METHOD_FULL_NAME property.

Call Site

In addition to identifiers, literals and calls, method references (type METHOD_REF) are allowed to represent locations in the code where a method is not called, but referenced, as is the case for programming languages where methods are first-class citizens. Method references are connected to method instances by reference edges (type REF).

Return nodes (type RETURN) are created for each location in the method body where control is returned to the caller. Unconditional control flow edges are created from preceding calls to return nodes. All remaining nodes are connected by control flow edges (type CFG) according to execution order and constraints. The method node is treated as the entry node of the Control Flow graph. Finally, a designated block node (type BLOCK) is created for the method body, and creates outgoing AST edges to all expressions that correspond to statements.

Background on the Method Body Representation

In machine-level languages, procedure bodies are defined by instructions and connected by control flow edges to form a Control Flow graph. Each instruction represents an operation carried out by the machine, which can modify the program state. In contrast, higher-level languages (C and above) typically eliminate the instruction concept in favor of statements. As for instructions, statements can modify the program state. They differ from instructions in that they can consist of multiple expressions. Expressions are anonymous blocks of code that receive input and produce an output value upon evaluation. Inputs to an expression can be literals and identifiers, but they may also be other expressions. In fact, the semantics of statements can be fully encoded via expression trees, and control flow edges attached to the roots of these trees, to represent the statement's control flow semantics.

The ability of a statement to represent several expressions allows for concise program formulation. However, it presents challenges to program analysis. While it is possible to create a Control Flow graph by introducing control flow edges between statements, this graph does not encode the intra-statement control flow. Fortunately, the tree combined with disambiguation rules of the programming language fully encodes the evaluation order of expressions within a statement. This allows unambiguously representation of their evaluation order with control flow edges.

Expressions consist of method evaluations and applications of operators provided by the language. By expressing operators as methods, and allowing methods to receive the return values of other methods as input, all expressions can be represented as method invocations. A program representation for the method body is thus produced, which consists of method invocations connected by control flow edges.

Meta Data Block

A metadata block (type META_DATA) is included in CPG with two fields: a language field (key LANGUAGE) to indicate the programming language from which the graph was generated, and a version field (key VERSION) holding the specification version. Both fields are free-text strings.

References

[1] Rodriguez and Neubauer - The Graph Traversal Pattern: https://pdfs.semanticscholar.org/ae6d/dcba8c848dd0a30a30c5a895cbb491c9e445.pdf

[2] Yamaguchi et al. - Modeling and Discovering Vulnerabilities with Code Property Graphs https://www.sec.cs.tu-bs.de/pubs/2014-ieeesp.pdf

[3] The ShiftLeft Tinkergraph https://github.com/ShiftLeftSecurity/tinkergraph-gremlin

io.shiftleft

ShiftLeft Inc.

Versions

Version
0.9.322
0.9.321
0.9.320
0.9.319
0.9.318
0.9.317
0.9.316
0.9.315
0.9.314
0.9.313
0.9.312
0.9.311
0.9.310
0.9.309
0.9.308
0.9.307
0.9.306
0.9.305
0.9.303
0.9.302
0.9.301
0.9.300
0.9.299
0.9.298
0.9.297
0.9.296
0.9.295
0.9.294
0.9.293
0.9.292
0.9.291
0.9.290
0.9.289
0.9.288
0.9.287
0.9.286
0.9.285
0.9.284
0.9.283
0.9.282
0.9.281
0.9.280
0.9.279
0.9.278
0.9.277
0.9.276
0.9.275
0.9.274
0.9.273
0.9.272
0.9.271
0.9.270
0.9.269
0.9.268
0.9.267
0.9.266
0.9.265
0.9.264
0.9.263
0.9.262
0.9.261
0.9.260
0.9.259
0.9.258
0.9.257
0.9.256
0.9.255
0.9.254
0.9.253
0.9.252
0.9.251
0.9.250
0.9.249
0.9.248
0.9.247
0.9.246
0.9.245
0.9.244
0.9.243
0.9.242
0.9.241
0.9.240
0.9.239
0.9.238
0.9.237
0.9.236
0.9.235
0.9.234
0.9.233
0.9.232
0.9.231
0.9.230
0.9.229
0.9.228
0.9.227
0.9.226
0.9.225
0.9.224
0.9.223
0.9.222
0.9.221
0.9.220
0.9.219
0.9.210
0.9.209
0.9.208
0.9.207
0.9.206
0.9.205
0.9.204
0.9.203
0.9.202
0.9.201
0.9.200
0.9.199
0.9.198
0.9.197
0.9.196
0.9.195
0.9.194
0.9.193
0.9.192
0.9.191
0.9.190
0.9.186
0.9.184
0.9.180
0.9.179
0.9.178
0.9.177
0.9.176
0.9.175
0.9.174
0.9.173
0.9.172
0.9.171
0.9.170
0.9.169
0.9.168
0.9.167
0.9.166
0.9.165
0.9.164
0.9.157
0.9.156
0.9.155
0.9.154
0.9.153
0.9.152
0.9.151
0.9.150
0.9.149
0.9.148
0.9.147
0.9.142
0.9.141
0.9.138
0.9.137
0.9.136
0.9.135
0.9.133
0.9.132
0.9.131
0.9.130
0.9.129
0.9.128
0.9.127
0.9.126
0.9.123
0.9.122
0.9.121
0.9.120
0.9.119
0.9.118
0.9.117
0.9.116
0.9.115
0.9.114
0.9.111
0.9.110
0.9.109
0.9.108
0.9.107
0.9.106
0.9.105
0.9.104
0.9.103
0.9.102
0.9.101
0.9.100
0.9.99
0.9.98
0.9.97
0.9.96
0.9.95
0.9.94
0.9.93
0.9.92
0.9.91
0.9.90
0.9.89
0.9.88
0.9.87
0.9.86
0.9.85
0.9.84
0.9.83
0.9.82
0.9.81
0.9.80
0.9.79
0.9.78
0.9.77
0.9.76
0.9.75
0.9.74
0.9.73
0.9.72
0.9.70
0.9.69
0.9.68
0.9.67
0.9.66
0.9.65
0.9.64
0.9.63
0.9.62
0.9.61
0.9.60
0.9.59
0.9.58
0.9.57
0.9.56
0.9.55
0.9.54
0.9.53
0.9.52
0.9.51
0.9.50
0.9.49
0.9.46
0.9.45