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
@@ -15,6 +15,7 @@ import (
"github.com/Tencent/WeKnora/internal/datasource"
"github.com/Tencent/WeKnora/internal/logger"
"github.com/Tencent/WeKnora/internal/utils"
)
// notionClient wraps the Notion API with rate limiting and retry logic.
@@ -378,6 +379,9 @@ const maxDownloadSize = 100 * 1024 * 1024 // 100MB — prevent OOM from oversize
// DownloadFile downloads a file from the given URL (typically an S3 signed URL).
// Does not go through the rate limiter since it's not a Notion API call.
func (c *notionClient) DownloadFile(ctx context.Context, fileURL string) ([]byte, error) {
if err := utils.ValidateURLForSSRF(fileURL); err != nil {
return nil, fmt.Errorf("attachment URL rejected: %w", err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, fileURL, nil)
if err != nil {
return nil, fmt.Errorf("create download request: %w", err)
@@ -347,3 +347,17 @@ func TestClientQueryDatabaseAll(t *testing.T) {
// Ensure time import is used (referenced by fakeNotion indirectly via time.Time in types).
var _ = time.Now
func TestDownloadFile_RejectsLoopbackURL(t *testing.T) {
secutils.ResetSSRFWhitelistForTest()
t.Cleanup(secutils.ResetSSRFWhitelistForTest)
client, err := newClient("test-token", "https://api.notion.com")
if err != nil {
t.Fatalf("newClient: %v", err)
}
_, err = client.DownloadFile(context.Background(), "http://127.0.0.1/secret")
if err == nil {
t.Fatal("expected loopback attachment URL to be rejected")
}
}
+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)
})
}
}