crossprotect

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2026 License: MPL-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package crossprotect implements a middleware around cross origin resource protection.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewMiddleware

func NewMiddleware(opts ...Option) func(next http.Handler) http.Handler

NewMiddleware returns a new middleware that adds cross origin protection. It silently ignore invalid origins.

Example
package main

import (
	"net/http"

	"codeberg.org/kema/kmicro/pkg/http/middleware/crossprotect"
	"codeberg.org/kema/kmicro/pkg/http/router"
)

func main() {
	r := router.New()
	r.Use(crossprotect.NewMiddleware())
	// curl -X POST localhost:8080/
	// curl -X POST -H 'Origin: evil.net' localhost:8080/
	r.HandleFunc("POST /", func(w http.ResponseWriter, _ *http.Request) {
		_, _ = w.Write([]byte("Hello, World!"))
	})

	_ = http.ListenAndServe("[::]:8080", r.Compile())
}

func WrapHandler

func WrapHandler(next http.Handler, opts ...Option) http.Handler

WrapHandler wraps the next handler in the chain with cross origin protection. It silently ignore invalid origins.

Types

type Option

type Option func(*config)

Option configures HTTP Cross Origin Protection.

func WithDenyHandler

func WithDenyHandler(h http.Handler) Option

WithDenyHandler sets handler to use for denied requests.

func WithInsecureBypassPattern

func WithInsecureBypassPattern(patterns []string) Option

WithInsecureBypassPattern sets patterns to always allow.

func WithTrustedOrigins

func WithTrustedOrigins(origins []url.URL) Option

WithTrustedOrigins sets trusted origins.

Jump to

Keyboard shortcuts

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