Skip to content

Expose verified GitHub comment intake in Code - #1082

Merged
cwilhit merged 3 commits into
mainfrom
codex/factory-code-github-comment-webhook
Nov 12, 2026
Merged

cwilhit merged 3 commits into
mainfrom
codex/factory-code-github-comment-webhook

Conversation

@cwilhit

@cwilhit cwilhit commented Nov 12, 2026

Copy link
Copy Markdown
Collaborator

Mount the signed GitHub issue-comment webhook and document its repository and installation binding. The base draft provides the verified receipt and tests. Repository webhook configuration remains explicit.

Stacked on #1081. This draft supplies the webhook for the Factory PR-comment intake follow-up.

Validation: Code provider tests and provider-contract checks passed locally.

@cwilhit
cwilhit added this pull request to stack #1084 November 12, 2026 00:30
@cwilhit
cwilhit force-pushed the codex/factory-code-github-comment-webhook branch from 2a8c533 to 82e2e58 Compare November 12, 2026 00:33
@cwilhit
cwilhit marked this pull request as ready for review November 12, 2026 00:36
@cwilhit

cwilhit commented Nov 12, 2026

Copy link
Copy Markdown
Collaborator Author

@mjudeikis-bot review

@mjudeikis mjudeikis left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two Warnings and one Note on the mount. The Webhook handler comes from the base PR (#1081). This PR makes it reachable, so I reviewed the handler as a newly exposed anonymous surface.

Not anchored

  • docs/provider-connectivity-contract.md:184 still describes class (g) as /webhooks/{kind}/{clusterID}/{name}/{token} with an "HMAC token" and names only agents as the example. Code's route has no {token} segment and authenticates with the X-Hub-Signature-256 HMAC over the body. Note: add Code's shape to that row, or the next provider author copies the wrong grammar.

Checked

  • Read beyond the diff: providers/code/githubworkflow/server.go (Webhook, binding, duplicateReceipt, Context, ServeHTTP, CheckPublication), github.go (Verify), provider-sdk/serve/serve.go (Extra, prefixForClass, longest-prefix dispatch), providers/agents/main.go (the other class-(g) mount), pkg/hub/providers/proxy.go (setHeaders, anonymous forwarding), and server_test.go's webhook helper.
  • Correctness: the handler flow (size cap, HMAC, event/action filter, installation/repo identity, dedupe, Create) is sound. Binding errors of any kind are reported as 404 "connection unavailable", including transient kcp failures (see the first inline finding).
  • Concurrency: no shared state is added. The only duplicate race is the Create-then-AlreadyExists path, and Webhook handles it by re-checking duplicateReceipt.
  • Tenancy and kcp: {cluster} comes from the URL and is used for AsProvider(cluster). That is intended for class (g), but it is attacker-addressable before authentication (first inline finding). No second credential is added, and no APIExport claims change.
  • Provider contract: the mount is ClassWebhook under /webhooks/, and there is no /api/* route and no bearer. make verify-provider-contract passes. No verb is added, so the manifest and chart are unchanged.
  • API, codegen and storage: not applicable, because no apis/ or CRD change.
  • Security: the HMAC is checked in constant time with a 32-byte minimum key, and the body is capped at 1 MiB. The pre-auth reads and error distinctions are covered in the first finding.
  • Portal: not applicable, no portal change.
  • Tests: the Webhook behaviour has tests in githubworkflow/server_test.go. The mount itself has none (second finding).
  • Commands: cd providers/code && go build ./... && go vet ./... fail only for the root package (assets.go: pattern all:portal/dist: no matching files found, because the portal is not built in this checkout). go test -count=1 ./... passes for every other package, including githubworkflow. The root main package tests could not be run for the same reason. make verify-provider-contract passes (21/21 plus the permission-claim policy scan).
  • Not run: root make build, make lint and make verify-codegen, since the diff is confined to providers/code and the README. No e2e was run: make e2e-code-provider was not attempted and nothing exercises the mount.

Comment thread providers/code/main.go Outdated
}

webhooks := http.NewServeMux()
webhooks.HandleFunc("POST /webhooks/github/{cluster}/{name}", workflow.Webhook)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning: Anonymous callers reach kcp reads, and failure responses reveal what exists, before the signature is checked
The hub forwards /services/providers/code/webhooks/... anonymously, and {cluster} and {name} come straight from the URL. Webhook (githubworkflow/server.go:164) calls AsProvider(cluster) and binding() for them before it verifies the HMAC. binding() does three provider-credentialed reads: the Connection, the credential Secret and the Repository. The replies differ by stage: 503 workspace unavailable, 404 connection unavailable, 403 invalid signature. Any unauthenticated client can therefore probe which connection names exist in a workspace and trigger kcp and Secret reads at request rate, with no throttle. In addition, any transient kcp or Secret error is reported as a 404 "connection unavailable", so an operator reading GitHub's delivery log is told the connection is gone.
The reads are unavoidable, because the signing key lives in the Secret. The fix is to make every pre-signature failure indistinguishable and to log the real cause server-side:

b, err := s.binding(ctx, dyn, name, false)
if err != nil {
	log.Printf("github webhook %s/%s: %v", cluster, name, err)
	reply(w, http.StatusNotFound, map[string]string{"error": "not found"})
	return
}
// and in the AsProvider failure branch: the same 404 body

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 56b6f97 and 239c7f9. Every failure before the signature check now returns the same 404 {"error":"not found"}: workspace unavailable, missing or unusable binding, transient kcp or Secret errors, and a bad signature. The real cause is logged server-side. TestWebhookPreAuthenticationFailuresMatch checks that the three responses are identical. 239c7f9 closes one more oracle: the 1 MiB body bound ran after the binding reads, so an unsigned oversized POST returned 400 only for connections that exist. The body is now read and bounded before any lookup, and TestWebhookOversizedBodyDoesNotRevealTheConnection covers it (it fails on the previous handler). Not addressed here: rate limiting of the pre-signature reads, and the timing difference between a missing connection and a bad signature.

Comment thread providers/code/main.go Outdated
MCP: mcpHandler,
Actions: codeActions,
DataPlane: dataPlane,
Extra: []serve.Route{{Prefix: serve.WebhooksPrefix, Class: serve.ClassWebhook, Handler: webhooks}},

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning: Nothing tests that the webhook route is mounted and reachable through serve.New
The existing tests call Server.Webhook directly and inject r.SetPathValue("cluster", ...) and r.SetPathValue("name", ...) (server_test.go:121). They would still pass if the mux pattern at line 222 used different wildcard names, which would leave both path values empty. They would also pass if the Extra route were dropped from serve.Options. This PR's whole purpose is that wiring, and there is no test for it. A table-style test in the providers/code main package (it already has native_routing_test.go) should build the handler as main does and assert a signed POST to /webhooks/github/<cluster>/<name> reaches Webhook with both values set. It should also assert that a GET to the same path returns 405 and that an unknown /webhooks/other path returns 404. For example:

func TestWebhookRouteMounted(t *testing.T) {
	var gotCluster, gotName string
	mux := http.NewServeMux()
	mux.HandleFunc("POST /webhooks/github/{cluster}/{name}", func(w http.ResponseWriter, r *http.Request) {
		gotCluster, gotName = r.PathValue("cluster"), r.PathValue("name")
	})
	// serve.New with Extra: []serve.Route{{Prefix: serve.WebhooksPrefix, Class: serve.ClassWebhook, Handler: mux}}
	// POST /webhooks/github/c1/app -> gotCluster=="c1", gotName=="app"; GET -> 405
}

Better still, pull the route registration into a helper that main and the test both call.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 56b6f97. The mux registration is pulled into codeWebhookRoutes, which both main and the test call. TestWebhookRouteMounted builds the handler through serve.New and checks that a POST to /webhooks/github/c1/app reaches the webhook with cluster and name set and the signature header intact. It also checks that GET returns 405 and that /webhooks/other returns 404. Verified locally with go test -count=1 ./... in providers/code (all packages pass, using a placeholder portal/dist).

@cwilhit

cwilhit commented Nov 12, 2026

Copy link
Copy Markdown
Collaborator Author

Re the note about docs/provider-connectivity-contract.md:184: addressed in 56b6f97. The class (g) row now lists both shapes: agents /webhooks/{kind}/{clusterID}/{name}/{token} with its URL token, and Code /webhooks/github/{clusterID}/{connection}, verified by the X-Hub-Signature-256 body HMAC. Both providers are named as examples. 239c7f9 also moves the webhook body bound ahead of the binding reads (details on the first inline thread). make verify-provider-contract passes.

Base automatically changed from codex/factory-code-pr-operations to main November 12, 2026 04:14
cwilhit and others added 3 commits November 11, 2026 22:14
The size check ran after the Connection, Secret and Repository reads, so
an unsigned oversized POST got 400 for a connection that exists and 404
for one that does not, which is still an existence probe. Read and bound
the body first: every pre-signature reply is now independent of what
exists, and oversized requests cost no kcp reads.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@cwilhit
cwilhit force-pushed the codex/factory-code-github-comment-webhook branch from b4f25d9 to 12c57d2 Compare November 12, 2026 04:14
@cwilhit
cwilhit merged commit b7a36ed into main Nov 12, 2026
35 checks passed
cwilhit added a commit that referenced this pull request Nov 12, 2026
Revert PR #1082 and the first-parent changes from PR #1081, including PR #1083. Preserve the unrelated changes on main through #1056.
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.

2 participants