天天看点

如何使用Reposaur检测开源项目代码的合规性

关于Reposaur

Reposaur是一款针对开发平台和开源项目的合规性检测工具,在该工具的帮助下,广大研究人员可以直接使用预定义或自定义的策略来对目标项目或代码进行审核跟验证,并对数据和配置进行合规性检测。因此,Reposaur能够确保代码库中每一位代码贡献者都能够符合特定的安全标准或最佳实践准则。

当前版本的Reposaur支持GitHub和GitLab,随后将添加对Gitea的支持。

功能介绍

1、使用了Rego策略语言实现自定义策略;

2、提供了简单、易于使用的命令行接口;

3、支持使用简单的SDK进行扩展(Go编写);

4、报告遵循标准的SARIF格式,便于与不同系统集成;

5、可以对策略进行单元测试,确保它们按预期工作;

6、支持与主流开发平台集成;

7、支持使用SDK轻松集成新平台;

工具安装

源码获取

广大研究人员可以使用下列命令将该项目源码克隆至本地:

git clone https://github.com/reposaur/reposaur.git           

复制

Homebrew安装

$ brew install reposaur/tap/reposaur           

复制

DEB、ROM和APK包

广大研究人员可以直接从该项目的【Releases页面】下载.deb、.rmp或.apk包,然后使用特定的工具来安装它们。

Go安装

$ go install github.com/reposaur/reposaur/cmd/rsr@latest           

复制

脚本安装

$ curl -o- https://raw.githubusercontent.com/reposaur/reposaur/main/install.sh | bash           

复制

工具使用

编写自定义策略

策略可以通过多个模块(文件)进行组合,必须符合同一命名空间(包),每一个模块可以定义多个规则。

下面的演示中,我们将通过一个github.repository命名空间下的单一模块进行演示。命名空间非常重要,因为Reposaur需要通过它来判断要对目标数据执行哪种规则:

package github.repository           

复制

接下来就要定义一个规则来获取默认的分支保护数据了,GitHub返回的数据不包含这部分内容,因此我们还需要添加额外的请求来获取:

protection = data {

resp := github.request("GET /repos/{owner}/{repo}/branches/{branch}/protection", {

"owner": input.owner.login,

"repo": input.name,

"branch": input.default_branch,

})



resp.status == 200



data := resp.body

}           

复制

结果如下所示:

violation_default_branch_not_protected {

not protection

}           

复制

接下来,我们可以通过下列规则来检测默认分支是否启用了其他保护策略:

violation_default_branch_pull_not_required {

not protection.required_pull_request_reviews

}



violation_default_branch_approvals_not_required {

not protection.required_pull_request_reviews.required_approving_review_count

}



violation_default_branch_approvals_not_required {

protection.required_pull_request_reviews.required_approving_review_count < 1

}



violation_default_branch_code_owners_reviews_not_required {

not protection.required_pull_request_reviews.require_code_owner_reviews

}



violation_default_branch_status_checks_not_required {

not protection.required_status_checks

}



violation_default_branch_up_to_date_not_required {

not protection.required_status_checks.strict

}           

复制

整合所有策略之后,我们的自定义策略将如下所示:

package github.repository



protection = data {

resp := github.request("GET /repos/{owner}/{repo}/branches/{branch}/protection", {

"owner": input.owner.login,

"repo": input.name,

"branch": input.default_branch,

})



resp.status == 200



data := resp.body

}



violation_default_branch_not_protected {

not protection

}



violation_default_branch_pull_not_required {

not protection.required_pull_request_reviews

}



violation_default_branch_approvals_not_required {

not protection.required_pull_request_reviews.required_approving_review_count

}



violation_default_branch_approvals_not_required {

protection.required_pull_request_reviews.required_approving_review_count < 1

}



violation_default_branch_code_owners_reviews_not_required {

not protection.required_pull_request_reviews.require_code_owner_reviews

}



violation_default_branch_status_checks_not_required {

not protection.required_status_checks

}



violation_default_branch_up_to_date_not_required {

not protection.required_status_checks.strict

}           

复制

策略执行

现在,我们就可以使用自定义策略来对真实场景中的数据进行合规性检测了。

下列命令可以单独对一个项目代码库执行检测:

$ gh api /repos/reposaur/test | rsr exec           

复制

或者,也可以对一个组织中的所有代码库进行检测:

$ gh api /orgs/reposaur | rsr exec           

复制

SARIF报告生成

工具执行完毕后,将生成如下所示的SARIF报告:

{

  "version": "2.1.0",

  "$schema": "https://json.schemastore.org/sarif-2.1.0-rtm.5.json",

  "runs": [

    {

      "tool": {

        "driver": {

          "informationUri": "https://github.com/reposaur/reposaur",

          "name": "Reposaur",

          "rules": [

            {

              "id": "github.repository/note/not_innersource_ready",

              "name": "Repository is not InnerSource ready",

              "shortDescription": {

                "text": "Repository is not InnerSource ready"

              },

              "fullDescription": {

                "text": "InnerSource repositories (that have the `innersource` topic) must have all of\nthese files: `README.md`, `CONTRIBUTING.md` and `LICENSE`, and at least one\nof them is missing.",

                "markdown": "InnerSource repositories (that have the `innersource` topic) must have all of\nthese files: `README.md`, `CONTRIBUTING.md` and `LICENSE`, and at least one\nof them is missing."

              },

              "help": {

                "markdown": "InnerSource repositories (that have the `innersource` topic) must have all of\nthese files: `README.md`, `CONTRIBUTING.md` and `LICENSE`, and at least one\nof them is missing."

              },

              "properties": {

                "security-severity": "1"

              }

            }

          ]

        }

      },

      "results": [

        {

          "ruleId": "github.repository/note/not_innersource_ready",

          "ruleIndex": 0,

          "level": "note",

          "message": {

            "text": "Repository is not InnerSource ready"

          },

          "locations": [

            {

              "physicalLocation": {

                "artifactLocation": {

                  "uri": "."

                }

              }

            }

          ]

        }

      ],

      "properties": {

        "default_branch": "main",

        "owner": "reposaur",

        "repo": "test"

      }

    }

  ]

}           

复制

许可证协议

本项目的开发与发布遵循MIT开源许可证协议。

项目地址

https://github.com/reposaur/reposaur

参考资料

https://www.openpolicyagent.org/docs/latest/policy-language/

https://docs.reposaur.com/guides/writing-your-first-policy