前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >如何使用Reposaur检测开源项目代码的合规性

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

作者头像
FB客服
发布2022-06-08 15:44:05
1.1K0
发布2022-06-08 15:44:05
举报
文章被收录于专栏:FreeBufFreeBuf

关于Reposaur

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

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

功能介绍

1、使用了Rego策略语言实现自定义策略; 2、提供了简单、易于使用的命令行接口; 3、支持使用简单的SDK进行扩展(Go编写); 4、报告遵循标准的SARIF格式,便于与不同系统集成; 5、可以对策略进行单元测试,确保它们按预期工作; 6、支持与主流开发平台集成; 7、支持使用SDK轻松集成新平台;

工具安装

源码获取

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

代码语言:javascript
复制
git clone https://github.com/reposaur/reposaur.git

Homebrew安装

代码语言:javascript
复制
$ brew install reposaur/tap/reposaur

DEB、ROM和APK包

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

Go安装

代码语言:javascript
复制
$ go install github.com/reposaur/reposaur/cmd/rsr@latest

脚本安装

代码语言:javascript
复制
$ curl -o- https://raw.githubusercontent.com/reposaur/reposaur/main/install.sh | bash

工具使用

编写自定义策略

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

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

代码语言:javascript
复制
package github.repository

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

代码语言:javascript
复制
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

}

结果如下所示:

代码语言:javascript
复制
violation_default_branch_not_protected {

not protection

}

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

代码语言:javascript
复制
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

}

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

代码语言:javascript
复制
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

}

策略执行

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

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

代码语言:javascript
复制
$ gh api /repos/reposaur/test | rsr exec

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

代码语言:javascript
复制
$ gh api /orgs/reposaur | rsr exec

SARIF报告生成

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

代码语言:javascript
复制
{

  "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

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2022-06-04,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 FreeBuf 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 关于Reposaur
  • 功能介绍
  • 工具安装
    • 源码获取
      • Homebrew安装
        • DEB、ROM和APK包
          • Go安装
            • 脚本安装
            • 工具使用
              • 编写自定义策略
                • 策略执行
                  • SARIF报告生成
                  • 许可证协议
                  • 项目地址
                  • 参考资料
                  相关产品与服务
                  检测工具
                  域名服务检测工具(Detection Tools)提供了全面的智能化域名诊断,包括Whois、DNS生效等特性检测,同时提供SSL证书相关特性检测,保障您的域名和网站健康。
                  领券
                  问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档