天天看點

如何使用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