-
前言
得益于Github雄厚的技术实力以及社区的蓬勃发展,Github为托管代码的安全性提供了强有力的保证和有效的扫描工具。今天,我们介绍下业界常用的扫描工具(CodeQL)及使用方法。我们可以在产品上线前,使用这些代码扫描工具及时的发现安全漏洞和代码错误。
-
代码检查示例
-
错误检查详情
-
高危漏洞示例
-
CodeQL介绍
- 关于
- Code query language
- 业界领先的代码语义分析引擎
- 开发人员可以书写规则以检查代码库中的安全漏洞、代码Bug、错误等
- 也可以使用社区提供的通用的查询语言
- 支持变异分析
- 安全漏洞、Bug、错误被建模为可执行的查询
- 步骤概览
- 通过创建基于代码的CodeQL数据库
- 对数据库运行CodeQL查询
- 解释查询结果
- 关键步骤
- 数据库创建
- 首先提取一个单一关系来标识代码库中的每个源文件
- 对于编译语言,通过监视正常构建的过程来提取。编译器处理源文件的同时,收集源文件的拷贝及相关的信息
- 对于解释型语言,直接基于源码提取
- CodeQL支持的每种语言都有一个提取器,以确保提供尽可能准确。对于多语言的代码,每次只生成一种数据库
- 提取后,所有用来分析的数据(关系数据、源文件拷贝、语言相关的数据库schema-指定数据间的关系)导入到一个目录,这就是所谓的CodeQL数据库
- CodeQL数据库示例(Java)
- expressions 表:行都包括构建过程中分析的源码中的一个表
- statements 表:行都包括构建过程中分析的源码中的一个声明
- 数据库创建
- CodeQL数据库总结
- 包含了从代码库中提取的可查询的数据,包括代码全面的分层表示(抽象的语法树、数据流图、控制流图)
- 每种语言有自己唯一的数据库schema,用来定义创建数据的关系,例如,每种语言都有一张表对应
- 每种语言都提供了基于数据库表的抽象表示,这种基于面向对象的数据视图,使得编写查询语句更便捷
- 支持语言
- C/C++
- Java
- Go
- Python
- JavaScript
- 如何编写查询语言
- 关于
-
Github配置CodeQL
1. Github ->Repository ->Security2. Set up code scanning
3. Set up this workflow
4. 自定义Workflow
``` name: "CodeQL" on: push: branches: [ main ] pull_request: # The branches below must be a subset of the branches above branches: [ main ] schedule: - cron: '0 0 * * *' jobs: analyze: name: Analyze runs-on: ubuntu-latest permissions: actions: read contents: read security-events: write strategy: fail-fast: false matrix: language: [ 'java' ] # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ] # Learn more: # https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed steps: - name: Checkout repository uses: actions/checkout@v2 # Initializes the CodeQL tools for scanning. - name: Initialize CodeQL uses: github/codeql-action/init@v1 with: languages: ${{ matrix.language }} # If you wish to specify custom queries, you can do so here or in a config file. # By default, queries listed here will override any specified in a config file. # Prefix the list here with "+" to use these queries and those in the config file. # queries: ./path/to/local/query, your-org/your-repo/queries@main # Autobuild attempts to build any compiled languages (C/C++, C#, or Java). # If this step fails, then you should remove it and run the build manually (see below) - name: Autobuild uses: github/codeql-action/autobuild@v1 - name: Perform CodeQL Analysis uses: github/codeql-action/analyze@v1 ```- 提交设置
-
其他安全配置
* Security and Licence Scanname: Scan # This section configures the trigger for the workflow. Feel free to customize depending on your convention on: pull_request: jobs: Scan-Build: runs-on: ubuntu-20.04 steps: - uses: actions/checkout@v1 - name: Cache multiple paths uses: actions/cache@v2 with: path: | ${{ github.workspace }}/db key: ${{ runner.os }}-${{ hashFiles('requirements*.txt') }} - name: Perform Scan uses: ShiftLeftSecurity/scan-action@master env: VDB_HOME: ${{ github.workspace }}/db WORKSPACE: "" GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} with: output: reports - name: Upload report uses: github/codeql-action/upload-sarif@v1 with: sarif_file: reports- 扫描结果
- 扫描结果
-
设置示例