fix(mcp): coordinate OAuth token refresh and authorization polling
Prevent concurrent refresh-token rotation across instances, distinguish expired-but-refreshable tokens from active authorization, and bind OAuth popup polling to a per-attempt ID so stale tokens cannot satisfy new consent.
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"bytes"
|
||||
"context"
|
||||
"io"
|
||||
"net/url"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -44,7 +45,8 @@ func (r loggerResponseBodyWriter) Write(b []byte) (int, error) {
|
||||
var sensitiveFieldRegex = regexp.MustCompile(
|
||||
`(?i)("(?:new[_-]?password|old[_-]?password|password|passwd|token|access[_-]?token|` +
|
||||
`refresh[_-]?token|id[_-]?token|authorization|auth[_-]?token|api[_-]?key|` +
|
||||
`api[_-]?secret|secret[_-]?key|client[_-]?secret|private[_-]?key|secret)")\s*:\s*"[^"]*"`,
|
||||
`api[_-]?secret|secret[_-]?key|client[_-]?secret|private[_-]?key|secret|` +
|
||||
`authorization[_-]?url|authorization[_-]?attempt)")\s*:\s*"[^"]*"`,
|
||||
)
|
||||
|
||||
// sanitizeBody 清理敏感信息
|
||||
@@ -52,6 +54,32 @@ func sanitizeBody(body string) string {
|
||||
return sensitiveFieldRegex.ReplaceAllString(body, `$1:"***"`)
|
||||
}
|
||||
|
||||
var sensitiveQueryFields = map[string]struct{}{
|
||||
"access_token": {},
|
||||
"authorization_attempt": {},
|
||||
"code": {},
|
||||
"id_token": {},
|
||||
"refresh_token": {},
|
||||
"state": {},
|
||||
"token": {},
|
||||
}
|
||||
|
||||
// sanitizeQuery prevents OAuth authorization codes and CSRF/attempt state from
|
||||
// being copied into access logs. Parsing the query also covers repeated and
|
||||
// percent-encoded parameters without relying on fragile string replacement.
|
||||
func sanitizeQuery(raw string) string {
|
||||
values, err := url.ParseQuery(raw)
|
||||
if err != nil {
|
||||
return "[invalid query omitted]"
|
||||
}
|
||||
for key := range values {
|
||||
if _, sensitive := sensitiveQueryFields[strings.ToLower(key)]; sensitive {
|
||||
values[key] = []string{"***"}
|
||||
}
|
||||
}
|
||||
return values.Encode()
|
||||
}
|
||||
|
||||
// readRequestBody 读取请求体(限制大小用于日志,但完整读取用于重置)
|
||||
func readRequestBody(c *gin.Context) string {
|
||||
if c.Request.Body == nil {
|
||||
@@ -172,7 +200,7 @@ func Logger() gin.HandlerFunc {
|
||||
method := c.Request.Method
|
||||
|
||||
if raw != "" {
|
||||
path = path + "?" + raw
|
||||
path = path + "?" + sanitizeQuery(raw)
|
||||
}
|
||||
|
||||
// 读取响应体
|
||||
|
||||
@@ -53,6 +53,11 @@ func TestSanitizeBody(t *testing.T) {
|
||||
in: `{"baseUrl":"https://example.com","modelName":"gpt"}`,
|
||||
want: `{"baseUrl":"https://example.com","modelName":"gpt"}`,
|
||||
},
|
||||
{
|
||||
name: "OAuth authorization response fields",
|
||||
in: `{"authorization_url":"https://idp.example/authorize?state=secret","authorization_attempt":"secret-state"}`,
|
||||
want: `{"authorization_url":"***","authorization_attempt":"***"}`,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
@@ -64,3 +69,11 @@ func TestSanitizeBody(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSanitizeQuery(t *testing.T) {
|
||||
got := sanitizeQuery("code=secret-code&state=secret-state&next=%2Fsettings&state=second")
|
||||
want := "code=%2A%2A%2A&next=%2Fsettings&state=%2A%2A%2A"
|
||||
if got != want {
|
||||
t.Fatalf("sanitizeQuery() = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user