fix(server): guard the dashboard raw config save in project mode

PUT /api/config wrote the raw editor content with os.WriteFile, which
follows a symlinked config.yaml. In project mode a link to a config
outside the project was overwritten, bypassing the boundary the other
config writers now enforce. Route this write through ConfigWritePath
and answer 400 with the link named.
This commit is contained in:
Willie
2026-10-07 10:36:22 +08:00
parent 04b6183ac9
commit 080bb3e5e4
3 changed files with 30 additions and 2 deletions
+8 -1
View File
@@ -12,6 +12,7 @@ import (
"skillshare/internal/config"
"skillshare/internal/hooks"
"skillshare/internal/plugin"
"skillshare/internal/utils"
)
func (s *Server) handleGetConfig(w http.ResponseWriter, r *http.Request) {
@@ -172,7 +173,13 @@ func (s *Server) handlePutConfig(w http.ResponseWriter, r *http.Request) {
warnings = w2
}
if err := os.WriteFile(s.configPath(), []byte(body.Raw), 0644); err != nil {
// Write a symlinked config's target; a project config must stay inside the project.
path, err := utils.ConfigWritePath(s.configPath(), s.IsProjectMode())
if err != nil {
writeError(w, http.StatusBadRequest, err.Error())
return
}
if err := os.WriteFile(path, []byte(body.Raw), 0644); err != nil {
writeError(w, http.StatusInternalServerError, "failed to write config: "+err.Error())
return
}
+21
View File
@@ -246,3 +246,24 @@ func TestHandleConfigSaveAcceptsAccountHooks(t *testing.T) {
})
}
}
func TestHandlePutConfig_ProjectLinkOutOfTheProject_400(t *testing.T) {
s, projectRoot := newTestProjectServerWithExtras(t, nil)
outside := filepath.Join(t.TempDir(), "elsewhere.yaml")
os.WriteFile(outside, []byte("targets: []\n"), 0644)
link := config.ProjectConfigPath(projectRoot)
os.Remove(link)
if err := os.Symlink(outside, link); err != nil {
t.Fatal(err)
}
body := `{"raw":"targets: [claude]\n"}`
rr := httptest.NewRecorder()
s.handler.ServeHTTP(rr, httptest.NewRequest(http.MethodPut, "/api/config", strings.NewReader(body)))
if rr.Code != http.StatusBadRequest || !strings.Contains(rr.Body.String(), "outside the project") {
t.Errorf("expected 400 naming the link, got %d: %s", rr.Code, rr.Body.String())
}
if data, _ := os.ReadFile(outside); string(data) != "targets: []\n" {
t.Errorf("file outside the project was written:\n%s", data)
}
}
+1 -1
View File
@@ -297,7 +297,7 @@ internal/server/handler_agentignore.go (*Server).handlePutAgentignore os.Remove
internal/server/handler_agentignore.go (*Server).handlePutAgentignore os.WriteFile ignorePath agents
internal/server/handler_audit.go (*Server).handlePutAuditRules os.MkdirAll filepath.Dir(path) config
internal/server/handler_audit.go (*Server).handlePutAuditRules os.WriteFile path config
internal/server/handler_config.go (*Server).handlePutConfig os.WriteFile s.configPath() config
internal/server/handler_config.go (*Server).handlePutConfig os.WriteFile path config
internal/server/handler_create_skill.go (*Server).handleCreateSkill os.MkdirAll dirPath source-unmigrated
internal/server/handler_create_skill.go (*Server).handleCreateSkill os.MkdirAll skillDir source-unmigrated
internal/server/handler_create_skill.go (*Server).handleCreateSkill os.RemoveAll skillDir source-unmigrated
Can't render this file because it contains an unexpected character in line 23 and column 89.