Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions pkg/channels/wecom/media.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/sipeed/picoclaw/pkg/bus"
"github.com/sipeed/picoclaw/pkg/media"
"github.com/sipeed/picoclaw/pkg/utils"
)

const (
Expand Down Expand Up @@ -281,6 +282,9 @@ func (c *WeComChannel) storeRemoteMedia(
return "", fmt.Errorf("no media store available")
}

if err := utils.ValidateSafeHTTPURL(resourceURL, nil, nil); err != nil {
return "", fmt.Errorf("download media: %w", err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, resourceURL, nil)
if err != nil {
return "", fmt.Errorf("create request: %w", err)
Expand Down Expand Up @@ -418,6 +422,9 @@ func (c *WeComChannel) downloadRemoteMediaToTemp(
ctx context.Context,
resourceURL, fallbackName string,
) (string, string, string, error) {
if err := utils.ValidateSafeHTTPURL(resourceURL, nil, nil); err != nil {
return "", "", "", fmt.Errorf("download media: %w", err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, resourceURL, nil)
if err != nil {
return "", "", "", fmt.Errorf("create request: %w", err)
Expand Down
45 changes: 45 additions & 0 deletions pkg/channels/wecom/media_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ import (
"encoding/base64"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"

basechannels "github.com/sipeed/picoclaw/pkg/channels"
"github.com/sipeed/picoclaw/pkg/media"
"github.com/sipeed/picoclaw/pkg/utils"
)

func TestStoreRemoteMedia_DetectsJPEGContentTypeFromBody(t *testing.T) {
Expand Down Expand Up @@ -163,6 +166,48 @@ func TestStoreRemoteMedia_PreservesSuffixFromContentDisposition(t *testing.T) {
}
}

func TestStoreRemoteMedia_BlocksPrivateRedirect(t *testing.T) {
t.Parallel()

privateHit := false
private := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
privateHit = true
_, _ = w.Write([]byte("SECRET"))
}))
t.Cleanup(private.Close)

public := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, private.URL+"/secret", http.StatusFound)
}))
t.Cleanup(public.Close)

client, err := utils.CreateSafeHTTPClient(utils.SafeHTTPClientOptions{
Timeout: 5 * time.Second,
})
if err != nil {
t.Fatalf("CreateSafeHTTPClient: %v", err)
}

store := media.NewFileMediaStore()
ch := &WeComChannel{
BaseChannel: basechannels.NewBaseChannel("wecom", nil, nil, nil),
mediaClient: client,
}
ch.SetMediaStore(store)

_, err = ch.storeRemoteMedia(context.Background(), "test-scope", "msg-ssrf", public.URL, "", "")
if err == nil {
t.Fatal("expected storeRemoteMedia to reject redirect to private host")
}
if privateHit {
t.Fatal("private target was reached via redirect")
}
if !strings.Contains(err.Error(), "private or local") &&
!strings.Contains(err.Error(), "blocked private") {
t.Fatalf("unexpected error: %v", err)
}
}

func decodeTestBase64(t *testing.T, value string) []byte {
t.Helper()

Expand Down
10 changes: 9 additions & 1 deletion pkg/channels/wecom/wecom.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/sipeed/picoclaw/pkg/config"
"github.com/sipeed/picoclaw/pkg/identity"
"github.com/sipeed/picoclaw/pkg/logger"
"github.com/sipeed/picoclaw/pkg/utils"
)

const (
Expand Down Expand Up @@ -124,14 +125,21 @@ func NewChannel(bc *config.Channel, cfg *config.WeComSettings, messageBus *bus.M
channels.WithReasoningChannelID(bc.ReasoningChannelID),
)

mediaClient, err := utils.CreateSafeHTTPClient(utils.SafeHTTPClientOptions{
Timeout: wecomMediaTimeout,
})
if err != nil {
return nil, fmt.Errorf("create wecom media http client: %w", err)
}

ch := &WeComChannel{
BaseChannel: base,
config: cfg,
pending: make(map[string]chan wecomEnvelope),
turns: make(map[string][]wecomTurn),
recent: newRecentMessageSet(wecomRecentMessageMax),
routes: newReqIDStore(""),
mediaClient: &http.Client{Timeout: wecomMediaTimeout},
mediaClient: mediaClient,
}
ch.SetOwner(ch)
return ch, nil
Expand Down