Documentation
¶
Overview ¶
Package pathalg 提供 XOps v2 纯函数领域核使用的路径、软链接、Unicode 折叠碰撞与受保护路径代数。
本包恪守 100% 纯数学与零副作用铁律(严禁导入 os、path/filepath、net、sync、sync/atomic、reflect 或 context; 仅依赖纯标准库与白名单数学包 golang.org/x/text/unicode/norm),提供以下职责正交、全字段私有且零值失败关闭的抽象:
- RemoteName / RemotePath:POSIX 远端目录项名与规范化远端路径代数(零 path/filepath 混用);
- ValidateLocalComponent:跨 Linux/macOS/Windows 统一执行的单本地路径分量安全校验;
- LocalRootedDest:面向 ScopedRootFS(*os.Root)的严格沙箱相对目标路径;
- LocalDir:面向 ScopedRootFS.WithRoot 的本地目录根标识(纯字符串数学支持 POSIX 与 Windows 盘符路径);
- LocalSourcePath:面向 OperatorReadFS.WithOperatorRead 的操作员显式本地只读源路径;
- SymlinkPolicy 与策略 (b) 链式纯数学解析器:限制下载软链接严格留在根目录内且无折叠碰撞;
- Unicode NFC/NFD 规范化与跨 OS(macOS APFS/HFS+、Windows NTFS)大小写/规范化折叠碰撞检测;
- ProtectedPathMatcher:OS 无关 UTF-8 受保护路径匹配器,支持 *、**、?、[...]、精确/前缀/祖先检查、 NFC 规范化与 Win32 扩展设备/回环管理共享/ADS 规范化,并返回确定性命中规则集合供 ApprovalFSM 识别 Protected-A -> Protected-B 重定向升级。
所有持久化或跨协议序列化的值对象均以值接收者实现 encoding.TextAppender 与 encoding.TextMarshaler, 并以指针接收者实现 encoding.TextUnmarshaler,原生支持 Go 1.27 encoding/json/v2 与 YAML 确定性往返。
Index ¶
- Constants
- Variables
- func CheckComponentCollisions(names []string) error
- func CheckPathCollisions(paths []LocalRootedDest) error
- func FoldCollisionKey(name string) (string, error)
- func FoldLocalDestKey(dest LocalRootedDest) (string, error)
- func NormalizeNFC(s string) (string, error)
- func NormalizeNFD(s string) (string, error)
- func SameFoldedKey(a, b string) (bool, error)
- func ValidateContainedSymlink(target string, linkDirDepth int) error
- func ValidateContainedSymlinkChain(link LocalRootedDest, target string, staged []SymlinkBinding) error
- func ValidateContainedSymlinkSet(bindings []SymlinkBinding) error
- func ValidateLocalComponent(name string) error
- func ValidateSymlinkTarget(policy SymlinkPolicy, target string, linkDirDepth int) error
- type CollisionSet
- type LocalDir
- func (d LocalDir) AppendText(b []byte) ([]byte, error)
- func (d LocalDir) Compare(other LocalDir) int
- func (d LocalDir) Drive() (byte, bool)
- func (d LocalDir) Equal(other LocalDir) bool
- func (d LocalDir) IsAbs() bool
- func (d LocalDir) IsValid() bool
- func (d LocalDir) IsZero() bool
- func (d LocalDir) JoinComponent(name string) (LocalDir, error)
- func (d LocalDir) JoinDest(dest LocalRootedDest) (LocalDir, error)
- func (d LocalDir) Kind() LocalPathKind
- func (d LocalDir) MarshalText() ([]byte, error)
- func (d LocalDir) String() string
- func (d *LocalDir) UnmarshalText(text []byte) error
- type LocalPathKind
- type LocalRootedDest
- func (d LocalRootedDest) AppendText(b []byte) ([]byte, error)
- func (d LocalRootedDest) Base() string
- func (d LocalRootedDest) Compare(other LocalRootedDest) int
- func (d LocalRootedDest) Depth() int
- func (d LocalRootedDest) Equal(other LocalRootedDest) bool
- func (d LocalRootedDest) IsValid() bool
- func (d LocalRootedDest) IsZero() bool
- func (d LocalRootedDest) JoinComponent(name string) (LocalRootedDest, error)
- func (d LocalRootedDest) JoinRemoteName(name RemoteName) (LocalRootedDest, error)
- func (d LocalRootedDest) MarshalText() ([]byte, error)
- func (d LocalRootedDest) Parent() (LocalRootedDest, bool)
- func (d LocalRootedDest) Segments() []string
- func (d LocalRootedDest) String() string
- func (d *LocalRootedDest) UnmarshalText(text []byte) error
- type LocalSourcePath
- func (s LocalSourcePath) AppendText(b []byte) ([]byte, error)
- func (s LocalSourcePath) Base() string
- func (s LocalSourcePath) Compare(other LocalSourcePath) int
- func (s LocalSourcePath) Dir() LocalDir
- func (s LocalSourcePath) Drive() (byte, bool)
- func (s LocalSourcePath) Equal(other LocalSourcePath) bool
- func (s LocalSourcePath) IsAbs() bool
- func (s LocalSourcePath) IsValid() bool
- func (s LocalSourcePath) IsZero() bool
- func (s LocalSourcePath) Kind() LocalPathKind
- func (s LocalSourcePath) MarshalText() ([]byte, error)
- func (s LocalSourcePath) String() string
- func (s *LocalSourcePath) UnmarshalText(text []byte) error
- type MatchKind
- type ProtectedMatch
- func (m ProtectedMatch) IntroducesNewZoneOver(before ProtectedMatch) bool
- func (m ProtectedMatch) IsValid() bool
- func (m ProtectedMatch) Matched() bool
- func (m ProtectedMatch) PrimaryRule() (ProtectedRuleMatch, bool)
- func (m ProtectedMatch) Rules() []ProtectedRuleMatch
- func (m ProtectedMatch) SameZones(other ProtectedMatch) bool
- func (m ProtectedMatch) Zones() []string
- type ProtectedPathMatcher
- func (m ProtectedPathMatcher) IsValid() bool
- func (m ProtectedPathMatcher) IsZero() bool
- func (m ProtectedPathMatcher) Match(target string) (ProtectedMatch, error)
- func (m ProtectedPathMatcher) MatchRecursive(dir string) (ProtectedMatch, error)
- func (m ProtectedPathMatcher) MatchRemotePath(p RemotePath) (ProtectedMatch, error)
- func (m ProtectedPathMatcher) RuleCount() int
- type ProtectedRule
- type ProtectedRuleMatch
- type RemoteName
- func (n RemoteName) AppendText(b []byte) ([]byte, error)
- func (n RemoteName) Compare(other RemoteName) int
- func (n RemoteName) Equal(other RemoteName) bool
- func (n RemoteName) IsValid() bool
- func (n RemoteName) IsZero() bool
- func (n RemoteName) MarshalText() ([]byte, error)
- func (n RemoteName) String() string
- func (n *RemoteName) UnmarshalText(text []byte) error
- type RemotePath
- func (p RemotePath) AppendText(b []byte) ([]byte, error)
- func (p RemotePath) Base() (RemoteName, bool)
- func (p RemotePath) Canonical() RemotePath
- func (p RemotePath) Compare(other RemotePath) int
- func (p RemotePath) Dir() RemotePath
- func (p RemotePath) Equal(other RemotePath) bool
- func (p RemotePath) HasPrefix(prefix RemotePath) bool
- func (p RemotePath) IsAbs() bool
- func (p RemotePath) IsRoot() bool
- func (p RemotePath) IsValid() bool
- func (p RemotePath) IsZero() bool
- func (p RemotePath) Join(names ...RemoteName) (RemotePath, error)
- func (p RemotePath) MarshalText() ([]byte, error)
- func (p RemotePath) Rel(target RemotePath) (RemotePath, error)
- func (p RemotePath) Resolve(target RemotePath) (RemotePath, error)
- func (p RemotePath) String() string
- func (p *RemotePath) UnmarshalText(text []byte) error
- type SymlinkBinding
- type SymlinkPolicy
- func (p SymlinkPolicy) AllowsContainedRelative() bool
- func (p SymlinkPolicy) AllowsOperatorFollow() bool
- func (p SymlinkPolicy) AppendText(b []byte) ([]byte, error)
- func (p SymlinkPolicy) IsValid() bool
- func (p SymlinkPolicy) MarshalText() ([]byte, error)
- func (p SymlinkPolicy) String() string
- func (p *SymlinkPolicy) UnmarshalText(text []byte) error
Constants ¶
const ( // MaxComponentBytes 是单个路径分量允许的最大 UTF-8 字节长度(对齐 POSIX NAME_MAX 与 NTFS/APFS 255 单元上限)。 MaxComponentBytes = 255 // MaxRemotePathBytes 是远端 POSIX 路径与沙箱相对路径允许的最大 UTF-8 字节长度(对齐 POSIX PATH_MAX)。 MaxRemotePathBytes = 4096 // MaxLocalPathBytes 是本地目录或操作员本地源路径允许的最大 UTF-8 字节长度(兼容 Windows 长路径上限)。 MaxLocalPathBytes = 32768 // MaxSymlinkHops 是链式相对软链接纯数学解析允许的最大展开跳数(对齐 POSIX SYMLOOP_MAX)。 MaxSymlinkHops = 40 // MaxSymlinkSteps 是链式相对软链接解析允许的最大总步数,阻断指数级链式放大攻击。 MaxSymlinkSteps = 4096 )
Variables ¶
var ErrInvalidComponent = errors.New("pathalg: 无效的本地路径分量")
ErrInvalidComponent 表示单个本地文件名/目录分量违反跨平台安全规则。
var ErrInvalidGlob = errors.New("pathalg: 无效的通配符模式")
ErrInvalidGlob 表示 UTF-8 通配符模式存在未闭合字符类或非法字符范围。
var ErrInvalidLocalDest = errors.New("pathalg: 无效的本地沙箱目标路径")
ErrInvalidLocalDest 表示面向 ScopedRootFS 的沙箱相对目标路径非法或存在逃逸风险。
var ErrInvalidLocalDir = errors.New("pathalg: 无效的本地根目录路径")
ErrInvalidLocalDir 表示面向 ScopedRootFS.WithRoot 的本地根目录路径非法或存在穿越风险。
var ErrInvalidLocalSource = errors.New("pathalg: 无效的本地只读源路径")
ErrInvalidLocalSource 表示面向 OperatorReadFS.WithOperatorRead 的本地源路径非法。
var ErrInvalidProtectedRule = errors.New("pathalg: 无效的受保护路径规则")
ErrInvalidProtectedRule 表示受保护路径规则或匹配器未初始化、语法无效或包含非法字符。
var ErrInvalidRemoteName = errors.New("pathalg: 无效的远端名称")
ErrInvalidRemoteName 表示单个远端 POSIX 目录项名称为空、含 '/'、为 '.'/'..' 或含控制/Bidi 字符。
var ErrInvalidRemotePath = errors.New("pathalg: 无效的远端路径")
ErrInvalidRemotePath 表示远端 POSIX 路径为空、超长、含非法 UTF-8 或含控制/Bidi 字符。
var ErrInvalidSymlinkPolicy = errors.New("pathalg: 无效的软链接策略")
ErrInvalidSymlinkPolicy 表示软链接策略未识别、格式无效或不允许当前操作。
var ErrInvalidUTF8 = errors.New("pathalg: 无效的 UTF-8 编码")
ErrInvalidUTF8 表示输入字符串包含非法 UTF-8 字节序列。
var ErrNameCollision = errors.New("pathalg: 路径名称存在 Unicode 或大小写折叠碰撞")
ErrNameCollision 表示两个不同名称或路径经 Unicode NFC/NFD 与跨 OS 大小写折叠后发生碰撞。
var ErrNilReceiver = errors.New("pathalg: 反序列化目标接收者不能为 nil")
ErrNilReceiver 表示在 nil 指针接收者上调用反序列化方法。
var ErrUnsafeSymlink = errors.New("pathalg: 不安全的符号链接")
ErrUnsafeSymlink 表示符号链接目标或链式解析违反安全策略(如绝对路径、逃逸根目录、环路或碰撞)。
Functions ¶
func CheckComponentCollisions ¶
CheckComponentCollisions 确定性地检验一组同级名称是否存在重复、NFC/NFD 或大小写折叠碰撞。
func CheckPathCollisions ¶
func CheckPathCollisions(paths []LocalRootedDest) error
CheckPathCollisions 确定性地检验一组 LocalRootedDest 是否存在路径级(含 NFC/NFD 与大小写折叠)碰撞。
func FoldCollisionKey ¶
FoldCollisionKey 计算用于跨 OS(macOS APFS/HFS+、Windows NTFS、Linux)文件名碰撞检测的规范折叠键。
算法仅依赖标准库 unicode 与白名单数学包 golang.org/x/text/unicode/norm:
- 校验输入为合法 UTF-8 并先规范分解为 NFD(消除 NFC/NFD 编码差异,如 "é" 与 "e\u0301"、 以及带规范分解的字符如 "İ" -> "I\u0307"、"ᾀ" -> "α\u0313\u0345");
- 对 NFD 惰性的 Unicode CaseFolding Status F 字符执行多码点全折叠展开(如 "ß"/"ẞ" -> "ss"、 "ʼn"、"և"、"ẚ" 与连字 "ff"–"st"/"ﬓ"–"ﬗ"),并对每个码点取 unicode.SimpleFold 轨道内 优先满足 IsLower 且 CCC == 0 的规范基字符代表(使希腊语 iota 轨道 {U+0345, Ι, ι, ι} 稳定映射为 CCC=0 的基字母 'ι',杜绝 U+0345 CCC=240 引发的跨重音重排误碰撞);
- 再次执行 NFD 规范重排与轨道折叠,保证满足幂等律 Fold(Fold(x)) == Fold(x) 与 规范化不变律 Fold(NFC(x)) == Fold(NFD(x))。
func FoldLocalDestKey ¶
func FoldLocalDestKey(dest LocalRootedDest) (string, error)
FoldLocalDestKey 计算沙箱相对路径 LocalRootedDest 的逐段跨 OS 规范折叠键(以 '/' 连接)。
func NormalizeNFC ¶
NormalizeNFC 将合法 UTF-8 字符串规范化为 Unicode 规范组合形式(NFC);非法 UTF-8 失败关闭。
func NormalizeNFD ¶
NormalizeNFD 将合法 UTF-8 字符串规范化为 Unicode 规范分解形式(NFD);非法 UTF-8 失败关闭。
func SameFoldedKey ¶
SameFoldedKey 报告两个字符串在跨 OS Unicode NFC/NFD + 大小写折叠下是否等价。 输入含非法 UTF-8 时失败关闭返回错误。
func ValidateContainedSymlink ¶
ValidateContainedSymlink 执行 RFC 软链下载策略 (b) 单跳词法与深度不越界校验。
仅允许满足以下全部条件的相对 POSIX 符号链接目标:
- 非空、长度 <= MaxRemotePathBytes、有效 UTF-8、无 NUL/控制/Bidi-Cf 字符;
- 相对 POSIX 路径:不以 '/' 开头、不含 '\'、不含 ':'(阻断 Windows 盘符与 NTFS ADS);
- 除 "." 与 ".." 外,每一分量均通过 ValidateLocalComponent 校验;
- 从 linkDirDepth(>= 0,表示符号链接所在父目录相对根目录的深度)出发, 逐段解析过程中任意中间步深度均严格保持 >= 0(绝不允许先逃出根再折返)。
func ValidateContainedSymlinkChain ¶
func ValidateContainedSymlinkChain(link LocalRootedDest, target string, staged []SymlinkBinding) error
ValidateContainedSymlinkChain 校验将 (link -> target) 加入 staged 后构成的完整软链图是否满足策略 (b)。
本函数将 (link, target) 与 staged 合并为完整图 G' 后委托 ValidateContainedSymlinkSet 执行全图校验, 严格阻断自环、增量互环、逆序链式逃逸、逆序祖先软链接管与重复路径覆写。
func ValidateContainedSymlinkSet ¶
func ValidateContainedSymlinkSet(bindings []SymlinkBinding) error
ValidateContainedSymlinkSet 在纯内存中对完整符号链接集合执行置换不变的策略 (b) 全图校验。
软链包含性属于整图全局不变量,与输入到达顺序无关,本函数强制校验:
- 集合内所有软链均有效,且不存在任何精确重复或跨 OS Unicode/大小写折叠碰撞;
- 集合内每条软链的所有祖先目录分量均不得命中集合内任何软链(精确或折叠匹配);
- 从集合内每条软链父目录出发,在完整全图索引上逐段展开所有命中的树内软链(含自环、增量互环与 逆序链式逃逸),受 MaxSymlinkHops(40 跳)与 MaxSymlinkSteps(4096 步)约束且任意中间步深度恒 >= 0。
func ValidateLocalComponent ¶
ValidateLocalComponent 以三平台(Linux/macOS/Windows)完全一致的失败关闭规则校验单个本地路径分量。
本函数不依赖当前运行 OS,统一拒绝:
- 空串、"."、".." 以及超长(> 255 UTF-8 字节)分量;
- 非法 UTF-8、NUL、ASCII/Unicode 控制字符(Cc)、不可见与双向排版格式字符(Cf,如 U+202E/U+200B/U+FEFF) 以及 Unicode 行/段分隔符(U+2028/U+2029);
- 路径分隔符 '/' 与 '\',冒号 ':'(阻断盘符前缀与 NTFS ADS 备用数据流)以及 Windows/NTFS 禁用字符 '*'、'?'、'"'、'<'、'>'、'|';
- 尾随点 '.' 或尾随 ASCII 空格 ' '(防止 Windows 静默裁剪导致同名覆写与扩展名欺骗);
- Windows DOS 保留设备名(在首个 '.' 之前、忽略尾随 ASCII 空格且大小写不敏感): CON、PRN、AUX、NUL、CONIN$、CONOUT$、COM1–COM9、LPT1–LPT9 及 Latin-1 上标变体 COM¹–COM³、LPT¹–LPT³。
所有拒绝错误均包装 ErrInvalidComponent。
func ValidateSymlinkTarget ¶
func ValidateSymlinkTarget(policy SymlinkPolicy, target string, linkDirDepth int) error
ValidateSymlinkTarget 按指定 SymlinkPolicy 对单条符号链接目标执行纯数学准入校验。
行为契约:
- SymlinkReject 与任意无效枚举值:一律失败关闭返回错误;
- SymlinkContainedRelative:强制执行 RFC 策略 (b) 词法与深度不越界校验;
- SymlinkFollowOperatorSource:仅允许合法且不含控制/Bidi 字符的非空目标文本(不用于沙箱下载创建)。
Types ¶
type CollisionSet ¶
type CollisionSet struct {
// contains filtered or unexported fields
}
CollisionSet 在纯内存中检测一组同级文件名或沙箱路径是否存在重复、NFC/NFD 等价或跨 OS 大小写折叠碰撞。
本类型仅用于单协程纯数学校验(零 sync),零值经首次 Add 自动懒初始化并始终保持确定性语义。
func (*CollisionSet) Add ¶
func (s *CollisionSet) Add(raw string) error
Add 将单个名称或 '/' 分隔相对路径加入集合;若与已有条目重复或折叠等价,则失败关闭返回 ErrNameCollision。
func (*CollisionSet) LookupFolded ¶
func (s *CollisionSet) LookupFolded(raw string) (string, bool, error)
LookupFolded 查询与 raw 在跨 OS 折叠意义下等价的已登记原始条目。
type LocalDir ¶
type LocalDir struct {
// contains filtered or unexported fields
}
LocalDir 表示面向 ScopedRootFS.WithRoot 的已校验本地目录根标识。
纯字符串数学同时支持 POSIX 绝对目录(如 "/"、"/var/lib/xops")、Windows 盘符绝对目录 (如 "C:/"、"C:\Users\alice\.xops" 规范化为 "C:/Users/alice/.xops")及无 ".." 穿越的安全相对目录 (如 "."、"downloads/sub");严格拒绝 ".." 遍历段、盘符相对路径("C:foo")、单反斜杠根相对("\foo")、 UNC("\\server\share")、Win32 设备命名空间("\\?\"、"\\.\"、"\??\")及控制/Bidi 字符。
func (LocalDir) AppendText ¶
AppendText 实现 encoding.TextAppender(值接收者);无效零值返回 ErrInvalidLocalDir。
func (LocalDir) JoinComponent ¶
JoinComponent 在当前本地目录根下安全追加单个经 ValidateLocalComponent 校验的子目录分量, 正确处理 POSIX 根目录 "/"、Windows 盘符根目录 "C:/" 与当前目录 "."(绝不产生 "//sub")。
func (LocalDir) JoinDest ¶
func (d LocalDir) JoinDest(dest LocalRootedDest) (LocalDir, error)
JoinDest 在当前本地目录根下安全拼接已校验的沙箱相对路径 LocalRootedDest, 正确处理 POSIX 根目录 "/"、Windows 盘符根目录 "C:/" 与当前目录 "."(绝不产生 "//sub")。
func (LocalDir) MarshalText ¶
MarshalText 实现 encoding.TextMarshaler(值接收者)。
func (*LocalDir) UnmarshalText ¶
UnmarshalText 实现 encoding.TextUnmarshaler;失败时重置为零值。
type LocalPathKind ¶
type LocalPathKind uint8
LocalPathKind 标识本地路径(LocalDir / LocalSourcePath)的命名空间类别;零值 LocalPathInvalid 严格代表无效态。
const ( // LocalPathInvalid 表示未初始化或校验失败的无效本地路径类别(失败关闭零值)。 LocalPathInvalid LocalPathKind = iota // LocalPathPOSIXAbs 表示以 '/' 开头的 POSIX 绝对路径(如 "/var/lib/xops")。 LocalPathPOSIXAbs // LocalPathWindowsDrive 表示以大写盘符开头的 Windows 绝对路径(规范表示为 "C:/...")。 LocalPathWindowsDrive // LocalPathRelative 表示不含 ".." 穿越段的安全相对本地路径(如 "." 或 "out/app.tgz")。 LocalPathRelative )
type LocalRootedDest ¶
type LocalRootedDest struct {
// contains filtered or unexported fields
}
LocalRootedDest 表示面向 ScopedRootFS(Go *os.Root)的严格沙箱相对目标路径。
核心安全不变量:
- 严格为非空相对路径,以 '/' 分隔且至少包含一个分量,100% 兼容 io/fs.ValidPath 与 *os.Root;
- 严禁前导/尾随 '/'、双斜杠 '//'、反斜杠 '\'、Windows 盘符/UNC 前缀、"." 或 ".." 段;
- 每一路径分量均经 ValidateLocalComponent 强制校验,阻断跨 OS 设备名、ADS、尾随点/空格与控制字符;
- 零值恒无效(IsValid() == false),且 Equal 对无效值恒返回 false。
func LocalRootedDestFromComponents ¶
func LocalRootedDestFromComponents(parts ...string) (LocalRootedDest, error)
LocalRootedDestFromComponents 将一组单个本地分量校验并拼接为 LocalRootedDest。
func ParseLocalRootedDest ¶
func ParseLocalRootedDest(raw string) (LocalRootedDest, error)
ParseLocalRootedDest 校验并构造严格位于沙箱根目录内的相对目标路径。
func (LocalRootedDest) AppendText ¶
func (d LocalRootedDest) AppendText(b []byte) ([]byte, error)
AppendText 实现 encoding.TextAppender(值接收者);无效零值返回 ErrInvalidLocalDest。
func (LocalRootedDest) Compare ¶
func (d LocalRootedDest) Compare(other LocalRootedDest) int
Compare 定义 LocalRootedDest 上的确定性全序关系。
func (LocalRootedDest) Depth ¶
func (d LocalRootedDest) Depth() int
Depth 返回路径分量个数(有效值 >= 1,无效零值返回 0);所在父目录相对根的深度为 Depth() - 1。
func (LocalRootedDest) Equal ¶
func (d LocalRootedDest) Equal(other LocalRootedDest) bool
Equal 判断两个 LocalRootedDest 是否均有效且完全相等;无效零值永不相等。
func (LocalRootedDest) IsValid ¶
func (d LocalRootedDest) IsValid() bool
IsValid 报告 LocalRootedDest 是否有效。
func (LocalRootedDest) IsZero ¶
func (d LocalRootedDest) IsZero() bool
IsZero 报告 LocalRootedDest 是否为无效零值。
func (LocalRootedDest) JoinComponent ¶
func (d LocalRootedDest) JoinComponent(name string) (LocalRootedDest, error)
JoinComponent 在当前沙箱目标路径下追加单个经 ValidateLocalComponent 校验的分量。
func (LocalRootedDest) JoinRemoteName ¶
func (d LocalRootedDest) JoinRemoteName(name RemoteName) (LocalRootedDest, error)
JoinRemoteName 将已校验的远端名称通过跨平台 ValidateLocalComponent 桥接追加到当前沙箱目标路径。
func (LocalRootedDest) MarshalText ¶
func (d LocalRootedDest) MarshalText() ([]byte, error)
MarshalText 实现 encoding.TextMarshaler(值接收者)。
func (LocalRootedDest) Parent ¶
func (d LocalRootedDest) Parent() (LocalRootedDest, bool)
Parent 返回父级 LocalRootedDest;若当前已处于根直属第一层或无效,则返回 (零值, false)。
func (LocalRootedDest) Segments ¶
func (d LocalRootedDest) Segments() []string
Segments 返回路径分量切片的新副本;无效零值返回 nil。
func (LocalRootedDest) String ¶
func (d LocalRootedDest) String() string
String 返回规范化沙箱相对路径(以 '/' 分隔);无效零值返回空串。
func (*LocalRootedDest) UnmarshalText ¶
func (d *LocalRootedDest) UnmarshalText(text []byte) error
UnmarshalText 实现 encoding.TextUnmarshaler;失败时重置为零值。
type LocalSourcePath ¶
type LocalSourcePath struct {
// contains filtered or unexported fields
}
LocalSourcePath 表示面向 OperatorReadFS.WithOperatorRead 的操作员显式本地只读源路径。
纯字符串数学支持 POSIX 绝对路径("/home/alice/.ssh/id_ed25519")、跨盘符 Windows 绝对路径 ("C:\Temp\a.tgz" -> "C:/Temp/a.tgz")及无 ".." 穿越的安全相对文件路径("dist/app.tgz"); 严格拒绝裸根目录("/"、"C:/")、"."、".." 穿越段、盘符相对路径、UNC、Win32 设备路径及控制/Bidi 字符。
func ParseLocalSourcePath ¶
func ParseLocalSourcePath(raw string) (LocalSourcePath, error)
ParseLocalSourcePath 校验并规范化操作员提供的本地只读源路径。
func (LocalSourcePath) AppendText ¶
func (s LocalSourcePath) AppendText(b []byte) ([]byte, error)
AppendText 实现 encoding.TextAppender(值接收者);无效零值返回 ErrInvalidLocalSource。
func (LocalSourcePath) Compare ¶
func (s LocalSourcePath) Compare(other LocalSourcePath) int
Compare 定义 LocalSourcePath 上的确定性全序关系。
func (LocalSourcePath) Dir ¶
func (s LocalSourcePath) Dir() LocalDir
Dir 返回源路径所在的已校验父目录 LocalDir;无效零值返回无效零值。
func (LocalSourcePath) Drive ¶
func (s LocalSourcePath) Drive() (byte, bool)
Drive 在路径为 Windows 盘符路径时返回大写盘符字母('A'..'Z')与 true,否则返回 (0, false)。
func (LocalSourcePath) Equal ¶
func (s LocalSourcePath) Equal(other LocalSourcePath) bool
Equal 判断两个 LocalSourcePath 是否均有效且完全相等;无效零值永不相等。
func (LocalSourcePath) IsValid ¶
func (s LocalSourcePath) IsValid() bool
IsValid 报告 LocalSourcePath 是否有效。
func (LocalSourcePath) IsZero ¶
func (s LocalSourcePath) IsZero() bool
IsZero 报告 LocalSourcePath 是否为无效零值。
func (LocalSourcePath) MarshalText ¶
func (s LocalSourcePath) MarshalText() ([]byte, error)
MarshalText 实现 encoding.TextMarshaler(值接收者)。
func (LocalSourcePath) String ¶
func (s LocalSourcePath) String() string
String 返回规范化路径文本(以 '/' 分隔);无效零值返回空串。
func (*LocalSourcePath) UnmarshalText ¶
func (s *LocalSourcePath) UnmarshalText(text []byte) error
UnmarshalText 实现 encoding.TextUnmarshaler;失败时重置为零值。
type ProtectedMatch ¶
type ProtectedMatch struct {
// contains filtered or unexported fields
}
ProtectedMatch 表示一次受保护路径匹配的确定性结果快照(全部命中规则按字典序排序且去重)。
零值恒无效(IsValid() == false):在安全比较谓词 SameZones / IntroducesNewZoneOver 中遵循失败关闭原则。
func (ProtectedMatch) IntroducesNewZoneOver ¶
func (m ProtectedMatch) IntroducesNewZoneOver(before ProtectedMatch) bool
IntroducesNewZoneOver 判断当前(通常为规范化后)匹配结果是否相对 before(规范化前)引入了新的保护区域。
用于 ApprovalFSM 精确捕获“非受保护 -> 受保护”以及“Protected-A -> Protected-B”软链重定向升级; 若任一输入为无效零值,则严格失败关闭返回 true(视作发生范围升级)。
func (ProtectedMatch) IsValid ¶
func (m ProtectedMatch) IsValid() bool
IsValid 报告该匹配结果是否来自有效的 ProtectedPathMatcher 计算。
func (ProtectedMatch) PrimaryRule ¶
func (m ProtectedMatch) PrimaryRule() (ProtectedRuleMatch, bool)
PrimaryRule 返回确定性首条命中规则(优先直接命中,其次按规范字典序);未命中返回 (零值, false)。
func (ProtectedMatch) Rules ¶
func (m ProtectedMatch) Rules() []ProtectedRuleMatch
Rules 返回全部命中规则的确定性只读副本(按 Class、CanonicalRule、Kind 升序排列)。
func (ProtectedMatch) SameZones ¶
func (m ProtectedMatch) SameZones(other ProtectedMatch) bool
SameZones 判断两次有效匹配结果所命中的受保护区域集合是否完全一致;任一结果无效均失败关闭返回 false。
func (ProtectedMatch) Zones ¶
func (m ProtectedMatch) Zones() []string
Zones 返回去重且升序排列的命中保护区域(Class)集合。
type ProtectedPathMatcher ¶
type ProtectedPathMatcher struct {
// contains filtered or unexported fields
}
ProtectedPathMatcher 是不可变的 OS 无关 UTF-8 受保护路径匹配器。
零值恒无效(IsValid() == false),调用 Match / MatchRecursive 会严格失败关闭返回错误。
func NewClassifiedProtectedPathMatcher ¶
func NewClassifiedProtectedPathMatcher(rules []ProtectedRule) (ProtectedPathMatcher, error)
NewClassifiedProtectedPathMatcher 从一组带显式区域/类别标签的 ProtectedRule 构造不可变匹配器。
func NewProtectedPathMatcher ¶
func NewProtectedPathMatcher(patterns []string) (ProtectedPathMatcher, error)
NewProtectedPathMatcher 从一组受保护路径模式字符串构造不可变匹配器。
func (ProtectedPathMatcher) IsValid ¶
func (m ProtectedPathMatcher) IsValid() bool
IsValid 报告匹配器是否已合法构造。
func (ProtectedPathMatcher) IsZero ¶
func (m ProtectedPathMatcher) IsZero() bool
IsZero 报告匹配器是否为无效零值。
func (ProtectedPathMatcher) Match ¶
func (m ProtectedPathMatcher) Match(target string) (ProtectedMatch, error)
Match 判定单个路径目标是否直接命中任一受保护路径规则,并返回确定性命中集合。 匹配器未初始化或目标为空/含非法 UTF-8/控制/Bidi 字符时严格失败关闭返回错误。
func (ProtectedPathMatcher) MatchRecursive ¶
func (m ProtectedPathMatcher) MatchRecursive(dir string) (ProtectedMatch, error)
MatchRecursive 判定递归目录操作目标是否直接受保护,或其子树下包含受保护路径规则(祖先命中)。
func (ProtectedPathMatcher) MatchRemotePath ¶
func (m ProtectedPathMatcher) MatchRemotePath(p RemotePath) (ProtectedMatch, error)
MatchRemotePath 对已校验的 RemotePath 执行受保护路径直接匹配。
func (ProtectedPathMatcher) RuleCount ¶
func (m ProtectedPathMatcher) RuleCount() int
RuleCount 返回已编译并去重后的规则数量。
type ProtectedRule ¶
type ProtectedRule struct {
// contains filtered or unexported fields
}
ProtectedRule 表示一条已编译前校验的受保护路径规则及其所属安全区域/类别标签。
零值恒无效(IsValid() == false)。若通过 ParseProtectedRule 构造,Class 默认与规范化规则文本一致; 若通过 NewProtectedRule 构造,可为同区域多条规则赋予统一 Class,供 ApprovalFSM 精准判定 Protected-A -> Protected-B 软链重定向升级。
func NewProtectedRule ¶
func NewProtectedRule(pattern, class string) (ProtectedRule, error)
NewProtectedRule 校验并编译带显式区域/类别标签的受保护路径规则。 支持 POSIX 绝对规则("/etc/**"、"/root"、"/etc/*.conf")、Windows 盘符规则("C:/Windows/**"、"C:\Windows\**")、 UNC 规则("//server/share/**")与全局递归规则("**/.ssh/**");拒绝空串、相对裸路径与非法控制/Bidi 字符。
func ParseProtectedRule ¶
func ParseProtectedRule(pattern string) (ProtectedRule, error)
ParseProtectedRule 校验并编译单条受保护路径规则(Class 默认等于规范化规则文本)。
func (ProtectedRule) Canonical ¶
func (r ProtectedRule) Canonical() string
Canonical 返回经 NFC 与斜杠规范化后的规则文本;无效零值返回空串。
type ProtectedRuleMatch ¶
type ProtectedRuleMatch struct {
// contains filtered or unexported fields
}
ProtectedRuleMatch 记录单条命中的受保护路径规则摘要。
func (ProtectedRuleMatch) CanonicalRule ¶
func (m ProtectedRuleMatch) CanonicalRule() string
CanonicalRule 返回命中的规范化规则文本。
func (ProtectedRuleMatch) Class ¶
func (m ProtectedRuleMatch) Class() string
Class 返回命中规则的保护区域/类别标识。
func (ProtectedRuleMatch) IsValid ¶
func (m ProtectedRuleMatch) IsValid() bool
IsValid 报告命中记录是否有效(kind != MatchNone 且规则非空)。
func (ProtectedRuleMatch) Kind ¶
func (m ProtectedRuleMatch) Kind() MatchKind
Kind 返回命中类别(MatchDirect 或 MatchAncestorContainsProtected)。
type RemoteName ¶
type RemoteName struct {
// contains filtered or unexported fields
}
RemoteName 表示经过严格校验的单个 POSIX 远端目录项名称(不含 '/'、非 "."/".."、无 NUL/控制/Bidi-Cf 字符)。
本类型仅约束 POSIX 远端单段合法性(不限制远端文件名含 ':' 或 '\'); 若需落盘至本地沙箱,必须经 LocalRootedDest.JoinRemoteName 触发跨平台 ValidateLocalComponent 校验。 零值恒无效(IsValid() == false),且 Equal 对无效值恒返回 false。
func ParseRemoteName ¶
func ParseRemoteName(raw string) (RemoteName, error)
ParseRemoteName 校验并构造单个远端 POSIX 目录项名称。
func (RemoteName) AppendText ¶
func (n RemoteName) AppendText(b []byte) ([]byte, error)
AppendText 实现 encoding.TextAppender(值接收者);无效零值失败关闭返回 ErrInvalidRemoteName。
func (RemoteName) Compare ¶
func (n RemoteName) Compare(other RemoteName) int
Compare 定义 RemoteName 上的确定性全序关系(无效零值排在所有有效值之前)。
func (RemoteName) Equal ¶
func (n RemoteName) Equal(other RemoteName) bool
Equal 判断两个 RemoteName 是否均有效且逐字节相等;无效零值永不相等。
func (RemoteName) IsZero ¶
func (n RemoteName) IsZero() bool
IsZero 报告 RemoteName 是否为无效零值(配合 encoding/json/v2 omitzero)。
func (RemoteName) MarshalText ¶
func (n RemoteName) MarshalText() ([]byte, error)
MarshalText 实现 encoding.TextMarshaler(值接收者)。
func (*RemoteName) UnmarshalText ¶
func (n *RemoteName) UnmarshalText(text []byte) error
UnmarshalText 实现 encoding.TextUnmarshaler;失败时重置为零值并失败关闭。
type RemotePath ¶
type RemotePath struct {
// contains filtered or unexported fields
}
RemotePath 表示经过规范化 POSIX 清洗的不可变远端路径值对象。
关键不变量与架构边界(ADR-007 / xopsvet/osboundary):
- 全程仅使用标准库 path 执行 POSIX 词法规范化,严禁在任何包内对 RemotePath 调用 path/filepath.*;
- 保留合法 UTF-8 原始字节序列(不做隐式 NFC/NFD 重写,避免在字节敏感的 Linux SFTP 服务器上发生 ENOENT);
- 严格拒绝空串、超长路径(> 4096 字节)、单段超长(> 255 字节)、非法 UTF-8、NUL、控制字符与 Bidi/Cf 格式字符;
- 零值恒无效(IsValid() == false),且 Equal / HasPrefix 对无效值恒返回 false。
func ParseAbsRemotePath ¶
func ParseAbsRemotePath(raw string) (RemotePath, error)
ParseAbsRemotePath 校验并规范化绝对 POSIX 远端路径(必须以 '/' 开头)。
func ParseRemotePath ¶
func ParseRemotePath(raw string) (RemotePath, error)
ParseRemotePath 校验并按 POSIX 语义规范化远端路径字符串。
func (RemotePath) AppendText ¶
func (p RemotePath) AppendText(b []byte) ([]byte, error)
AppendText 实现 encoding.TextAppender(值接收者);无效零值失败关闭返回 ErrInvalidRemotePath。
func (RemotePath) Base ¶
func (p RemotePath) Base() (RemoteName, bool)
Base 返回路径末段作为已校验的 RemoteName;当路径无效、为 "/"、"." 或末段为 ".." 时返回 (零值, false)。
func (RemotePath) Canonical ¶
func (p RemotePath) Canonical() RemotePath
Canonical 返回规范化后的 RemotePath(对有效值满足幂等律 p.Canonical() == p;无效值返回零值)。
func (RemotePath) Compare ¶
func (p RemotePath) Compare(other RemotePath) int
Compare 定义 RemotePath 上的确定性全序关系(无效零值排在所有有效值之前)。
func (RemotePath) Dir ¶
func (p RemotePath) Dir() RemotePath
Dir 返回当前路径的父目录(POSIX path.Dir 语义);无效零值返回无效零值。
func (RemotePath) Equal ¶
func (p RemotePath) Equal(other RemotePath) bool
Equal 判断两个 RemotePath 是否均有效且规范表示完全相等;无效零值永不相等。
func (RemotePath) HasPrefix ¶
func (p RemotePath) HasPrefix(prefix RemotePath) bool
HasPrefix 按 POSIX 段边界判断当前路径是否等于 prefix 或位于 prefix 子树下;无效路径恒返回 false。
func (RemotePath) IsAbs ¶
func (p RemotePath) IsAbs() bool
IsAbs 报告 RemotePath 是否为以 '/' 开头的绝对 POSIX 路径。
func (RemotePath) IsRoot ¶
func (p RemotePath) IsRoot() bool
IsRoot 报告 RemotePath 是否为 POSIX 根目录 "/"。
func (RemotePath) IsZero ¶
func (p RemotePath) IsZero() bool
IsZero 报告 RemotePath 是否为无效零值(配合 encoding/json/v2 omitzero)。
func (RemotePath) Join ¶
func (p RemotePath) Join(names ...RemoteName) (RemotePath, error)
Join 在当前有效路径下依次拼接一个或多个已校验的 RemoteName,返回规范化后的新 RemotePath。
func (RemotePath) MarshalText ¶
func (p RemotePath) MarshalText() ([]byte, error)
MarshalText 实现 encoding.TextMarshaler(值接收者)。
func (RemotePath) Rel ¶
func (p RemotePath) Rel(target RemotePath) (RemotePath, error)
Rel 计算从当前基准路径 p 到 target 的 POSIX 词法相对路径,满足 p.Resolve(rel) == target。
仅当 p 与 target 同为绝对路径或同为相对路径时可计算;若其中任一无效、绝对性不一致,或相对基准路径 在消除公共前缀后仍以 ".." 开头导致无法脱离当前工作目录确定上行段时,严格失败关闭返回错误。
func (RemotePath) Resolve ¶
func (p RemotePath) Resolve(target RemotePath) (RemotePath, error)
Resolve 以当前路径为基准解析目标路径:若 target 为绝对路径则直接返回 target,否则按 POSIX 词法拼接并清洗。
func (*RemotePath) UnmarshalText ¶
func (p *RemotePath) UnmarshalText(text []byte) error
UnmarshalText 实现 encoding.TextUnmarshaler;失败时重置为零值并失败关闭。
type SymlinkBinding ¶
type SymlinkBinding struct {
// contains filtered or unexported fields
}
SymlinkBinding 表示待下载树中已登记的一条沙箱内符号链接(位置 + 原始目标文本)。
func NewSymlinkBinding ¶
func NewSymlinkBinding(link LocalRootedDest, target string) (SymlinkBinding, error)
NewSymlinkBinding 构造经基础语法校验的沙箱符号链接绑定条目。
func (SymlinkBinding) IsValid ¶
func (b SymlinkBinding) IsValid() bool
IsValid 报告 SymlinkBinding 是否有效。
func (SymlinkBinding) Link ¶
func (b SymlinkBinding) Link() LocalRootedDest
Link 返回符号链接在沙箱根目录内的相对路径。
type SymlinkPolicy ¶
type SymlinkPolicy uint8
SymlinkPolicy 定义 XOps v2 在本地文件读取(OperatorReadFS)与下载落盘(TransferService)中的软链接处置策略。
零值 SymlinkReject 严格为最严失败关闭策略(等同 O_NOFOLLOW,拒绝一切符号链接):
- SymlinkReject:无条件拒绝所有符号链接;
- SymlinkContainedRelative:RFC 软链下载策略 (b),仅允许解析后每一步深度均严格留在沙箱根目录内 (depth >= 0)、全分量满足 ValidateLocalComponent 且无跨 OS Unicode/大小写折叠碰撞的相对 POSIX 软链;
- SymlinkFollowOperatorSource:仅用于 OperatorReadFS 读取人工显式指定的只读源文件(如软链私钥), 绝不允许用于 ScopedRootFS 写路径或下载落盘。
const ( // SymlinkReject 是失败关闭零值策略:无条件拒绝任何符号链接(等同 O_NOFOLLOW)。 SymlinkReject SymlinkPolicy = iota // SymlinkContainedRelative 是 RFC 策略 (b):仅保留解析后严格留在根目录内的相对 POSIX 符号链接。 SymlinkContainedRelative // SymlinkFollowOperatorSource 仅允许 OperatorReadFS 跟随人工显式指定的只读源文件软链接。 SymlinkFollowOperatorSource )
func ParseSymlinkPolicy ¶
func ParseSymlinkPolicy(raw string) (SymlinkPolicy, error)
ParseSymlinkPolicy 解析规范化软链接策略文本;空串或未知值一律失败关闭返回 SymlinkReject 与错误。
func (SymlinkPolicy) AllowsContainedRelative ¶
func (p SymlinkPolicy) AllowsContainedRelative() bool
AllowsContainedRelative 报告策略是否允许按 RFC 策略 (b) 保留/跟随根目录内受限相对软链接。
func (SymlinkPolicy) AllowsOperatorFollow ¶
func (p SymlinkPolicy) AllowsOperatorFollow() bool
AllowsOperatorFollow 报告策略是否允许 OperatorReadFS 跟随人工显式指定的只读源文件软链接。
func (SymlinkPolicy) AppendText ¶
func (p SymlinkPolicy) AppendText(b []byte) ([]byte, error)
AppendText 实现 encoding.TextAppender(值接收者);对非法枚举值失败关闭返回 ErrInvalidSymlinkPolicy。
func (SymlinkPolicy) IsValid ¶
func (p SymlinkPolicy) IsValid() bool
IsValid 报告 SymlinkPolicy 是否为已登记的合法策略枚举值。
func (SymlinkPolicy) MarshalText ¶
func (p SymlinkPolicy) MarshalText() ([]byte, error)
MarshalText 实现 encoding.TextMarshaler(值接收者)。
func (SymlinkPolicy) String ¶
func (p SymlinkPolicy) String() string
String 返回策略的规范文本表示;未识别枚举值返回 "invalid"。
func (*SymlinkPolicy) UnmarshalText ¶
func (p *SymlinkPolicy) UnmarshalText(text []byte) error
UnmarshalText 实现 encoding.TextUnmarshaler;失败时重置为最严零值 SymlinkReject 并返回错误。