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

58 lines
1.6 KiB
Go

package httpapi
import (
"errors"
"net/http"
gatewaypb "github.com/caelum0x/ration/pkg/pb/gateway"
)
type rationBridgeFundingRequestJSON struct {
RequestedMinorUnits int64 `json:"requested_minor_units"`
Reason string `json:"reason"`
}
type rationBridgeFundingResponseJSON struct {
GrantedMinorUnits int64 `json:"granted_minor_units"`
FullyGranted bool `json:"fully_granted"`
Explanation string `json:"explanation"`
}
// handleRationBridgeFunding serves POST /v1/tenants/{tenant_id}/bridge-funding.
func (h *Handler) handleRationBridgeFunding(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 rationBridgeFundingRequestJSON
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.RequestedMinorUnits <= 0 {
writeBadRequest(w, h.log, "requested_minor_units must be > 0")
return
}
resp, err := h.client.RationBridgeFunding(clientContext(r), &gatewaypb.RationBridgeFundingRequest{
TenantId: tenantID,
RequestedMinorUnits: body.RequestedMinorUnits,
Reason: body.Reason,
})
if err != nil {
writeGRPCError(w, h.log, err)
return
}
writeJSON(w, h.log, http.StatusOK, rationBridgeFundingResponseJSON{
GrantedMinorUnits: resp.GetGrantedMinorUnits(),
FullyGranted: resp.GetFullyGranted(),
Explanation: resp.GetExplanation(),
})
}