rest

package
v1.26.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 8, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RejectNUL added in v1.26.1

func RejectNUL() echo.MiddlewareFunc

RejectNUL refuses, with 400 ez.EINVALID and before any handler runs, a request that carries a NUL byte (0x00) where a handler would read it. Postgres cannot store a NUL byte in text, varchar or jsonb, and bun v1.3.0 refuses to render a string holding one into SQL, so such a string would fail its query with a 500 (ez.EINTERNAL); no legitimate client sends one.

It checks four places, in order:

  • the path parameters, as c.Param returns them;
  • the keys and values of the query string;
  • a JSON body (media type application/json, or one ending in +json): a raw NUL byte, or the escape \u0000 when its backslash is not itself escaped. The six-character text \u0000, written "\\u0000" in JSON, passes;
  • a form body (application/x-www-form-urlencoded or multipart/form-data): the keys and values of its fields and the names of its files. File contents are not inspected, since files such as images hold NUL bytes.

Media types match in any letter case, as net/http's form parsing does. The body is chosen by Content-Type, not by how the handler reads it, so a JSON body a handler reads raw, such as a webhook whose signature it checks over the bytes, is scanned and refused before the handler runs. An app that must accept such a payload on a route exempts the route by wrapping the middleware with a check on c.Path(), or by registering it on route groups instead of with e.Use. XML bodies are not checked, since encoding/xml already rejects a NUL byte, and nor are other content types. Headers need no check: net/http refuses a header holding a NUL byte before Echo runs.

It reads a JSON, urlencoded or multipart body of up to 32 MB into memory (two to three times that while reading and parsing) and puts it back, so handlers can still bind, read or stream it; a larger body passes through uninspected and intact, so it holds at most 32 MB per request whatever the body limit. Registered with e.Use it runs on every route, matched or not. Register it after CORS, so a preflight is answered first and its 400 carries the CORS headers; after any request-decompression middleware, since compressed bytes hold NUL bytes; and after a body limit such as middleware.BodyLimit, so a body over the limit gets the limit's 413 without being read first.

A refusal is logged at warn level as "Request Error" with the method and the route path, never the URI, query or body. The middleware writes the 400 itself, in the handler.ErrorResponse shape with an empty request_id since no handler has created its request yet, so neither an app ErrorTranslator nor Echo's HTTPErrorHandler sees it.

func Start

func Start(e *echo.Echo, log zerolog.Logger, port string, opts ...Option)

Start configures logging, recovery, CORS, the body limit, RejectNUL and server timeouts on e and serves on port. It always sets a 10s read-header timeout and a 120s idle timeout. The read and write timeouts are unset (unlimited) unless WithReadTimeout or WithWriteTimeout is passed.

Without WithAllowedOrigins every origin is allowed, and without WithBodyLimit request bodies are unbounded, so production callers should pass both. Start also registers RejectNUL unconditionally, after CORS and the body limit; see RejectNUL for what it inspects and its memory use.

Types

type Option added in v1.16.0

type Option func(*config)

Option configures Start.

func WithAllowedOrigins added in v1.16.0

func WithAllowedOrigins(origins ...string) Option

WithAllowedOrigins restricts CORS to the given origins. Without it, or with no origins, every origin is allowed.

func WithBodyLimit added in v1.16.0

func WithBodyLimit(limit string) Option

WithBodyLimit caps request bodies using echo's BodyLimit middleware, e.g. "10M". Larger bodies get 413. An invalid limit panics when the server is configured. Without it bodies are unbounded.

func WithCredentialedCORS added in v1.18.0

func WithCredentialedCORS() Option

WithCredentialedCORS sends Access-Control-Allow-Credentials: true, so a browser app on another origin can send and receive cookies. It is needed for cookie-based session auth from another origin. It only takes effect together with WithAllowedOrigins: browsers reject credentials with a wildcard origin, so without allowed origins it is ignored and Start logs a warning. The cookie path also needs the origin checks of requests.WithCookie (the CSRF rule in docs/auth.md), since CORS alone does not stop cross-site writes.

func WithIdleTimeout added in v1.16.0

func WithIdleTimeout(d time.Duration) Option

WithIdleTimeout overrides the default 120s keep-alive idle timeout.

func WithReadHeaderTimeout added in v1.16.0

func WithReadHeaderTimeout(d time.Duration) Option

WithReadHeaderTimeout overrides the default 10s read-header timeout.

func WithReadTimeout added in v1.17.0

func WithReadTimeout(d time.Duration) Option

WithReadTimeout caps the time to read a whole request, headers and body, so a client trickling body bytes cannot hold a connection open. It is off by default (zero means unlimited). Uploads that take longer than d are cut off, so size it for the slowest upload the service accepts.

func WithWriteTimeout added in v1.17.0

func WithWriteTimeout(d time.Duration) Option

WithWriteTimeout caps the time from the end of reading the request headers to the end of writing the response. It is off by default (zero means unlimited). Long-running handlers, large downloads and streaming or SSE responses that take longer than d are cut off, so only set it on services that do none of those.

Directories

Path Synopsis
Package generator derives an OpenAPI 3.0 document from the source of an Echo application built on compose's rest handlers.
Package generator derives an OpenAPI 3.0 document from the source of an Echo application built on compose's rest handlers.
Package reference renders an OpenAPI 3.x document as a self-hosted API reference page: an alternative to Scalar and Redoc that ships inside the backend, with no CDN assets.
Package reference renders an OpenAPI 3.x document as a self-hosted API reference page: an alternative to Scalar and Redoc that ships inside the backend, with no CDN assets.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL