46 lines
1.5 KiB
Go
46 lines
1.5 KiB
Go
package dingtalk
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestDingTalkRedirectClearsRefererAndInheritedHeaders(t *testing.T) {
|
|
req := httptest.NewRequest(http.MethodGet, "https://cdn-two.example/image.png", nil)
|
|
req.Header.Set("Referer", "https://cdn-one.example/image.png?signature=TOPSECRET")
|
|
req.Header.Set("Authorization", "Bearer TOPSECRET")
|
|
if err := validateDingTalkRedirect(req, nil, true); err != nil {
|
|
t.Fatalf("validateDingTalkRedirect: %v", err)
|
|
}
|
|
if len(req.Header) != 0 {
|
|
t.Fatalf("redirect headers = %#v, want empty", req.Header)
|
|
}
|
|
}
|
|
|
|
func TestReplyViaSessionWebhookRejectsRedirectWithoutLeakingURL(t *testing.T) {
|
|
var reached bool
|
|
target := httptest.NewServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) { reached = true }))
|
|
defer target.Close()
|
|
source := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
|
w.Header().Set("Location", target.URL)
|
|
w.WriteHeader(http.StatusTemporaryRedirect)
|
|
}))
|
|
defer source.Close()
|
|
|
|
a := &Adapter{allowInsecureDownload: true}
|
|
signedURL := source.URL + "/hook?access_token=TOPSECRET"
|
|
err := a.replyViaSessionWebhook(context.Background(), signedURL, "reply")
|
|
if err == nil {
|
|
t.Fatal("expected webhook redirect rejection")
|
|
}
|
|
if reached {
|
|
t.Fatal("session webhook followed redirect")
|
|
}
|
|
if strings.Contains(err.Error(), "TOPSECRET") || strings.Contains(err.Error(), signedURL) {
|
|
t.Fatalf("credential URL leaked in error: %v", err)
|
|
}
|
|
}
|