Help us shape the future of Go! Take the 2026 Go Developer Survey

security

package
v0.26.0 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package security 提供独立的安全原语:

  • DownloadToken: 取件下载令牌(时间窗 HMAC + 恒时比较)
  • SSRF: 存储端点 URL 校验(scheme 白名单 + 私网地址策略)

Index

Constants

This section is empty.

Variables

View Source
var ErrDownloadToken = errors.New("下载令牌无效或已过期,请重新获取取件信息")

ErrDownloadToken 无效或过期的下载令牌

View Source
var ErrEndpointURL = fmt.Errorf("存储端点 URL 不合法")

ErrEndpointURL 存储端点 URL 不合法

Functions

func ContentDispositionAttachment added in v0.18.0

func ContentDispositionAttachment(name string) string

ContentDispositionAttachment 构造下载 Content-Disposition(RFC 6266 + 5987):

  • 引号/反斜杠/控制字符消毒防头断义;
  • 纯 ASCII 名只给 filename=;
  • 非 ASCII 名(中文等)补 filename*=UTF-8”<pct-encoded>,且 filename= 改用 纯 ASCII 兜底名——老客户端(Android 9 下载器等)不认 filename*,遇到裸 UTF-8 字节会把整个头丢弃变成 download.bin,兜底名至少保住扩展名与可辨识度。

pct-encoded 按 RFC 5987 attr-char 白名单逐字节编码(PathEscape 会放行 `;`/`,` 等非 attr-char,ext-value 里裸分号会截断头参数,故不复用它)。

func GenerateDownloadToken

func GenerateDownloadToken(code string) string

GenerateDownloadToken 生成下载令牌 "exp.sig"。 空密钥(未注入)时返回空串——调用方按"功能未启用"处理。

func SetDownloadTokenSecret

func SetDownloadTokenSecret(secret string)

SetDownloadTokenSecret 注入下载令牌签名密钥(应与 jwt_secret 等强的独立随机值)。

func ValidateEndpointHost

func ValidateEndpointHost(hostPort string) error

ValidateEndpointHost 校验裸主机端点(FTP/SFTP 的 host 字段:host[:port], 无 scheme 不适用 URL 解析)。策略与 ValidateEndpointURL 一致:IP 字面量直接 判私网;域名解析后逐 IP 复判;allow_private_networks 开启时全放行。

func ValidateEndpointURL

func ValidateEndpointURL(rawURL string) error

ValidateEndpointURL 校验 s3/webdav 存储端点 URL:

  • 仅允许 http/https scheme
  • 必须携带 host
  • DNS 解析后逐 IP 复判私网策略(防重绑定攻击:解析结果非配置期望网段时拒绝)

allowPrivate 未配置(默认 false)时拒绝环回/私网/链路本地地址。 局域网 MinIO/WebDAV(飞牛 NAS)部署需显式 security.ssrf.allow_private_networks=true。

func VerifyDownloadToken

func VerifyDownloadToken(code, token string) bool

VerifyDownloadToken 恒时比较校验令牌(exp 窗口 + HMAC 签名)。

Types

This section is empty.

Jump to

Keyboard shortcuts

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