secret

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: MIT Imports: 6 Imported by: 1 module, 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 secret 提供 XOps v2 纯函数领域核的机密借用容器(SecretBox)、凭据引用标识(Ref) 及 Go 1.27 runtime/secret 实验性硬件/运行时擦除作用域钩子。

本包恪守以下安全与架构不变量(ADR-011 / ADR-014 / ADR-016):

  • 100% 零 sync 与零 sync/atomic:SecretBox 为单协程作用域借用容器,由调用方在闭包边界内独占访问;
  • 零值失败关闭:SecretBox 同时具备正向有效门控 valid 与销毁标记 wiped,Ref 零值恒无效;
  • 故意零序列化接口:SecretBox 不实现任何 Text/JSON/Binary 编解码接口,使 encoding/json/v2 原生返回 SemanticError;
  • 入站与明文消费权限分离:WithBytes 仅标注 //xops:syncclosure 与 //xops:borrowed(严禁 //xops:secretuse), SecretBox.Use 仅允许 5 个白名单包调用,常量时间比对 Equal 免除测试夹具的明文消费需求;
  • 独占标准库 "runtime/secret" 导入权限(通过 goexperiment.runtimesecret 构建标签隔离)。

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidRef = errors.New("secret: 无效的凭据引用 Ref")

ErrInvalidRef 表示凭据引用 Ref 的 store 或 item 标识为空、超长或包含非法字符。

View Source
var ErrNilCallback = errors.New("secret: 作用域回调函数不能为 nil")

ErrNilCallback 表示传入 WithBytes 或 SecretBox.Use 的作用域回调函数为 nil。

View Source
var ErrNilReceiver = errors.New("secret: 反序列化目标接收者不能为 nil")

ErrNilReceiver 表示在 nil 指针接收者上调用反序列化方法。

View Source
var ErrSecretInvalid = errors.New("secret: SecretBox 无效或未初始化")

ErrSecretInvalid 表示 SecretBox 为 nil 或未通过 WithBytes 初始化的无效零值。

View Source
var ErrSecretWiped = errors.New("secret: SecretBox 已销毁清零")

ErrSecretWiped 表示 SecretBox 已被 Wipe 清零销毁,禁止再次访问明文。

Functions

func Do

func Do(fn func())

Do 在未启用 GOEXPERIMENT=runtimesecret 的构建下以普通同步闭包直接执行 fn, 保持与启用构建完全一致的调用语义与 //xops:syncclosure 静态分析契约。

func InSecretScope

func InSecretScope() bool

InSecretScope 在未启用 GOEXPERIMENT=runtimesecret 的构建下恒返回 false。

func RuntimeSecretBuildEnabled

func RuntimeSecretBuildEnabled() bool

RuntimeSecretBuildEnabled 报告当前二进制是否以 GOEXPERIMENT=runtimesecret 构建(本分支恒为 false)。

func WithBytes

func WithBytes(raw []byte, fn func(box *SecretBox) error) error

WithBytes 将调用方原始机密字节 raw 深拷贝入受管 SecretBox 并立即 clear(raw), 随后在同步作用域闭包 fn 内借出 *SecretBox;无论 fn 正常返回、返回错误还是发生 panic, 退出前均无条件调用 box.Wipe() 清零内部缓冲并永久失效句柄。

runtime/secret.Do 仅精准包裹明文摄入与清零临界区(分配 cp、拷贝与 clear(raw)), 不包裹外层调用方回调 fn(box),避免在 Phase 2–4 场景下将 secret 模式永久遗传给 fn 内派生的 长生命周期协程或将非机密堆对象纳入 GC 擦除跟踪;fn 内对明文的真实访问仍经由受 Do 保护的 box.Use 与 box.Equal 完成。

安全标注契约(RFC §3.2 / Plan §4.6): 仅标注 //xops:syncclosure 与 //xops:borrowed,严禁标注 //xops:secretuse, 以使 core/ports/tck、internal/host/sys 等非明文消费白名单包可合法完成入站构造。

Types

type Ref

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

Ref 是指向凭据存储(Vault / OS Keychain / External Helper)中某条机密的不可变非敏感引用标识。

Ref 仅携带 (store, item) 逻辑定位信息而不含任何明文机密,可安全进入配置、计划、事件与审计日志; 规范文本格式为 "<store>/<item>"(两段均严格禁止 '/'、路径穿越与跨平台非法文件名字符,保证双射无歧义); 零值代表无效未设置状态(IsValid() == false),序列化与 Equal 均严格失败关闭。

func NewRef

func NewRef(store, item string) (Ref, error)

NewRef 校验并构造不可变凭据引用 Ref。 store 与 item 均须为长度 1..128 的安全 ASCII 标识符(首字符 [A-Za-z0-9_],其余 [A-Za-z0-9._-], 不以 '.' 结尾,且拒绝 Windows DOS 保留设备主干名 CON/PRN/AUX/NUL/COM1–COM9/LPT1–LPT9), 确保跨 Linux/macOS/Windows 文件系统与 dirfd 映射无路径穿越或保留设备名碰撞风险。

func ParseRef

func ParseRef(raw string) (Ref, error)

