Skip to content

feat(vanir): add vanir-sandbox service, gitter commit-pack relay, and input/output hardening - #6403

Draft
another-rex wants to merge 1 commit into
google:masterfrom
another-rex:vanir-sandbox-pr1
Draft

another-rex wants to merge 1 commit into
google:masterfrom
another-rex:vanir-sandbox-pr1

Conversation

@another-rex

Copy link
Copy Markdown
Contributor

Overview

(AI-assisted PR)

This is PR 1 of 2 to isolate Vanir signature generation (vanir_signatures) into a dedicated, zero-privilege Cloud Run Gen2 sandbox (vanir-sandbox) fed by shallow Git commit packs relayed from gitter.

Keeping this rollout split into two PRs avoids a deployment chicken-and-egg:

  • PR 1 (this PR): Adds gitter POST /commit-pack, --git-instead-ofs support in generate_signatures.py, the new go/cmd/vanir_sandbox Cloud Run service, input sanitization & output pinning/validation in go/internal/vanir, the vanir-sandbox Docker target, Terraform resources, and Cloud Build / Cloud Deploy wiring. The existing GKE vanir-signatures CronJob and Docker target remain backwards-compatible until vanir-sandbox is provisioned (and SanitizeForVanir + ValidateSignatures immediately protect the existing in-container PythonGenerator).
  • PR 2 (follow-up): Cuts over vanir-signatures to a distroless Go image pointing at VANIR_SANDBOX_URL + GITTER_HOST and moves the CronJob off the highend node pool.

Details

1. gitter Shallow Commit-Pack Relay (POST /commit-pack)

  • Added CommitPackRequest to go/internal/gitter/pb/repository/repository.proto and regenerated repository.pb.go.
  • Implemented CreateCommitPack and POST /commit-pack in go/cmd/gitter/:
    • Validates repository URL via prepareURL, caps requests at 1..64 validated hex commit hashes, and verifies each object is a commit (git cat-file -t).
    • Operates under repoLock.RLock() without mutating the cached repo's .git/config by configuring remote.origin.uploadpack on the temporary bare clone.
    • Fetches --filter=blob:none --depth=2 --no-tags, materializes only touched diff blobs via format-patch --stdout -1, detaches remote.origin, enables uploadpack.allowFilter/uploadpack.allowAnySHA1InWant, and returns a .tar.zst archive (capped at 128 MiB; retries once with ForceUpdate: true on missing commits before returning 404).
  • Added GetCommitPack and ErrPackTooLarge to go/internal/gitter.

2. Python Helper --git-instead-ofs Support

  • Added optional --git-instead-ofs flag to go/cmd/vanir_signatures/generate_signatures.py accepting a JSON string or file path of [[src_url, local_file_url], ...] pairs, validating non-empty string pairs and passing an immutable tuple[tuple[str, str], ...] to ExtractorConfig.git_instead_ofs.

3. Untrusted Mode B Sandbox Server (go/cmd/vanir_sandbox)

  • Serves GET /healthz and POST /v1/generate (64 MiB request cap, single-request mutex matching Cloud Run concurrency=1, optional -max-requests graceful exit).
  • Probes CAP_SETUID/CAP_SETGID and CLONE_NEWNET/CLONE_NEWIPC/CLONE_NEWUTS at startup.
  • Uses a private supervisor-owned root directory (0711) and os.Mkdir per request; unpacks .tar.zst packs safely in Go (klauspost/compress/zstd + archive/tar) with decoder memory/window caps, entry count cap, and 256 MiB total size cap, rejecting traversal/symlinks/hardlinks/special nodes and setting 0755/0644 owned by 0:0.
  • Runs generate_signatures.py under a unique non-root UID (10000..59999), stripped cmd.Env (GIT_ALLOW_PROTOCOL=file when all repos are packed, safe.directory=*), isolated HOME/TMPDIR, and empty network namespace (CLONE_NEWNET) when AllowNetwork is false.
  • Reaps the process group and all surviving processes owned by the request UID (-reap-uid running kill(-1, SIGKILL) as that non-root UID) before atomically reading pre-created output.json (O_NOFOLLOW|O_NONBLOCK|O_CLOEXEC), then sweeps any stray UID temp files and removes request directories.

