fiber/middleware/earlydata
leonklingele ac4ce21d9c
🐛 Bug: Fix issues introduced in linting PR (#2319)
* internal: revert linting changes

Changes to the internal package should not have been made in 167a8b5e94.

* middleware/monitor: revert changes to exported field "ChartJSURL"

This is a breaking change introduced in 167a8b5e94.

* middleware/monitor: fix error checking

Fix the errorenous error checking introduced in 167a8b5e94.

* 🐛 Bug: Fix issues introduced in linting PR #2319

* 🐛 Bug: Fix issues introduced in linting PR #2319

* Bug: Fix issues introduced in linting PR #2319

---------

Co-authored-by: René Werner <rene@gofiber.io>
2023-02-02 15:57:40 +01:00
..
README.md 🚀 Feature: Add earlydata middleware (v2 backport) (#2314) 2023-01-30 12:08:01 +01:00
config.go 🐛 Bug: Fix issues introduced in linting PR (#2319) 2023-02-02 15:57:40 +01:00
earlydata.go 🚀 Feature: Add earlydata middleware (v2 backport) (#2314) 2023-01-30 12:08:01 +01:00
earlydata_test.go 🚀 Feature: Add earlydata middleware (v2 backport) (#2314) 2023-01-30 12:08:01 +01:00

README.md

Early Data Middleware

The Early Data middleware for Fiber adds support for TLS 1.3's early data ("0-RTT") feature. Citing RFC 8446, when a client and server share a PSK, TLS 1.3 allows clients to send data on the first flight ("early data") to speed up the request, effectively reducing the regular 1-RTT request to a 0-RTT request.

Make sure to enable fiber's EnableTrustedProxyCheck config option before using this middleware in order to not trust bogus HTTP request headers of the client.

Also be aware that enabling support for early data in your reverse proxy (e.g. nginx, as done with a simple ssl_early_data on;) makes requests replayable. Refer to the following documents before continuing:

By default, this middleware allows early data requests on safe HTTP request methods only and rejects the request otherwise, i.e. aborts the request before executing your handler. This behavior can be controlled by the AllowEarlyData config option. Safe HTTP methods — GET, HEAD, OPTIONS and TRACE — should not modify a state on the server.

Table of Contents

Signatures

func New(config ...Config) fiber.Handler

Examples

First import the middleware from Fiber,

import (
	"github.com/gofiber/fiber/v2"
	"github.com/gofiber/fiber/v2/middleware/earlydata"
)

Then create a Fiber app with app := fiber.New().

Default Config

app.Use(earlydata.New())

Custom Config

app.Use(earlydata.New(earlydata.Config{
	Error: fiber.ErrTooEarly,
	// ...
}))

Config

type Config struct {
	// Next defines a function to skip this middleware when returned true.
	//
	// Optional. Default: nil
	Next func(c *fiber.Ctx) bool

	// IsEarlyData returns whether the request is an early-data request.
	//
	// Optional. Default: a function which checks if the "Early-Data" request header equals "1".
	IsEarlyData func(c *fiber.Ctx) bool

	// AllowEarlyData returns whether the early-data request should be allowed or rejected.
	//
	// Optional. Default: a function which rejects the request on unsafe and allows the request on safe HTTP request methods.
	AllowEarlyData func(c *fiber.Ctx) bool

	// Error is returned in case an early-data request is rejected.
	//
	// Optional. Default: fiber.ErrTooEarly.
	Error error
}

Default Config

var ConfigDefault = Config{
	IsEarlyData: func(c *fiber.Ctx) bool {
		return c.Get("Early-Data") == "1"
	},

	AllowEarlyData: func(c *fiber.Ctx) bool {
		return fiber.IsMethodSafe(c.Method())
	},

	Error: fiber.ErrTooEarly,
}