如何为OpenSSF Scorecard添加自定义安全检查:完整指南

张开发
2026/4/9 15:52:07 15 分钟阅读

分享文章

如何为OpenSSF Scorecard添加自定义安全检查:完整指南
如何为OpenSSF Scorecard添加自定义安全检查完整指南【免费下载链接】scorecardOpenSSF Scorecard - Security health metrics for Open Source项目地址: https://gitcode.com/gh_mirrors/sc/scorecardOpenSSF Scorecard是一个强大的开源安全健康度评估工具它通过自动化的安全检查来评估开源项目的安全实践。本文将为你提供详细的指南教你如何为Scorecard添加自定义安全检查扩展其安全评估能力。什么是OpenSSF ScorecardOpenSSF Scorecard是一个开源项目安全健康度评估工具它通过一系列自动化检查来评估开源项目的安全实践。Scorecard的核心是探针probe系统每个探针都是一个独立的启发式检查用于评估项目的特定安全行为。Scorecard探针系统架构Scorecard的探针系统设计得非常模块化每个探针都包含三个核心文件def.yml- 探针的定义文档impl.go- 探针的实际实现impl_test.go- 探针的测试代码探针目录结构每个探针都位于probes/目录下的独立子目录中例如probes/hasBinaryArtifacts/- 检查项目中是否包含二进制文件probes/branchesAreProtected/- 检查分支保护设置probes/hasOSVVulnerabilities/- 检查OSV漏洞创建自定义安全检查的步骤第一步确定安全检查需求在开始编码之前首先需要明确你的安全检查需求。思考以下问题你要检查什么安全实践这个检查的结果应该是布尔值吗需要哪些数据源来执行这个检查第二步创建探针目录在你的本地Scorecard仓库中创建一个新的探针目录cd /path/to/scorecard mkdir -p probes/myCustomCheck第三步编写探针定义文件def.yml创建probes/myCustomCheck/def.yml文件定义探针的基本信息id: myCustomCheck lifecycle: experimental short: Checks if the project has implemented custom security practice. motivation: This check ensures that projects follow our custom security practice to enhance overall security posture. implementation: The implementation analyzes project configuration files and source code to determine if the custom security practice is properly implemented. outcome: - If the practice is implemented correctly, returns OutcomeTrue. - If the practice is not implemented, returns OutcomeFalse. - If insufficient data is available, returns OutcomeNotAvailable. remediation: onOutcome: False effort: Medium text: - Implement the custom security practice by following the documentation. - Update project configuration files accordingly. ecosystem: languages: - all clients: - github - gitlab第四步实现探针逻辑impl.go创建probes/myCustomCheck/impl.go文件实现探针的核心逻辑package myCustomCheck import ( embed fmt github.com/ossf/scorecard/v5/checker github.com/ossf/scorecard/v5/finding github.com/ossf/scorecard/v5/internal/checknames github.com/ossf/scorecard/v5/internal/probes github.com/ossf/scorecard/v5/probes/internal/utils/uerror ) func init() { probes.MustRegister(Probe, Run, []checknames.CheckName{checknames.CustomCheck}) } //go:embed *.yml var fs embed.FS const Probe myCustomCheck func Run(raw *checker.RawResults) ([]finding.Finding, string, error) { if raw nil { return nil, , fmt.Errorf(%w: raw, uerror.ErrNil) } // 实现你的自定义检查逻辑 // 使用raw中的数据进行分析 // 示例检查是否满足条件 if customConditionMet { f, err : finding.NewWith(fs, Probe, Project implements the custom security practice., nil, finding.OutcomeTrue) if err ! nil { return nil, Probe, err } return []finding.Finding{*f}, Probe, nil } else { f, err : finding.NewWith(fs, Probe, Project does not implement the custom security practice., nil, finding.OutcomeFalse) if err ! nil { return nil, Probe, err } return []finding.Finding{*f}, Probe, nil } }第五步编写测试代码impl_test.go创建probes/myCustomCheck/impl_test.go文件为你的探针编写测试package myCustomCheck import ( testing github.com/ossf/scorecard/v5/checker github.com/ossf/scorecard/v5/finding ) func TestRun(t *testing.T) { t.Parallel() tests : []struct { name string raw *checker.RawResults want []finding.Finding wantErr bool }{ { name: Test when condition is met, raw: checker.RawResults{}, want: []finding.Finding{ { Probe: Probe, Outcome: finding.OutcomeTrue, }, }, wantErr: false, }, // 添加更多测试用例 } for _, tt : range tests { tt : tt t.Run(tt.name, func(t *testing.T) { t.Parallel() got, _, err : Run(tt.raw) // 断言和验证 }) } }第六步注册探针在probes/entries.go文件中注册你的新探针import ( // ... 其他导入 github.com/ossf/scorecard/v5/probes/myCustomCheck ) // 在init函数或适当的位置注册 func init() { // 确保探针被正确注册 }探针生命周期管理Scorecard探针支持三种生命周期状态Experimental- 探针语义可能变化无稳定性保证Stable- 探针行为和语义不会改变Deprecated- 探针不再支持不应期望维护最佳实践和注意事项1. 代码复用当多个探针使用相同的代码时可以将共享代码放在probes/internal/目录下。2. 动态内容显示探针定义支持动态内容显示。在def.yml中使用${{ metadata.variableName }}语法在impl.go中设置相应的元数据。3. 错误处理确保你的探针能够正确处理各种边界情况和错误条件。4. 测试覆盖率为你的探针编写全面的测试包括正例、反例和边界情况。5. 文档更新更新相关文档确保其他开发者了解你的新探针。实际示例分支保护检查让我们看一个实际的探针示例 -branchesAreProtected探针def.yml文件定义了探针的目的和行为id: branchesAreProtected lifecycle: stable short: Checks if the projects default branch is protected. motivation: Branch protection prevents force pushes and deletion of branches, which can help prevent accidental or malicious changes.impl.go实现了检查逻辑func Run(raw *checker.RawResults) ([]finding.Finding, string, error) { // 检查分支保护设置 if raw.BranchProtectionResults.DefaultBranch ! nil raw.BranchProtectionResults.DefaultBranch.Protected { return finding.OutcomeTrue } return finding.OutcomeFalse }调试和验证1. 本地测试使用Scorecard的测试框架验证你的探针go test ./probes/myCustomCheck/...2. 集成测试确保你的探针与其他Scorecard组件正确集成。3. 性能考虑考虑你的探针对性能的影响特别是当处理大型仓库时。贡献流程Fork仓库从 https://gitcode.com/gh_mirrors/sc/scorecard fork仓库创建分支为你的功能创建特性分支实现探针按照上述步骤创建自定义安全检查编写测试确保有足够的测试覆盖率提交PR提交拉取请求并等待审查响应反馈根据审查意见进行修改常见问题解答Q: 如何确定是否需要创建新的探针A: 浏览Scorecard的GitHub issues是找到新探针需求的最佳方式。对新工具、模糊测试引擎或其他启发式检查的请求通常可以转换为特定的探针。Q: 探针可以检查哪些类型的安全实践A: 探针可以检查各种安全实践包括但不限于代码审查要求依赖项管理漏洞管理构建和部署安全访问控制和权限管理Q: 如何确保探针的准确性A: 通过全面的测试用例、边界条件测试和实际项目验证来确保探针的准确性。总结为OpenSSF Scorecard添加自定义安全检查是一个强大的方式来扩展其安全评估能力。通过理解探针系统的架构和遵循本文提供的步骤你可以创建针对特定安全需求的自定义检查。记住好的探针应该是明确有清晰的检查目标和范围可测试有全面的测试覆盖文档完善有清晰的文档说明性能友好对系统性能影响最小开始为你的组织或社区创建自定义安全检查吧 通过扩展Scorecard的能力你可以帮助更多开源项目提升安全水平构建更安全的开源生态系统。【免费下载链接】scorecardOpenSSF Scorecard - Security health metrics for Open Source项目地址: https://gitcode.com/gh_mirrors/sc/scorecard创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

更多文章