Laravel style#8
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.