Skip to content

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

11.8 KB
Raw Blame
// SiYuan - https://github.com/siyuan-note/siyuan
// Copyright (c) 2020-present, b3log.org
// SPDX-License-Identifier: AGPL-3.0-or-later

package util

import (
	"os"
	"path/filepath"
	"reflect"
	"runtime"
	"testing"
)

func TestFilterSelfChildDocsPreservesInputOrder(t *testing.T) {
	paths := []string{
		"/20260810000001-abcdefg/20260810000002-abcdefg.sy",
		"/20260810000003-abcdefg.sy",
		"/20260810000004-abcdefg/20260810000005-abcdefg.sy",
	}
	want := append([]string(nil), paths...)

	if got := FilterSelfChildDocs(paths); !reflect.DeepEqual(got, want) {
		t.Fatalf("unexpected filtered paths: got %v, want %v", got, want)
	}
	if !reflect.DeepEqual(paths, want) {
		t.Fatalf("input paths changed: got %v, want %v", paths, want)
	}
}

func TestIsPossiblyImageWithQueryAndFragment(t *testing.T) {
	for _, assetPath := range []string{
		"assets/photo.heic",
		"assets/photo.HEIF?box=20260816000000-abcdefg",
		"assets/photo.heic?box=20260816000000-abcdefg#preview",
		"assets/photo.tif",
		"assets/photo.TIFF?box=20260816000000-abcdefg#preview",
	} {
		if !IsPossiblyImage(assetPath) {
			t.Fatalf("asset was not recognized as an image: %s", assetPath)
		}
	}
}

func TestIsDisplayableAssetTIFF(t *testing.T) {
	for _, path := range []string{"assets/photo.tif", "assets/photo.TIFF", "20261021000000-tiffbox/assets/photo.tiff"} {
		if !IsDisplayableAsset(path) {
			t.Fatalf("TIFF snapshot asset was not displayable: %s", path)
		}
	}
	if IsDisplayableAsset("assets/photo.tiff.txt") {
		t.Fatal("a TIFF name without a TIFF extension was displayable")
	}
}

func TestFilterSelfChildDocsRemovesChildrenAndDuplicates(t *testing.T) {
	parentPath := "/20260810000001-abcdefg.sy"
	childPath := "/20260810000001-abcdefg/20260810000002-abcdefg.sy"
	otherPath := "/20260810000003-abcdefg.sy"
	paths := []string{childPath, otherPath, parentPath, otherPath}

	if got, want := FilterSelfChildDocs(paths), []string{otherPath, parentPath}; !reflect.DeepEqual(got, want) {
		t.Fatalf("unexpected filtered paths: got %v, want %v", got, want)
	}
}

// TestGetTreeID 校验使用不同路径分隔符时均能提取相同的文档 ID。
func TestGetTreeID(t *testing.T) {
	id := "20240101120000-1a2b3c4"
	cases := []struct {
		name string
		path string
	}{
		{"bare ID", id},
		{"bare file", id + ".sy"},
		{"slash", "/20240101120000-parent/" + id + ".sy"},
		{"backslash", "\\20240101120000-parent\\" + id + ".sy"},
		{"mixed separators", "20240101120000-a\\20240101120000-b/" + id + ".sy"},
	}
	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			if got := GetTreeID(c.path); got != id {
				t.Fatalf("GetTreeID(%q) = %q, want %q", c.path, got, id)
			}
		})
	}
}

// TestIsSensitivePathCredentialDotfiles 覆盖 GHSA 报告中遗漏的家目录凭据 dotfile,
// 确保它们在 globalCopyFiles 等接受工作空间外绝对路径的接口处被拒绝。
func TestIsSensitivePathCredentialDotfiles(t *testing.T) {
	tmpHome := t.TempDir()
	tmpWorkspace := t.TempDir()
	origHome, origWorkspace := HomeDir, WorkspaceDir
	HomeDir, WorkspaceDir = tmpHome, tmpWorkspace
	t.Cleanup(func() { HomeDir, WorkspaceDir = origHome, origWorkspace })

	// 报告点名的攻击面 + 常见云/包管理器凭据。
	cases := []struct {
		name string
		rel  string // 相对 HomeDir 的路径
	}{
		// GHSA-q768-4j87-gf3w 点名的 dotenv 配置文件,.env 前缀同时覆盖派生文件。
		{"env file", ".env"},
		{"env local file", ".env.local"},
		{"env production file", ".env.production"},
		{"git-credentials", ".git-credentials"},
		{"netrc", ".netrc"},
		{"pgpass", ".pgpass"},
		{"kube config", filepath.Join(".kube", "config")},
		{"docker config", filepath.Join(".docker", "config.json")},
		{"gnupg private keyring", filepath.Join(".gnupg", "private-keys-v1.d", "key.key")},
		{"aws credentials", filepath.Join(".aws", "credentials")},
		{"azure token", filepath.Join(".azure", "accessTokens.json")},
		{"npmrc", ".npmrc"},
		{"pypirc", ".pypirc"},
		// 原有黑名单项不应回归。
		{"ssh id_rsa", filepath.Join(".ssh", "id_rsa")},
		{"bashrc", ".bashrc"},
		{"config dir", filepath.Join(".config", "some-app")},
	}
	for _, c := range cases {
		abs := filepath.Join(tmpHome, c.rel)
		if got := IsSensitivePath(abs); !got {
			t.Errorf("IsSensitivePath(%q) = false, want true [%s]", abs, c.name)
		}
	}
}

