tenseleyflow/shithub / c0c9bdf

Browse files

H6: cap /api/v1 body size at 256 KiB

Authored by mfwolffe <wolffemf@dukes.jmu.edu>
SHA
c0c9bdfd4d250035d7ddaa582cac19416c1f3400
Parents
f67813c
Tree
1617f55

1 changed file

StatusFile+-
M internal/web/handlers/api/api.go 12 0
internal/web/handlers/api/api.gomodified
@@ -47,10 +47,22 @@ func New(d Deps) (*Handlers, error) {
4747
 	return &Handlers{d: d, q: usersdb.New()}, nil
4848
 }
4949
 
50
+// apiMaxBodyBytes caps the request body for any /api/v1 handler. The
51
+// largest documented payload today is a check-run create with a 64 KiB
52
+// summary + 64 KiB text — comfortably below this. Tightening per-route
53
+// is fine; widening should happen at the route group, not here.
54
+//
55
+// The auth-side cap (signup/login/reset) is a separate, lower limit
56
+// (`MaxBodySize(8 KiB)`) wired in `auth_wiring.go`. This cap defends
57
+// the same surface against a misbehaving PAT-bearing client shipping
58
+// a 50 MB JSON blob to weaponize the parser.
59
+const apiMaxBodyBytes = 256 * 1024
60
+
5061
 // Mount registers /api/v1/* on r. Caller is responsible for putting r
5162
 // in a CSRF-exempt group.
5263
 func (h *Handlers) Mount(r chi.Router) {
5364
 	r.Group(func(r chi.Router) {
65
+		r.Use(middleware.MaxBodySize(apiMaxBodyBytes))
5466
 		r.Use(middleware.PATAuthMiddleware(middleware.PATConfig{
5567
 			Pool:      h.d.Pool,
5668
 			Debouncer: h.d.Debouncer,