diff --git a/.githooks/pre-commit b/.githooks/pre-commit new file mode 100755 index 0000000..db265a0 --- /dev/null +++ b/.githooks/pre-commit @@ -0,0 +1,5 @@ +#!/usr/bin/env sh +set -eu + +make fmt-check +make lint diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS new file mode 100644 index 0000000..fcb856c --- /dev/null +++ b/.github/CODEOWNERS @@ -0,0 +1,3 @@ +# Default owner for everything in the repo. +# Order matters: the last matching pattern takes precedence. +* @bomly-guy diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..6b37517 --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,29 @@ +# To get started with Dependabot version updates, you'll need to specify which +# package ecosystems to update and where the package manifests are located. +# Please see the documentation for all configuration options: +# https://docs.github.com/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file + +version: 2 +updates: + - package-ecosystem: "gomod" # See documentation for possible values + directory: "/" # Location of package manifests + schedule: + interval: "weekly" + groups: + gomod-minor: + update-types: + - "minor" + gomod-patch: + update-types: + - "patch" + - package-ecosystem: "github-actions" + directory: "/" + schedule: + interval: "weekly" + groups: + github-actions-minor: + update-types: + - "minor" + github-actions-patch: + update-types: + - "patch" diff --git a/.github/workflows/bomly-guard.yml b/.github/workflows/bomly-guard.yml new file mode 100644 index 0000000..5fcb0b0 --- /dev/null +++ b/.github/workflows/bomly-guard.yml @@ -0,0 +1,28 @@ +name: Bomly Guard + +on: + pull_request: + +permissions: + contents: read + +jobs: + guard: + runs-on: ubuntu-latest + timeout-minutes: 15 + permissions: + contents: read + pull-requests: write + issues: write + security-events: write + steps: + - name: Check out repository + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + fetch-depth: 0 + + - name: Run Bomly Guard + uses: bomly-dev/bomly-guard@3f0869a7d411103210a63c1e64315a48cb2a5c20 # v1.2.0 + with: + fail-on: high + comment-summary-in-pr: always diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index bc766bb..ef39791 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -11,30 +11,65 @@ on: permissions: contents: read +concurrency: + group: ci-${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + jobs: test: + name: Test runs-on: ubuntu-latest steps: - - uses: actions/checkout@v4 - - uses: actions/setup-go@v5 + - name: Check out repository + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + - name: Set up Go + uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 with: go-version-file: go.mod + cache: true + cache-dependency-path: go.sum - name: Test run: go test ./... - name: Vet run: go vet ./... - name: Format + run: make fmt-check + - name: Module tidy drift + run: go mod tidy -diff + - name: Forbid replace directives + # A library must resolve the same way for every consumer; a replace + # directive only applies inside this module and silently vanishes + # when bomly-cli or a plugin imports it. run: | - fmt_out="$(gofmt -l .)" - if [[ -n "$fmt_out" ]]; then - echo "gofmt needed on:" >&2 - echo "$fmt_out" >&2 + set -euo pipefail + replaces="$(go mod edit -json | jq '.Replace // [] | length')" + if [ "${replaces}" != "0" ]; then + echo "go.mod must not contain replace directives" >&2 + go mod edit -json | jq '.Replace' >&2 exit 1 fi - - name: Module tidy drift - run: | - go mod tidy - git diff --exit-code go.mod go.sum + + lint: + name: Lint + runs-on: ubuntu-latest + steps: + - name: Check out repository + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + - name: Set up Go + uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 + with: + go-version-file: go.mod + cache: true + cache-dependency-path: go.sum + - name: Run golangci-lint + uses: golangci/golangci-lint-action@ba0d7d2ec06a0ea1cb5fa41b2e4a3ab91d21278a # v9.3.0 + with: + # Keep in sync with GOLANGCI_LINT_VERSION in the Makefile. + version: v2.13.2 # API-compatibility gate (ADR-0040 program, plan step 0.1): diff the exported # Go API against the latest release tag with gorelease. Incompatible changes @@ -43,6 +78,7 @@ jobs: # Pull requests only: a push run on main has no PR labels, so a just-merged # approved break would fail there until the next tag moves the baseline. api-compat: + name: API compatibility if: github.event_name == 'pull_request' runs-on: ubuntu-latest steps: diff --git a/.github/workflows/dependency-review.yml b/.github/workflows/dependency-review.yml new file mode 100644 index 0000000..05853c6 --- /dev/null +++ b/.github/workflows/dependency-review.yml @@ -0,0 +1,27 @@ +name: Dependency review + +on: + pull_request: + branches: + - main + +permissions: + contents: read + +concurrency: + group: dependency-review-${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +jobs: + review: + name: Dependency review + runs-on: ubuntu-latest + steps: + - name: Check out repository + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + + - name: Review dependencies + uses: actions/dependency-review-action@a1d282b36b6f3519aa1f3fc636f609c47dddb294 # v5.0.0 + with: + fail-on-severity: high + comment-summary-in-pr: on-failure diff --git a/.github/workflows/fuzz.yml b/.github/workflows/fuzz.yml new file mode 100644 index 0000000..a392eb4 --- /dev/null +++ b/.github/workflows/fuzz.yml @@ -0,0 +1,50 @@ +# Scheduled native Go fuzzing over the module's untrusted-input boundaries: +# package URL parsing and canonicalization, SPDX expression classification, +# graph and package-registry JSON, wire payloads, and the origin, contact, +# URL, and external-reference normalizers. Every parser ships its own fuzz +# target (see AGENTS.md), and scripts/run-fuzz.sh discovers them all, so a +# new target is exercised here without registration. Deeper runs happen +# locally via `make fuzz FUZZTIME=...`. +name: Fuzz + +on: + workflow_dispatch: + schedule: + - cron: "0 8 * * *" + +permissions: + contents: read + +concurrency: + group: fuzz-${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +jobs: + fuzz: + name: Native Go fuzzing + runs-on: ubuntu-latest + steps: + - name: Check out repository + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + + - name: Set up Go + uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 + with: + go-version-file: go.mod + cache: true + cache-dependency-path: go.sum + + - name: Run native Go fuzz targets + run: make fuzz FUZZTIME=2m + + - name: Upload fuzz failures + if: failure() + uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 + with: + name: go-fuzz-failures + path: | + **/testdata/fuzz/** + if-no-files-found: ignore + retention-days: 7 diff --git a/.github/workflows/scorecard.yml b/.github/workflows/scorecard.yml new file mode 100644 index 0000000..2e35746 --- /dev/null +++ b/.github/workflows/scorecard.yml @@ -0,0 +1,54 @@ +name: Scorecard + +on: + branch_protection_rule: + push: + branches: + - main + schedule: + # Runs every Monday at 5:00am UTC. Adjust the schedule as needed. + - cron: "0 5 * * 1" + +# Declare default permissions as read only. +permissions: read-all + +concurrency: + group: scorecard-${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +jobs: + analysis: + name: Scorecard analysis + runs-on: ubuntu-latest + # `publish_results: true` only works when run from the default branch. conditional can be removed if disabled. + if: github.event.repository.default_branch == github.ref_name || github.event_name == 'pull_request' + permissions: + # Needed to upload the results to code-scanning dashboard. + security-events: write + # Needed to publish results and get a badge (see publish_results below). + id-token: write + + steps: + - name: Check out repository + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + + - name: Run analysis + uses: ossf/scorecard-action@2d1146689b8cda280b9bc96326124645441f03bc # v2.4.4 + with: + results_file: results.sarif + results_format: sarif + publish_results: true + + - name: Upload artifact + uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 + with: + name: SARIF file + path: results.sarif + retention-days: 5 + + - name: Upload to code-scanning + uses: github/codeql-action/upload-sarif@ff2f1c621b7f889edc0d3c761ac2e6a3f8cdb0dd # v4.31.11 + with: + sarif_file: results.sarif diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 0000000..3b75051 --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,37 @@ +version: "2" +linters: + default: none + enable: + - errcheck + - govet + - ineffassign + - staticcheck + exclusions: + generated: lax + presets: + - comments + - common-false-positives + - legacy + - std-error-handling + rules: + - linters: + - errcheck + path: _test\.go + - linters: + - staticcheck + text: "SA5011" + path: _test\.go + paths: + - third_party$ + - builtin$ + - examples$ +issues: + max-issues-per-linter: 0 + max-same-issues: 0 +formatters: + exclusions: + generated: lax + paths: + - third_party$ + - builtin$ + - examples$ diff --git a/AGENTS.md b/AGENTS.md index 02d2165..5ad6e54 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -111,12 +111,27 @@ branch. ## Build & test ```sh -go test ./... # all tests must pass before work is done -go vet ./... -gofmt -l . # CI gates on formatting -go mod tidy -diff # CI gates on go.mod/go.sum tidiness +make test # go test ./... — all tests must pass before work is done +make vet # go vet ./... +make fmt-check # CI gates on gofmt formatting (make fmt rewrites) +make lint # golangci-lint, pinned in the Makefile and ci.yml +make tidy-check # go mod tidy -diff — CI gates on go.mod/go.sum tidiness +make fuzz FUZZTIME=5s # every Fuzz* target briefly; nightly in CI at 2m each +make install-hooks # pre-commit runs fmt-check and lint ``` +CI also forbids `replace` directives in `go.mod` (a library must resolve the +same way for every consumer) and diffs the exported API against the latest +release tag with gorelease; a deliberate v0 break needs the +`api:break-approved` label. Fuzz targets are discovered by +`scripts/run-fuzz.sh`, not listed, so a new `Fuzz*` function is picked up +by the nightly run without registration. Dependabot (Go modules and +Actions, weekly, grouped), Dependency Review, OpenSSF Scorecard, and Bomly +Guard run as separate workflows under `.github/workflows/`. CodeQL runs +through GitHub's default code-scanning setup (Go and Actions), which is a +repository setting rather than a workflow file; GitHub rejects an advanced +CodeQL workflow while default setup is enabled, so do not add one. + The `conformance` package is the reusable plugin-contract suite; changes to descriptors, validation, or the serve surface must keep it green, and the CLI's `TestExamplePluginFixtureCompiles` compiles against the released SDK — diff --git a/CLAUDE.md b/CLAUDE.md index 02d2165..5ad6e54 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -111,12 +111,27 @@ branch. ## Build & test ```sh -go test ./... # all tests must pass before work is done -go vet ./... -gofmt -l . # CI gates on formatting -go mod tidy -diff # CI gates on go.mod/go.sum tidiness +make test # go test ./... — all tests must pass before work is done +make vet # go vet ./... +make fmt-check # CI gates on gofmt formatting (make fmt rewrites) +make lint # golangci-lint, pinned in the Makefile and ci.yml +make tidy-check # go mod tidy -diff — CI gates on go.mod/go.sum tidiness +make fuzz FUZZTIME=5s # every Fuzz* target briefly; nightly in CI at 2m each +make install-hooks # pre-commit runs fmt-check and lint ``` +CI also forbids `replace` directives in `go.mod` (a library must resolve the +same way for every consumer) and diffs the exported API against the latest +release tag with gorelease; a deliberate v0 break needs the +`api:break-approved` label. Fuzz targets are discovered by +`scripts/run-fuzz.sh`, not listed, so a new `Fuzz*` function is picked up +by the nightly run without registration. Dependabot (Go modules and +Actions, weekly, grouped), Dependency Review, OpenSSF Scorecard, and Bomly +Guard run as separate workflows under `.github/workflows/`. CodeQL runs +through GitHub's default code-scanning setup (Go and Actions), which is a +repository setting rather than a workflow file; GitHub rejects an advanced +CodeQL workflow while default setup is enabled, so do not add one. + The `conformance` package is the reusable plugin-contract suite; changes to descriptors, validation, or the serve surface must keep it green, and the CLI's `TestExamplePluginFixtureCompiles` compiles against the released SDK — diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..e4c3f10 --- /dev/null +++ b/Makefile @@ -0,0 +1,43 @@ +# Keep GOLANGCI_LINT_VERSION in sync with the golangci-lint-action `version` +# input in .github/workflows/ci.yml. +GOLANGCI_LINT_VERSION=v2.13.2 +GOPATH_BIN=$(shell go env GOPATH)/bin +EXE_SUFFIX=$(if $(filter Windows_NT,$(OS)),.exe,) +GOLANGCI_LINT=$(GOPATH_BIN)/golangci-lint$(EXE_SUFFIX) +FUZZTIME?=60s + +.PHONY: test vet fmt fmt-check lint tidy-check fuzz install-hooks + +test: + go test ./... + +vet: + go vet ./... + +fmt: + git ls-files -z '*.go' | xargs -0 gofmt -w + +# Fails, listing the offenders, when any tracked Go file is not gofmt-clean. +# `gofmt -l` exits 0 even when it lists files, so the check is on its output. +fmt-check: + @unformatted="$$(git ls-files -z '*.go' | xargs -0 gofmt -l)"; \ + if [ -n "$$unformatted" ]; then \ + echo "gofmt needed on:" >&2; \ + echo "$$unformatted" >&2; \ + exit 1; \ + fi + +$(GOLANGCI_LINT): Makefile + go install github.com/golangci/golangci-lint/v2/cmd/golangci-lint@$(GOLANGCI_LINT_VERSION) + +lint: $(GOLANGCI_LINT) + $(GOLANGCI_LINT) run + +tidy-check: + go mod tidy -diff + +fuzz: + FUZZTIME="$(FUZZTIME)" scripts/run-fuzz.sh + +install-hooks: + git config core.hooksPath .githooks diff --git a/README.md b/README.md index 800e596..799e565 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,13 @@ # Bomly SDK +
+ `github.com/bomly-dev/bomly-sdk` is the contract module for building Bomly components: detectors, matchers, auditors, and analyzers. It contains the neutral domain types (dependencies, packages, vulnerabilities, findings, the diff --git a/contact.go b/contact.go index c098abf..4975f54 100644 --- a/contact.go +++ b/contact.go @@ -299,7 +299,7 @@ func ParseSPDXContact(value string) (Contact, bool) { if strings.EqualFold(trimmed, spdxNoAssertion) { return Contact{Kind: ContactKindNoAssertion}, true } - kind := ContactKindUnknown + var kind ContactKind switch { case hasCaseInsensitivePrefix(trimmed, "Organization:"): kind = ContactKindOrganization diff --git a/scripts/run-fuzz.sh b/scripts/run-fuzz.sh new file mode 100755 index 0000000..0a96062 --- /dev/null +++ b/scripts/run-fuzz.sh @@ -0,0 +1,20 @@ +#!/usr/bin/env bash +# Runs every native Go fuzz target in the module for FUZZTIME each. +# +# Targets are discovered, not listed: AGENTS.md requires every parser of +# untrusted input to ship a fuzz target from its first commit, and a +# hand-maintained list is exactly the place such a target gets forgotten. +# `go test -list` prints test names only, so it is cheap to run per package. +set -euo pipefail + +FUZZTIME="${FUZZTIME:-60s}" + +cd "$(dirname "${BASH_SOURCE[0]}")/.." + +for package in $(go list ./...); do + targets="$(go test "${package}" -run='^$' -list='^Fuzz' | grep '^Fuzz' || true)" + for fuzz in ${targets}; do + echo "==> go test ${package} -run=^\$ -fuzz=^${fuzz}\$ -fuzztime=${FUZZTIME}" + go test "${package}" -run='^$' -fuzz="^${fuzz}\$" -fuzztime="${FUZZTIME}" + done +done diff --git a/spdxkit/fuzz_test.go b/spdxkit/fuzz_test.go index 956ec14..896d4fb 100644 --- a/spdxkit/fuzz_test.go +++ b/spdxkit/fuzz_test.go @@ -44,7 +44,9 @@ func FuzzValid(f *testing.F) { if len(value) > maxFuzzInputSize { t.Skip("input exceeds fuzz bound") } - if Valid(value) != Valid(value) { + first := Valid(value) + second := Valid(value) + if first != second { t.Fatal("Valid is not deterministic") } if ok, err := Satisfies(value, []string{"MIT"}); err == nil { diff --git a/vocabulary_registry_test.go b/vocabulary_registry_test.go index f5e7c76..3f176a0 100644 --- a/vocabulary_registry_test.go +++ b/vocabulary_registry_test.go @@ -32,7 +32,11 @@ func declaredConstants(t *testing.T, importPath, typeName string) map[string]str t.Helper() dir := packageDir(t, importPath) fset := token.NewFileSet() - pkgs, err := parser.ParseDir(fset, dir, func(info fs.FileInfo) bool { + // ParseDir is deprecated in favor of golang.org/x/tools/go/packages + // because it ignores build tags. The two vocabulary packages read here + // carry no build-tagged files, and go/packages would add a dependency + // the module does not otherwise need. + pkgs, err := parser.ParseDir(fset, dir, func(info fs.FileInfo) bool { //nolint:staticcheck // see above return !strings.HasSuffix(info.Name(), "_test.go") }, 0) if err != nil {