// TestIsSensitivePathPlatformHomeDirs 覆盖 GHSA-q768-4j87-gf3w 点名的平台应用数据目录:
// macOS 用户资源库中的钥匙串、Windows 漫游与本地应用数据中的凭据。
func TestIsSensitivePathPlatformHomeDirs(t *testing.T) {
	type platformCase struct {
		name string
		rel  string
	}
	var home string
	var cases []platformCase
	if runtime.GOOS == "darwin" {
		home = t.TempDir()
		cases = append(cases, platformCase{"macOS keychain", filepath.Join("Library", "Keychains", "login.keychain-db")})
	}
	if runtime.GOOS == "windows" {
		// AppData\Local 条目放行系统临时目录,而 t.TempDir() 正位于该目录中,因此使用真实用户主目录
		profile, err := os.UserHomeDir()
		if err != nil || profile == "" {
			t.Skipf("user home dir unavailable: %v", err)
		}
		home = profile
		cases = append(cases,
			platformCase{"Windows roaming credentials", filepath.Join("AppData", "Roaming", "gcloud", "credentials.db")},
			platformCase{"Windows local profile data", filepath.Join("AppData", "Local", "Google", "Chrome", "User Data", "Default", "Cookies")})
	}
	if len(cases) == 0 {
		t.Skipf("no platform-specific home entries on %s", runtime.GOOS)
	}

	origHome, origWorkspace := HomeDir, WorkspaceDir
	HomeDir, WorkspaceDir = home, t.TempDir()
	t.Cleanup(func() { HomeDir, WorkspaceDir = origHome, origWorkspace })

	for _, c := range cases {
		abs := filepath.Join(home, c.rel)
		if got := IsSensitivePath(abs); !got {
			t.Errorf("IsSensitivePath(%q) = false, want true [%s]", abs, c.name)
		}
	}
}

// TestIsSensitivePathSystemTempExemption 验证 Windows 系统临时目录中的剪贴板及 HTML 粘贴资源
// 不被 AppData\Local 条目误判。
func TestIsSensitivePathSystemTempExemption(t *testing.T) {
	if runtime.GOOS != "windows" {
		t.Skip("the system temp dir lives under AppData only on Windows")
	}
	profile, err := os.UserHomeDir()
	if err != nil || profile == "" {
		t.Skipf("user home dir unavailable: %v", err)
	}
	origHome := HomeDir
	HomeDir = profile
	t.Cleanup(func() { HomeDir = origHome })

	p := filepath.Join(os.TempDir(), "siyuan-clipboard-image.png")
	if IsSensitivePath(p) {
		t.Errorf("IsSensitivePath(%q) = true, want false", p)
	}
	if IsSensitiveHTMLAssetPath(p) {
		t.Errorf("IsSensitiveHTMLAssetPath(%q) = true, want false", p)
	}
}

// TestIsSensitivePathSymlinkBypass 验证通过符号链接绕过黑名单的尝试会被拦截:
// 在非敏感目录下放置一个指向 ~/.ssh/id_rsa 的符号链接,解析后应判定为敏感。
func TestIsSensitivePathSymlinkBypass(t *testing.T) {
	tmpHome := t.TempDir()
	tmpWorkspace := t.TempDir()
	origHome, origWorkspace := HomeDir, WorkspaceDir
	HomeDir, WorkspaceDir = tmpHome, tmpWorkspace
	t.Cleanup(func() { HomeDir, WorkspaceDir = origHome, origWorkspace })

	target := filepath.Join(tmpHome, ".ssh", "id_rsa")
	if err := os.MkdirAll(filepath.Dir(target), 0700); err != nil {
		t.Fatalf("mkdir: %v", err)
	}
	if err := os.WriteFile(target, []byte("PRIVATE"), 0600); err != nil {
		t.Fatalf("write target: %v", err)
	}

	// 在一个看起来无害的位置(家目录外的临时目录)放符号链接。
	innocentDir := t.TempDir()
	link := filepath.Join(innocentDir, "link")
	if err := os.Symlink(target, link); err != nil {
		t.Skipf("symlink not supported on this platform: %v", err)
	}

	// link 自身的路径不命中黑名单,但解析后指向 .ssh,应被拒绝。
	if got := IsSensitivePath(link); !got {
		t.Errorf("IsSensitivePath(symlink -> .ssh) = false, want true")
	}
}

