feat(security): add configurable CORS and origin checks

Add settings for allowed CORS origins and WebSocket origin validation so
deployments can restrict cross-origin access instead of using a global
wildcard policy.

Move CORS handling into the security middleware and update report cache
usage to use the dedicated report package.
This commit is contained in:
Akizon77 committed 2026-06-08 20:45:00 +08:00
1 parent 9d914e70b9
commit 7da616ce4c
31 files changed
+550 -320

No files matched your search

+3 -3
View File
@@ -7,7 +7,7 @@ import (
"github.com/komari-monitor/komari/database/auditlog"
"github.com/komari-monitor/komari/database/clients"
"github.com/komari-monitor/komari/database/records"
"github.com/komari-monitor/komari/web/ws"
agent_runtime "github.com/komari-monitor/komari/web/agent"
)
func AddClient(c *gin.Context) {
@@ -68,8 +68,8 @@ func RemoveClient(c *gin.Context) {
user_uuid, _ := c.Get("uuid")
auditlog.Log(c.ClientIP(), user_uuid.(string), "delete client:"+uuid, "warn")
c.JSON(200, gin.H{"status": "success"})
ws.DeleteConnectedClients(uuid)
ws.DeleteLatestReport(uuid)
agent_runtime.DeleteConnectedClients(uuid)
agent_runtime.DeleteLatestReport(uuid)
}
func ClearRecord(c *gin.Context) {
+7 -8
View File
@@ -12,9 +12,8 @@ import (
"github.com/komari-monitor/komari/database/tasks"
v2 "github.com/komari-monitor/komari/protocol/v2"
"github.com/komari-monitor/komari/utils"
agent_runtime "github.com/komari-monitor/komari/web/agent"
"github.com/komari-monitor/komari/web/api"
agent_api "github.com/komari-monitor/komari/web/api/agent"
"github.com/komari-monitor/komari/web/ws"
)
// 接受数据类型:
@@ -36,7 +35,7 @@ func Exec(c *gin.Context) {
api.RespondError(c, 400, "Command cannot be empty")
return
}
// for uuid := range ws.GetConnectedClients() {
// for uuid := range agent_runtime.GetConnectedClients() {
// if contain(req.Clients, uuid) {
// onlineClients = append(onlineClients, uuid)
// }
@@ -46,9 +45,9 @@ func Exec(c *gin.Context) {
// // }
// }
for _, uuid := range req.Clients {
if client := ws.GetConnectedClients()[uuid]; client != nil {
if client := agent_runtime.GetConnectedClients()[uuid]; client != nil {
onlineClients = append(onlineClients, uuid)
} else if agent_api.IsAgentOnline(uuid) {
} else if agent_runtime.IsAgentOnline(uuid) {
queuedClients = append(queuedClients, uuid)
} else {
offlineClients = append(offlineClients, uuid)
@@ -72,10 +71,10 @@ func Exec(c *gin.Context) {
TaskId string `json:"task_id"`
}{Message: "exec", Command: req.Command, TaskId: taskId}
payload, _ := json.Marshal(legacy)
if ws.IsV2Client(uuid) {
if agent_runtime.IsV2Client(uuid) {
payload, _ = json.Marshal(v2.Request{JSONRPC: v2.Version, Method: v2.MethodAgentExec, Params: v2.ExecParams{TaskID: taskId, Command: req.Command}})
}
client := ws.GetConnectedClients()[uuid]
client := agent_runtime.GetConnectedClients()[uuid]
if client != nil {
if err := client.WriteMessage(websocket.TextMessage, payload); err != nil {
api.RespondError(c, 400, "Client connection is broke: "+uuid)
@@ -87,7 +86,7 @@ func Exec(c *gin.Context) {
}
}
for _, uuid := range queuedClients {
agent_api.DispatchV2Event(uuid, v2.MethodAgentExec, v2.ExecParams{TaskID: taskId, Command: req.Command})
agent_runtime.DispatchV2Event(uuid, v2.MethodAgentExec, v2.ExecParams{TaskID: taskId, Command: req.Command})
}
uuid, _ := c.Get("uuid")
auditlog.Log(c.ClientIP(), uuid.(string), "REC, task id: "+taskId, "warn")
+8
View File
@@ -13,6 +13,7 @@ func UpdateUser(c *gin.Context) {
Name *string `json:"username"`
Password *string `json:"password"`
SsoType *string `json:"sso_type"`
TwoFa string `json:"2fa_code"`
}
if err := c.ShouldBindJSON(&req); err != nil {
api.RespondError(c, 400, "Invalid or missing request body: "+err.Error())
@@ -30,6 +31,13 @@ func UpdateUser(c *gin.Context) {
api.RespondError(c, 400, "Password must be at least 6 characters long")
return
}
if req.Password != nil {
c.Set("2fa_code", req.TwoFa)
if err := api.VerifySensitive2FA(c); err != nil {
api.RespondError(c, 401, err.Error())
return
}
}
if err := accounts.UpdateUser(req.Uuid, req.Name, req.Password, req.SsoType); err != nil {
api.RespondError(c, 500, "Failed to update user: "+err.Error())
return