Skip to content

Commit 06e19f8

Browse files
authored
Merge pull request #431 from dgageot/fix-load-agent
Fix loading an agent
2 parents d945ea5 + 346eaf2 commit 06e19f8

1 file changed

Lines changed: 15 additions & 16 deletions

File tree

pkg/server/server.go

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,7 @@ func (s *Server) getDesktopToken(c echo.Context) error {
153153

154154
func (s *Server) getAgentConfig(c echo.Context) error {
155155
agentID := c.Param("id")
156-
157-
path, err := s.secureAgentPath(agentID)
158-
if err != nil {
159-
slog.Error("Invalid agent ID", "agentID", agentID, "error", err)
160-
return c.JSON(http.StatusBadRequest, map[string]string{"error": "invalid agent ID"})
161-
}
156+
path := toYaml(agentID)
162157

163158
cfg, err := config.LoadConfigSecureDeprecated(path, s.agentsDir)
164159
if err != nil {
@@ -178,16 +173,7 @@ func (s *Server) editAgentConfig(c echo.Context) error {
178173
return c.JSON(http.StatusBadRequest, map[string]string{"error": "filename is required"})
179174
}
180175

181-
path, err := s.secureAgentPath(req.Filename)
182-
if err != nil {
183-
slog.Error("Invalid filename", "filename", req.Filename, "error", err)
184-
return c.JSON(http.StatusBadRequest, map[string]string{"error": "invalid filename"})
185-
}
186-
187-
// Check if the file exists
188-
if _, err := os.Stat(path); os.IsNotExist(err) {
189-
return c.JSON(http.StatusNotFound, map[string]string{"error": "agent not found"})
190-
}
176+
path := toYaml(req.Filename)
191177

192178
// Load the target file content
193179
currentConfig, err := config.LoadConfigSecureDeprecated(path, s.agentsDir)
@@ -203,6 +189,12 @@ func (s *Server) editAgentConfig(c echo.Context) error {
203189
}
204190
mergedConfig := *currentConfig
205191

192+
path, err = s.secureAgentPath(path)
193+
if err != nil {
194+
slog.Error("Invalid filename", "filename", req.Filename, "error", err)
195+
return c.JSON(http.StatusBadRequest, map[string]string{"error": "invalid filename"})
196+
}
197+
206198
// Read current file to preserve shebang and metadata structure
207199
currentContent, err := os.ReadFile(path)
208200
if err != nil {
@@ -1030,6 +1022,13 @@ func (s *Server) secureAgentPath(filename string) (string, error) {
10301022
return config.ValidatePathInDirectory(filename, s.agentsDir)
10311023
}
10321024

1025+
func toYaml(filename string) string {
1026+
if strings.HasSuffix(filename, ".yaml") || strings.HasSuffix(filename, ".yml") {
1027+
return filename
1028+
}
1029+
return filename + ".yaml"
1030+
}
1031+
10331032
func (s *Server) resumeStartOauth(c echo.Context) error {
10341033
sessionID := c.Param("id")
10351034
var req api.ResumeStartOauthRequest

0 commit comments

Comments
 (0)