Skip to content

Laravel style#8

Merged
devituz merged 3 commits into
mainfrom
laravel-style
May 30, 2026
Merged

Laravel style#8
devituz merged 3 commits into
mainfrom
laravel-style

Conversation

@devituz

@devituz devituz commented May 30, 2026

Copy link
Copy Markdown
Owner

No description provided.

devituz added 3 commits May 30, 2026 19:11
Adds Laravel-grade security middleware to the native web package so
production apps no longer need to assemble guards by hand:

- SecurityHeaders(): CSP, X-Frame-Options, X-Content-Type-Options,
  Referrer-Policy, Permissions-Policy, optional HSTS
- CSRF(): double-submit cookie + constant-time compare, safe-method skip
- RateLimit(n, window) / Throttle(): per-IP fixed-window limiter with
  Retry-After; gc'd in the background
- BodyLimit(n): http.MaxBytesReader wrapper to prevent payload DoS
- RequestID(): X-Request-ID generated or echoed for trace correlation
- CORSWithConfig(): credentials/methods/headers/max-age, rejects unsafe
  wildcard + credentials combo, never leaks ACAO to untrusted origins

Validation lives in the web package directly (no Gin dep) — Validate(),
ValidationError, and c.BindAndValidate() that auto-maps failures to
HTTP 422 with {"errors": {field: msg}}.

Server hardening:
- Server.ReadTimeout / WriteTimeout / IdleTimeout / MaxHeaderBytes set
  on web.App.Run() (was: only ReadHeaderTimeout)
- c.Bind() applies DefaultBodyLimit (1 MiB) and DisallowUnknownFields()
- Production-safe c.InternalError: APP_ENV=production hides raw err
- c.SetCookie / Cookie / ClearCookie with HttpOnly/Secure/SameSite=Lax
  defaults; CookieInsecure/CookieReadable opt-outs for dev/CSRF tokens

Bug fixes uncovered while building the secure example:
- c.JSON wrote the body twice when Bind() set a 400 then respond() also
  processed the returned err. Tracked with new bodyWritten flag; tested
  with TestBind_DoubleBodyWithLoggerMiddleware
- c.Created() flushed the status before respond() could set
  Content-Type, leaving 201 responses as text/plain. Replaced with
  pendingStatus that defers WriteHeader until body write

Adds examples/secure showing the full stack, TODO.md tracking remaining
gaps, SECURITY.md cataloguing every defense by layer.

go test ./... and go vet ./... pass.
Extends the struct-tag validator with five common Laravel rules:
- numeric  — string must parse as int or float
- integer  — string must parse as an int
- ip       — string must be a valid IPv4 or IPv6 address
- gt=N     — strictly greater (string len / int / uint / float)
- lt=N     — strictly less

TestValidate_ExtraRules covers both passing and failing paths for each
rule.

TODO.md flips the boxes on everything delivered in this branch so far
and notes the two pre-existing bugs uncovered (double-body write,
Created Content-Type) and how they were fixed.
@devituz devituz merged commit efb2dfd into main May 30, 2026
4 of 5 checks passed
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