securecookie

package module
v1.4.1 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2026 License: MIT Imports: 14 Imported by: 6 modules, 3 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

GoDoc Status

Encode and Decode secure cookies

This version 1 is deprecated. Please use version 2, which is more secure, robust and faster: automatic key rotation, authenticated encryption with AES-128-GCM, secure defaults that can't be misconfigured (__Host- prefix, Secure, HttpOnly, mandatory maximum age), and value decoding without allocation. See Migrating from v1. Version 1 only receives security fixes.

This package provides functions to encode and decode secure cookie values.

A secure cookie has its value ciphered and signed with a message authentication code. This prevents the remote cookie owner from knowing what information is stored in the cookie or modifying it. It also prevents an attacker from forging a fake cookie.

This package differs from the Gorilla secure cookie in that its encoding and decoding is 3 times faster and needs no heap allocation with an equivalent security strength. Both use AES128 and SHA256 to secure the value.

Note: This package uses its own secure cookie value encoding. It is thus incompatible with the Gorilla secure cookie package and the ones provided with other language frameworks. This encoding is simpler and more efficient, and adds a version number to support evolution with backwards compatibility.

Warning: Because this package impacts security of web applications, it is a critical functionality. Review feedbacks are always welcome.

If you enjoy the code and want to thank me 🙏, you can buy me a coffee.

Content

Installation

To install or update this secure cookie package use the instruction:

go get -u "github.com/chmike/securecookie"

Usage examples

To use this cookie package in your server, add the following import.

import "github.com/chmike/securecookie"
Generating a random key

It is strongly recommended to generate the random key with the following function. Save the key in a file using hex.EncodeToString() and restrict access to that file.

var key []byte = securecookie.MustGenerateRandomKey()

To mitigate the risk of an attacker getting the saved key, you might store a second key in another place and use the xor of both keys as secure cookie key. The attacker will have to get both keys to reconstruct the effective key which should be more difficult.

A secure cookie is instantiated with the New function. It returns an error if an argument is invalid.

obj, err := securecookie.New("__Host-session", key, securecookie.Params{
    Path:     "/",              // cookie received for all URL paths
    MaxAge:   3600,             // cookie becomes invalid 3600 seconds after it is set
    HTTPOnly: true,             // disallow access by remote javascript code 
    Secure:   true,             // cookie received only with HTTPS, never with HTTP
    SameSite: securecookie.Lax, // cookie not sent with most cross-site requests
})
if err != nil {
    // ...
}

The __Host- prefix of the cookie name ensures that only this host can set the cookie. It requires Secure, Path set to / and no Domain. Use the prefix __Secure- when the cookie must be shared with subdomains, which only requires Secure. New returns an error when these requirements are not satisfied. See Security recommendations.

It is also possible to instantiate a secure cookie object without returning an error and panic if an argument is invalid. To do this, use securecookie.MustNew(). In the following example, __Secure-Auth is the cookie name and the Path is /sec. A secured value may be stored in the remote browser by calling the SetValue() method. After that, every subsequent request from that browser with a URL starting with /sec will have the cookie sent along. Calling the method GetValues() will extract the secure values from the request. A request to delete the cookie may be sent to the remote browser by calling the method Delete().

var obj = securecookie.MustNew("__Secure-Auth", key, securecookie.Params{
    Path:     "/sec",           // cookie received only when URL starts with this path
    Domain:   "example.com",    // cookie received only when URL domain matches this one
    MaxAge:   3600,             // cookie becomes invalid 3600 seconds after it is set
    HTTPOnly: true,             // disallow access by remote javascript code 
    Secure:   true,             // cookie received only with HTTPS, never with HTTP
    SameSite: securecookie.Lax, // cookie not sent with most cross-site requests
})

Remember that the key should not be stored in the source code or in a repository.

var val = []byte("some value")
// with w as the http.ResponseWriter
if err := obj.SetValue(w, val); err != nil {
    // ...
}

A request may contain several cookies with the same name, for instance when another host has set a cookie with the same name (cookie tossing). The method GetValues() decodes all of them. The following example accepts the request only if all the valid values are equal.

// with r as the *http.Request
var val []byte
var found bool
for _, res := range obj.GetValues(r.Cookies()) {
    if res.Error != nil {
        continue // ignore invalid or expired values
    }
    if found && !bytes.Equal(val, res.Value) {
        // different valid values: reject the request
    }
    val, found = res.Value, true
}
if !found {
    // no valid value
}

Each result also holds the time stamp of the value in its Stamp field.

The methods GetValue() and GetValueAndStamp() are deprecated because they only decode the first cookie with a matching name.

// with w as the http.ResponseWriter
if err := obj.Delete(w); err != nil {
    // ...
}

Note: don't rely on the assumption that the remote user agent (browser) will effectively delete the cookie. Evil users will try anything to break your site.

Complete example

A complete example is provided here.

Follow these steps to test the example:

  1. create a directory in /tmp and set it as the working directory: "mkdir /tmp/sctest; cd /tmp/sctest"
  2. create a file named main.go and copy the example code referenced above into it
  3. create a go.mod file: "go mod init example.com"
  4. get the latest version of the securecookie package: "go get github.com/chmike/securecookie@latest"
  5. run the server: "go run main.go"
  6. with your browser, request "http://localhost:8080/set/someValue" to set the secure cookie value
  7. with your browser, request "http://localhost:8080/val" to retrieve the secure cookie value

Benchmarking

Encoding the cookie named "test" with value "some value". See benchmark functions are at the bottom of securecookie_test.go file. The ns/op values were obtained by running the benchmark 10 times and taking the minimal value. These values were obtained with go1.27.3 (8-Nov-2026) on an a Mac Book Air M2.

Chmike Gorilla
Value len 88 112
Set ns/op 745 1759
Get ns/op 503 1385
Set B/op 344 2902
Get B/op 208 2568
Set allocs/op 3 33
Get allocs/op 2 35

The secure cookie value encoding and decoding functions of this package need 0 heap allocations.

The benchmarks were obtained with release v1.4.0. Subsequent release may alter the benchmark results.

Qualitative comparison

The latest version was updated to put the security in line with the Gorilla secure cookie.

  • We both use CTR-AES-128 encryption with a 16 byte nonce, and HMAC-SHA-256.
  • We both encrypt first then compute the MAC over the cipher text.
  • A time stamp is added to the encoded value.
  • The hmac is computed over the cookie value name, the ciphered time stamp and value.
  • Both packages don't take special measures to secure the secret key.
  • Both packages don't effectively conceal the value byte length.

The differences between the Gorilla secure cookie and this implementation are:

  • This code is more efficient, and there is still room for improvement.
  • This secure value encoding is more compact without weakening the security.
  • This secure cookie encoding is incompatible with other secure cookie encoding. I don't know the status of Gorilla's encoding.
  • This encoding adds an encoding version number allowing to change or add new encoding without breaking backwards compatibility. Gorilla doesn't have this.
  • This package provides a Delete cookie method.

This package and Gorilla both provide equivalently secure cookies if we discard the fact that no special measure is taken to conceal the key in memory and the value length. This package is quite new and needs more reviews to validate the security of the implementation.

Feedback and contributions are welcome.

Value encoding

  1. A clear text message is first assembled as follow:

    [tag][nonce][stamp][value][padding]

  • The tag is 1 byte. The 6 most significant bits encode the version number of the encoding (currently 0). The 2 less significant bits encode the number of padding bytes (0, 1 or 2). 3 is an invalid padding length. The number is picked so that the total length including the MAC is a multiple of 3. This simplifies base64 encoding by avoiding its padding.
  • The nonce is 16 bytes long (AES block length) and contains cryptographically secure pseudorandom bytes.
  • The stamp is the unix time subtracted by an epochOffset value (1505230500), and encoded using the LEB128 encoding.
  • The value is a copy of the user provided value to secure.
  • The padding bytes are cryptographically secure pseudorandom bytes. There may be 0 to 2 padding bytes. The number is picked so that the total length including the MAC is a multiple of 3. This simplifies base64 encoding by avoiding its padding.
  1. The stamp, value and padding bytes are ciphered using CTR-AES with the 16 last bytes of the key as ciphering key. The nonce is used as iv and counter initialization value. The tag and nonce are left unciphered.

  2. An HMAC-SHA-256 is computed over (1) the cookie name and (2) the bytes sequence obtained after step 2. The 32 byte long MAC is appended after the padding.

  3. The whole byte sequence, from the tag to the last byte of the MAC is encoded in Base64 using the URL encoding. There is no padding since the byte length is a multiple of 3 bytes.

The tag which provides an encoding version allows completely changing the encoding while preserving backwards compatibility if required.

Security recommendations

  • Use the cookie name prefix __Host- for authentication and session cookies. User agents only accept such a cookie when it is set by the host itself, with Secure, Path set to / and no Domain. A sibling subdomain or a network attacker can then not set a cookie with the same name (cookie tossing). Use the prefix __Secure- when the cookie must be shared with subdomains.
  • An attacker can't forge a valid value, but he may set in the victim's browser a valid value obtained from his own session. The victim is then logged in the attacker's account without knowing it. Use GetValues() and reject requests with different valid values.
  • With MaxAge set to 0, the cookie is a session cookie for the browser, but the value has no expiration on the server side: a copied value is accepted forever. Use a MaxAge greater than 0 for authentication and session cookies.
  • Keep the key secret. Anyone knowing the key can read and forge values.
  • The domain name must be a valid host name. IP addresses and domain names with a leading period are rejected.

Contributors

  • lstokeworth (reddit):
    • suggest to replace copy with append,
    • remove the Expires Params field and use only the MaxAge,
    • provide a constant date in the past for Delete.
  • cstockton (github):
  • flowonyx (github):
    • fix many typos in the README and comments.

Usage advise

It is very important to understand that the security is limited to the cookie content. Nothing proves that the other data received by the server with a secure cookie has been created by the user's request.

When you have properly set the domain path, and HTTPOnly with the Secure flag, and use HTTPS, only the user's browser can send the cookie. But it is still possible for an attacker to trick your browser to send a request to the site without the user's knowledge and consent. This is known as a CSRF attack.

Consider this scenario with a Pizza ordering web site. First let's see what happens when everything goes as expected.

The user has to login to the site to be allowed to order pizzas. During the login transaction a secure cookie is added into the user's browser. The user is then shown a form with the number of pizzas to order. When the user clicks the Order button, his browser will make a request to an URL provided with the form. It will join the field values and the secure cookie since the URL path and domain match the one specified at the login transaction.

When the server receives this request, it checks the cookie validity to determine who that client is and if he is legitimate. All is fine. The order is then forwarded to the pizza chef. The pizza is later delivered to the user.

Now comes the villain. He sets up some random site with a form and a validation button that the victim will very likely click (e.g., "Subscribe to spam" with a Please no button as validation button). The villain has set up the form so that the URL associated to the validation button is the URL to order pizzas. He will have added a hidden field with the number of pizzas to order set to 10!

When the user clicks that validation button, his browser will send a request to the pizza ordering site with the field value and the secure cookie since the URL matches the cookie path and domain.

The pizza ordering site checks the secure cookie and it will be authenticated. It will assume that the user issued that order. When the delivery man rings at the user's door with 10 pizzas in his hand, there will be a conflict and no way to know to know who's fault it is. Notice how the value 10 associated with the cookie in the ordering request was not signed by the user's browser.

To avoid this, the solution is to add a way to authenticate the form response. This is done by adding a hidden field in the form with a random byte sequence, and set a secure cookie with that byte sequence as value and a validity date limit. When the user fills that form, the server will receive back the secure cookie and the hidden field value. The server then checks that they match to validate the response.

An attacker can forge a random byte sequence, but can't forge the secure cookie that goes with it.

Note that this protection is void in case of XSS attack (script injection).

The above method works with forms, not with REST API like requests because the server can't send the random token to the client that can be used as a challenge. For REST API like authenticated transactions, the client and server have to both know a secret byte sequence they use to compute a hmac value over the URI, the method, the data and a message sequence number. They can then authenticate the message and the source.

The secret byte sequence can be determined in the authentication transaction with public and private keys. There is no need for TLS to securely authenticate the client and server. A secret cookie is no help here.

Documentation

Overview

Package securecookie is a fast, efficient and safe cookie value encoder/decoder.

A secure cookie has its value ciphered and signed with a message authentication code. This prevents the remote cookie owner from knowing what information is stored in the cookie or modifying it. It also prevents an attacker from forging a fake securecookie.

What makes this secure cookie package different is that it is fast (faster than the Gorilla secure cookie), and the value encoding and decoding needs no heap allocations.

The intended use is to instantiate all secure cookie objects of your web site at program start up. In the following example "__Host-session" is the cookie name. The key is a byte sequence generated with the GenerateRandomKey() function. The same key must be used to retrieve the value.

obj, err := securecookie.New("__Host-session", key, securecookie.Params{
	Path:     "/",                // cookie received for all URL paths
	MaxAge:   3600,               // cookie becomes invalid 3600
	                              // seconds after it is set.
	HTTPOnly: true,               // disallow access by remote
	                              // javascript code.
	Secure:   true,               // cookie received only with HTTPS,
	                              // never with HTTP.
	SameSite: securecookie.Lax,   // cookie not sent with most
	                              // cross-site requests.
})
if err != nil {
	// ...
}

A securecookie can also be instantiated as a global variable with the MustNew function. The function will panic if an error occurs.

var obj = securecookie.MustNew("__Host-session", key, securecookie.Params{
	Path:     "/",
	MaxAge:   3600,
	HTTPOnly: true,
	Secure:   true,
	SameSite: securecookie.Lax,
})

A securecookie object is immutable. Its methods can be called from different goroutines. The Delete(), SetValue() and GetValues() methods are independent.

To set a cookie value, call the SetValue() method with a http.ResponseWriter w, and a value val as arguments.

var val = []byte("some value")
if err := obj.SetValue(w, val); err != nil {
	// ...
}

A request may contain several cookies with the same name. Securecookie values are retrieved from an http.Request with the GetValues() method. The following example accepts the request only if all the valid values are equal.

var val []byte
var found bool
for _, res := range obj.GetValues(r.Cookies()) {
	if res.Error != nil {
		continue // ignore invalid or expired values
	}
	if found && !bytes.Equal(val, res.Value) {
		// different valid values: reject the request
	}
	val, found = res.Value, true
}
if !found {
	// no valid value
}

A securecookie is deleted by calling the Delete() method with the http.ResponseWriter w as argument.

if err := obj.Delete(w); err != nil {
	// ...
}

Security recommendations

Use the cookie name prefix "__Host-" for authentication or session cookies. User agents only accept such a cookie when it is set with Secure, without Domain, and with Path "/" by the host itself. Another host, like a sibling subdomain, or a network attacker, can then not set a cookie with the same name in the user agent (cookie tossing). Use the prefix "__Secure-" when the cookie must be shared with subdomains. New returns an error when the parameters don't satisfy the prefix requirements.

An attacker can't forge a valid value, but may set in the user agent a valid value obtained from its own session. Use GetValues and reject requests with different valid values, as shown above.

With MaxAge 0, the cookie has no Max-Age attribute and is a session cookie for the user agent. But the value has no expiration on the server side: a copied value is accepted forever. Use a MaxAge greater than 0 for authentication or session cookies.

Keep the key secret. Anyone knowing the key can read and forge values.

Index

Constants

View Source
const (
	Undefined int = iota
	Lax
	None
	Strict
)

Keywords used for parameter value.

View Source
const KeyLen = 32

KeyLen is the byte length of the key.

Variables

This section is empty.

Functions

func GenerateRandomKey

func GenerateRandomKey() ([]byte, error)

GenerateRandomKey returns a random key of KeyLen bytes. Use hex.EncodeToString(key) to get the key as hexadecimal string, and hex.DecodeString(keyStr) to convert back from string to byte slice.

func MustGenerateRandomKey added in v1.1.0

func MustGenerateRandomKey() []byte

MustGenerateRandomKey returns a random key of KeyLen bytes. It calls GenerateRandomKey and panics if it returns an error.

Types

