@@ -41,6 +41,16 @@ func wantsAllUsers(c echo.Context) bool {
4141 return c .QueryParam ("all_users" ) == "true" && isAdminUser (c )
4242}
4343
44+ // effectiveUserID returns the user ID to scope operations to.
45+ // SECURITY: Only admins may supply ?user_id=<id> to operate on another user's
46+ // resources. Non-admin callers always get their own ID regardless of query params.
47+ func effectiveUserID (c echo.Context ) string {
48+ if targetUID := c .QueryParam ("user_id" ); targetUID != "" && isAdminUser (c ) {
49+ return targetUID
50+ }
51+ return getUserID (c )
52+ }
53+
4454func ListAgentsEndpoint (app * application.Application ) echo.HandlerFunc {
4555 return func (c echo.Context ) error {
4656 svc := app .AgentPoolService ()
@@ -99,7 +109,7 @@ func CreateAgentEndpoint(app *application.Application) echo.HandlerFunc {
99109func GetAgentEndpoint (app * application.Application ) echo.HandlerFunc {
100110 return func (c echo.Context ) error {
101111 svc := app .AgentPoolService ()
102- userID := getUserID (c )
112+ userID := effectiveUserID (c )
103113 name := c .Param ("name" )
104114 ag := svc .GetAgentForUser (userID , name )
105115 if ag == nil {
@@ -114,7 +124,7 @@ func GetAgentEndpoint(app *application.Application) echo.HandlerFunc {
114124func UpdateAgentEndpoint (app * application.Application ) echo.HandlerFunc {
115125 return func (c echo.Context ) error {
116126 svc := app .AgentPoolService ()
117- userID := getUserID (c )
127+ userID := effectiveUserID (c )
118128 name := c .Param ("name" )
119129 var cfg state.AgentConfig
120130 if err := c .Bind (& cfg ); err != nil {
@@ -133,7 +143,7 @@ func UpdateAgentEndpoint(app *application.Application) echo.HandlerFunc {
133143func DeleteAgentEndpoint (app * application.Application ) echo.HandlerFunc {
134144 return func (c echo.Context ) error {
135145 svc := app .AgentPoolService ()
136- userID := getUserID (c )
146+ userID := effectiveUserID (c )
137147 name := c .Param ("name" )
138148 if err := svc .DeleteAgentForUser (userID , name ); err != nil {
139149 return c .JSON (http .StatusInternalServerError , map [string ]string {"error" : err .Error ()})
@@ -145,7 +155,7 @@ func DeleteAgentEndpoint(app *application.Application) echo.HandlerFunc {
145155func GetAgentConfigEndpoint (app * application.Application ) echo.HandlerFunc {
146156 return func (c echo.Context ) error {
147157 svc := app .AgentPoolService ()
148- userID := getUserID (c )
158+ userID := effectiveUserID (c )
149159 name := c .Param ("name" )
150160 cfg := svc .GetAgentConfigForUser (userID , name )
151161 if cfg == nil {
@@ -158,7 +168,7 @@ func GetAgentConfigEndpoint(app *application.Application) echo.HandlerFunc {
158168func PauseAgentEndpoint (app * application.Application ) echo.HandlerFunc {
159169 return func (c echo.Context ) error {
160170 svc := app .AgentPoolService ()
161- userID := getUserID (c )
171+ userID := effectiveUserID (c )
162172 if err := svc .PauseAgentForUser (userID , c .Param ("name" )); err != nil {
163173 return c .JSON (http .StatusNotFound , map [string ]string {"error" : err .Error ()})
164174 }
@@ -169,7 +179,7 @@ func PauseAgentEndpoint(app *application.Application) echo.HandlerFunc {
169179func ResumeAgentEndpoint (app * application.Application ) echo.HandlerFunc {
170180 return func (c echo.Context ) error {
171181 svc := app .AgentPoolService ()
172- userID := getUserID (c )
182+ userID := effectiveUserID (c )
173183 if err := svc .ResumeAgentForUser (userID , c .Param ("name" )); err != nil {
174184 return c .JSON (http .StatusNotFound , map [string ]string {"error" : err .Error ()})
175185 }
@@ -180,7 +190,7 @@ func ResumeAgentEndpoint(app *application.Application) echo.HandlerFunc {
180190func GetAgentStatusEndpoint (app * application.Application ) echo.HandlerFunc {
181191 return func (c echo.Context ) error {
182192 svc := app .AgentPoolService ()
183- userID := getUserID (c )
193+ userID := effectiveUserID (c )
184194 name := c .Param ("name" )
185195 history := svc .GetAgentStatusForUser (userID , name )
186196 if history == nil {
@@ -209,7 +219,7 @@ func GetAgentStatusEndpoint(app *application.Application) echo.HandlerFunc {
209219func GetAgentObservablesEndpoint (app * application.Application ) echo.HandlerFunc {
210220 return func (c echo.Context ) error {
211221 svc := app .AgentPoolService ()
212- userID := getUserID (c )
222+ userID := effectiveUserID (c )
213223 name := c .Param ("name" )
214224 history , err := svc .GetAgentObservablesForUser (userID , name )
215225 if err != nil {
@@ -225,7 +235,7 @@ func GetAgentObservablesEndpoint(app *application.Application) echo.HandlerFunc
225235func ClearAgentObservablesEndpoint (app * application.Application ) echo.HandlerFunc {
226236 return func (c echo.Context ) error {
227237 svc := app .AgentPoolService ()
228- userID := getUserID (c )
238+ userID := effectiveUserID (c )
229239 name := c .Param ("name" )
230240 if err := svc .ClearAgentObservablesForUser (userID , name ); err != nil {
231241 return c .JSON (http .StatusNotFound , map [string ]string {"error" : err .Error ()})
@@ -237,7 +247,7 @@ func ClearAgentObservablesEndpoint(app *application.Application) echo.HandlerFun
237247func ChatWithAgentEndpoint (app * application.Application ) echo.HandlerFunc {
238248 return func (c echo.Context ) error {
239249 svc := app .AgentPoolService ()
240- userID := getUserID (c )
250+ userID := effectiveUserID (c )
241251 name := c .Param ("name" )
242252 var payload struct {
243253 Message string `json:"message"`
@@ -266,7 +276,7 @@ func ChatWithAgentEndpoint(app *application.Application) echo.HandlerFunc {
266276func AgentSSEEndpoint (app * application.Application ) echo.HandlerFunc {
267277 return func (c echo.Context ) error {
268278 svc := app .AgentPoolService ()
269- userID := getUserID (c )
279+ userID := effectiveUserID (c )
270280 name := c .Param ("name" )
271281 manager := svc .GetSSEManagerForUser (userID , name )
272282 if manager == nil {
@@ -294,7 +304,7 @@ func GetAgentConfigMetaEndpoint(app *application.Application) echo.HandlerFunc {
294304func ExportAgentEndpoint (app * application.Application ) echo.HandlerFunc {
295305 return func (c echo.Context ) error {
296306 svc := app .AgentPoolService ()
297- userID := getUserID (c )
307+ userID := effectiveUserID (c )
298308 name := c .Param ("name" )
299309 data , err := svc .ExportAgentForUser (userID , name )
300310 if err != nil {
0 commit comments