diff --git a/internal/api/README.md b/internal/api/README.md index deeaa374..aaa14c29 100644 --- a/internal/api/README.md +++ b/internal/api/README.md @@ -36,6 +36,7 @@ The REST API (`/api/v1/...`) and its `/api/repos/{repo_name}` alias layer. `New` - `validUpstreamHost` rejects userinfo, query, fragment, and non-http(s) schemes [handlers_sync.go:validUpstreamHost] - Text the request asks to store answers 400 on every backend when it holds a NUL byte (a JSON body decodes invalid UTF-8 to U+FFFD, so only a NUL reaches the check): a webhook `secret`, a credential `username` and `password`, the `owner`, `name` and `auth.token` of a sync link, and a `pull-upstream` `ref`. A webhook or credential ID with a NUL byte or invalid UTF-8 answers the 404 of an unknown ID [errors.go:invalidTextFields] [handlers_webhooks.go:handleCreate] [handlers_credentials.go:handleCreateCredential] [handlers_sync.go:validateSyncBaseRepo] [handlers_sync.go:parseUpstreamConfig] [handlers_sync.go:handlePullUpstream] - A request value that git takes as one argument or one environment entry answers 400 before git starts when it does not fit `gitlimit.MaxArgBytes` with what the server puts around it: a read `path` or grep `paths` entry (`maxGitPathBytes`), a blame `range` and a grep `query.pattern`, and the `name` and `email` of an author, a committer or a note author (`maxGitIdentBytes`). A commit message goes to `commit-tree` on stdin. A list of such values (the grep `paths`, the diff `path` params, the blame `range` params) also answers 400 when its entries do not fit `gitlimit.MaxArgListBytes` together [handlers_read_query.go:listTooLong] [handlers_read_core.go:readPathOK] [handlers_read_query.go:cleanPathParams] [handlers_read_query.go:handleBlame] [handlers_read_query.go:handleGrep] [handlers_read_query.go:grepPathProblems] [handlers_write_commit.go:wcAuthorProblems] [handlers_write_refs.go:wrefHandleNote] [handlers_write_commit.go:wcScratch.writeCommit] +- A sync link or credential that the sync worker could not hand to git answers 400 when it is stored: an upstream URL (`upsync.Upstream.CloneURL`) over `upsync.MaxUpstreamURLBytes`, naming the owner, the name and the host field the body sent, and a `base_repo.auth.token`, credential `username` or `password` over `upsync.MaxCredentialBytes`. The worker checks the same limits again for values stored before [handlers_sync.go:upstreamURLProblem] [handlers_sync.go:validateSyncBaseRepo] [handlers_credentials.go:gcredValidate] - `handleDeleteBranch` refuses to delete the default branch in the persistent namespace [handlers_write_refs.go:handleDeleteBranch] ## Side Effects diff --git a/internal/api/handlers_credentials.go b/internal/api/handlers_credentials.go index 94a3ab73..3d38fbe7 100644 --- a/internal/api/handlers_credentials.go +++ b/internal/api/handlers_credentials.go @@ -138,8 +138,8 @@ func gcredValidate(withID bool, id, username, password string) error { if withID && id == "" { idErr = newFieldError("id", "is required", "id is required") } - // The sync worker hands each value to git in an environment entry, - // which must fit upsync.MaxCredentialBytes. + // The sync worker hands each value to git in an environment entry of + // its own, so each value must be at most upsync.MaxCredentialBytes. if password == "" { passwordErr = newFieldError("password", "is required", "password is required") } else if passwordErr = invalidTextFields([2]string{"password", password}); passwordErr == nil { diff --git a/internal/api/long_input_test.go b/internal/api/long_input_test.go index cb853fa4..70ef1442 100644 --- a/internal/api/long_input_test.go +++ b/internal/api/long_input_test.go @@ -606,6 +606,21 @@ func TestReadValueListsPastGitArgBudget(t *testing.T) { } } + // The list is checked before the repository is looked up and + // before git starts: a list over the budget answers its 400 for + // a repository that does not exist, not 404. + org := e.newOrg(t) + missing := e.mint(t, tokenSpec{iss: org, repo: "https://example.com/" + org + "/missing", scopes: []string{auth.ScopeGitRead}}) + for _, tc := range cases { + suffix, body := tc.req(list(tc.count, gitlimit.MaxArgListBytes+1, tc.value)) + method := http.MethodGet + if body != nil { + method = http.MethodPost + } + resp, out := e.doReq(t, method, "/api/v1/repos"+suffix, missing, body, "") + requireListTooLong(t, resp, out, tc.list, body != nil) + } + // 200,000 entries of one byte fit a 1 MiB body. git got them as // 4 MB of arguments, past the 2 MiB that exec takes with an 8 MiB // stack limit, and the grep answered 500. @@ -616,16 +631,56 @@ func TestReadValueListsPastGitArgBudget(t *testing.T) { resp, out := e.doReq(t, http.MethodPost, "/api/v1/repos/grep", token, grepBody(paths), "") requireListTooLong(t, resp, out, "paths", true) - // An entry with a problem of its own is named, and the list is not, - // also when good entries follow it. - paths[0] = "../x" - resp, out = e.doReq(t, http.MethodPost, "/api/v1/repos/grep", token, grepBody(paths[:3001]), "") - var p problemBody - if err := json.Unmarshal(out, &p); err != nil { - t.Fatal(err) + // The count is of the request as sent: a bare "/" gives git no + // argument, but counts 1 + 32 bytes like any other entry. + for _, n := range []int{1985, 1986} { + resp, out = e.doReq(t, http.MethodPost, "/api/v1/repos/grep", token, grepBody(slices.Repeat([]string{"/"}, n)), "") + if n*(1+gitlimit.ArgListEntryBytes) > gitlimit.MaxArgListBytes { + requireListTooLong(t, resp, out, "paths", true) + } else if resp.StatusCode != http.StatusOK { + t.Fatalf("%d bare / entries: status = %d, want 200 (body %.300s)", n, resp.StatusCode, out) + } + } + + // An entry with a problem of its own is named, and the list total is + // not checked, wherever that entry is in a list over the budget. + over := paths[:3001] // 3,001 one-byte entries count 99,033 bytes + for _, bad := range []int{0, 1500, 3000} { + entries := slices.Clone(over) + entries[bad] = "../x" + resp, out = e.doReq(t, http.MethodPost, "/api/v1/repos/grep", token, grepBody(entries), "") + var p problemBody + if err := json.Unmarshal(out, &p); err != nil { + t.Fatal(err) + } + want := "paths." + strconv.Itoa(bad) + if resp.StatusCode != http.StatusBadRequest || len(p.InvalidParams) != 1 || p.InvalidParams[0].Name != want { + t.Fatalf("status %d, invalid_params %+v; want 400 naming %s only", resp.StatusCode, p.InvalidParams, want) + } } - if resp.StatusCode != http.StatusBadRequest || len(p.InvalidParams) != 1 || p.InvalidParams[0].Name != "paths.0" { - t.Fatalf("status %d, invalid_params %+v; want 400 naming paths.0 only", resp.StatusCode, p.InvalidParams) + // The query routes answer with the entry's own message. + ranges := slices.Repeat([]string{blameRange(5000)}, maxBlameRanges) // 80,512 bytes counted + for _, tc := range []struct { + name, route, param string + list []string + bad int + entry, want string + }{ + {"diff outside", "/api/v1/repos/diff?ref=main&", "path", over, 1500, "../x", `path filter "../x" is outside the repository`}, + {"diff blank", "/api/v1/repos/diff?ref=main&", "path", over, 1500, " ", `invalid path filter " "`}, + {"branches/diff outside", "/api/v1/repos/branches/diff?branch=main&", "path", over, 3000, "../x", `path filter "../x" is outside the repository`}, + {"blame blank", "/api/v1/repos/blame?ref=main&path=a.txt&", "range", ranges, 8, " ", `invalid range " "`}, + } { + entries := slices.Clone(tc.list) + entries[tc.bad] = tc.entry + resp, out = e.doReq(t, http.MethodGet, tc.route+url.Values{tc.param: entries}.Encode(), token, nil, "") + var p problemBody + if err := json.Unmarshal(out, &p); err != nil { + t.Fatal(err) + } + if resp.StatusCode != http.StatusBadRequest || p.Detail != tc.want { + t.Fatalf("%s: status %d, detail %q; want 400 %q", tc.name, resp.StatusCode, p.Detail, tc.want) + } } }) } diff --git a/internal/api/sync_limits_test.go b/internal/api/sync_limits_test.go index 8d0b0b0f..1ab585b8 100644 --- a/internal/api/sync_limits_test.go +++ b/internal/api/sync_limits_test.go @@ -172,6 +172,14 @@ func TestSyncValuesGitCannotTakeAtRegistration(t *testing.T) { {name: "PUT by path username and password over", method: http.MethodPut, path: byPath, token: repoTok, body: both, wantError: bothMsg, want: bothItems}, {name: "POST password over", method: http.MethodPost, path: v1, token: repoTok, body: `{"password":"` + long + `"}`, wantError: "invalid password (want at most 131042 bytes)", want: []ipParam{{"password", credWhy}}}, + // A username over the limit is a problem on its own, next to + // a valid password. + {name: "POST username over next to a short password", method: http.MethodPost, path: v1, token: repoTok, + body: `{"username":"` + long + `","password":"p"}`, wantError: "invalid username (want at most 131042 bytes)", + want: []ipParam{{"username", credWhy}}}, + {name: "PUT by path username over next to a short password", method: http.MethodPut, path: byPath, token: repoTok, + body: `{"username":"` + long + `","password":"p"}`, wantError: "invalid username (want at most 131042 bytes)", + want: []ipParam{{"username", credWhy}}}, // A value over the limit that also holds a NUL is one problem. {name: "POST username over with a NUL", method: http.MethodPost, path: v1, token: repoTok, body: `{"username":"` + long + `\u0000","password":"p"}`, wantError: "invalid username (want UTF-8 text without NUL)",