An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:bug: Prevent appearance resource symlink escapes https://github.com/siyuan-note/siyuan/issues/19456
Daniel28b23ac3d12e
diff --git a/kernel/server/appearance_path.go b/kernel/server/appearance_path.gonew file mode 100644index 0000000000..5ec2a7dd4b--- /dev/null+++ b/kernel/server/appearance_path.go@@ -0,0 +1,76 @@+// SiYuan - From thought to insight, with agents+// Copyright (c) 2020-present, b3log.org+//+// This program is free software: you can redistribute it and/or modify+// it under the terms of the GNU Affero General Public License as published by+// the Free Software Foundation, either version 3 of the License, or+// (at your option) any later version.+//+// This program is distributed in the hope that it will be useful,+// but WITHOUT ANY WARRANTY; without even the implied warranty of+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+// GNU Affero General Public License for more details.+//+// You should have received a copy of the GNU Affero General Public License+// along with this program. If not, see <https://www.gnu.org/licenses/>.++package server++import (+ "net/http"+ "os"+ "path/filepath"+ "strings"++ "github.com/88250/gulu"+)++// resolveAppearanceFile 在读取前限定真实路径,仅允许主题和图标包目录链接到外部目录。+func resolveAppearanceFile(root, requestPath string) (string, int) {+ relativePath, ok := cleanStaticRelativePath(requestPath)+ if !ok {+ return "", http.StatusForbidden+ }+ root, err := filepath.Abs(root)+ if err != nil {+ return "", http.StatusInternalServerError+ }+ root, err = filepath.EvalSymlinks(root)+ if err != nil {+ return "", http.StatusNotFound+ }+ allowedRoot, target := root, root+ segments := strings.Split(filepath.ToSlash(relativePath), "/")+ for i, segment := range segments {+ target = filepath.Join(target, segment)+ info, statErr := os.Lstat(target)+ if os.IsNotExist(statErr) {+ // 保留缺失主题脚本的空响应和缺失语言包的英文补全行为。+ return filepath.Join(append([]string{target}, segments[i+1:]...)...), 0+ }+ if statErr != nil {+ return "", http.StatusForbidden+ }+ if info.Mode()&os.ModeSymlink != 0 {+ target, err = filepath.EvalSymlinks(target)+ if err != nil {+ return "", http.StatusForbidden+ }+ }+ if i == 1 && (segments[0] == "themes" || segments[0] == "icons") {+ info, err = os.Stat(target)+ if err != nil || !info.IsDir() {+ return "", http.StatusNotFound+ }+ allowedRoot = target+ }+ if target != allowedRoot && !gulu.File.IsSubPath(allowedRoot, target) {+ return "", http.StatusForbidden+ }+ }+ info, err := os.Stat(target)+ if err != nil || !info.Mode().IsRegular() {+ return "", http.StatusNotFound+ }+ return target, 0+}diff --git a/kernel/server/appearance_path_test.go b/kernel/server/appearance_path_test.gonew file mode 100644index 0000000000..38e7240434--- /dev/null+++ b/kernel/server/appearance_path_test.go@@ -0,0 +1,105 @@+// SiYuan - From thought to insight, with agents+// Copyright (c) 2020-present, b3log.org+//+// This program is free software: you can redistribute it and/or modify+// it under the terms of the GNU Affero General Public License as published by+// the Free Software Foundation, either version 3 of the License, or+// (at your option) any later version.+//+// This program is distributed in the hope that it will be useful,+// but WITHOUT ANY WARRANTY; without even the implied warranty of+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+// GNU Affero General Public License for more details.+//+// You should have received a copy of the GNU Affero General Public License+// along with this program. If not, see <https://www.gnu.org/licenses/>.++package server++import (+ "net/http/httptest"+ "os"+ "path/filepath"+ "strings"+ "testing"++ "github.com/gin-gonic/gin"+ "github.com/siyuan-note/siyuan/kernel/model"+ "github.com/siyuan-note/siyuan/kernel/util"+)++func TestAppearanceFileBoundaries(t *testing.T) {+ gin.SetMode(gin.TestMode)+ originalPath, originalMode, originalConf := util.AppearancePath, util.Mode, model.Conf+ util.AppearancePath, util.Mode = filepath.Join(t.TempDir(), "conf", "appearance"), "prod"+ model.Conf = model.NewAppConf()+ model.Conf.AccessAuthCode = "test-password"+ t.Cleanup(func() {+ util.AppearancePath, util.Mode, model.Conf = originalPath, originalMode, originalConf+ })+ outside := t.TempDir()+ write := func(name, content string) {+ t.Helper()+ if err := os.MkdirAll(filepath.Dir(name), 0755); err != nil {+ t.Fatal(err)+ }+ if err := os.WriteFile(name, []byte(content), 0644); err != nil {+ t.Fatal(err)+ }+ }+ for _, name := range []string{"themes/local/theme.css", "icons/local/icon.js", "fonts/font.woff"} {+ write(filepath.Join(util.AppearancePath, name), "resource")+ }+ write(filepath.Join(util.AppearancePath, "langs/en.json"), `{"fallback":"English"}`)+ write(filepath.Join(util.AppearancePath, "langs/fr.json"), `{"label":"French"}`)+ write(filepath.Join(filepath.Dir(util.AppearancePath), "conf.json"), `{"secret":"credential"}`)+ write(filepath.Join(outside, "theme.css"), "resource")+ write(filepath.Join(outside, "icon.js"), "resource")+ engine := gin.New()+ serveAppearance(engine)+ check := func(path string, status int, body string) {+ t.Helper()+ recorder := httptest.NewRecorder()+ engine.ServeHTTP(recorder, httptest.NewRequest("GET", "/appearance/"+path, nil))+ if recorder.Code != status || (body != "" && !strings.Contains(recorder.Body.String(), body)) || strings.Contains(recorder.Body.String(), "credential") {+ t.Fatalf("%s: status=%d body=%q, want %d %q", path, recorder.Code, recorder.Body.String(), status, body)+ }+ }+ check("themes/local/theme.css", 200, "resource")+ check("icons/local/icon.js", 200, "resource")+ check("fonts/font.woff", 200, "resource")+ check("langs/fr.json", 200, `"fallback":"English"`)+ check("langs/missing.json", 200, "English")+ check("themes/local/theme.js", 200, "")+ check("themes/local/", 404, "")+ check("../conf.json", 403, "")+ link := func(target, name string) {+ t.Helper()+ if err := os.Symlink(target, filepath.Join(util.AppearancePath, name)); err != nil {+ t.Skipf("symlinks unavailable: %s", err)+ }+ }+ link(outside, "themes/linked")+ link(outside, "icons/linked")+ check("themes/linked/theme.css", 200, "resource")+ check("icons/linked/icon.js", 200, "resource")+ link("theme.css", "themes/local/alias.css")+ check("themes/local/alias.css", 200, "resource")+ link(filepath.Dir(util.AppearancePath), "themes/local/escape")+ link(filepath.Join(filepath.Dir(util.AppearancePath), "conf.json"), "langs/leak.json")+ link(filepath.Join(filepath.Dir(util.AppearancePath), "conf.json"), "themes/local/theme.js")+ check("themes/local/escape/conf.json", 403, "")+ check("themes/local/escape/theme.js", 403, "")+ check("themes/local/theme.js", 403, "")+ check("langs/leak.json", 403, "")+ if err := os.Symlink(filepath.Dir(util.AppearancePath), filepath.Join(outside, "escape")); err != nil {+ t.Fatal(err)+ }+ check("themes/linked/escape/conf.json", 403, "")+ if err := os.Remove(filepath.Join(util.AppearancePath, "langs/en.json")); err != nil {+ t.Fatal(err)+ }+ link(filepath.Join(filepath.Dir(util.AppearancePath), "conf.json"), "langs/en.json")+ check("langs/fr.json", 403, "")+ check("langs/en.json", 403, "")+}diff --git a/kernel/server/serve.go b/kernel/server/serve.goindex d8297d271a..17dae5b154 100644--- a/kernel/server/serve.go+++ b/kernel/server/serve.go@@ -740,9 +740,9 @@ func serveAppearance(ginServer *gin.Engine) {appearancePath = filepath.Join(util.WorkingDir, "appearance")}siyuan.GET("/appearance/*filepath", func(c *gin.Context) {- filePath := filepath.Join(appearancePath, strings.TrimPrefix(c.Request.URL.Path, "/appearance/"))- if !gulu.File.IsSubPath(appearancePath, filePath) {- c.Status(http.StatusUnauthorized)+ filePath, status := resolveAppearanceFile(appearancePath, strings.TrimPrefix(c.Request.URL.Path, "/appearance/"))+ if status != 0 {+ c.Status(status)return}@@ -762,7 +762,11 @@ func serveAppearance(ginServer *gin.Engine) {if "zh-CN" != lang && "en" != lang {// 多语言配置缺失项使用对应英文配置项补齐 https://github.com/siyuan-note/siyuan/issues/5322- enUSFilePath := filepath.Join(appearancePath, "langs", "en.json")+ enUSFilePath, status := resolveAppearanceFile(appearancePath, "langs/en.json")+ if status != 0 {+ c.Status(status)+ return+ }enUSData, err := os.ReadFile(enUSFilePath)if err != nil {logging.LogErrorf("read en_US.json [%s] failed: %s", enUSFilePath, err)