querylimit

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jul 18, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package querylimit provides middleware that rejects requests whose raw query string exceeds a configured maximum length. Oversized requests receive a 414 URI Too Long response before any route handler runs.

It plays the same defensive role as guards found around Node's query parsers (for example the parameterLimit / maximum URL length caps in qs and body-parser, and the URL-length limits enforced by reverse proxies): it bounds the amount of query-string data the application will accept so that a single request cannot force expensive parsing of an enormous or maliciously crafted query. Rather than counting individual parameters, this port caps the total byte length of the raw query string, which is a cheap, allocation-free check performed before the query is parsed.

Use it when endpoints accept user-supplied query parameters and you want a hard upper bound on their size to protect against denial-of-service or memory pressure from abusive URLs. Register it early with app.Use, ahead of any middleware that parses or iterates the query (such as querynormalize or route handlers that call req.Query), so oversized requests are rejected before that work happens.

On each request the middleware reads req.Raw.URL.RawQuery (the portion after the leading '?', not including it) and compares its byte length against MaxLength. When the length is within the limit it calls next() and the chain proceeds normally; no headers or state are modified. When the length exceeds the limit it short-circuits: it sets the status to 414 (http.StatusRequest URITooLong) via res.Status, writes Message as the body via res.Send, and returns without calling next(), so downstream handlers never run.

Options control the threshold and the rejection body. MaxLength is measured in bytes and values <= 0 fall back to a default of 2048; Message defaults to "URI Too Long" when empty. Note the comparison is strict (len > MaxLength), so a query exactly MaxLength bytes long is accepted. The check operates on the raw, still-encoded query string, meaning percent-encoded sequences count by their encoded byte length.

There is no single Express package this mirrors one-to-one; stock Express delegates query parsing to qs and relies on the underlying HTTP server and any fronting proxy for URL-length limits. This middleware makes that limit an explicit, in-application, configurable policy with a conventional 414 response.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(opts ...Options) express.Handler

New returns query-limit middleware configured by opts.

Example

ExampleNew demonstrates rejecting requests whose raw query string is too long. The middleware is registered with app.Use and configured through querylimit.Options with a small MaxLength so the limit is easy to exceed. A GET route is added that would return "ok" for accepted requests. Two requests are driven through the app with httptest: one whose query fits within the limit and one whose query overflows it. The example prints the resulting status codes, showing the within-limit request succeeding with 200 and the oversized request being short-circuited with a 414 URI Too Long response before the handler runs.

package main

import (
	"fmt"
	"net/http/httptest"

	"github.com/malcolmston/express"
	"github.com/malcolmston/express/middleware/querylimit"
)

func main() {
	app := express.New()
	app.Use(querylimit.New(querylimit.Options{MaxLength: 10}))
	app.Get("/", func(req *express.Request, res *express.Response, next express.Next) {
		res.Send("ok")
	})

	small := httptest.NewRecorder()
	app.ServeHTTP(small, httptest.NewRequest("GET", "/?a=1", nil))

	big := httptest.NewRecorder()
	app.ServeHTTP(big, httptest.NewRequest("GET", "/?q=aaaaaaaaaaaaaaaaaaaa", nil))

	fmt.Println(small.Code)
	fmt.Println(big.Code)
}
Output:
200
414

Types

type Options

type Options struct {
	// MaxLength is the maximum permitted length, in bytes, of the raw query
	// string (excluding the leading '?'). Values <= 0 default to 2048.
	MaxLength int
	// Message is the response body sent on rejection. Defaults to
	// "URI Too Long".
	Message string
}

Options configures the query-limit middleware.

Jump to

Keyboard shortcuts

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