Skip to content

Commit 18ff026

Browse files
🚀 Feature: Add earlydata middleware (gofiber#2270)
* middleware: add earlydata middleware * middleware/earlydata: address comments * Update README.md * Update README.md Co-authored-by: RW <[email protected]>
1 parent 6dc7a12 commit 18ff026

File tree

4 files changed

+410
-0
lines changed

4 files changed

+410
-0
lines changed

Diff for: middleware/earlydata/README.md

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# Early Data Middleware
2+
3+
The Early Data middleware for [Fiber](https://github.com/gofiber/fiber) adds support for TLS 1.3's early data ("0-RTT") feature.
4+
Citing [RFC 8446](https://datatracker.ietf.org/doc/html/rfc8446#section-2-3), 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.
5+
6+
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.
7+
8+
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:
9+
10+
- https://datatracker.ietf.org/doc/html/rfc8446#section-8
11+
- https://blog.trailofbits.com/2019/03/25/what-application-developers-need-to-know-about-tls-early-data-0rtt/
12+
13+
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.
14+
Safe HTTP methods — `GET`, `HEAD`, `OPTIONS` and `TRACE` — should not modify a state on the server.
15+
16+
## Table of Contents
17+
18+
- [Early Data Middleware](#early-data-middleware)
19+
- [Table of Contents](#table-of-contents)
20+
- [Signatures](#signatures)
21+
- [Examples](#examples)
22+
- [Default Config](#default-config)
23+
- [Custom Config](#custom-config)
24+
- [Config](#config)
25+
- [Default Config](#default-config-1)
26+
27+
## Signatures
28+
29+
```go
30+
func New(config ...Config) fiber.Handler
31+
```
32+
33+
## Examples
34+
35+
First import the middleware from Fiber,
36+
37+
```go
38+
import (
39+
"github.com/gofiber/fiber/v3"
40+
"github.com/gofiber/fiber/v3/middleware/earlydata"
41+
)
42+
```
43+
44+
Then create a Fiber app with `app := fiber.New()`.
45+
46+
### Default Config
47+
48+
```go
49+
app.Use(earlydata.New())
50+
```
51+
52+
### Custom Config
53+
54+
```go
55+
app.Use(earlydata.New(earlydata.Config{
56+
Error: fiber.ErrTooEarly,
57+
// ...
58+
}))
59+
```
60+
61+
### Config
62+
63+
```go
64+
type Config struct {
65+
// Next defines a function to skip this middleware when returned true.
66+
//
67+
// Optional. Default: nil
68+
Next func(c fiber.Ctx) bool
69+
70+
// IsEarlyData returns whether the request is an early-data request.
71+
//
72+
// Optional. Default: a function which checks if the "Early-Data" request header equals "1".
73+
IsEarlyData func(c fiber.Ctx) bool
74+
75+
// AllowEarlyData returns whether the early-data request should be allowed or rejected.
76+
//
77+
// Optional. Default: a function which rejects the request on unsafe and allows the request on safe HTTP request methods.
78+
AllowEarlyData func(c fiber.Ctx) bool
79+
80+
// Error is returned in case an early-data request is rejected.
81+
//
82+
// Optional. Default: fiber.ErrTooEarly.
83+
Error error
84+
}
85+
```
86+
87+
### Default Config
88+
89+
```go
90+
var ConfigDefault = Config{
91+
IsEarlyData: func(c fiber.Ctx) bool {
92+
return c.Get("Early-Data") == "1"
93+
},
94+
95+
AllowEarlyData: func(c fiber.Ctx) bool {
96+
return fiber.IsMethodSafe(c.Method())
97+
},
98+
99+
Error: fiber.ErrTooEarly,
100+
}
101+
```

Diff for: middleware/earlydata/config.go

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package earlydata
2+
3+
import (
4+
"github.com/gofiber/fiber/v3"
5+
)
6+
7+
const (
8+
DefaultHeaderName = "Early-Data"
9+
DefaultHeaderTrueValue = "1"
10+
)
11+
12+
// Config defines the config for middleware.
13+
type Config struct {
14+
// Next defines a function to skip this middleware when returned true.
15+
//
16+
// Optional. Default: nil
17+
Next func(c fiber.Ctx) bool
18+
19+
// IsEarlyData returns whether the request is an early-data request.
20+
//
21+
// Optional. Default: a function which checks if the "Early-Data" request header equals "1".
22+
IsEarlyData func(c fiber.Ctx) bool
23+
24+
// AllowEarlyData returns whether the early-data request should be allowed or rejected.
25+
//
26+
// Optional. Default: a function which rejects the request on unsafe and allows the request on safe HTTP request methods.
27+
AllowEarlyData func(c fiber.Ctx) bool
28+
29+
// Error is returned in case an early-data request is rejected.
30+
//
31+
// Optional. Default: fiber.ErrTooEarly.
32+
Error error
33+
}
34+
35+
// ConfigDefault is the default config
36+
var ConfigDefault = Config{
37+
IsEarlyData: func(c fiber.Ctx) bool {
38+
return c.Get(DefaultHeaderName) == DefaultHeaderTrueValue
39+
},
40+
41+
AllowEarlyData: func(c fiber.Ctx) bool {
42+
return fiber.IsMethodSafe(c.Method())
43+
},
44+
45+
Error: fiber.ErrTooEarly,
46+
}
47+
48+
// Helper function to set default values
49+
func configDefault(config ...Config) Config {
50+
// Return default config if nothing provided
51+
if len(config) < 1 {
52+
return ConfigDefault
53+
}
54+
55+
// Override default config
56+
cfg := config[0]
57+
58+
// Set default values
59+
60+
if cfg.IsEarlyData == nil {
61+
cfg.IsEarlyData = ConfigDefault.IsEarlyData
62+
}
63+
64+
if cfg.AllowEarlyData == nil {
65+
cfg.AllowEarlyData = ConfigDefault.AllowEarlyData
66+
}
67+
68+
if cfg.Error == nil {
69+
cfg.Error = ConfigDefault.Error
70+
}
71+
72+
return cfg
73+
}

Diff for: middleware/earlydata/earlydata.go

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package earlydata
2+
3+
import (
4+
"github.com/gofiber/fiber/v3"
5+
)
6+
7+
const (
8+
localsKeyAllowed = "earlydata_allowed"
9+
)
10+
11+
func IsEarly(c fiber.Ctx) bool {
12+
return c.Locals(localsKeyAllowed) != nil
13+
}
14+
15+
// New creates a new middleware handler
16+
// https://datatracker.ietf.org/doc/html/rfc8470#section-5.1
17+
func New(config ...Config) fiber.Handler {
18+
// Set default config
19+
cfg := configDefault(config...)
20+
21+
// Return new handler
22+
return func(c fiber.Ctx) error {
23+
// Don't execute middleware if Next returns true
24+
if cfg.Next != nil && cfg.Next(c) {
25+
return c.Next()
26+
}
27+
28+
// Abort if we can't trust the early-data header
29+
if !c.IsProxyTrusted() {
30+
return cfg.Error
31+
}
32+
33+
// Continue stack if request is not an early-data request
34+
if !cfg.IsEarlyData(c) {
35+
return c.Next()
36+
}
37+
38+
// Continue stack if we allow early-data for this request
39+
if cfg.AllowEarlyData(c) {
40+
_ = c.Locals(localsKeyAllowed, true)
41+
return c.Next()
42+
}
43+
44+
// Else return our error
45+
return cfg.Error
46+
}
47+
}

0 commit comments

Comments
 (0)