Skip to content

release: v0.5.1 — error pipeline, security hardening (audit fixes) - #15

Merged
kolkov merged 6 commits into
mainfrom
fix/error-pipeline
Sep 10, 2026
Merged

release: v0.5.1 — error pipeline, security hardening (audit fixes)#15
kolkov merged 6 commits into
mainfrom
fix/error-pipeline

Conversation

@kolkov

@kolkov kolkov commented Sep 10, 2026

Copy link
Copy Markdown
Contributor

Summary

Security hardening and critical bug fixes identified by independent audit (Claude Fable 5.1).

Critical Fixes

  • Error pipeline — validation errors now return 422 (was 500), Problem errors use their status code, binding errors return 400/415
  • Group middleware — child groups inherit parent middleware (was silently replaced — auth bypass risk)
  • Shutdown order — connections drain before cleanup callbacks run
  • CORS preflight — OPTIONS requests reach middleware without explicit route

Security

  • Body size limit (default 4MB) prevents memory DoS
  • Rate limit uses RemoteAddr by default (XFF spoofing prevention)
  • Error handler does not leak internal details
  • CORS Vary: Origin header prevents cache poisoning

Removed (broken stubs)

  • c.DB() → use database.GetDB(c)
  • c.SSE() → use stream.SSEUpgrade(c, handler)
  • c.WebSocket() → use stream.WebSocketUpgrade(c, handler, opts)

Test plan

  • 9 TDD tests for error pipeline (validation→422, Problem→404, binding→400, etc.)
  • 3 TDD tests for CORS preflight
  • 5 TDD tests for body size limit
  • 1 TDD test for group middleware inheritance
  • All existing tests pass, 0 lint issues

TDD: 9 tests written BEFORE implementation, all pass.

- Add ErrorHandler type and Router.SetErrorHandler()
- Default handler maps: Problem→status, ValidationErrors→422,
  binding errors→400/415, unknown→500 without details
- Add c.written flag to prevent double-write on late errors
- Update existing tests: validation 500→422, binding 500→400

Fixes critical audit finding: every error was plain-text 500.
F2: Group() now appends parent middleware instead of replacing.
    Child groups always inherit parent middleware chain.
    Prevents auth bypass when nesting groups with explicit middleware.
    TDD: TestGroup_ChildInheritsParentMiddleware — trace verified.

F3: Shutdown() now drains active connections (server.Shutdown) BEFORE
    calling cleanup callbacks (db.Close). Prevents active requests
    from using closed resources.
F4: OPTIONS preflight now reaches middleware without explicit route.
    handleNotFound runs middleware chain for OPTIONS when handleOPTIONS
    enabled and path exists for other methods.
    Added Vary: Origin header on all CORS responses.
    TDD: 3 tests (preflight without route, no middleware, Vary header).

F5: MaxBytesReader wraps request body in adaptGenericHandler.
    Default limit 4MB, configurable via SetMaxBodySize().
    MaxBytesError mapped to 413 in defaultErrorHandler.
    TDD: 5 tests (large→413, normal→200, custom, zero disables, plain unaffected).
… F7)

F6: Remove c.DB(), c.SSE(), c.WebSocket() stubs from Context.
    c.DB() had broken context key (type declared inside function body).
    SSE/WS stubs always returned ErrStreamNotImported.
    Users should use plugin helpers: database.GetDB(c), stream.SSEUpgrade(c).

F7: handleWithGroupMiddleware now writes RouteInfo to r.routes.
    Group routes appear in OpenAPI spec.
    405 Method Not Allowed now includes Allow header (RFC 9110).
F8: RateLimit default KeyFunc now uses RemoteAddr instead of trusting
    X-Forwarded-For/X-Real-IP headers. Prevents XFF spoofing bypass.
    To use proxy headers, set KeyFunc explicitly with trusted proxy logic.

F10: Replace encoding/json/v2 imports with encoding/json.
     Go 1.27 makes json/v2 the default — explicit import unnecessary.
     Changed: openapi.go, openapi_test.go, examples/07-sse-notifications.
@kolkov
kolkov merged commit 6cd45e8 into main Sep 10, 2026
5 checks passed
@codecov

codecov Bot commented Sep 10, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 89.02439% with 9 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
router.go 87.32% 9 Missing ⚠️

📢 Thoughts on this report? Let us know!

@kolkov
kolkov deleted the fix/error-pipeline branch September 10, 2026 21:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant