security

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2026 License: Apache-2.0 Imports: 11 Imported by: 0 modules, 0 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.

Documentation

Overview

Package security provides cryptographic utilities for session token binding and hijacking prevention. It handles HMAC-SHA256 token hashing, salt generation, and constant-time comparison to prevent timing attacks.

Index

Constants

View Source
const (
	// SHA256HexLen is the length of a hex-encoded SHA256 hash (32 bytes = 64 hex characters)
	SHA256HexLen = 64
)

Variables

This section is empty.

Functions

func PreventSessionHijacking

func PreventSessionHijacking(
	session sessiontypes.MultiSession,
	hmacSecret []byte,
	identity *auth.Identity,
) (sessiontypes.MultiSession, error)

PreventSessionHijacking wraps a session with hijack prevention security measures. It computes token binding hashes, stores them in session metadata, and returns a decorated session that validates caller identity on every operation.

Whether the session is anonymous is derived from the identity: nil identity or empty token means anonymous, a non-empty token means bound/authenticated.

For authenticated sessions (identity.Token != ""):

  • Generates a unique random salt
  • Computes HMAC-SHA256 hash of the bearer token
  • Stores hash and salt in session metadata
  • Returns decorator that validates every request against the creator's token

For anonymous sessions (identity == nil or identity.Token == ""):

  • Stores an empty string sentinel for the token hash metadata key
  • Omits the salt metadata key entirely (no salt is generated for anonymous sessions)
  • Returns decorator that allows nil callers and rejects token presentation

Security:

  • Makes defensive copies of secret and salt to prevent external mutation
  • Uses constant-time comparison to prevent timing attacks
  • Prevents session upgrade attacks (anonymous → authenticated)
  • Raw tokens are never stored, only HMAC-SHA256 hashes

Returns an error if:

  • session is nil
  • salt generation fails

Types

This section is empty.

Jump to

Keyboard shortcuts

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