Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:lock: Reject unrecognized Windows namespace aliases

Daniel598f33ba1eb7
diff --git a/kernel/util/path.go b/kernel/util/path.go
index 7be09a8647..763d4248ea 100644
--- a/kernel/util/path.go
+++ b/kernel/util/path.go
@@ -451,10 +451,10 @@ func IsSensitivePath(p string) bool {
if p == "" {
return false
}
- if IsPluginDevelopmentRawPathForbidden(p, false) {
+ if isSensitivePath(p, false) {
return true
}
- if isSensitivePath(p, false) {
+ if IsPluginDevelopmentRawPathForbidden(p, false) {
return true
}
// 仅对工作空间外的路径解析符号链接,防止用符号链接绕过黑名单指向敏感目标。
@@ -476,15 +476,15 @@ func IsSensitiveHTMLAssetPath(p string) bool {
if p == "" {
return false
}
- if IsPluginDevelopmentRawPathForbidden(p, false) {
+ if isSensitivePath(p, true) || IsPluginDevelopmentRawPathForbidden(p, false) {
return true
}
- return isSensitivePath(p, true) || isSensitivePath(ResolveLongestExistingParent(p), true)
+ return isSensitivePath(ResolveLongestExistingParent(p), true)
}
// deviceNamespacePath 标记无法归一化的设备命名空间路径。
//
-// 这类路径只能由内核对象管理器解析,无法转换为常规 Win32 路径参与黑名单匹配,因此一律按敏感路径处理(fail-closed)。
+// 未能可靠归一化的命名空间路径一律按敏感路径处理,避免设备别名绕过黑名单匹配。
// 该值不是合法 Windows 路径的形态,故按字面量比较即可,无需解析。
const deviceNamespacePath = "<device-namespace>"
@@ -494,34 +494,44 @@ const deviceNamespacePath = "<device-namespace>"
// 既无法命中按前缀比较的黑名单,也会被文件系统归属判断当作工作空间外的路径,从而绕过敏感路径保护。
// 本机管理共享(`\\localhost\<盘符>$\...` 或 `\\<本机主机名>\<盘符>$\...`)指向本地卷,同样归一化为盘符路径。
//
-// 设备命名空间(`\\?\GLOBALROOT\Device\HarddiskVolumeN\...` 等)只能由内核对象管理器解析,无法用盘符表达,
-// 故不尝试映射为卷路径,而是返回 deviceNamespacePath 由调用方按敏感路径拒绝。匹配在剥离 `\\?\`、`\\.\` 之后进行,
-// 因此正斜杠写法(`//?/GLOBALROOT/...`)经 filepath.Clean 归一化后同样被识别。
+// 命名空间前缀后只接受绝对盘符路径和完整 UNC 路径,其余形式返回 deviceNamespacePath,由调用方按敏感路径拒绝。
+// 先统一斜杠并识别命名空间,再清理路径,避免对象管理器别名被当作普通相对路径参与黑名单匹配。
func resolveNamespaceAlias(p string) string {
if runtime.GOOS != "windows" || p == "" {
return p
}
- // 仅剥离 `\\?\` 与 `\\.\` 前缀,保留其后的 `UNC\` 标记,以便下面还原 UNC 根路径
- for _, prefix := range []string{`\\?\`, `\\.\`} {
- if strings.HasPrefix(p, prefix) {
- p = p[len(prefix):]
- break
+ p = strings.ReplaceAll(p, "/", `\`)
+ for _, prefix := range []string{`\\?`, `\\.`, `\??`} {
+ if p != prefix && !strings.HasPrefix(p, prefix+`\`) {
+ continue
}
- }
- // 剥离前缀后设备命名空间只剩 `GLOBALROOT\...`、`Device\...`、`Volume{...}` 形态
- if isDeviceNamespacePath(p) {
- return deviceNamespacePath
- }
- if strings.HasPrefix(p, `UNC\`) {
- p = `\\` + p[len(`UNC\`):]
+ if len(p) <= len(prefix)+1 {
+ return deviceNamespacePath
+ }
+ rest := p[len(prefix)+1:]
+ switch {
+ case len(rest) >= 3 && isASCIILetter(rest[0]) && rest[1] == ':' && rest[2] == '\\':
+ p = rest
+ case len(rest) >= 4 && strings.EqualFold(rest[:4], `UNC\`):
+ // UNC 必须包含有效的主机名和共享名,不能归一化为另一个设备命名空间或相对路径。
+ parts := strings.SplitN(rest[4:], `\`, 3)
+ if len(parts) < 2 {
+ return deviceNamespacePath
+ }
+ for _, part := range parts[:2] {
+ if part == "" || part == "." || part == ".." || strings.ContainsAny(part, `<>:"|?*`) {
+ return deviceNamespacePath
+ }
+ }
+ p = `\\` + rest[4:]
+ default:
+ return deviceNamespacePath
+ }
+ break
}
cleaned := filepath.Clean(p)
- // 正斜杠写法(`//?/GLOBALROOT/...`)未命中上面的字面量前缀,但经 filepath.Clean 会归一化为 `\\?\GLOBALROOT\...`,故再判定一次
- if isDeviceNamespacePath(cleaned) {
- return deviceNamespacePath
- }
if strings.HasPrefix(cleaned, `\\`) {
// 仅当主机名是本机且共享名就是盘符时才等价于本地卷,其余 UNC 路径一律按常规路径匹配黑名单
if drive, prefixLen := localAdminShareDrive(cleaned); drive != "" {
@@ -568,36 +578,6 @@ func isASCIILetter(b byte) bool {
return 'a' <= b && b <= 'z' || 'A' <= b && b <= 'Z'
}
-// hasAnyPrefixFold 判断 p 是否以任一前缀开头,忽略大小写。
-//
-// Windows 的路径与设备命名空间组件都不区分大小写,故前缀比较必须忽略大小写,否则 `GLOBALROOT` 的小写写法仍会漏过。
-func hasAnyPrefixFold(p string, prefixes ...string) bool {
- for _, prefix := range prefixes {
- if len(p) >= len(prefix) && strings.EqualFold(p[:len(prefix)], prefix) {
- return true
- }
- }
- return false
-}
-
-// deviceNamespacePrefixes 是设备命名空间路径的起始形态:`GLOBALROOT\Device\...`、`\Device\...` 与卷 GUID 写法。
-var deviceNamespacePrefixes = []string{
- `GLOBALROOT\Device\`,
- `GLOBALROOT\Volume{`,
- `Device\`,
- `Volume{`,
-}
-
-// isDeviceNamespacePath 判断路径是否为设备命名空间形式(如 `GLOBALROOT\Device\HarddiskVolumeN\...`)。
-//
-// 这类路径无盘符等价形式,无法参与黑名单前缀匹配,故一律按敏感路径拒绝(fail-closed)。
-// 调用点传入的是剥离 `\\?\`、`\\.\` 之后的路径或 filepath.Clean 结果,故同时接受带前导反斜杠与命名空间前缀的写法。
-func isDeviceNamespacePath(p string) bool {
- return hasAnyPrefixFold(p, deviceNamespacePrefixes...) ||
- hasAnyPrefixFold(p, `\\?\GLOBALROOT\`, `\\.\GLOBALROOT\`, `\\?\Device\`, `\\.\Device\`,
- `\\?\Volume{`, `\\.\Volume{`)
-}
-
func isHTMLAssetUserPath(p string) bool {
for _, root := range []string{os.TempDir(), HomeDir, systemHomeDir} {
if root == "" {
diff --git a/kernel/util/path_device_namespace_windows_test.go b/kernel/util/path_device_namespace_windows_test.go
index cf309fc2be..fd6672e115 100644
--- a/kernel/util/path_device_namespace_windows_test.go
+++ b/kernel/util/path_device_namespace_windows_test.go
@@ -86,6 +86,53 @@ func TestDeviceNamespaceAliasRejected(t *testing.T) {
f.Close()
}
+// TestIsSensitivePathDosDeviceAliases 使用真实文件验证对象管理器别名不能绕过敏感路径检查。
+func TestIsSensitivePathDosDeviceAliases(t *testing.T) {
+ originalHome, originalWorkspace := HomeDir, WorkspaceDir
+ HomeDir, WorkspaceDir = t.TempDir(), t.TempDir()
+ t.Cleanup(func() { HomeDir, WorkspaceDir = originalHome, originalWorkspace })
+
+ drive := filepath.VolumeName(WorkspaceDir)
+ if len(drive) != 2 || drive[1] != ':' {
+ t.Skipf("workspace volume is not a drive letter: %s", drive)
+ }
+ confPath := filepath.Join(WorkspaceDir, "conf", "conf.json")
+ if err := os.MkdirAll(filepath.Dir(confPath), 0700); err != nil {
+ t.Fatal(err)
+ }
+ const fixture = "namespace alias fixture"
+ if err := os.WriteFile(confPath, []byte(fixture), 0600); err != nil {
+ t.Fatal(err)
+ }
+
+ for _, prefix := range []string{`\\?\`, `\\.\`} {
+ for _, alias := range []string{`GLOBALROOT\GLOBAL??\`, `GLOBALROOT\??\`, `GLOBALROOT\DosDevices\`, `Global\`} {
+ p := prefix + alias + confPath
+ t.Run(prefix+alias, func(t *testing.T) {
+ // 先核实别名确实读取同一文件,避免仅凭构造出的路径判断修复是否有效。
+ data, err := os.ReadFile(p)
+ if err != nil {
+ t.Fatalf("read fixture through alias: %v", err)
+ }
+ if string(data) != fixture {
+ t.Fatalf("alias read %q, want %q", data, fixture)
+ }
+ for _, variant := range []string{p, strings.ToLower(p), strings.ReplaceAll(p, `\`, "/")} {
+ // 同时覆盖新建目标,不能依赖文件存在或符号链接解析才能识别命名空间。
+ for _, target := range []string{variant, filepath.Join(filepath.Dir(variant), "new", "conf.json")} {
+ if !IsSensitivePath(target) {
+ t.Errorf("IsSensitivePath(%q) = false, want true", target)
+ }
+ if !IsSensitiveHTMLAssetPath(target) {
+ t.Errorf("IsSensitiveHTMLAssetPath(%q) = false, want true", target)
+ }
+ }
+ }
+ })
+ }
+ }
+}
+
// globalRootVolumePath 返回形如 `GLOBALROOT\Device\HarddiskVolume3\` 的卷设备路径前缀。
func globalRootVolumePath(t *testing.T, drive string) string {
t.Helper()
diff --git a/kernel/util/path_namespace_test.go b/kernel/util/path_namespace_test.go
index b233f9c137..9ec36e4ea6 100644
--- a/kernel/util/path_namespace_test.go
+++ b/kernel/util/path_namespace_test.go
@@ -85,7 +85,9 @@ func TestIsSensitivePathNamespaceAlias(t *testing.T) {
{"设备命名空间 conf 目录", `\\.\` + confDir},
{"扩展长度小写盘符的 conf 目录", `\\?\` + strings.ToLower(drive) + confDir[len(drive):]},
{"扩展长度正斜杠的 conf 目录", `\\?\` + strings.ReplaceAll(confDir, `\`, `/`)},
+ {"正斜杠命名空间的 conf 目录", `//?/` + strings.ReplaceAll(confDir, `\`, `/`)},
{"扩展长度 UNC 本机管理共享的 conf 目录", `\\?\UNC\localhost\` + strings.ToUpper(drive[:1]) + "$" + confDir[len(drive):]},
+ {"小写 UNC 本机管理共享的 conf 目录", `\\?\unc\localhost\` + strings.ToUpper(drive[:1]) + "$" + confDir[len(drive):]},
{"UNC 本机管理共享的 conf 目录", `\\localhost\` + strings.ToUpper(drive[:1]) + "$" + confDir[len(drive):]},
}
// GLOBALROOT 与卷 GUID 指向同一位置,但只能由内核对象管理器解析,无法归一化为盘符路径
@@ -147,15 +149,25 @@ func TestResolveNamespaceAlias(t *testing.T) {
}{
{"扩展长度前缀", `\\?\D:\ws\conf\conf.json`, `D:\ws\conf\conf.json`},
{"扩展长度前缀与正斜杠", `\\?\D:/ws/conf/conf.json`, `D:\ws\conf\conf.json`},
+ {"正斜杠命名空间与盘符", `//?/D:/ws/conf/conf.json`, `D:\ws\conf\conf.json`},
+ {"混合斜杠命名空间与盘符", `\/.\D:/ws/conf/conf.json`, `D:\ws\conf\conf.json`},
+ {"根本地设备前缀与盘符", `\??\D:\ws\conf\conf.json`, `D:\ws\conf\conf.json`},
{"设备命名空间", `\\.\D:\ws`, `D:\ws`},
{"本机管理共享", `\\localhost\D$\ws\conf`, `D:\ws\conf`},
{"本机管理共享仅含共享名", `\\localhost\D$`, `D:\`},
{"本机管理共享与小写盘符", `\\localhost\d$\ws`, `D:\ws`},
{"扩展长度 UNC 本机管理共享", `\\?\UNC\localhost\D$\ws\conf`, `D:\ws\conf`},
{"设备命名空间 UNC 本机管理共享", `\\.\UNC\localhost\D$\ws`, `D:\ws`},
+ {"小写 UNC 本机管理共享", `\\?\unc\localhost\D$\ws`, `D:\ws`},
+ {"正斜杠 UNC 本机管理共享", `//?/unc/localhost/D$/ws`, `D:\ws`},
+ {"扩展长度远程共享", `\\?\UNC\server\share\ws`, `\\server\share\ws`},
+ {"设备命名空间远程共享", `\\.\unc\server\share\ws`, `\\server\share\ws`},
{"远程共享保持 UNC 形式", `\\server\share\ws`, `\\server\share\ws`},
{"非盘符共享不映射", `\\localhost\data\ws`, `\\localhost\data\ws`},
{"普通路径不变", `D:\ws\conf`, `D:\ws\conf`},
+ {"设备同名相对目录", `Device\image.png`, `Device\image.png`},
+ {"卷同名相对目录", `Volume{notes}\image.png`, `Volume{notes}\image.png`},
+ {"命名空间同名相对目录", `GLOBALROOT\image.png`, `GLOBALROOT\image.png`},
{"GLOBALROOT 设备命名空间不可归一化", `\\?\GLOBALROOT\Device\HarddiskVolume3\ws\conf`, deviceNamespacePath},
{"设备命名空间 GLOBALROOT 不可归一化", `\\.\GLOBALROOT\Device\HarddiskVolume3\ws\conf`, deviceNamespacePath},
{"正斜杠 GLOBALROOT 不可归一化", `//?/GLOBALROOT/Device/HarddiskVolume3/ws/conf`, deviceNamespacePath},
@@ -163,7 +175,21 @@ func TestResolveNamespaceAlias(t *testing.T) {
{"卷影副本不可归一化", `\\?\GLOBALROOT\Device\HarddiskVolumeShadowCopy2\ws\conf`, deviceNamespacePath},
{"设备对象路径不可归一化", `\\.\Device\HarddiskVolume3\ws\conf`, deviceNamespacePath},
{"卷 GUID 不可归一化", `\\?\Volume{01234567-89ab-cdef-0123-456789abcdef}\ws\conf`, deviceNamespacePath},
- {"UNC 仍正常归一化", `\\?\UNC\localhost\D$\ws\conf`, `D:\ws\conf`},
+ {"全局 DOS 设备目录不可归一化", `\\?\GLOBALROOT\GLOBAL??\D:\ws\conf`, deviceNamespacePath},
+ {"当前 DOS 设备目录不可归一化", `\\?\GLOBALROOT\??\D:\ws\conf`, deviceNamespacePath},
+ {"DOS 设备链接不可归一化", `\\?\GLOBALROOT\DosDevices\D:\ws\conf`, deviceNamespacePath},
+ {"全局设备链接不可归一化", `\\.\Global\D:\ws\conf`, deviceNamespacePath},
+ {"根本地设备别名不可归一化", `\??\Global\D:\ws\conf`, deviceNamespacePath},
+ {"未知设备别名不可归一化", `\\?\OtherDevice\ws\conf`, deviceNamespacePath},
+ {"卷设备句柄不可归一化", `\\.\D:`, deviceNamespacePath},
+ {"盘符相对路径不可归一化", `\\?\D:conf\conf.json`, deviceNamespacePath},
+ {"裸命名空间不可归一化", `\\?`, deviceNamespacePath},
+ {"空命名空间不可归一化", `\\.\`, deviceNamespacePath},
+ {"缺少共享名不可归一化", `\\?\UNC\server`, deviceNamespacePath},
+ {"空共享名不可归一化", `\\?\UNC\server\\file`, deviceNamespacePath},
+ {"空主机名不可归一化", `\\?\UNC\\share\file`, deviceNamespacePath},
+ {"设备主机名不可归一化", `\\?\UNC\.\D:\ws`, deviceNamespacePath},
+ {"相对共享名不可归一化", `\\?\UNC\server\..\D$\ws`, deviceNamespacePath},
} {
t.Run(test.name, func(t *testing.T) {
if got := resolveNamespaceAlias(test.in); !strings.EqualFold(got, test.want) {