cookies

package
v0.1.7 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package cookies provides proxy-safe cookie rewriting helpers for mounted reverse proxies.

The package focuses on a narrow but important boundary: rewriting Set-Cookie and Cookie headers so a mounted reverse proxy can safely expose multiple upstreams without browser-cookie collisions. It does not own routing, storage, or session semantics.

Example
headers := http.Header{}
headers.Add("Set-Cookie", "session=abc; Path=/; HttpOnly")
headers.Set("Cookie", "gpx_demo__session=abc; theme=dark")

RewriteSetCookies(headers, RewriteOptions{
	Mode:            ModeIsolate,
	Namespace:       "demo",
	DomainStrategy:  "hostOnly",
	PathStrategy:    "prefix",
	ProxyPathPrefix: "/proxy",
})
RewriteOutboundCookies(headers, RewriteOptions{
	Mode:      ModeIsolate,
	Namespace: "demo",
})

fmt.Println(headers.Values("Set-Cookie")[0])
fmt.Println(headers.Get("Cookie"))
Output:
gpx_demo__session=abc; Path=/proxy; HttpOnly
session=abc

Index

Examples

Constants

View Source
const (
	ModeIsolate = "isolate"
	ModeAuto    = "auto"
	ModeOff     = "off"
)

Variables

This section is empty.

Functions

func BuildSetCookieLine

func BuildSetCookieLine(p ParsedSetCookie) string

BuildSetCookieLine serializes a parsed Set-Cookie value back to a header line.

func DomainAttrSafe

func DomainAttrSafe(host string) bool

DomainAttrSafe reports whether a cookie Domain attribute is safe for the supplied host.

func NamespaceForURL

func NamespaceForURL(u *url.URL) string

NamespaceForURL derives a stable namespace from scheme and host.

func NamespacePrefix

func NamespacePrefix(ns string) string

NamespacePrefix returns the browser-storage prefix used for isolate mode.

func RewriteOutboundCookies

func RewriteOutboundCookies(h Header, opts RewriteOptions)

RewriteOutboundCookies rewrites Cookie headers for upstream requests in isolate mode by filtering to the current namespace and unwrapping names.

func RewriteSetCookies

func RewriteSetCookies(h Header, opts RewriteOptions)

RewriteSetCookies mutates Set-Cookie headers in h according to opts.

func SanitizeHost

func SanitizeHost(hostport string) string

SanitizeHost strips a port and any leading dot from a host value.

Types

type Header interface {
	Values(key string) []string
	Set(key, value string)
	Add(key, value string)
	Del(key string)
}

Header is the minimal interface needed to rewrite cookie headers.

type ParsedSetCookie

type ParsedSetCookie struct {
	Name        string
	Value       string
	Attrs       map[string]string
	Flags       map[string]bool
	ExtraTokens []string
}

ParsedSetCookie is a lightweight Set-Cookie representation that preserves unrecognized tokens on rebuild.

func ParseSetCookieLine

func ParseSetCookieLine(raw string) (ParsedSetCookie, bool)

ParseSetCookieLine parses a Set-Cookie line while preserving unrecognized tokens for round-trip rebuilding.

type RewriteOptions

type RewriteOptions struct {
	Mode            string
	DomainStrategy  string
	PathStrategy    string
	ProxyHost       string
	ProxyPathPrefix string
	HTTPS           bool
	Namespace       string
}

RewriteOptions controls how Set-Cookie and Cookie headers are rewritten at a reverse-proxy boundary.

Jump to

Keyboard shortcuts

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