Secure, Fast, and Extensible Sandbox runtime for AI agents.
refactor(execd): keep request token selection with auth middleware
ninan.nn295eb48fdb11
diff --git a/components/execd/pkg/web/pty_auth.go b/components/execd/pkg/web/pty_auth.goindex 231a96df6..f48e49364 100644--- a/components/execd/pkg/web/pty_auth.go+++ b/components/execd/pkg/web/pty_auth.go@@ -19,33 +19,11 @@ import ("net/http""strings"- "github.com/gin-gonic/gin"- "github.com/gorilla/websocket"-"github.com/alibaba/opensandbox/execd/pkg/web/model")-// requestAccessToken accepts the browser credential only on an actual PTY-// WebSocket upgrade. It strips credential entries before downstream handshake-// negotiation, including when an explicit API-token header takes precedence.-func requestAccessToken(ctx *gin.Context) (string, bool) {- r := ctx.Request- token := ""- if r.Method == http.MethodGet && ctx.FullPath() == "/pty/:sessionId/ws" && websocket.IsWebSocketUpgrade(r) {- var valid bool- token, valid = ptyWebSocketAccessToken(r)- if !valid {- return "", false- }- }- // Presence, not a non-empty value, determines precedence. A bad explicit- // header must not be rescued by a valid subprotocol credential.- if _, present := r.Header[http.CanonicalHeaderKey(model.ApiAccessTokenHeader)]; present {- return r.Header.Get(model.ApiAccessTokenHeader), true- }- return token, true-}-+// ptyWebSocketAccessToken extracts and strips the credential subprotocol before+// the PTY handler negotiates the application subprotocol.func ptyWebSocketAccessToken(r *http.Request) (string, bool) {const header = "Sec-WebSocket-Protocol"var protocols []stringdiff --git a/components/execd/pkg/web/router.go b/components/execd/pkg/web/router.goindex 4c5219e9d..8baba45e9 100644--- a/components/execd/pkg/web/router.go+++ b/components/execd/pkg/web/router.go@@ -18,6 +18,7 @@ import ("net/http""github.com/gin-gonic/gin"+ "github.com/gorilla/websocket""github.com/alibaba/opensandbox/execd/pkg/binding""github.com/alibaba/opensandbox/execd/pkg/flag"@@ -234,6 +235,26 @@ func accessTokenMiddleware(legacyToken string) gin.HandlerFunc {}}+// requestAccessToken selects the existing API-token header or, for PTY+// WebSocket upgrades only, a browser credential from the subprotocol list.+func requestAccessToken(ctx *gin.Context) (string, bool) {+ r := ctx.Request+ token := ""+ if r.Method == http.MethodGet && ctx.FullPath() == "/pty/:sessionId/ws" && websocket.IsWebSocketUpgrade(r) {+ var valid bool+ token, valid = ptyWebSocketAccessToken(r)+ if !valid {+ return "", false+ }+ }+ // Presence, not a non-empty value, determines precedence. A bad explicit+ // header must not be rescued by a valid subprotocol credential.+ if _, present := r.Header[http.CanonicalHeaderKey(model.ApiAccessTokenHeader)]; present {+ return r.Header.Get(model.ApiAccessTokenHeader), true+ }+ return token, true+}+func abortUnauthorized(ctx *gin.Context) {ctx.AbortWithStatusJSON(http.StatusUnauthorized, map[string]any{"error": "Unauthorized: invalid or missing header " + model.ApiAccessTokenHeader,