4. Go Orchestrator Hardening (go/internal/vanir & go/cmd/vanir_signatures)

  • Added SanitizeForVanir: strips unused free-text fields (Summary, Details, References, Credits), strips non-canonical Android/Pixel/Wear entries while preserving index alignment, allowlists canonical Android fix URLs, validates Range_GIT URLs (blocking non-HTTP(S), credentials, @, option prefixes, internal/loopback/RFC1918/link-local/CGNAT/alternate-IP hosts), normalizes git://github.com to https://github.com, and validates/caps fixed commit hashes.
  • Added ValidateSignatures: pins output strictly to the requested vulnerability ID (<id>- prefix), verifies affected index bounds and post-sanitization eligibility, caps at 256 signatures per affected entry and 2 MiB serialized JSON per record, and strictly validates Vanir Function/Line signature schemas.
  • Wired SanitizeForVanir + ValidateSignatures + ID pinning directly into PythonGenerator (with bounded stderr capture and O_NOFOLLOW output reads).
  • Added HTTPSignatureGenerator (idtoken.NewClient OIDC auth, 128 MiB byte-bounded LRU + cancellation-isolated singleflight commit-pack cache, graceful skips on 404/403/413 repos) and -sandbox-url/-gitter-host flags in go/cmd/vanir_signatures/main.go.

5. Docker, Terraform, Cloud Deploy, Cloud Build, & Docs

  • Added vanir-sandbox target in go/Dockerfile (reusing vanir-python-deps; existing vanir-signatures target left unchanged for PR 1).
  • Added deployment/terraform/modules/osv/vanir_sandbox.tf (zero-IAM-role vanir-sandbox SA, deployer actAs binding, Cloud Run Gen2 service with no VPC access, concurrency=1, 4 vCPU / 8Gi, max_instance_count=30, roles/run.invoker restricted to worker_service_account_email) and exported vanir_sandbox_uri outputs.
  • Added deployment/clouddeploy/vanir-sandbox/ manifests and wired build/release/promote/tag steps in deployment/build-and-stage.yaml and deployment/deploy-prod.yaml.
  • Updated Makefile (vanir-signatures-tests) and AGENTS.md.

Rollout Note: Before the first build-and-stage run on master, apply the Cloud Deploy delivery pipeline once:

gcloud deploy apply --file=deployment/clouddeploy/vanir-sandbox/clouddeploy.yaml --region=us-central1 --project=oss-vdb

Testing

  • Added unit & integration tests:
    • go/cmd/gitter/commit_pack_test.go (standalone pack verification after deleting source repo, unrelated blob exclusion, option-injection rejection, 404 handling)
    • go/internal/gitter/http_test.go (TestGetCommitPack including 400/403/404/413 error mapping)
    • go/internal/vanir/sanitize_test.go (SanitizeForVanir, ValidateSignatures, HTTPSignatureGenerator caching/skipping/index-remapping/singleflight cancellation isolation)
    • go/cmd/vanir_sandbox/main_test.go (end-to-end HTTP test with real .tar.zst commit pack, --git-instead-ofs fetch, process reaping, symlink/FIFO output rejection, safe tar extraction checks)
  • Verified locally:
    • make vanir-signatures-tests — PASS
    • cd go && go test -race -count=1 ./internal/vanir/... ./cmd/vanir_signatures/... ./cmd/vanir_sandbox/... ./internal/gitter/... — PASS
    • cd go && go test -race -count=1 ./cmd/gitter -run TestCommitPackHandler — PASS
    • cd go && go build ./... — PASS
    • ./tools/lint_and_format.sh (pylint 10.00/10, yapf, gofmt, golangci-lint 0 issues, terraform fmt) — PASS
    • Live smoke test with vanir==1.1.0 + generate_signatures.py + GIT_ALLOW_PROTOCOL=file against a read-only unpacked commit pack — PASS

agy

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant