mirror of
https://github.com/Buriburizaem0n/nezha_domains.git
synced 2026-09-19 09:40:12 +00:00
GHSA-jg62-j5h6-8mpq: the terminal and file-manager endpoints created unbounded IO streams; an authenticated member could open thousands, each spawning goroutines, a 1MiB buffer and an agent-side PTY, exhausting dashboard and agent resources. CreateStream now enforces a per-user (20) and per-server (40) cap in the existing ioStreamMutex critical section, using the stream map as the single source of truth. Dashboard-internal streams (uid==0: NAT, server transfer, MCP transfer) skip the per-user cap but still count per-server. Adds caps, exemption, slot-release and no-leak regression tests.
122 lines
3.3 KiB
Go
122 lines
3.3 KiB
Go
package controller
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/goccy/go-json"
|
|
"github.com/gorilla/websocket"
|
|
"github.com/hashicorp/go-uuid"
|
|
|
|
"github.com/nezhahq/nezha/model"
|
|
"github.com/nezhahq/nezha/pkg/websocketx"
|
|
"github.com/nezhahq/nezha/proto"
|
|
"github.com/nezhahq/nezha/service/rpc"
|
|
"github.com/nezhahq/nezha/service/singleton"
|
|
)
|
|
|
|
// Create web ssh terminal
|
|
// @Summary Create web ssh terminal
|
|
// @Description Create web ssh terminal
|
|
// @Tags auth required
|
|
// @Accept json
|
|
// @Param terminal body model.TerminalForm true "TerminalForm"
|
|
// @Produce json
|
|
// @Success 200 {object} model.CreateTerminalResponse
|
|
// @Router /terminal [post]
|
|
func createTerminal(c *gin.Context) (*model.CreateTerminalResponse, error) {
|
|
var createTerminalReq model.TerminalForm
|
|
if err := c.ShouldBind(&createTerminalReq); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
server, _ := singleton.ServerShared.Get(createTerminalReq.ServerID)
|
|
if server == nil {
|
|
return nil, singleton.Localizer.ErrorT("server not found or not connected")
|
|
}
|
|
if server.GetTaskStream() == nil {
|
|
return nil, singleton.Localizer.ErrorT("server not found or not connected")
|
|
}
|
|
|
|
if !server.HasPermission(c) {
|
|
return nil, singleton.Localizer.ErrorT("permission denied")
|
|
}
|
|
|
|
streamId, err := uuid.GenerateUUID()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if err := rpc.NezhaHandlerSingleton.CreateStream(streamId, getUid(c), server.ID); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
terminalData, _ := json.Marshal(&model.TerminalTask{
|
|
StreamID: streamId,
|
|
})
|
|
if err := server.SendTask(&proto.Task{
|
|
Type: model.TaskTypeTerminalGRPC,
|
|
Data: string(terminalData),
|
|
}); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &model.CreateTerminalResponse{
|
|
SessionID: streamId,
|
|
ServerID: server.ID,
|
|
ServerName: server.Name,
|
|
}, nil
|
|
}
|
|
|
|
// TerminalStream web ssh terminal stream
|
|
// @Summary Terminal stream
|
|
// @Description Terminal stream
|
|
// @Tags auth required
|
|
// @Param id path string true "Stream UUID"
|
|
// @Success 200 {object} model.CommonResponse[any]
|
|
// @Router /ws/terminal/{id} [get]
|
|
func terminalStream(c *gin.Context) (any, error) {
|
|
streamId := c.Param("id")
|
|
// GHSA-style fix: io_stream sessions must be reachable only by their creator
|
|
// (or an admin). Without this, any authenticated user who learns a stream
|
|
// UUID — via Referer leak, access logs, browser history — can hijack a live
|
|
// terminal and gain shell access to the target server.
|
|
if !streamAttachAllowedForRequest(c, streamId) {
|
|
return nil, singleton.Localizer.ErrorT("permission denied")
|
|
}
|
|
if _, err := rpc.NezhaHandlerSingleton.GetStream(streamId); err != nil {
|
|
return nil, err
|
|
}
|
|
defer rpc.NezhaHandlerSingleton.CloseStream(streamId)
|
|
|
|
wsConn, err := upgrader.Upgrade(c.Writer, c.Request, nil)
|
|
if err != nil {
|
|
return nil, newWsError("%v", err)
|
|
}
|
|
defer wsConn.Close()
|
|
conn := websocketx.NewConn(wsConn)
|
|
|
|
deregisterPAT := registerPATConnection(c, func() { _ = wsConn.Close() })
|
|
defer deregisterPAT()
|
|
|
|
go func() {
|
|
// PING 保活
|
|
for {
|
|
if err = conn.WriteMessage(websocket.PingMessage, []byte{}); err != nil {
|
|
return
|
|
}
|
|
time.Sleep(time.Second * 10)
|
|
}
|
|
}()
|
|
|
|
if err = rpc.NezhaHandlerSingleton.UserConnected(streamId, conn); err != nil {
|
|
return nil, newWsError("%v", err)
|
|
}
|
|
|
|
if err = rpc.NezhaHandlerSingleton.StartStream(streamId, time.Second*10); err != nil {
|
|
return nil, newWsError("%v", err)
|
|
}
|
|
|
|
return nil, newWsError("")
|
|
}
|