ParseRef 解析规范文本格式 "<store>/<item>" 为 Ref;缺失分隔符、含多段斜杠或标识非法均失败关闭。

func (Ref) AppendText

func (r Ref) AppendText(b []byte) ([]byte, error)

AppendText 实现 encoding.TextAppender(值接收者);对无效零值严格失败关闭。

func (Ref) Compare

func (r Ref) Compare(other Ref) int

Compare 定义 Ref 上的确定性全序关系(无效零值排在有效值之前,有效值按 store 再按 item 字典序排列)。

func (Ref) Equal

func (r Ref) Equal(other Ref) bool

Equal 比较两个 Ref 是否均有效且指向同一 (store, item);遵循失败关闭原则,无效零值永不相等。

func (Ref) IsValid

func (r Ref) IsValid() bool

IsValid 报告 Ref 是否包含合法的非空 store 与 item 标识。

func (Ref) IsZero

func (r Ref) IsZero() bool

IsZero 报告 Ref 是否为无效零值(配合 encoding/json/v2 omitzero)。

func (Ref) Item

func (r Ref) Item() string

Item 返回存储内条目唯一标识;零值返回空串。

func (Ref) MarshalText

func (r Ref) MarshalText() ([]byte, error)

MarshalText 实现 encoding.TextMarshaler(值接收者),语义与 AppendText(nil) 完全一致。

func (Ref) Store

func (r Ref) Store() string

Store 返回所属凭据存储标识;零值返回空串。

func (Ref) String

func (r Ref) String() string

String 返回规范文本 "<store>/<item>";无效零值返回空串。

func (*Ref) UnmarshalText

func (r *Ref) UnmarshalText(text []byte) error

UnmarshalText 实现 encoding.TextUnmarshaler,解析 "<store>/<item>";失败时重置为零值并失败关闭。

type SecretBox

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

SecretBox 是单协程作用域借用机密字节容器(100% 零 sync/atomic)。

核心不变量:

  • 全字段私有且禁止包外直接构造(//xops:opaque);
  • 同时维护正向有效门控 valid 与销毁标记 wiped,零值、nil 及销毁后均严格失败关闭(//xops:failclosed);
  • 故意不实现任何 Text/JSON/Binary 序列化接口且不含任何 json 标签,确保 Go 1.27 encoding/json/v2 在尝试编码或解码本类型时原生返回 *json/v2.SemanticError;
  • 工程安全边界遵循 ADR-016:阻断日志/审计/错误/事件/YAML/计划/摘要泄露,并对自有缓冲执行 clear() 零化。

func (*SecretBox) Equal

func (b *SecretBox) Equal(other *SecretBox) bool

Equal 使用 crypto/subtle.ConstantTimeCompare 以常量时间比对两个 SecretBox 的明文内容。 本方法免除测试夹具(core/ports/tck)等非白名单包的明文消费需求; 严格失败关闭:若任一操作数为 nil、未初始化零值或已销毁(IsValid() == false),恒返回 false。

func (SecretBox) GoString

func (b SecretBox) GoString() string

GoString 返回恒定脱敏占位符,防止 %#v 格式化意外暴露内部字段。 采用值接收者,确保非可寻址 SecretBox 值与非 nil *SecretBox 指针均满足 fmt.GoStringer。

func (*SecretBox) IsValid

func (b *SecretBox) IsValid() bool

IsValid 报告 SecretBox 当前是否已有效初始化且尚未销毁。

func (*SecretBox) IsWiped

func (b *SecretBox) IsWiped() bool

IsWiped 报告 SecretBox 是否已经历 Wipe 销毁;对 nil 或未初始化零值返回 false。

func (*SecretBox) Len

func (b *SecretBox) Len() int

Len 返回有效 SecretBox 持有的字节长度;对 nil、无效零值或已销毁实例返回 0。

func (SecretBox) String

func (b SecretBox) String() string

String 返回恒定脱敏占位符,绝不输出明文、哈希或长度(纵深防御,不替代 secretbox 静态拦截)。 采用值接收者,确保非可寻址 SecretBox 值与非 nil *SecretBox 指针均满足 fmt.Stringer, 避免 fmt 回退到结构体反射打印原始字节(对 (*SecretBox)(nil),fmt 内置空指针防护会安全输出 <nil>)。

func (*SecretBox) Use

func (b *SecretBox) Use(fn func(plain []byte) error) error

Use 在同步作用域内以临时防御性副本向 fn 暴露明文字节,回调退出(含 panic)时立即 clear 临时副本。 内部使用副本而非暴露底层切片,确保回调对 plain 的原地修改或 clear(plain) 不会破坏同一 SecretBox 的后续读取或比对。

本方法标注 //xops:secretuse,由 xopsvet/secretbox 静态锁死仅 5 个白名单包可调用, 并由 xopsvet/secrettaint 与 linearlease 双重审计回调内明文零逃逸。

func (*SecretBox) Wipe

func (b *SecretBox) Wipe()

Wipe 立即使用 clear() 清零内部字节缓冲并永久失效 SecretBox;可安全重复调用,对 nil 接收者无操作。

Jump to

Keyboard shortcuts

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