Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .agents/skills/verify-lane-schedule/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
16 changes: 7 additions & 9 deletions .agents/skills/verify-lane-schedule/model/fingerprints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
6 changes: 5 additions & 1 deletion docs/reference/http-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions docs/reference/webhooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

```
Expand Down
23 changes: 6 additions & 17 deletions internal/api/decode_body.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -152,23 +144,20 @@ 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)
}

// 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
Expand Down
139 changes: 139 additions & 0 deletions internal/api/echo_bounds_test.go
Original file line number Diff line number Diff line change
@@ -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())
}
})
}
}
4 changes: 2 additions & 2 deletions internal/api/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
Loading
Loading