Featured image of post CodeQL 1

CodeQL 1

环境安装

CodeQL-Cli

闭源

https://github.com/github/codeql-cli-binaries

CodeQL

开源规则库

https://github.com/github/codeql

环境变量设置

设置为code-cli文件夹位置

VSCode插件

设置CodeQL-Cli的code.exe位置

创建QL数据库

codeql database create ./codeql-db \
  --language=java \
  --command="mvn clean compile"  # 或 ./gradlew compileJava(Gradle 项目)

编写QL规则

新建detect.ql文件

示例内容

/**
 * 目标:检测代码中初始化 InvokerTransformer 的位置(CC 链核心组件)
 */
import java

from int x, int y, int z
where x in [1..10] and y in [1..10] and z in [1..10] and
      x*x + y*y = z*z
select x, y, z

可能踩坑

需要创建qlback.yml配置文件

name: detect-invoker
version: 0.0.1
dependencies:
  codeql/java-all: "*"

否则会报错Failed to run query: ERROR: could not resolve module java

执行查询

可以在命令行执行

codeql query run --database=./cc6-db --search-path=E:\tools\CodeQL\codeql ./detect-invoker.ql

或者在VsCode插件中执行

参考文档

https://codeql.github.com/docs

使用 Hugo 构建
主题 StackJimmy 设计