hsts

package
v0.18.3 Latest Latest
Warning

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

Go to latest
Published: Jun 12, 2026 License: MIT Imports: 4 Imported by: 2

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type HSTS

type HSTS struct {
	MaxAge            time.Duration
	IncludeSubDomains bool
	Preload           bool

	// ShareValueSlice writes the Strict-Transport-Security value from a single
	// slice shared across requests instead of allocating one per request. The
	// value is fixed at construction, so this is safe as long as nothing
	// mutates the response header value slice in place. Off by default; see
	// header.SetShared.
	ShareValueSlice bool
}

HSTS middleware

Example

Configure the policy explicitly, e.g. a shorter max-age while rolling HSTS out across an apex domain and its subdomains.

package main

import (
	"time"

	"github.com/moonrhythm/parapet"
	"github.com/moonrhythm/parapet/pkg/hsts"
)

func main() {
	s := parapet.New()
	s.Use(&hsts.HSTS{
		MaxAge:            90 * 24 * time.Hour,
		IncludeSubDomains: true,
	})
}
Example (ShareValueSlice)

Share the (fixed) header value slice across requests to avoid a per-request allocation on the hot path. Safe because the value never changes after construction.

package main

import (
	"time"

	"github.com/moonrhythm/parapet"
	"github.com/moonrhythm/parapet/pkg/hsts"
)

func main() {
	s := parapet.New()
	s.Use(&hsts.HSTS{
		MaxAge:          365 * 24 * time.Hour,
		ShareValueSlice: true,
	})
}

func Default

func Default() *HSTS

Default returns default hsts

Example

Add a Strict-Transport-Security response header with sensible defaults (max-age of one year, no includeSubDomains, no preload).

package main

import (
	"github.com/moonrhythm/parapet"
	"github.com/moonrhythm/parapet/pkg/hsts"
)

func main() {
	s := parapet.New()
	s.Use(hsts.Default())
}

func Preload

func Preload() *HSTS

Preload returns hsts preload

Example

Use the preload-ready policy: a two-year max-age with includeSubDomains and preload, suitable for submission to the HSTS preload list.

package main

import (
	"github.com/moonrhythm/parapet"
	"github.com/moonrhythm/parapet/pkg/hsts"
)

func main() {
	s := parapet.New()
	s.Use(hsts.Preload())
}

func (HSTS) ServeHandler

func (m HSTS) ServeHandler(h http.Handler) http.Handler

ServeHandler implements middleware interface

Jump to

Keyboard shortcuts

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