security

package
v1.7.2 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HasBearerCredential added in v1.6.7

func HasBearerCredential(r *http.Request) bool

HasBearerCredential 判断请求是否带着 Bearer 形式的凭据。

它**不校验**凭据是否有效——那是 IdentityMiddleware 的事。这里只回答一个问题: "这是不是一个自己设置请求头的非浏览器客户端"。WebSocket 的 Origin 校验需要这个答案。

为什么请求头与查询参数等价:浏览器**无法**在 WebSocket 握手上设置 Authorization 头 (WebSocket API 不允许自定义头),所以"无 Origin 且带 Bearer 头"与 agent 原先用的 "无 Origin 且带 ?token=" 一样,都是非浏览器客户端的可靠标志。少了这一条, 把凭据从查询串挪到请求头之后,agent 的握手会被跨站 WebSocket 劫持防护拦成 403 (v1.6.6 的发布校验正是这样失败的:13 passed, 2 failed,agent 日志 `403 Forbidden`)。

func IsAPIKeyRequest

func IsAPIKeyRequest(r *http.Request) bool

func IsAuthorizationPreflight

func IsAuthorizationPreflight(r *http.Request) bool

func OriginInAllowlist

func OriginInAllowlist(origin, rawAllowlist string) bool

func OriginMatchesHost

func OriginMatchesHost(origin, host string) bool

func SplitAllowlist

func SplitAllowlist(raw string) []string

Types

type CorsController

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

CorsController 保存 CORS 中间件的可热更新状态。

相比过去把 config.Subscribe 直接埋在中间件闭包里,这里把「当前配置」抽成显式对象:

  • 需要热更新的状态集中在 controller 上,由外部(启动生命周期里的 reload 管理器) 统一调用 Update,避免订阅逻辑散落在中间件内部。
  • Middleware() 只负责读取当前状态并执行 CORS 校验,不再自行订阅配置事件。

func NewCorsController

func NewCorsController(enabled bool, allowedOrigins string) *CorsController

NewCorsController 使用初始配置构造一个 CORS 控制器。

func (*CorsController) Middleware

func (ctrl *CorsController) Middleware() gin.HandlerFunc

Middleware 返回读取当前控制器状态的 gin 中间件。

func (*CorsController) Update

func (ctrl *CorsController) Update(event config.ConfigEvent) bool

Update 根据配置事件刷新 CORS 相关状态。返回是否有字段发生变化。

Jump to

Keyboard shortcuts

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