caelum0x 6f5cff1b57 fix(gateway): REST facade's JSON body decode had no size cap — a single unauthenticated request could balloon memory by the full body size
decodeJSONBody (IssueCard, AdjustCardLimit, RationBridgeFunding,
SimulateDeposit) read r.Body with a bare json.NewDecoder and no bound,
before any auth check ever ran. Reproduced live: one unauthenticated
300MB POST to /v1/cards drove gateway's RSS from ~20MB to ~1.3GB in
~5.6s. Capped at 1MB via http.MaxBytesReader (same limit and
justification services/webhook-ingestion's handler already uses for
the identical class of payload), mapped to a 413 instead of the
existing 400. Verified live post-fix: the same 300MB body now returns
413 in ~2.5ms with RSS flat at ~25MB; real IssueCard/AdjustCardLimit/
SimulateDeposit/RationBridgeFunding round-trips through the REST
facade with a real minted API key still succeed unchanged. go build/
go vet/go test clean for services/gateway, full make build (Rust+Go+
SDK) clean, 0 regressions.
2026-10-29 16:22:51 +03:00

56 lines
1.5 KiB
Go

package httpapi
import (
"errors"
"net/http"
gatewaypb "github.com/caelum0x/ration/pkg/pb/gateway"
)
type simulateDepositRequestJSON struct {
AmountMinorUnits int64 `json:"amount_minor_units"`
ExpectedAtUnix int64 `json:"expected_at_unix,omitempty"`
MarkLate bool `json:"mark_late,omitempty"`
}
type simulateDepositResponseJSON struct {
DepositID string `json:"deposit_id"`
}
// handleSimulateDeposit serves POST /v1/tenants/{tenant_id}/deposits/simulate
// — a demo/judging hook, same as the RPC it fronts (see gateway.proto's
// SimulateDeposit doc comment).
func (h *Handler) handleSimulateDeposit(w http.ResponseWriter, r *http.Request) {
tenantID := r.PathValue("tenant_id")
if tenantID == "" {
writeBadRequest(w, h.log, "tenant_id path parameter is required")
return
}
var body simulateDepositRequestJSON
if err := decodeJSONBody(w, r, &body); err != nil {
if errors.Is(err, errBodyTooLarge) {
writeRequestEntityTooLarge(w, h.log, err.Error())
} else {
writeBadRequest(w, h.log, err.Error())
}
return
}
if body.AmountMinorUnits <= 0 {
writeBadRequest(w, h.log, "amount_minor_units must be > 0")
return
}
resp, err := h.client.SimulateDeposit(clientContext(r), &gatewaypb.SimulateDepositRequest{
TenantId: tenantID,
AmountMinorUnits: body.AmountMinorUnits,
ExpectedAtUnix: body.ExpectedAtUnix,
MarkLate: body.MarkLate,
})
if err != nil {
writeGRPCError(w, h.log, err)
return
}
writeJSON(w, h.log, http.StatusCreated, simulateDepositResponseJSON{DepositID: resp.GetDepositId()})
}