// TestIsSensitivePathWorkspaceFilesNotBlocked 确保工作空间内的合法文件不会被误判。
func TestIsSensitivePathWorkspaceFilesNotBlocked(t *testing.T) {
	tmpWorkspace := t.TempDir()
	origWorkspace := WorkspaceDir
	WorkspaceDir = tmpWorkspace
	t.Cleanup(func() { WorkspaceDir = origWorkspace })

	cases := []string{
		filepath.Join(tmpWorkspace, "data", "assets", "image.png"),
		filepath.Join(tmpWorkspace, "data", "note.md"),
		filepath.Join(tmpWorkspace, "temp", "export", "output.pdf"),
	}
	for _, p := range cases {
		if got := IsSensitivePath(p); got {
			t.Errorf("IsSensitivePath(%q) = true, want false (workspace file)", p)
		}
	}
}

func TestIsSensitivePathWorkspaceAssetSymlink(t *testing.T) {
	originalHome, originalWorkspace := HomeDir, WorkspaceDir
	HomeDir, WorkspaceDir = t.TempDir(), t.TempDir()
	t.Cleanup(func() { HomeDir, WorkspaceDir = originalHome, originalWorkspace })
	target := filepath.Join(HomeDir, ".config", "example")
	if err := os.MkdirAll(target, 0700); err != nil {
		t.Fatal(err)
	}
	if err := os.WriteFile(filepath.Join(target, "existing.txt"), []byte("fixture"), 0600); err != nil {
		t.Fatal(err)
	}
	assets := filepath.Join(WorkspaceDir, "data", "assets")
	if err := os.MkdirAll(assets, 0755); err != nil {
		t.Fatal(err)
	}
	link := filepath.Join(assets, "linked")
	if err := os.Symlink(target, link); err != nil {
		t.Skipf("create asset symlink: %v", err)
	}
	for _, relative := range []string{"existing.txt", filepath.Join("missing", "example.txt")} {
		if path := filepath.Join(link, relative); IsSensitivePath(path) {
			t.Errorf("workspace asset symlink should remain allowed: %s", path)
		}
	}
}

// TestIsSensitivePathSymlinkWorkspace 验证工作空间父目录为符号链接时的真实路径判定。
func TestIsSensitivePathSymlinkWorkspace(t *testing.T) {
	realHome, err := filepath.EvalSymlinks(t.TempDir())
	if err != nil {
		t.Fatal(err)
	}
	linkedHome := filepath.Join(t.TempDir(), "home")
	if err = os.Symlink(realHome, linkedHome); err != nil {
		t.Skipf("create directory symlink failed: %s", err)
	}
	relWorkspace := filepath.Join(".var", "app", "org.b3log.siyuan", "SiYuan")
	realWorkspace := filepath.Join(realHome, relWorkspace)
	if err = os.MkdirAll(realWorkspace, 0755); err != nil {
		t.Fatal(err)
	}
	originalHome, originalWorkspace := HomeDir, WorkspaceDir
	HomeDir, WorkspaceDir = realHome, filepath.Join(linkedHome, relWorkspace)
	t.Cleanup(func() { HomeDir, WorkspaceDir = originalHome, originalWorkspace })

	for _, test := range []struct {
		rel       string
		sensitive bool
	}{
		{"data/emojis/fontawesome-free-solid/robot.svg", false},
		{"data/widgets/listChildDocs/index.html", false},
		{"data/assets/image.png", false},
		{"temp/export/document.html", false},
		{"conf/conf.json", true},
		{"temp/private.txt", true},
		{"data/widgets/example/credentials.json", true},
		{"data/widgets/example/id_rsa", true},
	} {
		t.Run(test.rel, func(t *testing.T) {
			for _, root := range []string{WorkspaceDir, realWorkspace} {
				p := filepath.Join(root, filepath.FromSlash(test.rel))
				if got := IsSensitivePath(p); got != test.sensitive {
					t.Errorf("IsSensitivePath(%q) = %v, want %v", p, got, test.sensitive)
				}
			}
		})
	}

	// 工作空间外的凭据不能因工作空间解析成功而获得放行。
	if p := filepath.Join(realHome, ".ssh", "id_rsa"); !IsSensitivePath(p) {
		t.Errorf("external credential should be sensitive: %s", p)
	}
}

// TestIsSensitivePathWorkspacePrefixBoundary 显式覆盖敏感父目录下的工作空间路径边界。
func TestIsSensitivePathWorkspacePrefixBoundary(t *testing.T) {
	parents := []string{"/tmp", "/var/home"}
	if filepath.Separator == '\\' {
		parents = []string{`C:\Windows\System32`}
	}
	originalWorkspace := WorkspaceDir
	t.Cleanup(func() { WorkspaceDir = originalWorkspace })
	for _, parent := range parents {
		WorkspaceDir = filepath.Join(parent, "siyuan-test-workspace")
		if isSensitivePath(filepath.Join(WorkspaceDir, "data", "public.txt"), false) {
			t.Fatalf("workspace data should be allowed: %s", WorkspaceDir)
		}
		if !isSensitivePath(filepath.Join(WorkspaceDir+"-outside", "public.txt"), false) {
			t.Fatalf("workspace prefix sibling should remain sensitive: %s", WorkspaceDir)
		}
	}
}