Skip to content

Commit fcf0ac4

Browse files
Merge pull request #119 from naveensrinivasan/feature/protected-branches
feature - Checks for branch protections
2 parents b506c6f + 3191c55 commit fcf0ac4

File tree

4 files changed

+570
-25
lines changed

4 files changed

+570
-25
lines changed

README.md

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
A short motivational video clip to inspire us: https://youtu.be/rDMMYT3vkTk "You passed! All D's ... and an A!"
88

99
## Goals
10-
1. Automate analysis and trust decisions on the security posture of open source projects.
10+
11+
1. Automate analysis and trust decisions on the security posture of open source projects.
1112

1213
1. Use this data to proactively improve the security posture of the critical projects the world depends on.
1314

@@ -42,6 +43,7 @@ The program only requires one argument to run, the name of the repo:
4243
$ go build
4344
$ ./scorecard --repo=github.com/kubernetes/kubernetes
4445
Starting [Active]
46+
Starting [Branch-Protection]
4547
Starting [CI-Tests]
4648
Starting [CII-Best-Practices]
4749
Starting [Code-Review]
@@ -59,6 +61,7 @@ Finished [Frozen-Deps]
5961
Finished [Security-Policy]
6062
Finished [Contributors]
6163
Finished [Signed-Releases]
64+
Finished [Branch-Protection]
6265
Finished [Signed-Tags]
6366
Finished [CI-Tests]
6467
Finished [SAST]
@@ -69,6 +72,7 @@ Finished [Active]
6972
RESULTS
7073
-------
7174
Active: Pass 10
75+
Branch-Protection: Fail 5
7276
CI-Tests: Pass 10
7377
CII-Best-Practices: Pass 10
7478
Code-Review: Pass 10
@@ -116,27 +120,29 @@ These can be obtained from the GitHub [developer settings](https://github.com/se
116120

117121
The following checks are all run against the target project:
118122

119-
| Name | Description |
120-
|---|---|
121-
| Security-MD | Does the project contain a [security policy](https://docs.github.com/en/free-pro-team@latest/github/managing-security-vulnerabilities/adding-a-security-policy-to-your-repository)? |
122-
| Contributors | Does the project have contributors from at least two different organizations? |
123-
| Frozen-Deps | Does the project declare and freeze [dependencies](https://docs.github.com/en/free-pro-team@latest/github/visualizing-repository-data-with-graphs/about-the-dependency-graph#supported-package-ecosystems)? |
124-
| Signed-Releases | Does the project cryptographically [sign releases](https://wiki.debian.org/Creating%20signed%20GitHub%20releases)? |
125-
| Signed-Tags | Does the project cryptographically sign release tags? |
126-
| CI-Tests | Does the project run tests in CI, e.g. [GitHub Actions](https://docs.github.com/en/free-pro-team@latest/actions), [Prow](https://github.com/kubernetes/test-infra/tree/master/prow)? |
127-
| Code-Review | Does the project require code review before code is merged? |
128-
| CII-Best-Practices | Does the project have a [CII Best Practices Badge](https://bestpractices.coreinfrastructure.org/en)? |
129-
| Pull-Requests | Does the project use [Pull Requests](https://docs.github.com/en/free-pro-team@latest/github/collaborating-with-issues-and-pull-requests/about-pull-requests) for all code changes? |
130-
| Fuzzing | Does the project use fuzzing tools, e.g. [OSS-Fuzz](https://github.com/google/oss-fuzz)? |
131-
| SAST | Does the project use static code analysis tools, e.g. [CodeQL](https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/enabling-code-scanning-for-a-repository#enabling-code-scanning-using-actions), [SonarCloud](https://sonarcloud.io)? |
132-
| Active | Did the project get any commits in the last 90 days? |
123+
| Name | Description |
124+
| ------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
125+
| Security-MD | Does the project contain a [security policy](https://docs.github.com/en/free-pro-team@latest/github/managing-security-vulnerabilities/adding-a-security-policy-to-your-repository)? |
126+
| Contributors | Does the project have contributors from at least two different organizations? |
127+
| Frozen-Deps | Does the project declare and freeze [dependencies](https://docs.github.com/en/free-pro-team@latest/github/visualizing-repository-data-with-graphs/about-the-dependency-graph#supported-package-ecosystems)? |
128+
| Signed-Releases | Does the project cryptographically [sign releases](https://wiki.debian.org/Creating%20signed%20GitHub%20releases)? |
129+
| Signed-Tags | Does the project cryptographically sign release tags? |
130+
| CI-Tests | Does the project run tests in CI, e.g. [GitHub Actions](https://docs.github.com/en/free-pro-team@latest/actions), [Prow](https://github.com/kubernetes/test-infra/tree/master/prow)? |
131+
| Code-Review | Does the project require code review before code is merged? |
132+
| CII-Best-Practices | Does the project have a [CII Best Practices Badge](https://bestpractices.coreinfrastructure.org/en)? |
133+
| Pull-Requests | Does the project use [Pull Requests](https://docs.github.com/en/free-pro-team@latest/github/collaborating-with-issues-and-pull-requests/about-pull-requests) for all code changes? |
134+
| Fuzzing | Does the project use fuzzing tools, e.g. [OSS-Fuzz](https://github.com/google/oss-fuzz)? |
135+
| SAST | Does the project use static code analysis tools, e.g. [CodeQL](https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/enabling-code-scanning-for-a-repository#enabling-code-scanning-using-actions), [SonarCloud](https://sonarcloud.io)? |
136+
| Active | Did the project get any commits in the last 90 days? |
137+
| Branch-Protection | Does the project use [Branch Protection](https://docs.github.com/en/free-pro-team@latest/github/administering-a-repository/about-protected-branches) ? |
133138

134139
To see detailed information on how each check works, see the [check-specific documentation page](checks.md).
135140

136141
If you'd like to add a check, make sure it is something that meets the following criteria:
137-
* automate-able
138-
* objective
139-
* actionable
142+
143+
- automate-able
144+
- objective
145+
- actionable
140146

141147
and then create a new GitHub Issue.
142148

@@ -163,15 +169,17 @@ There are three formats currently: `default`, `json`, and `csv`. Others may be a
163169
These may be specified with the `--format` flag.
164170

165171
## Requirements
166-
* The scorecard must only be composed of automate-able, objective data. For example, a project having 10 contributors doesn’t necessarily mean it’s more secure than a project with say 50 contributors. But, having two maintainers might be preferable to only having one - the larger bus factor and ability to provide code reviews is objectively better.
167-
* The scorecard criteria can be as specific as possible and not limited general recommendations. For example, for Go, we can recommend/require specific linters and analyzers to be run on the codebase.
168-
* The scorecard can be populated for any open source project without any work or interaction from maintainers.
169-
* Maintainers must be provided with a mechanism to correct any automated scorecard findings they feel were made in error, provide "hints" for anything we can't detect automatically, and even dispute the applicability of a given scorecard finding for that repository.
170-
* Any criteria in the scorecard must be actionable. It should be possible, with help, for any project to "check all the boxes".
171-
* Any solution to compile a scorecard should be usable by the greater open source community to monitor upstream security.
172+
173+
- The scorecard must only be composed of automate-able, objective data. For example, a project having 10 contributors doesn’t necessarily mean it’s more secure than a project with say 50 contributors. But, having two maintainers might be preferable to only having one - the larger bus factor and ability to provide code reviews is objectively better.
174+
- The scorecard criteria can be as specific as possible and not limited general recommendations. For example, for Go, we can recommend/require specific linters and analyzers to be run on the codebase.
175+
- The scorecard can be populated for any open source project without any work or interaction from maintainers.
176+
- Maintainers must be provided with a mechanism to correct any automated scorecard findings they feel were made in error, provide "hints" for anything we can't detect automatically, and even dispute the applicability of a given scorecard finding for that repository.
177+
- Any criteria in the scorecard must be actionable. It should be possible, with help, for any project to "check all the boxes".
178+
- Any solution to compile a scorecard should be usable by the greater open source community to monitor upstream security.
172179

173180
## Docker
174-
* The Dockerfile in the root directory utilizes [experimental features](https://github.com/moby/buildkit/blob/master/frontend/dockerfile/docs/syntax.md) which is available in Docker v18.09 or later.
181+
182+
- The Dockerfile in the root directory utilizes [experimental features](https://github.com/moby/buildkit/blob/master/frontend/dockerfile/docs/syntax.md) which is available in Docker v18.09 or later.
175183

176184
## Contributing
177185

checks.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,7 @@ It currently works by looking for well-known results ([CodeQL](https://securityl
6464

6565
This check tries to determine if the project is still "actively maintained".
6666
It currently works by looking for releases or commits within the last 90 days.
67+
68+
## Branch-Protection
69+
70+
This check tries to determine if the project has branch protection enabled.

checks/branch_protected.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// Copyright 2020 Security Scorecard Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package checks
16+
17+
import (
18+
"github.com/google/go-github/v32/github"
19+
"github.com/ossf/scorecard/checker"
20+
)
21+
22+
func init() {
23+
registerCheck("Branch-Protection", BranchProtection)
24+
}
25+
26+
func BranchProtection(c checker.Checker) checker.CheckResult {
27+
repo, _, err := c.Client.Repositories.Get(c.Ctx, c.Owner, c.Repo)
28+
if err != nil {
29+
return checker.RetryResult(err)
30+
}
31+
32+
protection, _, err := c.Client.Repositories.
33+
GetBranchProtection(c.Ctx, c.Owner, c.Repo, *repo.DefaultBranch)
34+
35+
if err != nil {
36+
c.Logf("!! branch protection not enabled")
37+
return checker.CheckResult{
38+
Pass: false,
39+
Confidence: 10,
40+
}
41+
}
42+
return IsBranchProtected(protection, c)
43+
44+
}
45+
46+
func IsBranchProtected(protection *github.Protection, c checker.Checker) checker.CheckResult {
47+
totalChecks := 6
48+
totalSuccess := 0
49+
50+
if protection.AllowForcePushes.Enabled {
51+
c.Logf("!! branch protection AllowForcePushes enabled")
52+
} else {
53+
totalSuccess++
54+
}
55+
56+
if protection.AllowDeletions.Enabled {
57+
c.Logf("!! branch protection AllowDeletions enabled")
58+
} else {
59+
totalSuccess++
60+
}
61+
62+
if !protection.EnforceAdmins.Enabled {
63+
c.Logf("!! branch protection EnforceAdmins not enabled")
64+
} else {
65+
totalSuccess++
66+
}
67+
68+
if !protection.RequireLinearHistory.Enabled {
69+
c.Logf("!! branch protection require linear history not enabled")
70+
} else {
71+
totalSuccess++
72+
}
73+
74+
if !protection.RequiredStatusChecks.Strict {
75+
c.Logf("!! branch protection require status checks to pass before merging not enabled")
76+
} else {
77+
if len(protection.RequiredStatusChecks.Contexts) == 0 {
78+
c.Logf("!! branch protection require status checks to pass before merging has no specific status to check for")
79+
} else {
80+
totalSuccess++
81+
}
82+
}
83+
84+
if protection.RequiredPullRequestReviews.RequiredApprovingReviewCount < 1 {
85+
c.Logf("!! branch protection require pullrequest before merging not enabled")
86+
} else {
87+
totalSuccess++
88+
}
89+
90+
return checker.ProportionalResult(totalSuccess, totalChecks, 1.0)
91+
}

0 commit comments

Comments
 (0)