diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index 5baff51..25896f3 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -1,6 +1,7 @@ name: Check on: push: + branches: [main] pull_request: workflow_dispatch: permissions: @@ -18,8 +19,8 @@ jobs: steps: - name: Use canonical runner temporary directory run: echo "TMPDIR=$RUNNER_TEMP" >> "$GITHUB_ENV" - - uses: actions/checkout@v7.0.1 - - uses: actions/setup-go@v7.0.0 + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 with: go-version: 1.27.4 cache: false @@ -31,8 +32,8 @@ jobs: runs-on: ubuntu-latest timeout-minutes: 15 steps: - - uses: actions/checkout@v7.0.1 - - uses: actions/setup-go@v7.0.0 + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 with: go-version: 1.27.4 cache: false @@ -76,7 +77,7 @@ jobs: PYTHON - name: Upload proof receipts if: always() - uses: actions/upload-artifact@v7.0.2 + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 with: name: proof-receipts path: | @@ -91,21 +92,21 @@ jobs: env: TRUSTED_REVIEW_POLICY_JSON: ${{ vars.PDD_REVIEW_POLICY }} steps: - - uses: actions/checkout@v7.0.1 + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: path: target - name: Checkout independently pinned verifier orchestration - uses: actions/checkout@v7.0.1 + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: - ref: 148f1398b2703a7724a44a43096e42f4875a8d74 + ref: 4fe69675096a9e270ca3b97b8563d52a7c045af0 path: trusted-tooling - - uses: actions/setup-go@v7.0.0 + - uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 with: go-version: 1.27.4 cache: false - name: Build trusted CLI working-directory: trusted-tooling - run: go build -ldflags '-X main.version=148f139-policy-gate' -o ../pdd-trusted ./cmd/pdd + run: go build -ldflags '-X main.version=v0.2.0-policy-gate' -o ../pdd-trusted ./cmd/pdd - name: Read public authority policy from independently managed repository variable run: | python3 - <<'PYTHON' @@ -133,7 +134,7 @@ jobs: exit 1 fi done - - uses: actions/upload-artifact@v7.0.2 + - uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 if: always() with: name: authenticated-gate-evidence diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 05e9a2a..891304f 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -16,8 +16,8 @@ jobs: RELEASE_VERSION: ${{ inputs.version }} TRUSTED_REVIEW_POLICY_JSON: ${{ vars.PDD_REVIEW_POLICY }} steps: - - uses: actions/checkout@v7.0.1 - - uses: actions/setup-go@v7.0.0 + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 with: go-version: 1.27.4 cache: false @@ -41,7 +41,7 @@ jobs: (pathlib.Path(os.environ['RUNNER_TEMP']) / 'review-policy.json').write_text(json.dumps(policy)) PYTHON python3 scripts/smoke.py --pdd "dist/extracted/pdd-kit-go_${RELEASE_VERSION}_linux_amd64/pdd" --version "$RELEASE_VERSION" --policy "$RUNNER_TEMP/review-policy.json" - - uses: actions/upload-artifact@v7.0.2 + - uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 with: name: pdd-kit-go-${{ inputs.version }} path: dist/first/ diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..3630462 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,28 @@ +# Changelog + +## v0.2.1 + +- Prevent `status` from accepting historical success while another project + operation is active; honor status cancellation and recheck authorization after + input/log inspection. +- Reject unsigned approval/import operations that would replace a signed review, + preserving the active record on failure. Authorized signed renewal still works. +- Reassess saved Gobra and named Go-test output before accepting successful + receipts. Require the exact targeted postcondition failure for every killed + mutant; reject missing/truncated logs and contradictory verifier summaries. +- Clean up descendants when a check parent exits, including detached children. + Windows uses a kill-on-close Job Object and registers a suspended root before + allowing user code to run; unsupported containment fails closed. +- Require the final successful package event as well as each named test pass; + incomplete Go JSON cannot establish success. +- Pin CI action dependencies to immutable commit SHAs and verify feature branches + through pull requests, avoiding duplicate push/PR proof runs. +- Preserve separate diagnostic stderr while reassessing Go JSON evidence. +- Report the installed module version for `go install ...@v0.2.1` and provide + persistent, checksummed release binaries with reproducible build metadata. +- Update the independent signed CI gate to the previously qualified v0.2.0 tooling + commit. New orchestration is tested separately before promotion. + +Supported proof language and host/trust boundaries remain explicit. Receipts are +local unsigned records; these checks do not turn them into proof certificates or +establish long-term deployment history. diff --git a/README.md b/README.md index deef168..d64fc4d 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,9 @@ See [authenticated review gates](docs/signed-reviews.md) and [production qualification](docs/production-readiness.md) for signed authority, native platform checks and repeatable failure/recovery testing. +Install the tagged CLI with `go install github.com/cruskell/pdd-kit-go/cmd/pdd@v0.2.1`, +or download checksummed binaries from [Releases](https://github.com/cruskell/pdd-kit-go/releases). + ## Quick start The CLI uses only Go's standard library. Build with Go 1.20+ on a 64-bit host. Formal checks additionally require Docker and the pinned Gobra image, or a local Gobra jar with Java and Z3 configured. @@ -119,7 +122,7 @@ There is no automatic fallback from failed formal verification to a green test r ## Receipts and trust -Verification and approval are protected by an OS-backed project lock, automatically released when the owning process exits. Concurrent runs are rejected. `.pdd/run.json` identifies the newest run; an interrupted or incomplete newest run cannot fall back to an older success. SIGINT/SIGTERM cancellation and per-check timeouts terminate child process groups on supported Unix hosts, and the Docker backend removes its verifier container. A canceled run records negative evidence. +Verification and approval are protected by an OS-backed project lock, automatically released when the owning process exits. Concurrent proof, approval and status operations are rejected. `.pdd/run.json` identifies the newest run; an interrupted or incomplete newest run cannot fall back to an older success. SIGINT/SIGTERM cancellation and per-check timeouts terminate child process groups on supported Unix hosts, and the Docker backend removes its verifier container. A canceled run records negative evidence. Default-plan artifacts live under `.pdd/receipts//` (named plans use `.pdd/state/NAME/receipts/`): `receipt.json`, `receipt.md`, `inputs.json` and per-check logs. Receipts include source/plan SHA-256 digests, reviewer assertion, commit (when present), tool identities, commands, durations, outcomes, mutation results, trust assumptions, exclusions and gaps. They are ignored by Git; CI uploads them as artifacts. diff --git a/cmd/pdd/main.go b/cmd/pdd/main.go index 6a95b13..0eedc2b 100644 --- a/cmd/pdd/main.go +++ b/cmd/pdd/main.go @@ -1,10 +1,23 @@ package main import ( - "github.com/cruskell/pdd-kit-go/internal/cli" "os" + "runtime/debug" + + "github.com/cruskell/pdd-kit-go/internal/cli" ) -var version = "0.2.0-dev" +// Set by release builds. go install @version obtains the module version below. +var version string + +func buildVersion() string { + if version != "" { + return version + } + if info, ok := debug.ReadBuildInfo(); ok && info.Main.Version != "" && info.Main.Version != "(devel)" { + return info.Main.Version + } + return "0.2.1-dev" +} -func main() { os.Exit(cli.Run(os.Args[1:], os.Stdout, os.Stderr, version)) } +func main() { os.Exit(cli.Run(os.Args[1:], os.Stdout, os.Stderr, buildVersion())) } diff --git a/docs/distribution.md b/docs/distribution.md index e14cb99..6eaf8ba 100644 --- a/docs/distribution.md +++ b/docs/distribution.md @@ -1,5 +1,17 @@ # Install and reproduce a release +Versioned binaries and checksums are published on [GitHub Releases](https://github.com/cruskell/pdd-kit-go/releases). +Go users can install the tagged CLI directly: + +```sh +go install github.com/cruskell/pdd-kit-go/cmd/pdd@v0.2.1 +pdd --version +``` + +The installed version comes from Go module build information. Release archives +embed the same version explicitly; local checkout builds show a development +version. Verification still needs the Go toolchain and the pinned Gobra backend. + The CLI has no third-party Go dependencies. Development builds need Go 1.20+: ```sh @@ -12,10 +24,10 @@ Release builds use the exact Go 1.27.4 toolchain. Fetch it from From a clean checkout of the release commit, with Python 3 available: ```sh -GO=/path/to/go1.27.4/bin/go python3 scripts/release.py v0.1.0 +GO=/path/to/go1.27.4/bin/go python3 scripts/release.py v0.2.1 ``` -The script produces five archives under `dist/v0.1.0`: Linux amd64/arm64, +The script produces five archives under `dist/v0.2.1`: Linux amd64/arm64, macOS amd64/arm64 and Windows amd64, plus `BUILD.json` and `SHA256SUMS`. It disables CGO, VCS stamping and local file paths, embeds the requested CLI version, records the commit/toolchain/flags, and normalizes archive metadata. @@ -55,10 +67,10 @@ example reviews, run actual proofs and HTTP tests, kill mutants and check stale source handling. It needs Go and Docker with the pinned image already pulled: ```sh -python3 scripts/smoke.py --pdd /absolute/path/to/pdd --version v0.1.0 +python3 scripts/smoke.py --pdd /absolute/path/to/pdd --version v0.2.1 ``` The [release workflow](../.github/workflows/release.yml) builds and validates -artifacts on manual dispatch. It uploads CI artifacts and does not publish a -release or change repository access. A maintainer can attach the verified output -to a versioned GitHub release after reviewing its checks. +artifacts on manual dispatch. It uploads CI artifacts without publishing automatically. Validated output can be +attached to a versioned GitHub release after the source commit passes required +checks. Release publication never changes repository access. diff --git a/docs/inputs.md b/docs/inputs.md index 7b3efc9..1a4a092 100644 --- a/docs/inputs.md +++ b/docs/inputs.md @@ -47,3 +47,12 @@ an older green receipt. JSON artifacts are independently limited to 8 MiB. Receipts made before the v2 input manifest must be regenerated with `prove`. Existing contract approvals remain usable when their review digest still matches; the input algorithm change alone does not approve new intent. + +`status` takes the same exclusive project lock as proof and approval operations. +It refuses a concurrent operation instead of returning a historical green result. +Its input capture responds to cancellation, and it rechecks policy authorization +and expiration after inspecting inputs and logs. Successful receipts must retain +valid baseline logs (including every named Go test) and the exact targeted mutant +failure logs. Missing or truncated mutant evidence cannot establish success. +These checks detect inconsistent local records; receipts remain unsigned and +cannot authenticate their own execution against a malicious project owner. diff --git a/docs/production-readiness.md b/docs/production-readiness.md index 1f80fbe..a9211c3 100644 --- a/docs/production-readiness.md +++ b/docs/production-readiness.md @@ -49,3 +49,29 @@ and the signed-authority job. The current authenticated gate covers both the permission-aware slice core and the HTTP wallet with three repeated proofs per plan. Consult the current commit's run rather than treating this historical run as certification of subsequent changes. + +## v0.2.1 integrity audit + +`internal/pdd/integrity_test.go` reproduces and prevents concurrent-status reuse, +unsigned replacement of a signed approval, and successful receipt metadata whose +logs do not support the named tests/proofs or targeted mutation rejection. It also +covers canceled status, contradictory verifier summaries and truncated mutant +output. Diagnostic stderr remains separate from Go JSON events when saved evidence +is reassessed. Signed renewal remains supported. The real Gobra integration suite +and native platform jobs exercise these checks with the shipped examples. + +The public repository's main branch requires proof, authenticated-gate and the +three native test jobs, including for administrators; force pushes and deletion +are disabled. This enforces the configured checks, not a claim of long-term +production history or support for every possible Go library. + +Process qualification also covers a parent that exits while descendants retain its +pipes, and one whose descendants have detached their output. Unix cleanup runs on +every completion path. On Windows, the root starts suspended, is assigned to a +kill-on-close Job Object, and is resumed only after containment is established. +Cancellation terminates the job; completion closes it, cleaning up descendants. +Unexpected startup layouts or unavailable job assignment fail closed. This is +lifecycle containment, not an OS security sandbox for hostile project code. +The Windows implementation follows Microsoft's [Job Objects](https://learn.microsoft.com/en-us/windows/win32/procthread/job-objects) +and [process creation flags](https://learn.microsoft.com/en-us/windows/win32/procthread/process-creation-flags) +documentation. Native CI must pass before this change is promoted. diff --git a/docs/signed-reviews.md b/docs/signed-reviews.md index 4a987e5..130a81f 100644 --- a/docs/signed-reviews.md +++ b/docs/signed-reviews.md @@ -82,3 +82,13 @@ Protect the CLI, gate workflow and authority configuration under your existing repository/organization policy. Target tests execute host code: run them in an isolated, disposable worker without secrets, with read-only trusted tooling where possible. This mechanism is not an adversarial sandbox or a signed proof system. + +## Preventing accidental downgrade + +Once an active approval is signed, ordinary `approve` and `import-approval` cannot +replace it with an unsigned assertion, even when `--policy` was accidentally +omitted. Renew using `--signing-key` and the inspected digest, or import another +valid signed review. The rejection leaves the active approval unchanged. A corrupt +active record also requires inspection before an unsigned replacement. A project +owner can deliberately remove local state; the external CI policy must still be +required independently, since local state is not an authorization boundary. diff --git a/internal/cli/cli.go b/internal/cli/cli.go index 927e6a0..5a65bfb 100644 --- a/internal/cli/cli.go +++ b/internal/cli/cli.go @@ -285,7 +285,7 @@ func Run(args []string, out, errOut io.Writer, version string) (exitCode int) { if err != nil { return printer.finish(1, nil, "execution", err, "") } - _, latest, currentErr := pdd.StatusWithPolicy(cwd, plan, global.policy) + _, latest, currentErr := pdd.StatusContext(ctx, cwd, plan, global.policy) current := currentErr == nil && latest == dir data := receiptResult(receipt, dir, current) if !global.json { @@ -308,7 +308,7 @@ func Run(args []string, out, errOut io.Writer, version string) (exitCode int) { } return printer.finish(0, data, "", nil, "") case "status": - receipt, dir, err := pdd.StatusWithPolicy(cwd, plan, global.policy) + receipt, dir, err := pdd.StatusContext(ctx, cwd, plan, global.policy) data := receiptResult(receipt, dir, err == nil) if err != nil { return printer.finish(1, data, "status", err, "Run prove against the current approved plan to obtain current evidence.") diff --git a/internal/pdd/approval_transfer.go b/internal/pdd/approval_transfer.go index b776889..9e01b10 100644 --- a/internal/pdd/approval_transfer.go +++ b/internal/pdd/approval_transfer.go @@ -106,5 +106,8 @@ func ImportApprovalWithPolicy(root string, p Plan, source, policyPath string) (A if string(a) != string(b) { return approval, fmt.Errorf("imported saved review differs from current intent and contracts") } + if err = protectSignedApproval(root, p, approval); err != nil { + return approval, err + } return approval, WriteJSON(filepath.Join(root, p.stateFile("approval.json")), approval) } diff --git a/internal/pdd/integrity_test.go b/internal/pdd/integrity_test.go new file mode 100644 index 0000000..c77a58c --- /dev/null +++ b/internal/pdd/integrity_test.go @@ -0,0 +1,156 @@ +package pdd + +import ( + "context" + "errors" + "path/filepath" + "testing" +) + +func TestStatusCannotAcceptEvidenceWhileProjectIsLocked(t *testing.T) { + root, p := fixture(t) + approveFixture(t, root, p) + receipt, _, err := Prove(context.Background(), root, p, Options{Runner: &fakeRunner{}}) + if err != nil || !receipt.Success { + t.Fatal("fixture proof failed", err) + } + unlock, err := lockProject(root) + if err != nil { + t.Fatal(err) + } + defer unlock() + if _, _, err = Status(root, p); err == nil { + t.Fatal("status accepted an old receipt while a mutating project operation held the lock") + } +} + +func TestSignedApprovalCannotBeDowngradedByApproveOrImport(t *testing.T) { + for _, operation := range []string{"approve", "import"} { + t.Run(operation, func(t *testing.T) { + root, p, _, _, signed := signingFixture(t) + var err error + if operation == "approve" { + _, err = Approve(root, p, "maintainer", []string{"identity"}) + } else { + unsigned := signed + unsigned.Signature = nil + path := filepath.Join(root, ".pdd/reviews/unsigned.json") + if e := WriteJSON(path, unsigned); e != nil { + t.Fatal(e) + } + _, err = ImportApproval(root, p, path) + } + if err == nil { + t.Fatal("signed review silently replaced by an unsigned assertion") + } + var active Approval + if e := ReadJSON(filepath.Join(root, ApprovalPath), &active); e != nil { + t.Fatal(e) + } + if !sameApproval(active, signed) { + t.Fatal("rejected downgrade modified the active signed approval") + } + }) + } +} + +func TestStatusRechecksSuccessfulReceiptLogs(t *testing.T) { + for _, kind := range []string{"formal-summary", "contradictory-formal-summary", "required-test", "incomplete-package", "failed-test-event", "missing-mutant-log", "unrelated-mutant-failure"} { + t.Run(kind, func(t *testing.T) { + root, p := fixture(t) + approveFixture(t, root, p) + receipt, dir, err := Prove(context.Background(), root, p, Options{Runner: &fakeRunner{}}) + if err != nil || !receipt.Success { + t.Fatal("fixture proof failed", err) + } + replaceLog := func(name string, data string) string { + t.Helper() + if err := AtomicWrite(filepath.Join(dir, name), []byte(data)); err != nil { + t.Fatal(err) + } + return hashBytes([]byte(data)) + } + switch kind { + case "formal-summary": + receipt.Evidence[0].LogSHA256 = replaceLog(receipt.Evidence[0].Log, "Gobra found 0 errors.\n") + case "contradictory-formal-summary": + receipt.Evidence[0].LogSHA256 = replaceLog(receipt.Evidence[0].Log, "Verifying package fixture\nGobra found 0 errors.\nGobra found 1 error.\n") + case "required-test": + receipt.Evidence[1].LogSHA256 = replaceLog(receipt.Evidence[1].Log, "{\"Action\":\"pass\",\"Test\":\"TestUnrelated\"}\n") + case "incomplete-package": + receipt.Evidence[1].LogSHA256 = replaceLog(receipt.Evidence[1].Log, "{\"Action\":\"pass\",\"Package\":\"fixture\",\"Test\":\"TestBoundary\"}\n") + case "failed-test-event": + receipt.Evidence[1].LogSHA256 = replaceLog(receipt.Evidence[1].Log, "{\"Action\":\"pass\",\"Test\":\"TestBoundary\"}\n{\"Action\":\"fail\"}\n") + case "missing-mutant-log": + receipt.Mutations[0].Log = "" + receipt.Mutations[0].LogSHA256 = "" + case "unrelated-mutant-failure": + receipt.Mutations[0].LogSHA256 = replaceLog(receipt.Mutations[0].Log, "syntax error\n") + } + if err = WriteJSON(filepath.Join(dir, "receipt.json"), receipt); err != nil { + t.Fatal(err) + } + if _, _, err = Status(root, p); err == nil { + t.Fatal("success accepted without the required saved verifier/test/mutation output") + } + }) + } +} + +type diagnosticFixtureRunner struct{ delegate fakeRunner } + +func (r *diagnosticFixtureRunner) Run(ctx context.Context, root string, c Check) Execution { + e := r.delegate.Run(ctx, root, c) + if c.Kind == "go-test" { + // This raw marker is JSON-escaped inside the event; it is not the stream separator. + e.Output += "{\"Action\":\"output\",\"Output\":\"example\\n[stderr]\\ntext\"}\n" + e.Output += "\n[stderr]\ngo: downloading example.org/diagnostic v1.0.0\n" + } + return e +} +func TestStatusRetainsValidGoDiagnosticEvidence(t *testing.T) { + root, p := fixture(t) + approveFixture(t, root, p) + receipt, _, err := Prove(context.Background(), root, p, Options{Runner: &diagnosticFixtureRunner{}}) + if err != nil || !receipt.Success { + t.Fatalf("fixture failed: %v %v", err, receipt.Gaps) + } + if _, _, err = Status(root, p); err != nil { + t.Fatal("valid test JSON and separate diagnostics rejected:", err) + } +} +func TestTruncatedPostconditionFailureIsInconclusive(t *testing.T) { + _, p := fixture(t) + e := Execution{Status: "failed", ExitCode: 1, Output: proofFailureOutput("core.go", []byte(coreSource), p.Requirements[0].Ensures) + "\n[pdd: output truncated]\n"} + if RejectedMutation(e, p.Requirements[0], []byte(coreSource)) { + t.Fatal("truncated output certified a killed mutant") + } +} +func TestSignedApprovalCanBeRenewedWithAuthorizedSignature(t *testing.T) { + root, p, key, policy, _ := signingFixture(t) + digest, err := Digest(root, p) + if err != nil { + t.Fatal(err) + } + renewed, err := ApproveSignedWithPolicy(root, p, "maintainer", []string{"identity"}, digest, key, policy) + if err != nil || renewed.Signature == nil { + t.Fatal("authorized renewal rejected", err) + } + if err = CheckApproval(root, p); err != nil { + t.Fatal(err) + } +} + +func TestCanceledStatusNeverReturnsOldGreen(t *testing.T) { + root, p := fixture(t) + approveFixture(t, root, p) + r, _, err := Prove(context.Background(), root, p, Options{Runner: &fakeRunner{}}) + if err != nil || !r.Success { + t.Fatal("fixture failed", err) + } + ctx, cancel := context.WithCancel(context.Background()) + cancel() + if _, _, err = StatusContext(ctx, root, p, ""); !errors.Is(err, context.Canceled) { + t.Fatalf("canceled status returned %v", err) + } +} diff --git a/internal/pdd/lock.go b/internal/pdd/lock.go index 6c40e7a..a0d13eb 100644 --- a/internal/pdd/lock.go +++ b/internal/pdd/lock.go @@ -23,7 +23,7 @@ func lockProject(root string) (func(), error) { } if err = lockFile(f); err != nil { f.Close() - return nil, fmt.Errorf("another verification or approval may be using this project: %w", err) + return nil, fmt.Errorf("another proof, approval or status operation may be using this project: %w", err) } return func() { _ = f.Close() }, nil } diff --git a/internal/pdd/packages_test.go b/internal/pdd/packages_test.go index 0024333..f8b0308 100644 --- a/internal/pdd/packages_test.go +++ b/internal/pdd/packages_test.go @@ -210,7 +210,7 @@ type dependencyRunner struct { func (r *dependencyRunner) Run(_ context.Context, root string, c Check) Execution { r.calls = append(r.calls, c.ID) - e := Execution{Status: "passed", ExitCode: 0, Output: "baseline", Command: []string{"test-double"}} + e := passedFixtureExecution(c) policy, _ := os.ReadFile(filepath.Join(root, "policy/policy.go")) handler, _ := os.ReadFile(filepath.Join(root, "handler/handler.go")) pointer, _ := os.ReadFile(filepath.Join(root, "policy/pointer.go")) diff --git a/internal/pdd/pdd_test.go b/internal/pdd/pdd_test.go index 7e9d287..dcf98be 100644 --- a/internal/pdd/pdd_test.go +++ b/internal/pdd/pdd_test.go @@ -175,7 +175,7 @@ type fakeRunner struct { func (f *fakeRunner) Run(_ context.Context, root string, c Check) Execution { f.calls++ - e := Execution{Status: "passed", ExitCode: 0, Command: []string{"test-double"}, Output: "baseline evidence"} + e := passedFixtureExecution(c) if f.touch && f.calls == 1 { _ = os.WriteFile(filepath.Join(f.root, "new.go"), []byte("package fixture\n"), 0644) } @@ -279,7 +279,7 @@ func TestGoDiagnosticsDoNotCorruptJSONEvidence(t *testing.T) { wrapper := filepath.Join(t.TempDir(), "go-wrapper") // A real Go invocation can write dependency-download notices to stderr. // This fixture reproduces that separate diagnostic channel without networking. - source := "#!/bin/sh\nprintf 'go: downloading example.org/dependency v1.0.0\\n' >&2\nprintf '%s\\n' '{\"Action\":\"pass\",\"Test\":\"TestBoundary\"}'\n" + source := "#!/bin/sh\nprintf 'go: downloading example.org/dependency v1.0.0\\n' >&2\nprintf '%s\\n' '{\"Action\":\"pass\",\"Package\":\"fixture\",\"Test\":\"TestBoundary\"}' '{\"Action\":\"pass\",\"Package\":\"fixture\"}'\n" if err := os.WriteFile(wrapper, []byte(source), 0755); err != nil { t.Fatal(err) } @@ -294,3 +294,16 @@ func proofFailureOutput(file string, source []byte, ensures string) string { line := strings.Count(string(source[:index]), "\n") + 1 return fmt.Sprintf("Error at: Postcondition might not hold.\nAssertion %s might not hold.\n", file, line, ensures) } + +// Test doubles supply the same output shape that a saved receipt must recheck. +func passedFixtureExecution(c Check) Execution { + e := Execution{Status: "passed", ExitCode: 0, Command: []string{"test-double"}, Output: "Verifying package fixture\nGobra found 0 errors.\n"} + if c.Kind == "go-test" { + e.Output = "" + for _, name := range c.Tests { + e.Output += fmt.Sprintf("{\"Action\":\"pass\",\"Package\":\"fixture\",\"Test\":%q}\n", name) + } + e.Output += "{\"Action\":\"pass\",\"Package\":\"fixture\"}\n" + } + return e +} diff --git a/internal/pdd/plan.go b/internal/pdd/plan.go index 8b46af5..6965481 100644 --- a/internal/pdd/plan.go +++ b/internal/pdd/plan.go @@ -568,6 +568,9 @@ func ApproveSignedWithPolicy(root string, p Plan, reviewer string, accepted []st return Approval{}, err } } + if err = protectSignedApproval(root, p, a); err != nil { + return Approval{}, err + } return a, WriteJSON(filepath.Join(root, p.stateFile("approval.json")), a) } func CheckApproval(root string, p Plan) error { diff --git a/internal/pdd/process_other.go b/internal/pdd/process_other.go index 4521704..7aa78ac 100644 --- a/internal/pdd/process_other.go +++ b/internal/pdd/process_other.go @@ -2,7 +2,11 @@ package pdd -import "os/exec" +import ( + "context" + "os/exec" +) -func configureProcess(*exec.Cmd) {} -func cleanupProcess(*exec.Cmd, error) {} +func configureProcess(*exec.Cmd, context.Context) (processControl, error) { + return processControl{start: func() error { return nil }, cleanup: func(error) {}}, nil +} diff --git a/internal/pdd/process_portable_test.go b/internal/pdd/process_portable_test.go index e6765a2..0594e30 100644 --- a/internal/pdd/process_portable_test.go +++ b/internal/pdd/process_portable_test.go @@ -73,15 +73,26 @@ func TestPortableProcessHelper(t *testing.T) { if role == "" { return } - if role == "parent" { + if role == "parent" || role == "parent-exit" || role == "parent-detached" { child := exec.Command(os.Args[0], "-test.run=^TestPortableProcessHelper$") child.Env = append(os.Environ(), "PDD_PROCESS_ROLE=child") // Pipes are deliberately inherited to exercise cleanup of descendants. - child.Stdout = os.Stdout - child.Stderr = os.Stderr + if role != "parent-detached" { + child.Stdout = os.Stdout + child.Stderr = os.Stderr + } if child.Start() != nil { os.Exit(3) } + if role != "parent" { + for deadline := time.Now().Add(10 * time.Second); time.Now().Before(deadline); { + if _, err := os.Stat(os.Getenv("PDD_PROCESS_PID")); err == nil { + os.Exit(0) + } + time.Sleep(10 * time.Millisecond) + } + os.Exit(5) + } _ = child.Wait() return } @@ -145,3 +156,38 @@ func TestPortableProcessTimeout(t *testing.T) { t.Fatalf("timeout: %+v", result) } } + +func TestExitedParentCannotLeaveCheckDescendants(t *testing.T) { + for _, role := range []string{"parent-exit", "parent-detached"} { + t.Run(role, func(t *testing.T) { + root := t.TempDir() + pidFile := filepath.Join(root, "child.pid") + t.Setenv("PDD_PROCESS_ROLE", role) + t.Setenv("PDD_PROCESS_PID", pidFile) + result := execute(context.Background(), root, []string{os.Args[0], "-test.run=^TestPortableProcessHelper$"}, 20*time.Second) + data, err := os.ReadFile(pidFile) + if err != nil { + t.Fatal("child did not start", err, result) + } + pid, err := strconv.Atoi(string(data)) + if err != nil { + t.Fatal(err) + } + defer func() { + if processRunning(pid) { + child, e := os.FindProcess(pid) + if e == nil { + _ = child.Kill() + } + } + }() + for deadline := time.Now().Add(5 * time.Second); time.Now().Before(deadline); { + if !processRunning(pid) { + return + } + time.Sleep(20 * time.Millisecond) + } + t.Fatalf("child survived its runner parent (%s, %+v)", role, result) + }) + } +} diff --git a/internal/pdd/process_unix.go b/internal/pdd/process_unix.go index 691c6ed..32f6b15 100644 --- a/internal/pdd/process_unix.go +++ b/internal/pdd/process_unix.go @@ -3,13 +3,14 @@ package pdd import ( + "context" "errors" "os" "os/exec" "syscall" ) -func configureProcess(cmd *exec.Cmd) { +func configureProcess(cmd *exec.Cmd, _ context.Context) (processControl, error) { cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} cmd.Cancel = func() error { if cmd.Process == nil { @@ -21,10 +22,11 @@ func configureProcess(cmd *exec.Cmd) { } return err } -} -func cleanupProcess(cmd *exec.Cmd, err error) { - // A child retaining stdout/stderr must not outlive an otherwise exited runner. - if errors.Is(err, exec.ErrWaitDelay) && cmd.Process != nil { - _ = syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL) - } + return processControl{start: func() error { return nil }, cleanup: func(error) { + // Descendants are scoped to one check, even when the parent has exited and + // they no longer hold its output pipes. Clean up on every completion path. + if cmd.Process != nil { + _ = syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL) + } + }}, nil } diff --git a/internal/pdd/process_windows.go b/internal/pdd/process_windows.go index f4ada8d..6fd4bc7 100644 --- a/internal/pdd/process_windows.go +++ b/internal/pdd/process_windows.go @@ -2,23 +2,156 @@ package pdd import ( "context" + "fmt" "os" "os/exec" - "strconv" - "time" + "sync" + "syscall" + "unsafe" ) -func configureProcess(cmd *exec.Cmd) { +var processKernel = syscall.NewLazyDLL("kernel32.dll") +var ( + createJobObject = processKernel.NewProc("CreateJobObjectW") + setJobInformation = processKernel.NewProc("SetInformationJobObject") + assignJobProcess = processKernel.NewProc("AssignProcessToJobObject") + terminateJob = processKernel.NewProc("TerminateJobObject") + firstThread = processKernel.NewProc("Thread32First") + nextThread = processKernel.NewProc("Thread32Next") + openThread = processKernel.NewProc("OpenThread") + resumeThread = processKernel.NewProc("ResumeThread") +) + +type jobBasicLimits struct { + ProcessTime int64 + JobTime int64 + Flags uint32 + MinWorkingSet uintptr + MaxWorkingSet uintptr + ActiveProcesses uint32 + Affinity uintptr + Priority uint32 + Scheduling uint32 +} +type jobExtendedLimits struct { + Basic jobBasicLimits + IO [6]uint64 + ProcessMemory uintptr + JobMemory uintptr + PeakProcessMemory uintptr + PeakJobMemory uintptr +} +type threadEntry struct { + Size uint32 + Usage uint32 + ID uint32 + ProcessID uint32 + BasePriority int32 + DeltaPriority int32 + Flags uint32 +} + +func configureProcess(cmd *exec.Cmd, ctx context.Context) (processControl, error) { + var limits jobExtendedLimits + if unsafe.Sizeof(limits) != 144 { + return processControl{}, fmt.Errorf("Windows process containment requires a 64-bit host") + } + raw, _, err := createJobObject.Call(0, 0) + if raw == 0 { + return processControl{}, fmt.Errorf("create process job: %w", err) + } + job := syscall.Handle(raw) + // JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE; the handle is not inheritable. + limits.Basic.Flags = 0x2000 + ok, _, err := setJobInformation.Call(uintptr(job), 9, uintptr(unsafe.Pointer(&limits)), unsafe.Sizeof(limits)) + if ok == 0 { + syscall.CloseHandle(job) + return processControl{}, fmt.Errorf("configure process job: %w", err) + } + var mu sync.Mutex + cleanup := func(error) { + mu.Lock() + defer mu.Unlock() + if job != 0 { + _ = syscall.CloseHandle(job) + job = 0 + } + } + // CREATE_SUSPENDED prevents child code from spawning descendants before the + // root process has been assigned to the job. Never fall back to an uncontained run. + cmd.SysProcAttr = &syscall.SysProcAttr{CreationFlags: 0x00000004} cmd.Cancel = func() error { - if cmd.Process == nil { + mu.Lock() + defer mu.Unlock() + if job == 0 { return os.ErrProcessDone } - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) - defer cancel() - if err := exec.CommandContext(ctx, "taskkill", "/PID", strconv.Itoa(cmd.Process.Pid), "/T", "/F").Run(); err != nil { - return cmd.Process.Kill() + ok, _, err := terminateJob.Call(uintptr(job), 1) + if ok == 0 { + return fmt.Errorf("terminate process job: %w", err) } return nil } + start := func() error { + process, err := syscall.OpenProcess(0x0100|0x0001, false, uint32(cmd.Process.Pid)) + if err != nil { + return fmt.Errorf("open suspended process: %w", err) + } + defer syscall.CloseHandle(process) + ok, _, err := assignJobProcess.Call(uintptr(job), uintptr(process)) + if ok == 0 { + return fmt.Errorf("assign process job: %w", err) + } + if err = ctx.Err(); err != nil { + return err + } + return resumeInitialThread(uint32(cmd.Process.Pid)) + } + return processControl{start: start, cleanup: cleanup}, nil +} + +// os/exec closes the primary thread handle returned by CreateProcess. Recover +// that still-suspended thread using the documented Toolhelp API. Unexpected +// thread layouts fail closed rather than running outside containment. +func resumeInitialThread(pid uint32) error { + snapshot, err := syscall.CreateToolhelp32Snapshot(0x00000004, 0) + if err != nil { + return fmt.Errorf("snapshot suspended thread: %w", err) + } + defer syscall.CloseHandle(snapshot) + entry := threadEntry{Size: uint32(unsafe.Sizeof(threadEntry{}))} + ok, _, err := firstThread.Call(uintptr(snapshot), uintptr(unsafe.Pointer(&entry))) + if ok == 0 { + return fmt.Errorf("enumerate suspended thread: %w", err) + } + var threads []uint32 + for { + if entry.ProcessID == pid { + threads = append(threads, entry.ID) + } + entry.Size = uint32(unsafe.Sizeof(entry)) + ok, _, err = nextThread.Call(uintptr(snapshot), uintptr(unsafe.Pointer(&entry))) + if ok == 0 { + if err != syscall.ERROR_NO_MORE_FILES { + return fmt.Errorf("enumerate suspended thread: %w", err) + } + break + } + } + if len(threads) != 1 { + return fmt.Errorf("expected one initial suspended thread, found %d", len(threads)) + } + raw, _, err := openThread.Call(0x0002, 0, uintptr(threads[0])) + if raw == 0 { + return fmt.Errorf("open suspended thread: %w", err) + } + defer syscall.CloseHandle(syscall.Handle(raw)) + previous, _, err := resumeThread.Call(raw) + if uint32(previous) == ^uint32(0) { + return fmt.Errorf("resume suspended thread: %w", err) + } + if previous != 1 { + return fmt.Errorf("unexpected initial thread suspend count: %d", previous) + } + return nil } -func cleanupProcess(*exec.Cmd, error) {} diff --git a/internal/pdd/prove.go b/internal/pdd/prove.go index 2691113..f0768c1 100644 --- a/internal/pdd/prove.go +++ b/internal/pdd/prove.go @@ -431,6 +431,17 @@ func Markdown(r Receipt) string { // receipt was green. Receipt files are local attestations, not signed proofs. func Status(root string, p Plan) (Receipt, string, error) { return StatusWithPolicy(root, p, "") } func StatusWithPolicy(root string, p Plan, policyPath string) (Receipt, string, error) { + return StatusContext(context.Background(), root, p, policyPath) +} +func StatusContext(ctx context.Context, root string, p Plan, policyPath string) (Receipt, string, error) { + if err := ctx.Err(); err != nil { + return Receipt{}, "", err + } + unlock, err := lockProject(root) + if err != nil { + return Receipt{}, "", err + } + defer unlock() var receipt Receipt var dir string state, stateErr := readRunState(root, p) @@ -463,7 +474,6 @@ func StatusWithPolicy(root string, p Plan, policyPath string) (Receipt, string, if err := NoSymlinks(dir); err != nil { return receipt, dir, err } - var err error if err = ReadJSON(filepath.Join(dir, "receipt.json"), &receipt); err != nil { return receipt, dir, err } @@ -548,7 +558,7 @@ func StatusWithPolicy(root string, p Plan, policyPath string) (Receipt, string, if err != nil { return receipt, dir, err } - snapshot, err := CaptureContext(context.Background(), root, p.Inputs) + snapshot, err := CaptureContext(ctx, root, p.Inputs) if err != nil { return receipt, dir, err } @@ -582,28 +592,69 @@ func StatusWithPolicy(root string, p Plan, policyPath string) (Receipt, string, return receipt, dir, fmt.Errorf("input manifest does not match source digest") } } - checkLog := func(name, hash string) error { + checkLog := func(name, hash string) ([]byte, error) { + if err := ctx.Err(); err != nil { + return nil, err + } if name == "" || filepath.Base(name) != name { - return fmt.Errorf("invalid log reference") + return nil, fmt.Errorf("invalid log reference") } - b, err := readArtifact(filepath.Join(dir, name), (8<<20)+1024) - if err != nil || hashBytes(b) != hash { - return fmt.Errorf("missing or altered log %s", name) + data, err := readArtifact(filepath.Join(dir, name), (8<<20)+1024) + if err != nil || hashBytes(data) != hash { + return nil, fmt.Errorf("missing or altered log %s", name) } - return nil + return data, nil + } + checks := map[string]Check{} + for _, c := range p.Checks { + checks[c.ID] = c } for _, e := range receipt.Evidence { - if err = checkLog(e.Log, e.LogSHA256); err != nil { + data, err := checkLog(e.Log, e.LogSHA256) + if err != nil { return receipt, dir, err } + if receipt.Success { + if err = validateExecutionOutput(checks[e.Check], recordedStdout(string(data)), string(data)); err != nil { + return receipt, dir, fmt.Errorf("saved check %s does not support success: %w", e.Check, err) + } + } + } + requirements := map[string]Requirement{} + for _, r := range p.Requirements { + requirements[r.ID] = r } - for _, m := range receipt.Mutations { - if m.Log != "" { - if err = checkLog(m.Log, m.LogSHA256); err != nil { - return receipt, dir, err + for i, m := range receipt.Mutations { + if m.Log == "" && m.Status != "killed" { + continue + } + data, err := checkLog(m.Log, m.LogSHA256) + if err != nil { + return receipt, dir, err + } + if receipt.Success { + execution := m.Execution + execution.Output = string(data) + if !RejectedMutation(execution, requirements[p.Mutations[i].Requirement], snapshot.Files[p.Mutations[i].File]) { + return receipt, dir, fmt.Errorf("saved mutation %s does not reject its target postcondition", m.ID) } } } + + if err := ctx.Err(); err != nil { + return receipt, dir, err + } + // Authorization is independent of the project lock. Recheck after hashing + // inputs/logs so expiry or an operator policy update during status fails closed. + if policyPath != "" { + evidence, err := verifyPolicy(policyPath, p, currentApproval, time.Now().UTC()) + if err != nil { + return receipt, dir, err + } + if receipt.ApprovalPolicy == nil || evidence != *receipt.ApprovalPolicy { + return receipt, dir, fmt.Errorf("review policy changed while checking status") + } + } return receipt, dir, nil } diff --git a/internal/pdd/runner.go b/internal/pdd/runner.go index d9012ed..2f6975b 100644 --- a/internal/pdd/runner.go +++ b/internal/pdd/runner.go @@ -40,7 +40,7 @@ type ToolRunner struct { Timeout time.Duration } -var formalPassRE = regexp.MustCompile(`(?i)Gobra found 0 errors\.`) +var formalSummaryRE = regexp.MustCompile(`(?i)Gobra found ([0-9]+) errors?\.`) func (r ToolRunner) Run(ctx context.Context, root string, c Check) Execution { goExe := r.Go @@ -81,15 +81,11 @@ func (r ToolRunner) Run(ctx context.Context, root string, c Check) Execution { if e.Status != "passed" { return e } - if c.Kind == "go-test" { - if err := validateTests(e.stdout, c.Tests); err != nil { - e.Status = "failed" - e.Detail = err.Error() - } - } else if !formalPassRE.MatchString(e.Output) || !strings.Contains(e.Output, "Verifying package") || strings.Contains(strings.ToLower(e.Output), "verification failed") { + if err := validateExecutionOutput(c, e.stdout, e.Output); err != nil { e.Status = "failed" - e.Detail = "Gobra exited zero without a verification run and success summary" + e.Detail = err.Error() } + return e } @@ -113,6 +109,12 @@ func (b *boundedOutput) Write(p []byte) (int, error) { b.b.Write(p) return n, nil } + +type processControl struct { + start func() error + cleanup func(error) +} + func execute(ctx context.Context, root string, command []string, timeout time.Duration) Execution { e := Execution{Status: "failed", Command: command, ExitCode: -1} if timeout <= 0 { @@ -122,15 +124,28 @@ func execute(ctx context.Context, root string, command []string, timeout time.Du defer cancel() cmd := exec.CommandContext(ctx, command[0], command[1:]...) cmd.Dir = root - configureProcess(cmd) + control, setupErr := configureProcess(cmd, ctx) + if setupErr != nil { + e.Status = "unavailable" + e.Detail = setupErr.Error() + return e + } cmd.WaitDelay = 2 * time.Second cmd.Env = append(os.Environ(), "GOWORK=off", "GOFLAGS=", "GOOS="+runtime.GOOS, "GOARCH="+runtime.GOARCH) var output, diagnostics boundedOutput cmd.Stdout = &output cmd.Stderr = &diagnostics start := time.Now() - err := cmd.Run() - cleanupProcess(cmd, err) + err := cmd.Start() + if err == nil { + if err = control.start(); err != nil { + _ = cmd.Process.Kill() + _ = cmd.Wait() + } else { + err = cmd.Wait() + } + } + control.cleanup(err) e.DurationMS = time.Since(start).Milliseconds() e.stdout = output.b.String() e.Output = e.stdout @@ -171,11 +186,39 @@ func execute(ctx context.Context, root string, command []string, timeout time.Du e.Status = "passed" return e } + +// The saved log separates stdout from diagnostic stderr using a literal marker. +// go test -json encodes newlines inside Output fields, so this separator cannot +// appear as a raw line inside a valid JSON stdout event. +func recordedStdout(combined string) string { + out, _, _ := strings.Cut(combined, "\n[stderr]\n") + return out +} +func validateExecutionOutput(c Check, stdout, combined string) error { + if strings.Contains(combined, "\n[pdd: output truncated]\n") { + return fmt.Errorf("verification output was truncated") + } + if c.Kind == "go-test" { + return validateTests(stdout, c.Tests) + } + summaries := formalSummaryRE.FindAllStringSubmatch(combined, -1) + if len(summaries) == 0 || !strings.Contains(combined, "Verifying package") || strings.Contains(strings.ToLower(combined), "verification failed") { + return fmt.Errorf("Gobra exited zero without a verification run and success summary") + } + for _, summary := range summaries { + if summary[1] != "0" { + return fmt.Errorf("Gobra output includes a failing verification summary") + } + } + return nil +} + func validateTests(output string, names []string) error { - passed := map[string]bool{} + passed := map[string]string{} + packages := map[string]bool{} decoder := json.NewDecoder(strings.NewReader(output)) for { - var event struct{ Action, Test, Output string } + var event struct{ Action, Test, Output, Package string } err := decoder.Decode(&event) if err == io.EOF { break @@ -183,13 +226,18 @@ func validateTests(output string, names []string) error { if err != nil { return fmt.Errorf("invalid go test JSON: %w", err) } + if event.Action == "fail" { + return fmt.Errorf("go test output records a failure for %s", event.Test) + } if event.Action == "pass" && event.Test != "" { - passed[event.Test] = true + passed[event.Test] = event.Package + } else if event.Action == "pass" && event.Test == "" && event.Package != "" { + packages[event.Package] = true } } for _, name := range names { - if !passed[name] { - return fmt.Errorf("required test %s did not execute and pass (missing or skipped)", name) + if passed[name] == "" || !packages[passed[name]] { + return fmt.Errorf("required test %s did not execute and pass in a successfully completed package (missing, skipped or incomplete output)", name) } } return nil @@ -199,7 +247,7 @@ func validateTests(output string, names []string) error { // the exact authored clause line. Matching expression text elsewhere is not a // rejection of this requirement's mutant. func RejectedMutation(e Execution, requirement Requirement, source []byte) bool { - if e.Status != "failed" || e.ExitCode <= 0 { + if e.Status != "failed" || e.ExitCode <= 0 || strings.Contains(e.Output, "\n[pdd: output truncated]\n") { return false } fset := token.NewFileSet() diff --git a/internal/pdd/signing.go b/internal/pdd/signing.go index c11410f..f26baed 100644 --- a/internal/pdd/signing.go +++ b/internal/pdd/signing.go @@ -226,3 +226,23 @@ func verifyPolicy(path string, p Plan, a Approval, now time.Time) (PolicyEvidenc } return PolicyEvidence{Digest: hashBytes(encoded), KeyID: keyID, Reviewer: a.Reviewer}, nil } + +// Called under the project lock before installing a new active approval. Local +// draft mode must not silently remove the signature from a previously signed plan. +func protectSignedApproval(root string, p Plan, next Approval) error { + if next.Signature != nil { + return nil + } + var current Approval + err := ReadJSON(filepath.Join(root, p.stateFile("approval.json")), ¤t) + if os.IsNotExist(err) { + return nil + } + if err != nil { + return fmt.Errorf("cannot inspect existing approval before replacement: %w", err) + } + if current.Signature != nil { + return fmt.Errorf("cannot replace a signed approval with an unsigned assertion; renew with --signing-key or import a signed review") + } + return nil +} diff --git a/scripts/release.py b/scripts/release.py index 68b23eb..8b1669f 100644 --- a/scripts/release.py +++ b/scripts/release.py @@ -48,7 +48,7 @@ def main(): "Run pdd --version and pdd --help. Go is needed for empirical tests;\n" "formal verification needs Docker and the pinned Gobra image (pdd doctor).\n" "Source, examples and full instructions:\nhttps://github.com/cruskell/pdd-kit-go\n" - "Approvals are unsigned reviewer assertions; read intent and trust boundaries.\n").encode() + "Use --policy with authorized signed reviews for production CI; read trust boundaries.\n").encode() for system, arch in TARGETS: name = "pdd.exe" if system == "windows" else "pdd" binary = out / name