diff --git a/.agents/skills/verify-lane-schedule/SKILL.md b/.agents/skills/verify-lane-schedule/SKILL.md index ad83098dd..8089b14b0 100644 --- a/.agents/skills/verify-lane-schedule/SKILL.md +++ b/.agents/skills/verify-lane-schedule/SKILL.md @@ -226,7 +226,7 @@ With every fact TRUE in the code, setting each fact FALSE in the model alone fai ## Coverage record -Sync commit: `22ac87c7554d56bcd3cd0f7fca999728cb8d0334` (the last commit at which someone confirmed that the model describes this code; only `check.sh --record` moves it). +Sync commit: `6aa76aaaa7fbff368097700d882d17967ae7a2c5` (the last commit at which someone confirmed that the model describes this code; only `check.sh --record` moves it). Every declaration in these files is covered: diff --git a/.agents/skills/verify-lane-schedule/model/fingerprints.txt b/.agents/skills/verify-lane-schedule/model/fingerprints.txt index 5d841c3ae..4fc8b6266 100644 --- a/.agents/skills/verify-lane-schedule/model/fingerprints.txt +++ b/.agents/skills/verify-lane-schedule/model/fingerprints.txt @@ -65,7 +65,6 @@ cmd/openstorage/wire_eventing_local.go:import_"time" 9dae0539db6e1e0ec43a0c7ef22 cmd/openstorage/wire_eventing_local.go:restIntentBeforeCAS 304db8998e396d39da976d3d2218d2972dbc2ddadc6b51b576bb053d4575c747 internal/api/handlers_sync.go:addFields eb7bbf22ad89084587f511413a6d677d529a41ac089b5b08b3d9729d491eb66a internal/api/handlers_sync.go:const_deprecationHeader 188405d30be79bf4a07cacfd258badc10e47e41baa71a10f0ea6ca320d0669df -internal/api/handlers_sync.go:const_maxQuotedValueBytes 3535e2652322f905ebf42eedd9adf5ac981ab62d766c317af592007564638cac internal/api/handlers_sync.go:const_pullUpstreamAcceptedMsg 76a52910111a2580c149b7ea535374dd1196bde9753bf900ed6667fc5763bdb4 internal/api/handlers_sync.go:const_pushUpstreamAcceptedMsg a2ae388107c13efba0294df359ec78e2d5b92bbb2774f068e5507891b5fea698 internal/api/handlers_sync.go:const_upstreamAppAccessMsg e754e2f035f781ed53cc2ee4cf96a4ee8988a87fa93954aca9989c2040b75599 @@ -84,24 +83,23 @@ internal/api/handlers_sync.go:import_"io" 93c8d110e63e2ae040bf1dfd2ae6e7c4b4ff1b internal/api/handlers_sync.go:import_"net/http" 480be5af96d08206ab18c2509cb88ca7831e506d1256768f81e9dd07ebf030a8 internal/api/handlers_sync.go:import_"net/netip" ee93e0a6774d156ffb7dee8e4b29220dbe7dc488e036f596c8ae70f318ffc70e internal/api/handlers_sync.go:import_"net/url" dbfdd047eb1b2a38b79ee147694d9d1308190f3c019908fa1ebf5e3fd13e2343 -internal/api/handlers_sync.go:import_"strconv" 4259f4eb8a51f255fe15fe2b5dea61adc7a54b5df7345920fed692a971b4af46 internal/api/handlers_sync.go:import_"strings" 347f34c3a37316b52e87798b22bb4c8e6173f20f936b730bf65dc5ce6c1fb407 internal/api/handlers_sync.go:import_"time" 9dae0539db6e1e0ec43a0c7ef2211f001e3ddc574b4fc19244eab79b05e23f5a internal/api/handlers_sync.go:isStrictPullBody ec4bff99487fd47a4c11c3cc2714cee70b94035771aa148211f4abbfd7e798f5 internal/api/handlers_sync.go:isUpstreamDetach 25715f9e18d1bf7bc62e630e8164eab041ad70b6a627b2bb52c4ba6f1c177165 internal/api/handlers_sync.go:parseUpstreamConfig dd27982847b44628b2a14a4364e4a23ea414ef90a9e7a05cb5f7edc52e12cc00 internal/api/handlers_sync.go:policyDeniesPullUpstream 098960c7bef537c91ca905ce894432882eef7ef465a3ddb0b0f9e7224be746e7 -internal/api/handlers_sync.go:quoteValue 65ba4a04ae8697e574a042770e053ec58c5e667a3a12ab1ee3f171afac190ff4 +internal/api/handlers_sync.go:refPolicyOpDenies 14c74420941dbb32b29b6bccd73d4c8029dc794fba6a8df30beaf91104b7c0f1 internal/api/handlers_sync.go:registerSyncRoutes 30a124203d6812c4b8fa939a4866caae6a74ac2b0b71e36a7de2fed3e0c25134 internal/api/handlers_sync.go:sentHost 06d61ad20c0eda8ee6d85423eba7a886af4d12740a62517242e024cb01e1381d internal/api/handlers_sync.go:syncServer.answerTakenSyncName 22cf55fac3487ceec250be953518f3cd9b54265fa9061a6f94c4dd2d8567b0e5 internal/api/handlers_sync.go:syncServer.completeOwnSyncCreate a301e0bad45ed03f793aa89eca3036f4e652051a33060812075c97ccf1076b16 internal/api/handlers_sync.go:syncServer.createAbandon 07809dcf477a5a8cec0ff8c63825bddf587cc49377865352ec4ce616d5ac9fda -internal/api/handlers_sync.go:syncServer.finishPendingSync bf0466eb3d30c1df4c6678fb30b4e84026f794f35227ddc79676f911b2a26e79 -internal/api/handlers_sync.go:syncServer.handleCreateRepo 2a16e28b69a5ba40b28a10b26bc4eb76f41e4bfdeea45f2547ef1ade0f085a92 +internal/api/handlers_sync.go:syncServer.finishPendingSync f6c0ab3647777b351f0a1a47390eb5a0f7923abb338ae110c1ab352b9d944dd3 +internal/api/handlers_sync.go:syncServer.handleCreateRepo 89ee49bf35d2743731f3301ddc56e75f8bfe60875c72b8069c1fea080d0eb933 internal/api/handlers_sync.go:syncServer.handleDetachBase a1a966b58e18eee556a253455d87b8f1642cdc009314c592e5e7371e87e7013b -internal/api/handlers_sync.go:syncServer.handlePullUpstream eeacae04d7ad19517907644396e70fb4d3f14f06ead3a0e600c6b493b75dbbaa -internal/api/handlers_sync.go:syncServer.handlePushUpstream 75f5fc0a37eba5b95b2e82b6f049ede62606857395b42ef79fe61a05aa4c71fd +internal/api/handlers_sync.go:syncServer.handlePullUpstream ecec930462dc4e4add2c6ffc630a6550a27f167f55348ebf3612982041b67a12 +internal/api/handlers_sync.go:syncServer.handlePushUpstream 8fca21a57ba65c7a3090d8a71f08e2c44c8bad6b4fa8d32c2130eef484fc8d65 internal/api/handlers_sync.go:syncServer.linked 8459608354ca43e6b1b94a7cd6ecb02d7f1817b80b4b939f6e6653afeab141e3 internal/api/handlers_sync.go:syncServer.syncCompletionOK c365642e8d5279cb9c112afd7f1bec02933dd02b4712137194b8a437507dcf66 internal/api/handlers_sync.go:textField d5b3a42d9e3669c19d63416a4b6c478834b86a73f011a25d7e2d60a8badb65b9 @@ -114,11 +112,11 @@ internal/api/handlers_sync.go:type_usyncBaseRepo fa63c2d8bb62db2a56acff9a8de55e5 internal/api/handlers_sync.go:type_usyncBaseRepoAuth cbce39e3f3df0443cfcb499a9e439e361335ee86e6e97c205c21b18b48141cd6 internal/api/handlers_sync.go:type_usyncPullUpstreamRequest b98132ea1ae7690f3f00aece7dd8fb2a00f69d0e9ea5f1f95c198c983cc18b0b internal/api/handlers_sync.go:type_usyncPushUpstreamRequest 3ad57ba2d21ad394741cb6b3ced05a273c72b38ca84d7a73b34d79b4510f4284 -internal/api/handlers_sync.go:upstreamConfig.validate 5d6cda757706a13060fb8b917b6931dd5927bf427d038cd707471b23ae4b555f +internal/api/handlers_sync.go:upstreamConfig.validate 4c75ed7ea1b26bcef15daa0358e3d79ee704eb27b8d1fbdb9861d7a0d4f2f509 internal/api/handlers_sync.go:upstreamHostname f65b41a625e2606e71c567cbc6d99d991e59b6ef4da9e976294b9ff3895aac34 internal/api/handlers_sync.go:upstreamURLProblem e66d2c29ef48b6891a62cca5ace9afbd52d579f5f4ae11aeba3cc61b3f3c488d internal/api/handlers_sync.go:validUpstreamHost f63de7d7ddea98fa3760cd88a1077488c937d0aae1dcf51e8aa30075618027af -internal/api/handlers_sync.go:validateSyncBaseRepo 0d0acd7ad181d241c2da2a6e48dfaa23aed47c8847cfb8a4992e3693056c2ed1 +internal/api/handlers_sync.go:validateSyncBaseRepo df7737d1b71ebccd3220b074e9002ee36edc83b549338e321394c012a9a6a807 internal/api/handlers_sync.go:withStrictPullBody 449923a2f639f42183416d391cac81b304bcf8b6cda9d207474e66e16178766f internal/api/handlers_sync.go:withUpstreamDetach b16659bb293b5f9eb05b5ade652dfe4fb224e8a7b6c475829007ee1933a164ec internal/events/refstore.go:BaseRepoID d45f84f8f7f8b9e27d21c7b17553527e76db8c3a5fa34718d2768840b351c8ae diff --git a/docs/reference/http-api.md b/docs/reference/http-api.md index b1530b793..a24ba4d7c 100644 --- a/docs/reference/http-api.md +++ b/docs/reference/http-api.md @@ -1003,7 +1003,11 @@ through this API) holds a ref that the run would otherwise forward at an older v the request stays pending, and a new run starts a few seconds later. **Ref policies fail closed:** any policy op denies pull-upstream, push-upstream, and a -sync create at `POST /repos` (403). +sync create at `POST /repos` (403). The 403 quotes the op (`ref policy op "no-push" +denies pull-upstream`); an op of more than 256 bytes is cut there, as an unknown member +name is (`"xx…" (100000 bytes)`). The 403s of `PATCH /repo` and `DELETE /repos/delete` +quote it the same way, and so does the 403 of a ref, commit or merge write for an op +that the server does not know (`unknown policy op "x" for refs/heads/main`). The same gate covers the sync that a ref write schedules. A REST write or git push from a token that carries any ref-policy op does not trigger the inbound sync, since the sync force-updates every head and tag from the upstream, not just the refs that token may diff --git a/docs/reference/webhooks.md b/docs/reference/webhooks.md index 8aea648a9..ad4d70dac 100644 --- a/docs/reference/webhooks.md +++ b/docs/reference/webhooks.md @@ -21,6 +21,13 @@ the body omits it. `GET` never echoes secrets. A `secret` that holds a NUL byte (`invalid secret (want UTF-8 text without NUL)`), and nothing is stored. A `DELETE` `{id}` that holds a NUL byte or invalid UTF-8 names no webhook → 404, as an unknown `id` does. +A `url` that the egress guard refuses at registration → 400 (`invalid webhook url: …`), +and nothing is stored. The answer quotes the refused host, or the whole URL when it has +no host (`https://:80/hook`). A host or URL of more than 256 bytes is cut there to its +first 256 bytes or fewer, at a character, and ends with `…` and its length in bytes. For +a URL whose IPv6 zone has 100,000 bytes: `invalid webhook url: webhook target +"fe80::1%xx…" (100008 bytes) denied: zone-scoped address`. + ## Delivery headers ``` diff --git a/internal/api/decode_body.go b/internal/api/decode_body.go index 1c9d91a5c..59651dbc1 100644 --- a/internal/api/decode_body.go +++ b/internal/api/decode_body.go @@ -130,14 +130,6 @@ func paramsText(params []invalidParam) string { return strings.Join(items, "; ") } -// maxUnknownNameBytes bounds the member name that an answer naming one -// unknown member repeats (unknownNameText). The longest field name of a -// request body has 25 bytes, so a misspelled name is repeated whole. A -// longer name is no field name, and the body limit lets it reach 1 MiB: -// `error` and `detail` repeat it with escapes of up to 6 bytes for each -// byte, which made answers of 12 MB. -const maxUnknownNameBytes = 256 - // unknownFieldText returns `unknown field "x"` for the error that // encoding/json gives a member that DisallowUnknownFields refuses, and "" // for any other error. That error has no type: its text is fixed (pinned by @@ -152,7 +144,7 @@ func unknownFieldText(err error) string { if uerr != nil { return "" } - if len(name) <= maxUnknownNameBytes { + if len(name) <= errtext.MaxQuotedBytes { return "unknown field " + rest } return unknownNameText("", name) @@ -160,15 +152,12 @@ func unknownFieldText(err error) string { // unknownNameText returns `unknown field "x"` for an unknown member name // of the object at the dotted path path ("" for the body), x being -// path+name. A name of more than maxUnknownNameBytes is cut at a -// character to at most that many bytes, and the text ends with an -// ellipsis and the length of the name: `unknown field "xx…" (1000000 bytes)`. +// path+name. The name is quoted with errtext.Quote: one of more than +// errtext.MaxQuotedBytes is cut, and the text ends with the length of the +// name: `unknown field "xx…" (1000000 bytes)`. No field name of a request +// body has more than 25 bytes, so a misspelled one is quoted whole. func unknownNameText(path, name string) string { - text := "unknown field " + strconv.Quote(path+errtext.Cut(name, maxUnknownNameBytes)) - if len(name) > maxUnknownNameBytes { - text += " (" + strconv.Itoa(len(name)) + " bytes)" - } - return text + return "unknown field " + errtext.Quote(path, name) } // nullMembers returns body with every value at the dotted path field diff --git a/internal/api/echo_bounds_test.go b/internal/api/echo_bounds_test.go new file mode 100644 index 000000000..5eae02ed8 --- /dev/null +++ b/internal/api/echo_bounds_test.go @@ -0,0 +1,139 @@ +package api + +import ( + "encoding/json" + "net/http" + "net/http/httptest" + "strings" + "testing" + + "github.com/coder/openstorage/internal/auth" + "github.com/coder/openstorage/internal/ref" +) + +// TestRefusalsQuoteRequestTextBounded: an answer that quotes a webhook URL, +// its host, or the ref policy op of a token cuts the text at 256 bytes and +// names its length (errtext.Quote), so it stays small whatever the request +// holds. Before, each of these answered about 196 KB for 100,000 bytes of +// `<`, which JSON escapes to six bytes each. +func TestRefusalsQuoteRequestTextBounded(t *testing.T) { + long := strings.Repeat("<", 100_000) + cut := strings.Repeat("<", 256) + "…" + check := func(t *testing.T, resp *http.Response, out []byte, wantStatus int, want string) { + t.Helper() + var p problemBody + if err := json.Unmarshal(out, &p); err != nil { + t.Fatal(err) + } + if resp.StatusCode != wantStatus || p.Detail != want { + t.Fatalf("status %d, detail %.400q; want %d %.400q", resp.StatusCode, p.Detail, wantStatus, want) + } + if len(out) > 8<<10 { + t.Fatalf("answer is %d bytes, want at most 8 KiB", len(out)) + } + } + runBackends(t, func(t *testing.T, e *testEnv) { + org := e.newOrg(t) + whTok := orgToken(t, e, org) + for _, tc := range []struct{ name, url, want string }{ + {"a zone-scoped host", "https://[fe80::1%25" + long + "]/hook", + `invalid webhook url: webhook target "fe80::1%` + strings.Repeat("<", 256-8) + `…" (100008 bytes) denied: zone-scoped address`}, + {"a URL with no host", "https://:80/" + long, + `invalid webhook url: webhook url "https://:80/` + strings.Repeat("<", 256-12) + `…" (100012 bytes) has no host`}, + {"a short host", "https://[fe80::1%25eth0]/hook", + `invalid webhook url: webhook target "fe80::1%eth0" denied: zone-scoped address`}, + } { + t.Run("webhook "+tc.name, func(t *testing.T) { + body, err := json.Marshal(map[string]string{"url": tc.url}) + if err != nil { + t.Fatal(err) + } + resp, out := e.doReq(t, http.MethodPost, "/api/v1/webhooks", whTok, body, "") + check(t, resp, out, http.StatusBadRequest, tc.want) + }) + } + + for _, op := range []string{long, "no-push"} { + quoted := `"no-push"` + if op == long { + quoted = `"` + cut + `" (100000 bytes)` + } + refs := `[["*",["` + op + `"]]]` + name := "ops-" + randomHex(t, 4) + // Each route checks the policy before it looks the repository + // up, so the repository need not exist. + for _, tc := range []struct { + name, method, path, body, what string + scopes []string + }{ + {"sync create", http.MethodPost, "/api/v1/repos", `{"upstream":{"provider":"gitea","owner":"o","name":"n","host":"forge.example.invalid"}}`, "sync repository create", []string{auth.ScopeRepoWrite}}, + {"repository deletion", http.MethodDelete, "/api/v1/repos/delete", "", "repository deletion", []string{auth.ScopeRepoWrite}}, + {"default branch change", http.MethodPatch, "/api/v1/repo", `{"default_branch":"main"}`, "changing the default branch", []string{auth.ScopeRepoWrite}}, + {"pull-upstream", http.MethodPost, "/api/v1/repos/pull-upstream", "", "pull-upstream", []string{auth.ScopeGitWrite}}, + {"push-upstream", http.MethodPost, "/api/v1/repos/push-upstream", "", "push-upstream", []string{auth.ScopeGitWrite, auth.ScopeGitRead}}, + } { + opName := "a short op" + if op == long { + opName = "a long op" + } + t.Run(tc.name+" with "+opName, func(t *testing.T) { + tok := e.mint(t, tokenSpec{iss: org, repo: name, scopes: tc.scopes, refs: refs}) + var body []byte + if tc.body != "" { + body = []byte(tc.body) + } + resp, out := e.doReq(t, tc.method, tc.path, tok, body, "") + check(t, resp, out, http.StatusForbidden, "ref policy op "+quoted+" denies "+tc.what) + }) + } + } + }) +} + +// TestUnknownPolicyOpQuotedBounded: a ref write (refs and tags), a commit +// write and a merge refuse a ref policy op that they do not know with a +// 403 that quotes the op with errtext.Quote, so an op of any length makes +// a short answer. +func TestUnknownPolicyOpQuotedBounded(t *testing.T) { + const refName = "refs/heads/main" + long := strings.Repeat("<", 100_000) + for _, tc := range []struct{ name, op, quoted string }{ + {"a long op", long, `"` + strings.Repeat("<", 256) + `…" (100000 bytes)`}, + {"a short op", "no-fly", `"no-fly"`}, + } { + t.Run(tc.name, func(t *testing.T) { + want := "unknown policy op " + tc.quoted + " for " + refName + claims := &auth.Claims{Refs: json.RawMessage(`[["main",["` + tc.op + `"]]]`)} + newSHA := strings.Repeat("a", 40) + + deny, err := wrefPolicyCheck(claims, refName, ref.ZeroSHA, newSHA, nil) + if err != nil || deny != want { + t.Fatalf("wrefPolicyCheck = %.300q, %v; want %.300q", deny, err, want) + } + + // A commit write route answers a 403 in a problem body + // (commitWriteEnvelopeStatuses lists no 403). + rec := httptest.NewRecorder() + if wcCheckRefPolicy(&envelopeWriter{ResponseWriter: rec}, claims, refName) { + t.Fatal("wcCheckRefPolicy admitted an unknown op") + } + var wc problemBody + if err := json.Unmarshal(rec.Body.Bytes(), &wc); err != nil || rec.Code != http.StatusForbidden || wc.Detail != want { + t.Fatalf("wcCheckRefPolicy: %d %.300s; want 403 %.300q", rec.Code, rec.Body.Bytes(), want) + } + + rec = httptest.NewRecorder() + r := httptest.NewRequest(http.MethodPost, "/api/v1/repos/merge", nil).WithContext(auth.WithClaims(t.Context(), claims)) + if (&server{}).wmCheckRefPolicy(rec, r, nil, refName, ref.ZeroSHA, newSHA, false, "main") { + t.Fatal("wmCheckRefPolicy admitted an unknown op") + } + var p problemBody + if err := json.Unmarshal(rec.Body.Bytes(), &p); err != nil || rec.Code != http.StatusForbidden || p.Detail != want { + t.Fatalf("wmCheckRefPolicy: %d %.300s; want 403 %.300q", rec.Code, rec.Body.Bytes(), want) + } + if rec.Body.Len() > 8<<10 { + t.Fatalf("merge answer is %d bytes, want at most 8 KiB", rec.Body.Len()) + } + }) + } +} diff --git a/internal/api/handlers.go b/internal/api/handlers.go index 28995606b..41dc525c0 100644 --- a/internal/api/handlers.go +++ b/internal/api/handlers.go @@ -616,7 +616,7 @@ func (s *server) handlePatchRepo(w http.ResponseWriter, r *http.Request) { // against refs/heads/HEAD must match. The pseudoref itself is checked // separately inside the helper. if op, denied := policyDeniesDefaultBranchChange(policies, target); denied { - writeErr(w, http.StatusForbidden, "ref policy op "+strconv.Quote(op)+" denies changing the default branch") + writeErr(w, http.StatusForbidden, refPolicyOpDenies(op, "changing the default branch")) return } // ADR-21: the repo claim carries the url or the repo_id; resolve either @@ -788,7 +788,7 @@ func (s *server) handleDeleteRepo(w http.ResponseWriter, r *http.Request) { return } if op, denied := policyDeniesRepoDelete(policies); denied { - writeErr(w, http.StatusForbidden, "ref policy op "+strconv.Quote(op)+" denies repository deletion") + writeErr(w, http.StatusForbidden, refPolicyOpDenies(op, "repository deletion")) return } repo, err := s.store.SoftDeleteRepo(r.Context(), claims.Iss, claims.Repo) diff --git a/internal/api/handlers_sync.go b/internal/api/handlers_sync.go index ed6cc44c6..e1f46982f 100644 --- a/internal/api/handlers_sync.go +++ b/internal/api/handlers_sync.go @@ -28,7 +28,6 @@ import ( "net/http" "net/netip" neturl "net/url" - "strconv" "strings" "time" @@ -122,17 +121,17 @@ func validateSyncBaseRepo(spec *usyncBaseRepo, branchErr error, errs *bodyErrors errs.add(newFieldError("base_repo.operation", "is not supported", `base_repo operation "fork" not supported`)) return "" default: - errs.add(newFieldError("base_repo.operation", "must be sync or fork", "invalid base_repo operation "+quoteValue(spec.Operation))) + errs.add(newFieldError("base_repo.operation", "must be sync or fork", "invalid base_repo operation "+errtext.Quote("", spec.Operation))) return "" } if spec.Provider == "code" || spec.Provider == "code.storage" { errs.add(newFieldError("base_repo.operation", "must be fork for this provider", - "base_repo provider "+quoteValue(spec.Provider)+` requires operation "fork"`)) + "base_repo provider "+errtext.Quote("", spec.Provider)+` requires operation "fork"`)) return "" } defaultHost, known := upsync.ProviderHost(spec.Provider) if !known { - errs.add(newFieldError("base_repo.provider", "is not supported", "unsupported base_repo provider "+quoteValue(spec.Provider))) + errs.add(newFieldError("base_repo.provider", "is not supported", "unsupported base_repo provider "+errtext.Quote("", spec.Provider))) } // Owner, name and token are stored, and PostgreSQL refuses a NUL in a // text value (the token reaches it encrypted only when a secrets key is @@ -153,11 +152,11 @@ func validateSyncBaseRepo(spec *usyncBaseRepo, branchErr error, errs *bodyErrors host = spec.UpstreamHost switch { case host != "" && !validUpstreamHost(host): - hostErr = newFieldError("base_repo.upstream_host", "is not a valid host", "invalid base_repo upstream_host "+quoteValue(spec.UpstreamHost)) + hostErr = newFieldError("base_repo.upstream_host", "is not a valid host", "invalid base_repo upstream_host "+errtext.Quote("", spec.UpstreamHost)) case host == "" && known: if host = defaultHost; host == "" { hostErr = newFieldError("base_repo.upstream_host", "is required for this provider", - "base_repo upstream_host is required for provider "+quoteValue(spec.Provider)) + "base_repo upstream_host is required for provider "+errtext.Quote("", spec.Provider)) } } urlErr := upstreamURLProblem("base_repo", spec.Owner, spec.Name, host, hostErr, sentHost(spec.UpstreamHost, "base_repo.upstream_host"), tokenErr, branchErr) @@ -206,24 +205,6 @@ func upstreamURLProblem(prefix, owner, name, host string, hostErr error, hostPat return newFieldErrors(strings.Join(msgs, "; "), params...) } -// maxQuotedValueBytes bounds a value of a sync link that an answer quotes -// (a provider, an operation, an access or a host), as maxUnknownNameBytes -// bounds an unknown member name. A valid value is far shorter, and the body -// limit lets a value reach 1 MiB, which JSON escapes can make six times -// longer. -const maxQuotedValueBytes = 256 - -// quoteValue quotes v for an answer. A value of more than -// maxQuotedValueBytes is cut at a character to at most that many bytes, -// and ends with an ellipsis and its length in bytes: "xx…" (1000000 bytes). -func quoteValue(v string) string { - q := strconv.Quote(errtext.Cut(v, maxQuotedValueBytes)) - if len(v) > maxQuotedValueBytes { - q += " (" + strconv.Itoa(len(v)) + " bytes)" - } - return q -} - // sentHost is path, the wire path of a host field, when the body sent the // host (value), and "" when the host is the provider's default. func sentHost(value, path string) string { @@ -325,14 +306,14 @@ func (cfg upstreamConfig) validate(errs *bodyErrors) (host string) { [][2]string{{"upstream.provider", cfg.Provider}, {"upstream.owner", cfg.Owner}, {"upstream.name", cfg.Name}}, func(name, value string) error { if name == "upstream.provider" && !known { - return newFieldError(name, "is not supported", "unsupported upstream provider "+quoteValue(cfg.Provider)) + return newFieldError(name, "is not supported", "unsupported upstream provider "+errtext.Quote("", cfg.Provider)) } return textField(name, value) }) var accessErr, hostErr error switch { case cfg.Access != "" && cfg.Access != "app" && cfg.Access != "public": - accessErr = newFieldError("upstream.access", "must be app or public", "invalid upstream access "+quoteValue(cfg.Access)) + accessErr = newFieldError("upstream.access", "must be app or public", "invalid upstream access "+errtext.Quote("", cfg.Access)) case known && cfg.Provider != "github" && cfg.Access != "": accessErr = newFieldError("upstream.access", "applies to provider github only", "upstream access applies to provider github only") case known && cfg.Provider == "github" && cfg.Access != "public": @@ -341,10 +322,10 @@ func (cfg upstreamConfig) validate(errs *bodyErrors) (host string) { host = cfg.Host switch { case host != "" && !validUpstreamHost(host): - hostErr = newFieldError("upstream.host", "is not a valid host", "invalid upstream host "+quoteValue(cfg.Host)) + hostErr = newFieldError("upstream.host", "is not a valid host", "invalid upstream host "+errtext.Quote("", cfg.Host)) case host == "" && known: if host = defaultHost; host == "" { - hostErr = newFieldError("upstream.host", "is required for this provider", "upstream host is required for provider "+quoteValue(cfg.Provider)) + hostErr = newFieldError("upstream.host", "is required for this provider", "upstream host is required for provider "+errtext.Quote("", cfg.Provider)) } } if urlErr := upstreamURLProblem("upstream", cfg.Owner, cfg.Name, host, hostErr, sentHost(cfg.Host, "upstream.host"), accessErr); urlErr != nil { @@ -500,7 +481,7 @@ func (s *syncServer) handleCreateRepo(w http.ResponseWriter, r *http.Request) { // applies here, before the repo row, the credential and the first // sync exist. if op, denied := policyDeniesPullUpstream(policies); denied { - writeErr(w, http.StatusForbidden, "ref policy op "+strconv.Quote(op)+" denies sync repository create") + writeErr(w, http.StatusForbidden, refPolicyOpDenies(op, "sync repository create")) return } } @@ -747,7 +728,7 @@ func (s *syncServer) finishPendingSync(ctx context.Context, claims *auth.Claims, return err.Error(), 0, nil } if op, denied := policyDeniesPullUpstream(policies); denied { - return "ref policy op " + strconv.Quote(op) + " denies completing a pending sync create", 0, nil + return refPolicyOpDenies(op, "completing a pending sync create"), 0, nil } if !claims.HasScope(auth.ScopeGitWrite) { return "completing a pending sync create needs scope " + auth.ScopeGitWrite + "; POST /repos/pull-upstream with a git:write token finishes it", 0, nil @@ -820,6 +801,13 @@ func policyDeniesPullUpstream(policies []auth.RefPolicy) (string, bool) { return "", false } +// refPolicyOpDenies is the 403 message for the ref policy op of a token +// that denies what. The op is token text of any length, so it is quoted +// with errtext.Quote. +func refPolicyOpDenies(op, what string) string { + return "ref policy op " + errtext.Quote("", op) + " denies " + what +} + // strictPullBodyKey marks a request re-dispatched from POST // /api/repos/{repo_name}/upstream/pull (alias.go); only the alias layer // sets it. @@ -851,7 +839,7 @@ func (s *syncServer) handlePullUpstream(w http.ResponseWriter, r *http.Request) return } if op, denied := policyDeniesPullUpstream(policies); denied { - writeErr(w, http.StatusForbidden, "ref policy op "+strconv.Quote(op)+" denies pull-upstream") + writeErr(w, http.StatusForbidden, refPolicyOpDenies(op, "pull-upstream")) return } var req usyncPullUpstreamRequest @@ -982,7 +970,7 @@ func (s *syncServer) handlePushUpstream(w http.ResponseWriter, r *http.Request) return } if op, denied := policyDeniesPullUpstream(policies); denied { - writeErr(w, http.StatusForbidden, "ref policy op "+strconv.Quote(op)+" denies push-upstream") + writeErr(w, http.StatusForbidden, refPolicyOpDenies(op, "push-upstream")) return } var req usyncPushUpstreamRequest diff --git a/internal/api/handlers_write_commit.go b/internal/api/handlers_write_commit.go index 99c137901..a6abbd4b3 100644 --- a/internal/api/handlers_write_commit.go +++ b/internal/api/handlers_write_commit.go @@ -324,7 +324,7 @@ func wcCheckRefPolicy(w http.ResponseWriter, claims *auth.Claims, refName string wcFail(w, http.StatusForbidden, "forbidden", "policy verify-sig denies unsigned server-created commits on "+refName, "", "") return false default: - wcFail(w, http.StatusForbidden, "forbidden", "unknown policy op "+op+" for "+refName, "", "") + wcFail(w, http.StatusForbidden, "forbidden", "unknown policy op "+errtext.Quote("", op)+" for "+refName, "", "") return false } } diff --git a/internal/api/handlers_write_merge.go b/internal/api/handlers_write_merge.go index eb925162f..96fc86051 100644 --- a/internal/api/handlers_write_merge.go +++ b/internal/api/handlers_write_merge.go @@ -60,6 +60,7 @@ import ( "unicode/utf8" "github.com/coder/openstorage/internal/auth" + "github.com/coder/openstorage/internal/errtext" "github.com/coder/openstorage/internal/gitlimit" "github.com/coder/openstorage/internal/gitread" "github.com/coder/openstorage/internal/ref" @@ -503,7 +504,7 @@ func (s *server) wmCheckRefPolicy(w http.ResponseWriter, r *http.Request, sc *wc // promoted commits already passed hook verification at push // time, so verify-sig lets them through (hook parity). default: - return deny("unknown policy op " + op + " for " + refName) + return deny("unknown policy op " + errtext.Quote("", op) + " for " + refName) } } return true diff --git a/internal/api/handlers_write_refs.go b/internal/api/handlers_write_refs.go index ec9aa3487..c439d8721 100644 --- a/internal/api/handlers_write_refs.go +++ b/internal/api/handlers_write_refs.go @@ -227,7 +227,7 @@ func wrefPolicyCheck(claims *auth.Claims, refName, old, new string, isAncestor f } return fmt.Sprintf("policy verify-sig cannot be satisfied by this endpoint for %s", refName), nil default: - return fmt.Sprintf("unknown policy op %q for %s", op, refName), nil + return "unknown policy op " + errtext.Quote("", op) + " for " + refName, nil } } return "", nil @@ -1046,7 +1046,7 @@ func strictJSON(body []byte, dst any) error { // that JSON escapes to at most 192: at most 105,203 bytes and the request // path in `instance` (TestStrictBodiesBoundTheNamesTheyRepeat). No field // name in upstream's spec has more than 25 bytes. An answer that names -// one unknown member cuts its name at maxUnknownNameBytes, as the other +// one unknown member cuts its name at errtext.MaxQuotedBytes, as the other // decoders do (unknownNameText). const maxListedNameBytes = 32 @@ -1111,7 +1111,7 @@ func (p strictProblem) err(listed bool) error { case strictNotObject: return newFieldError(path, "must be an object", path+" must be a JSON object") } - path = p.path + errtext.Cut(p.name, maxUnknownNameBytes) + path = p.path + errtext.Cut(p.name, errtext.MaxQuotedBytes) msg := unknownNameText(p.path, p.name) if listed { path = p.path + errtext.Cut(p.name, maxListedNameBytes) diff --git a/internal/errtext/README.md b/internal/errtext/README.md index 11706cd1d..056a5f372 100644 --- a/internal/errtext/README.md +++ b/internal/errtext/README.md @@ -7,6 +7,8 @@ Bounds the request text that an error answer repeats, so that an answer stays sm - `MaxBytes` — 16384, the most bytes of a text that an answer repeats [errtext.go:MaxBytes] - `Bound` — cuts a longer text at a rune boundary and ends it with `…`; a text of up to `MaxBytes` bytes comes back unchanged [errtext.go:Bound] - `Cut` — the same cut at a limit that the caller gives [errtext.go:Cut] +- `MaxQuotedBytes` — 256, the most bytes of one request value that an answer quotes [errtext.go:MaxQuotedBytes] +- `Quote` — quotes a server prefix and a request value; a value of more than `MaxQuotedBytes` bytes is cut and followed by its length, `"xx…" (1000000 bytes)` [errtext.go:Quote] ## Invariants - The cut counts bytes before JSON escapes them, so the answer can hold up to six bytes for each byte kept [errtext.go:MaxBytes] @@ -14,7 +16,9 @@ Bounds the request text that an error answer repeats, so that an answer stays sm ## Callers - `api` cuts `error`, `detail` and each `invalid_params` name of a problem body (`newProblem`), and the `result.message` of a commit write (`wcRefUpdate`) [../api/errors.go:newProblem] [../api/handlers_write_commit.go:wcRefUpdate] -- `api` cuts an unknown member name at 256 bytes in an answer that names one unknown member (`unknownNameText`), and each unknown member name at 32 bytes in the answer of a strictly decoded body that names several members (`Cut`) [../api/decode_body.go:unknownNameText] [../api/handlers_write_refs.go:strictProblem.err] +- `api` quotes an unknown member name in an answer that names one unknown member (`unknownNameText`), and cuts each unknown member name at 32 bytes in the answer of a strictly decoded body that names several members (`Cut`) [../api/decode_body.go:unknownNameText] [../api/handlers_write_refs.go:strictProblem.err] +- `api` quotes a refused sync provider, operation, access or host (`upstreamConfig.validate`, `validateSyncBaseRepo`) and the ref policy op of a token in a 403 (`refPolicyOpDenies`) [../api/handlers_sync.go:upstreamConfig.validate] [../api/handlers_sync.go:refPolicyOpDenies] +- `events` quotes a webhook URL or its host in a registration refusal (`CheckRegistration`) [../events/guard.go:CheckRegistration] - `githttp` cuts the error text of the git and LFS routes [../githttp/server.go:httpError] [../githttp/lfs.go:lfsError] ## Verification diff --git a/internal/errtext/errtext.go b/internal/errtext/errtext.go index b20655dbb..4dca5eb84 100644 --- a/internal/errtext/errtext.go +++ b/internal/errtext/errtext.go @@ -1,7 +1,10 @@ // Package errtext bounds the request text that an error answer repeats. package errtext -import "unicode/utf8" +import ( + "strconv" + "unicode/utf8" +) // MaxBytes bounds a text that an error answer can repeat from the request: // the `error`, `detail` and `invalid_params` names of an API problem body, @@ -10,6 +13,14 @@ import "unicode/utf8" // escapes to six, would otherwise make an answer of several MiB. const MaxBytes = 16384 +// MaxQuotedBytes bounds one request value that an answer quotes (Quote): +// an unknown member name, a sync provider, operation, access or host, a +// ref policy op of a token, a webhook URL or its host. A value that the +// server can use is far shorter, so it is quoted whole. A body value can +// reach 1 MiB, and JSON escapes up to six bytes for each byte, so a value +// quoted whole could make an answer of several MiB. +const MaxQuotedBytes = 256 + // Bound cuts s to MaxBytes (Cut). func Bound(s string) string { return Cut(s, MaxBytes) } @@ -25,3 +36,15 @@ func Cut(s string, limit int) string { } return s[:cut] + "…" } + +// Quote quotes prefix+v with strconv.Quote for an answer. A v of more than +// MaxQuotedBytes is cut (Cut), and its length in bytes follows the quote: +// "xx…" (1000000 bytes). prefix is text of the server, such as the path of +// a known field, and is never cut. +func Quote(prefix, v string) string { + q := strconv.Quote(prefix + Cut(v, MaxQuotedBytes)) + if len(v) > MaxQuotedBytes { + q += " (" + strconv.Itoa(len(v)) + " bytes)" + } + return q +} diff --git a/internal/errtext/errtext_test.go b/internal/errtext/errtext_test.go index 27cebb686..1a83c4b60 100644 --- a/internal/errtext/errtext_test.go +++ b/internal/errtext/errtext_test.go @@ -28,3 +28,30 @@ func TestBound(t *testing.T) { }) } } + +func TestQuote(t *testing.T) { + if MaxQuotedBytes != 256 { + t.Fatalf("MaxQuotedBytes = %d, the docs say 256", MaxQuotedBytes) + } + exact := strings.Repeat("<", 256) + for _, tc := range []struct{ name, prefix, in, want string }{ + {"a short value", "", "gitea", `"gitea"`}, + {"a value of exactly the limit", "", exact, `"` + exact + `"`}, + {"a value one byte over", "", exact + "<", `"` + exact + `…" (257 bytes)`}, + {"a long value", "", strings.Repeat("x", 1_000_000), `"` + strings.Repeat("x", 256) + `…" (1000000 bytes)`}, + // The prefix is never cut, and the length is the value's own. + {"a prefix", "base_repo.", exact + "<", `"base_repo.` + exact + `…" (257 bytes)`}, + // Only v counts: a prefix that takes the text past the limit cuts + // nothing. + {"a prefix and a value of exactly the limit", "base_repo.", exact, `"base_repo.` + exact + `"`}, + // A cut inside a rune moves back to its start; Go quotes "é" as is. + {"a cut inside a rune", "", "a" + strings.Repeat("é", 200), `"a` + strings.Repeat("é", 127) + `…" (401 bytes)`}, + {"control characters are escaped", "", "a\nb", `"a\nb"`}, + } { + t.Run(tc.name, func(t *testing.T) { + if got := Quote(tc.prefix, tc.in); got != tc.want { + t.Fatalf("Quote(%q, %d bytes) = %.300q, want %.300q", tc.prefix, len(tc.in), got, tc.want) + } + }) + } +} diff --git a/internal/events/guard.go b/internal/events/guard.go index 3663b401d..35f338789 100644 --- a/internal/events/guard.go +++ b/internal/events/guard.go @@ -11,6 +11,8 @@ import ( "strconv" "syscall" "time" + + "github.com/coder/openstorage/internal/errtext" ) // ErrTargetDenied marks dial-time policy denials (as opposed to ordinary @@ -221,18 +223,28 @@ func (g TargetGuard) vetHost(ctx context.Context, host string) ([]netip.Addr, er // is NOT a delivery-reachability promise (the resolver may be partitioned, // or the record may change later either way). Dial-time enforcement in // NewClient is authoritative. +// +// The error quotes the URL or its host with errtext.Quote: the URL is +// request text of any length, and its host can hold an IPv6 zone of any +// length. func (g TargetGuard) CheckRegistration(ctx context.Context, rawURL string) error { u, err := url.Parse(rawURL) if err != nil { - return fmt.Errorf("parse webhook url: %w", err) + var uerr *url.Error + if errors.As(err, &uerr) { + err = uerr.Err // url.Error repeats the whole URL + } + // The parse error can repeat a part of the URL too, such as an + // invalid port. + return fmt.Errorf("parse webhook url %s: %s", errtext.Quote("", rawURL), errtext.Cut(err.Error(), errtext.MaxQuotedBytes)) } host := u.Hostname() if host == "" { - return fmt.Errorf("webhook url %s has no host", rawURL) + return fmt.Errorf("webhook url %s has no host", errtext.Quote("", rawURL)) } if ip, err := netip.ParseAddr(host); err == nil { if reason := g.deniedReason(ip); reason != "" { - return fmt.Errorf("webhook target %s denied: %s", host, reason) + return fmt.Errorf("webhook target %s denied: %s", errtext.Quote("", host), reason) } return nil } @@ -244,7 +256,7 @@ func (g TargetGuard) CheckRegistration(ctx context.Context, rawURL string) error } for _, ip := range addrs { if reason := g.deniedReason(ip); reason != "" { - return fmt.Errorf("webhook target %s resolves to denied address %s: %s", host, ip, reason) + return fmt.Errorf("webhook target %s resolves to denied address %s: %s", errtext.Quote("", host), ip, reason) } } return nil diff --git a/internal/events/guard_quote_test.go b/internal/events/guard_quote_test.go new file mode 100644 index 000000000..bafd35d0b --- /dev/null +++ b/internal/events/guard_quote_test.go @@ -0,0 +1,39 @@ +package events + +import ( + "context" + "net/netip" + "strings" + "testing" +) + +// TestCheckRegistrationQuotesURLTextBounded: a webhook registration refusal +// quotes the host or the URL it names with errtext.Quote, cut at 256 bytes +// with its length, also for a host that resolves to a denied address and +// for a URL that does not parse. +func TestCheckRegistrationQuotesURLTextBounded(t *testing.T) { + t.Parallel() + + guard := TargetGuard{LookupNetIP: func(context.Context, string) ([]netip.Addr, error) { + return []netip.Addr{netip.MustParseAddr("10.0.0.1")}, nil + }} + longName := strings.Repeat("a", 300) + ".example" + longPort := strings.Repeat("<", 1000) + for _, tc := range []struct{ name, rawURL, want string }{ + {"a host that resolves to a denied address", "https://private.example/hook", + `webhook target "private.example" resolves to denied address 10.0.0.1: private address`}, + {"a long host that resolves to a denied address", "https://" + longName + "/hook", + `webhook target "` + strings.Repeat("a", 256) + `…" (308 bytes) resolves to denied address 10.0.0.1: private address`}, + // url.Parse names the bad port in its error too, so that is cut. + {"a URL that does not parse", "https://h:" + longPort, + `parse webhook url "https://h:` + strings.Repeat("<", 256-10) + `…" (1010 bytes): invalid port ":` + strings.Repeat("<", 256-15) + `…`}, + } { + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + err := guard.CheckRegistration(context.Background(), tc.rawURL) + if err == nil || err.Error() != tc.want { + t.Fatalf("CheckRegistration = %.400v\nwant %.400s", err, tc.want) + } + }) + } +}