An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:lock: Restrict /api/lute/spinBlockDOM to admin role with input size limit https://github.com/siyuan-note/siyuan/security/advisories/GHSA-3j8q-5c8c-grwm
Daniel2c3b7af3f5fe
diff --git a/kernel/api/lute.go b/kernel/api/lute.goindex 6001c0bb49..702bcebfa3 100644--- a/kernel/api/lute.go+++ b/kernel/api/lute.go@@ -34,6 +34,9 @@ import ("github.com/siyuan-note/siyuan/kernel/util")+// maxSpinBlockDOMBytes 限制 spinBlockDOM 输入 DOM 的最大字节数。+const maxSpinBlockDOMBytes = 1024 * 1024+func copyStdMarkdown(c *gin.Context) {ret := gulu.Ret.NewResult()defer c.JSON(http.StatusOK, ret)@@ -244,6 +247,12 @@ func spinBlockDOM(c *gin.Context) {if !util.ParseJsonArgs(arg, ret, util.BindJsonArg("dom", &dom, true, false)) {return}+ if len(dom) > maxSpinBlockDOMBytes {+ // 限制输入大小,避免解析超大 DOM 导致资源消耗+ ret.Code = http.StatusRequestEntityTooLarge+ ret.Msg = "dom input exceeds the maximum permitted size"+ return+ }luteEngine := model.NewLute()dom = luteEngine.SpinBlockDOM(dom)diff --git a/kernel/api/lute_authorization_test.go b/kernel/api/lute_authorization_test.gonew file mode 100644index 0000000000..ec616b4c54--- /dev/null+++ b/kernel/api/lute_authorization_test.go@@ -0,0 +1,122 @@+// SiYuan - Refactor your thinking+// 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 api++import (+ "bytes"+ "encoding/json"+ "net/http"+ "net/http/httptest"+ "strings"+ "testing"++ "github.com/gin-gonic/gin"+ "github.com/siyuan-note/siyuan/kernel/conf"+ "github.com/siyuan-note/siyuan/kernel/model"+)++func TestSpinBlockDOMAuthorization(t *testing.T) {+ gin.SetMode(gin.TestMode)++ previousConf := model.Conf+ model.Conf = model.NewAppConf()+ model.Conf.Editor = &conf.Editor{}+ model.Conf.Export = &conf.Export{}+ t.Cleanup(func() {+ model.Conf = previousConf+ })++ body, err := json.Marshal(map[string]string{"dom": `<div data-node-id="20240101000000-abcdefg"><p>test</p></div>`})+ if err != nil {+ t.Fatal(err)+ }++ roles := []struct {+ name string+ role model.Role+ statusCode int+ }{+ {name: "administrator", role: model.RoleAdministrator, statusCode: http.StatusOK},+ {name: "editor", role: model.RoleEditor, statusCode: http.StatusForbidden},+ {name: "reader", role: model.RoleReader, statusCode: http.StatusForbidden},+ }+ for _, role := range roles {+ t.Run(role.name, func(t *testing.T) {+ engine := gin.New()+ engine.Use(func(c *gin.Context) {+ c.Set(model.RoleContextKey, role.role)+ c.Next()+ })+ ServeAPI(engine)++ recorder := httptest.NewRecorder()+ request := httptest.NewRequest(http.MethodPost, "/api/lute/spinBlockDOM", bytes.NewReader(body))+ request.Header.Set("Content-Type", "application/json")+ engine.ServeHTTP(recorder, request)++ if recorder.Code != role.statusCode {+ t.Fatalf("unexpected status code: got %d, want %d", recorder.Code, role.statusCode)+ }+ if role.role != model.RoleAdministrator {+ return+ }++ response := &struct {+ Code int `json:"code"`+ }{}+ if err := json.Unmarshal(recorder.Body.Bytes(), response); err != nil {+ t.Fatal(err)+ }+ if response.Code != 0 {+ t.Fatalf("unexpected response code: got %d, want 0", response.Code)+ }+ })+ }+}++func TestSpinBlockDOMInputSizeLimit(t *testing.T) {+ gin.SetMode(gin.TestMode)++ previousConf := model.Conf+ model.Conf = model.NewAppConf()+ model.Conf.Editor = &conf.Editor{}+ model.Conf.Export = &conf.Export{}+ t.Cleanup(func() {+ model.Conf = previousConf+ })++ body, err := json.Marshal(map[string]string{"dom": strings.Repeat("<div>", maxSpinBlockDOMBytes/6+1) + strings.Repeat("</div>", maxSpinBlockDOMBytes/6+1)})+ if err != nil {+ t.Fatal(err)+ }++ engine := gin.New()+ engine.Use(func(c *gin.Context) {+ c.Set(model.RoleContextKey, model.RoleAdministrator)+ c.Next()+ })+ ServeAPI(engine)++ recorder := httptest.NewRecorder()+ request := httptest.NewRequest(http.MethodPost, "/api/lute/spinBlockDOM", bytes.NewReader(body))+ request.Header.Set("Content-Type", "application/json")+ engine.ServeHTTP(recorder, request)++ if recorder.Code != http.StatusRequestEntityTooLarge {+ t.Fatalf("unexpected status code: got %d, want %d", recorder.Code, http.StatusRequestEntityTooLarge)+ }+}diff --git a/kernel/api/router.go b/kernel/api/router.goindex f5868eb39f..35f5130168 100644--- a/kernel/api/router.go+++ b/kernel/api/router.go@@ -206,7 +206,7 @@ func ServeAPI(ginServer *gin.Engine) {ginServer.Handle("POST", "/api/tag/renameTag", model.CheckAuth, model.CheckAdminRole, model.CheckReadonly, renameTag)ginServer.Handle("POST", "/api/tag/removeTag", model.CheckAuth, model.CheckAdminRole, model.CheckReadonly, removeTag)- ginServer.Handle("POST", "/api/lute/spinBlockDOM", model.CheckAuth, spinBlockDOM) // 未测试+ ginServer.Handle("POST", "/api/lute/spinBlockDOM", model.CheckAuth, model.CheckAdminRole, spinBlockDOM)ginServer.Handle("POST", "/api/lute/html2BlockDOM", model.CheckAuth, model.CheckAdminRole, model.CheckReadonly, html2BlockDOM)ginServer.Handle("POST", "/api/lute/copyStdMarkdown", model.CheckAuth, copyStdMarkdown)ginServer.Handle("POST", "/api/lute/md2html", model.CheckAuth, md2HTML)