carlos / platform Public

console: cap a step-up's parked form at 1 MiB

StashPendingAction had no size cap, and most gated forms are read with
net/http's 10 MB default. A form whose URL encoding (the stored column)
is over 1 MiB is not stashed; the gate answers with its existing
stash-failed message and nothing is applied. The largest legitimate
form, the letterhead with a 200 KB logo, parks at about 273 KB.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 6da7b88894e64ad6565f2c9addd48a6ffb5a7995 parent b70e5ce
2 files changed, +76 −2
  • internal/console/stepup.go +17 −2
  • internal/console/stepup_test.go +59 −0
diff --git a/internal/console/stepup.go b/internal/console/stepup.go
index a80f1b016..9256c5771 100644
--- a/internal/console/stepup.go
+++ b/internal/console/stepup.go
@@ -43,6 +43,7 @@ import (
"context"
"crypto/subtle"
"database/sql"
+ "errors"
"log"
"net/http"
"net/url"
@@ -84,6 +85,13 @@ const (
// never a query parameter: a URL ends up in history, referrers and logs,
// and the pending-action token must not.
stepUpCookie = "carlos_console_stepup"
+
+ // pendingFormMax is the most a parked form may be, in bytes of its URL
+ // encoding (the form column). Most gated forms read their body with
+ // net/http's 10 MB default, so without a cap one POST could park
+ // megabytes per session in the database. The largest legitimate form,
+ // the letterhead with a logo at its limit, is about 273 KB.
+ pendingFormMax = 1 << 20
)
// stepUpUnavailable is what a human sees when the identity provider cannot
@@ -184,10 +192,17 @@ type pendingAction struct {
Form string
}
+// errPendingTooLarge is a form over pendingFormMax, which is not stashed.
+var errPendingTooLarge = errors.New("pending action form too large")
+
// StashPendingAction parks the request the gate interrupted and returns the
-// opaque token that redeems it. Housekeeping on the write path: expired stashes
-// have nothing left to resume.
+// opaque token that redeems it. A form over pendingFormMax is refused, so the
+// caller fails closed as for any stash that failed. Housekeeping on the write
+// path: expired stashes have nothing left to resume.
func (s *Store) StashPendingAction(rawSession, address, method, path, form string, now time.Time) (string, error) {
+ if len(form) > pendingFormMax {
+ return "", errPendingTooLarge
+ }
s.db.Exec(`DELETE FROM pending_actions WHERE expires_at < ?`, now.Unix())
raw, err := NewVerifier()
if err != nil {
diff --git a/internal/console/stepup_test.go b/internal/console/stepup_test.go
index aa7ace9c9..bdf79bc63 100644
--- a/internal/console/stepup_test.go
+++ b/internal/console/stepup_test.go
@@ -7,6 +7,7 @@ package console
// becomes an oracle telling a stranger which accounts exist.
import (
+ "html"
"net/http"
"net/url"
"strconv"
@@ -399,3 +400,61 @@ func TestSignOutClearsPresence(t *testing.T) {
t.Fatal("presence outlived the session it was proved for")
}
}
+
+// TestPendingActionStashCap: a parked form is at most pendingFormMax bytes
+// as stored (its URL encoding). A larger one is not stashed, the gate
+// answers as for any stash that failed, and nothing is applied; the largest
+// legitimate form, the letterhead with a logo at its limit, still parks.
+func TestPendingActionStashCap(t *testing.T) {
+ fx, c, csrf := memberFixture(t)
+ now := time.Now()
+
+ if _, err := fx.st.StashPendingAction(csrf, "paul@keymail.dev", "POST", fx.path("/members/remove"),
+ strings.Repeat("a", pendingFormMax), now); err != nil {
+ t.Fatalf("a form at the cap was refused: %v", err)
+ }
+ if _, err := fx.st.StashPendingAction(csrf, "paul@keymail.dev", "POST", fx.path("/members/remove"),
+ strings.Repeat("a", pendingFormMax+1), now); err == nil {
+ t.Fatal("a form over the cap was stashed")
+ }
+ if _, err := fx.st.db.Exec(`DELETE FROM pending_actions`); err != nil {
+ t.Fatal(err)
+ }
+
+ // Through the gate: a removal padded past the cap.
+ status, loc, body := postForm(t, c, fx.u("/members/remove"), url.Values{"csrf": {csrf},
+ "address": {"kim@keymail.dev"}, "pad": {strings.Repeat("a", pendingFormMax)}})
+ if status != http.StatusInternalServerError || strings.HasPrefix(loc, fx.fk.srv.URL) ||
+ !strings.Contains(html.UnescapeString(body), stepUpHoldFailed) {
+ t.Fatalf("oversize removal = %d %q, want the stash-failed refusal:\n%s", status, loc, body)
+ }
+ var stashed int
+ fx.st.db.QueryRow(`SELECT COUNT(*) FROM pending_actions`).Scan(&stashed)
+ if stashed != 0 {
+ t.Fatalf("%d pending actions parked for an oversize form", stashed)
+ }
+ if role, _ := fx.st.Role(OpsAccountID, "kim@keymail.dev"); role != RoleMember {
+ t.Fatalf("an oversize form was applied (role %q)", role)
+ }
+}
+
+// TestPendingActionStashCapFitsLetterhead: the letterhead form with a logo
+// at letterheadLogoMax (about 273 KB as base64) parks under the cap.
+func TestPendingActionStashCapFitsLetterhead(t *testing.T) {
+ fx := newConsoleFixture(t)
+ c := signInOperator(t, fx)
+ logo := append(oplPNG(t), make([]byte, letterheadLogoMax)...)[:letterheadLogoMax]
+ fields := oplLetterheadFields()
+ for _, k := range []string{"office", "footer"} {
+ fields[k] = strings.Repeat("é", letterheadLong)
+ }
+ status, loc, body := oplPostLetterhead(t, fx, c, fields, logo)
+ if status != http.StatusSeeOther || !strings.HasPrefix(loc, fx.fk.srv.URL) {
+ t.Fatalf("letterhead at its limits = %d %q, want 303 to Keymail:\n%s", status, loc, body)
+ }
+ var size int
+ fx.st.db.QueryRow(`SELECT LENGTH(form) FROM pending_actions`).Scan(&size)
+ if size == 0 || size > pendingFormMax/2 {
+ t.Fatalf("parked letterhead is %d bytes, want well under %d", size, pendingFormMax)
+ }
+}