type Obj

type Obj struct {
	// contains filtered or unexported fields
}

Obj is a validated cookie object.

func MustNew

func MustNew(name string, key []byte, p Params) *Obj

MustNew panics if New returns a non-nil error, otherwise returns a cookie object. The intended use is to instantiate the object as a global variable.

func New

func New(name string, key []byte, p Params) (*Obj, error)

New instantiates a validated cookie object with the given cookie name, key and parameters.

A cookie name starting with the prefix "__Secure-" requires Secure to be true. A cookie name starting with the prefix "__Host-" requires Secure to be true, Domain to be empty and Path to be "/". User agents enforce these rules and ignore cookies that don't satisfy them. The prefixes are matched case-insensitively.

func (*Obj) Delete

func (o *Obj) Delete(w http.ResponseWriter) error

Delete adds a request to delete the securecookie to the server response w. The user agent may ignore the request.

func (*Obj) Domain

func (o *Obj) Domain() string

Domain returns the securecookie domain field value.

func (*Obj) GetValue deprecated

func (o *Obj) GetValue(dst []byte, r *http.Request) ([]byte, error)

GetValue returns the value of the first cookie with a matching name. The function uses the dst slice as storage if not nil and big enough.

Deprecated: a request may contain several cookies with the same name, for instance one set by another host (cookie tossing), and GetValue only decodes the first one. Use GetValues instead.

func (*Obj) GetValueAndStamp deprecated added in v1.3.3

func (o *Obj) GetValueAndStamp(dst []byte, r *http.Request) ([]byte, time.Time, error)

GetValueAndStamp appends the decoded securecookie value to dst. dst is allocated if nil, or grown if too small. Returns the value and the time stamp truncated to seconds when the cookie was generated and added to an http response with SetValue.

Deprecated: a request may contain several cookies with the same name, for instance one set by another host (cookie tossing), and GetValueAndStamp only decodes the first one. Use GetValues instead.

func (*Obj) GetValues added in v1.3.4

func (o *Obj) GetValues(cookies []*http.Cookie) []ValueResult

GetValues returns the values and stamp of cookies with a matching name in cookies, or nil if none. A request may contain multiple cookies with the same name, and cookies is usually r.Cookies(). For each returned value, Error is nil and Value holds the decoded value (nil when the value is empty), or Error is not nil and Value is nil.

func (*Obj) HTTPOnly

func (o *Obj) HTTPOnly() bool

HTTPOnly returns the securecookie HTTPOnly field value.

func (*Obj) MaxAge

func (o *Obj) MaxAge() int

MaxAge returns the securecookie max age field value.

func (*Obj) Name added in v1.2.0

func (o *Obj) Name() string

Name return the securecookie name field value.

func (*Obj) Path

func (o *Obj) Path() string

Path returns the securecookie path field value.

func (*Obj) SameSite added in v1.3.0

func (o *Obj) SameSite() int

SameSite returns the same site field value. Note that the value None is ignored when the cookie is not secure. When SameSite is undefined, the default is Lax.

func (*Obj) Secure

func (o *Obj) Secure() bool

Secure returns the securecookie Secure field value.

func (*Obj) SetValue

func (o *Obj) SetValue(w http.ResponseWriter, v []byte) error

SetValue adds the securecookie with the value v to the server response w. The value v is encrypted, signed with a MAC, and encoded in base64.

type Params

type Params struct {
	Path     string // Optional : URL path to which the cookie will be returned
	Domain   string // Optional : domain to which the cookie will be returned
	MaxAge   int    // Optional : time offset in seconds from now, must be >= 0, 0 means no expiration on the server side
	HTTPOnly bool   // Optional : disallow access by remote javascript code
	Secure   bool   // Optional : cookie is only sent through HTTPS connections
	SameSite int    // Optional : (None, Lax, or Strict) restrict cookie return (None requires Secure)
}

Params holds the optional cookie parameters.

type ValueResult added in v1.3.4

type ValueResult struct {
	Value []byte
	Stamp time.Time
	Error error
}

ValueResult is a decoded value with its time stamp, or the error if any. ValueResult are returned by the GetValues method.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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