Thanks to visit codestin.com
Credit goes to github.com

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
95 changes: 95 additions & 0 deletions coderd/apidoc/docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

82 changes: 82 additions & 0 deletions coderd/apidoc/swagger.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions coderd/coderd.go
Original file line number Diff line number Diff line change
Expand Up @@ -1816,6 +1816,7 @@ func New(options *Options) *API {
r.Put("/gitsshkey", api.regenerateGitSSHKey)
r.Route("/secrets", func(r chi.Router) {
r.Post("/", api.postUserSecret)
r.Post("/batch", api.postUserSecretsBatch)
r.Get("/", api.getUserSecrets)
r.Route("/{name}", func(r chi.Router) {
r.Get("/", api.getUserSecret)
Expand Down
7 changes: 7 additions & 0 deletions coderd/httpapi/httpapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,13 @@ func Read(ctx context.Context, rw http.ResponseWriter, r *http.Request, value in

err := json.NewDecoder(r.Body).Decode(value)
if err != nil {
if _, ok := errors.AsType[*http.MaxBytesError](err); ok {

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.

From Coder Agents:

FYI for reviewers (not a change request): this teaches the shared Read helper to return 413 on *http.MaxBytesError, which is a strict improvement over the old behavior (an oversized body previously surfaced as 400 "Request body must be valid JSON."). Flagging only that it's cross-cutting — the new status now applies to every endpoint that wraps its body in http.MaxBytesReader, not just the new batch import. That looks intentional and it's covered by the new TestRead/BodyTooLarge case; noting it so the shared-behavior change is a conscious sign-off rather than an incidental side effect.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yep, intentional. 413 is the right status for an oversized body, and it only affects endpoints already using http.MaxBytesReader 👍

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

TIL of this syntax vs errors.As

Write(ctx, rw, http.StatusRequestEntityTooLarge, codersdk.Response{
Message: "Request body too large.",
Detail: err.Error(),
})
return false
}
Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
Message: "Request body must be valid JSON.",
Detail: err.Error(),
Expand Down
11 changes: 11 additions & 0 deletions coderd/httpapi/httpapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,17 @@ func TestRead(t *testing.T) {
require.False(t, httpapi.Read(ctx, rw, r, v))
})

t.Run("BodyTooLarge", func(t *testing.T) {
t.Parallel()
ctx := context.Background()
rw := httptest.NewRecorder()
r := httptest.NewRequest("POST", "/", strings.NewReader(`{"value":"too large"}`))
r.Body = http.MaxBytesReader(rw, r.Body, 4)
var v json.RawMessage
require.False(t, httpapi.Read(ctx, rw, r, &v))
require.Equal(t, http.StatusRequestEntityTooLarge, rw.Code)
})

t.Run("Validate", func(t *testing.T) {
t.Parallel()
type toValidate struct {
Expand Down
Loading
Loading