fix(security): close remaining GHSA gaps from security triage

Block scheme-prefixed loopback storage checks, validate Notion attachment
URLs, sanitize wiki markdown HTML, and require MCP_SERVER_AUTH_TOKEN for
network MCP transports.
This commit is contained in:
wizardchen
2026-07-02 14:58:19 +08:00
committed by lyingbug
parent f23c674bad
commit cd65665894
9 changed files with 158 additions and 13 deletions
+25 -3
View File
@@ -7,6 +7,7 @@ import (
"fmt"
"net"
"net/http"
"net/url"
"os"
"regexp"
"strconv"
@@ -634,14 +635,35 @@ func sanitizeStorageCheckError(err error) string {
}
}
// storageEndpointHost extracts the hostname from a storage endpoint string.
// Endpoints may be bare host:port, hostnames, or full URLs with a scheme
// (e.g. "http://127.0.0.1:9000" for S3-compatible stores).
func storageEndpointHost(endpoint string) string {
endpoint = strings.TrimSpace(endpoint)
if endpoint == "" {
return ""
}
if strings.Contains(endpoint, "://") {
if u, err := url.Parse(endpoint); err == nil {
if h := u.Hostname(); h != "" {
return h
}
}
}
if host, _, err := net.SplitHostPort(endpoint); err == nil {
return host
}
return endpoint
}
// isBlockedStorageEndpoint checks whether a storage endpoint resolves to a dangerous
// address (cloud metadata, loopback, link-local). Unlike the stricter isSSRFSafeURL,
// this allows private IPs since MinIO is commonly deployed on internal networks.
// It also respects the SSRF_WHITELIST environment variable for whitelisted hosts.
func isBlockedStorageEndpoint(endpoint string) (bool, string) {
host, _, err := net.SplitHostPort(endpoint)
if err != nil {
host = endpoint
host := storageEndpointHost(endpoint)
if host == "" {
return true, "无效的地址"
}
// Check SSRF whitelist first – whitelisted hosts bypass the block check.
@@ -0,0 +1,39 @@
package handler
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestStorageEndpointHost(t *testing.T) {
tests := []struct {
endpoint string
want string
}{
{"127.0.0.1:9000", "127.0.0.1"},
{"http://127.0.0.1:9000", "127.0.0.1"},
{"https://127.0.0.1:9000", "127.0.0.1"},
{"minio.internal:9000", "minio.internal"},
{"https://s3.amazonaws.com", "s3.amazonaws.com"},
}
for _, tt := range tests {
t.Run(tt.endpoint, func(t *testing.T) {
assert.Equal(t, tt.want, storageEndpointHost(tt.endpoint))
})
}
}
func TestIsBlockedStorageEndpoint_SchemePrefixedLoopback(t *testing.T) {
for _, endpoint := range []string{
"http://127.0.0.1:9000",
"https://127.0.0.1:9000",
"127.0.0.1:9000",
} {
t.Run(endpoint, func(t *testing.T) {
blocked, reason := isBlockedStorageEndpoint(endpoint)
assert.True(t, blocked, "expected loopback endpoint to be blocked")
assert.NotEmpty(t, reason)
})
}
}