csrf

package
v2.3.1 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultTokenName  = "X-CSRF-Token"      // HTTP Header/Form/Query中的字段名
	DefaultCookieName = "csrf_token"        // HttpOnly Cookie名称
	DefaultStatusName = "csrf_token_status" // 状态Cookie名称
)

Variables

This section is empty.

Functions

func GenerateCSRFToken

func GenerateCSRFToken() string

GenerateCSRFToken 通用CSRF Token生成

Types

type Config

type Config struct {
	Enabled       bool          `json:"enabled,omitempty" yaml:"enabled" ini:"enabled"`                      // 是否启用CSRF保护
	Strategy      Strategy      `json:"strategy,omitempty" yaml:"strategy" ini:"strategy"`                   // 防护策略
	Timeout       time.Duration `json:"timeout,omitempty" yaml:"timeout" ini:"timeout"`                      // Token超时时间(秒)
	TokenMode     TokenMode     `json:"token_mode,omitempty" yaml:"token_mode" ini:"token_mode"`             // Token模式
	SameSite      http.SameSite `json:"same_site,omitempty" yaml:"same_site" ini:"same_site"`                // SameSite策略: strict/lax/none
	Secure        bool          `json:"secure,omitempty" yaml:"secure" ini:"secure"`                         // 是否仅HTTPS
	ExemptMethods []string      `json:"exempt_methods,omitempty" yaml:"exempt_methods" ini:"exempt_methods"` // 豁免的HTTP方法
}

func PerRequestConfig

func PerRequestConfig() *Config

PerRequestConfig 每次请求前都需要获取一次token 的预设配置

func PermissiveConfig

func PermissiveConfig() *Config

PermissiveConfig 宽松配置(用于内部API)

func SessionTokenConfig

func SessionTokenConfig() *Config

SessionTokenConfig 使用session存储token的预设配置

func TimedTokenConfig

func TimedTokenConfig(timeout time.Duration) *Config

TimedTokenConfig 使用定时存储token的预设配置

func (*Config) Clone

func (c *Config) Clone() *Config

func (*Config) Disable

func (c *Config) Disable() *Config

func (*Config) Enable

func (c *Config) Enable() *Config

func (*Config) GetTokenBinding

func (c *Config) GetTokenBinding(path string) string

GetTokenBinding 这里需要用xxhash对 path进行hash编码。

func (*Config) GobDecode

func (c *Config) GobDecode(data []byte) error

func (Config) GobEncode

func (c Config) GobEncode() ([]byte, error)

func (Config) GormDBDataType

func (Config) GormDBDataType(db *gorm.DB, field *schema.Field) string

func (Config) GormDataType

func (c Config) GormDataType() string

func (Config) GormValue

func (c Config) GormValue(_ context.Context, db *gorm.DB) clause.Expr

func (*Config) IsValid

func (c *Config) IsValid() bool

IsValid 验证配置是否有效

func (*Config) Scan

func (c *Config) Scan(val any) (err error)

func (*Config) ShouldValidate

func (c *Config) ShouldValidate(method ...string) bool

ShouldValidate 验证是否需要进行CSRF验证

func (Config) Value

func (c Config) Value() (driver.Value, error)

func (*Config) WithExemptMethods

func (c *Config) WithExemptMethods(methods ...string) *Config

func (*Config) WithSameSite

func (c *Config) WithSameSite(sameSite http.SameSite) *Config

func (*Config) WithSecure

func (c *Config) WithSecure(secure bool) *Config

func (*Config) WithStrategy

func (c *Config) WithStrategy(strategy Strategy) *Config

func (*Config) WithTimeout

func (c *Config) WithTimeout(seconds time.Duration) *Config

func (*Config) WithTokenMode

func (c *Config) WithTokenMode(tokenMode TokenMode) *Config

type Strategy

type Strategy string
const (
	StrategyNone  Strategy = "none"  // 不需要CSRF保护
	StrategyToken Strategy = "token" // 使用Token验证
	// StrategyDoubleTap 双重提交Cookie(Double Submit Cookie) 是一种CSRF防护技术,它的核心思想是:
	// 服务端生成一个随机Token,通过Cookie发送给浏览器
	// 前端在请求中携带这个token,通常放在header或者query中,
	// 服务端验证请求中的Token和Cookie中的Token是否一致
	// /api/csrf-token接口在生成token时,需要根据对应path的配置来处理,另外需要设置两个cookie,一个可读,一个用于验证;可读的不设置具体值
	StrategyDoubleTap Strategy = "double_tap" // 双重提交Cookie
)

func (Strategy) String

func (s Strategy) String() string

type TokenMode

type TokenMode string

TokenMode 令牌模式 支持三种模式 每次请求前都需要获取一次 (需要和path绑定) 会话级别 带有效期 (需要绑定到path)

const (
	TokenModePerRequest TokenMode = "per_request" // 每次请求前获取(最安全)
	TokenModeSession    TokenMode = "session"     // 会话级Token(登录后全局使用)
	TokenModeTimed      TokenMode = "timed"       // 带有效期Token
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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