Anydoc / anydoc parser engine (push) Canceled after 0s
App / Format, vet, test, and build (push) Canceled after 0s
CLI / test (macos-latest / go 1.26) (push) Canceled after 0s
CLI / test (ubuntu-latest / go 1.26) (push) Canceled after 0s
CLI / test (windows-latest / go 1.26) (push) Canceled after 0s
Build and Push Docker Image / build-ui (push) Canceled after 0s
Build and Push Docker Image / build-docreader (push) Canceled after 0s
Build and Push Docker Image / build-sandbox (push) Canceled after 0s
Build and Push Docker Image / build-app (amd64, linux/amd64, ubuntu-latest) (push) Canceled after 0s
Build and Push Docker Image / build-app (arm64, linux/arm64, ubuntu-24.04-arm) (push) Canceled after 0s
DocReader / Python tests and Go client tests (push) Canceled after 0s
Build & Publish (dsh-weknora) / Typecheck, test, pack (push) Canceled after 0s
Build & Publish (dsh-weknora) / End-to-end inside dsh (pinned) (push) Canceled after 0s
Build & Publish (dsh-weknora) / End-to-end inside dsh (latest, advisory) (push) Canceled after 0s
Frontend / Test, type-check, and build (push) Canceled after 0s
Go Lint Cache / warm lint cache (.) (push) Canceled after 0s
Go Lint Cache / warm lint cache (cli) (push) Canceled after 0s
Go Lint Cache / warm lint cache (client) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.10) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.11) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.12) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.13) (push) Canceled after 0s
Build and Push Docker Image / merge (push) Canceled after 0s
Build & Publish (dsh-weknora) / Publish to npm (push) Canceled after 0s
Build & Publish (mcp-server) / Build sdist + wheel (push) Canceled after 0s
Build & Publish (mcp-server) / Publish to PyPI (push) Canceled after 0s
140 lines
4.8 KiB
Go
140 lines
4.8 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/Tencent/WeKnora/internal/config"
|
|
secutils "github.com/Tencent/WeKnora/internal/utils"
|
|
)
|
|
|
|
func withOIDCSSRFWhitelist(t *testing.T, raw string) {
|
|
t.Helper()
|
|
t.Setenv("SSRF_WHITELIST", raw)
|
|
secutils.ResetSSRFWhitelistForTest()
|
|
t.Cleanup(secutils.ResetSSRFWhitelistForTest)
|
|
}
|
|
|
|
func TestOIDCDiscoveryRejectsInternalDiscoveredEndpoint(t *testing.T) {
|
|
withOIDCSSRFWhitelist(t, "127.0.0.1")
|
|
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
_ = json.NewEncoder(w).Encode(oidcDiscoveryDocument{
|
|
AuthorizationEndpoint: serverURL(r, "/authorize"),
|
|
TokenEndpoint: "http://169.254.169.254/latest/meta-data/",
|
|
})
|
|
}))
|
|
defer server.Close()
|
|
|
|
svc := &userService{}
|
|
cfg := &config.OIDCAuthConfig{DiscoveryURL: server.URL}
|
|
err := svc.populateOIDCEndpoints(context.Background(), cfg)
|
|
if err == nil || !strings.Contains(err.Error(), "OIDC token endpoint failed SSRF validation") {
|
|
t.Fatalf("populateOIDCEndpoints error = %v, want token SSRF validation failure", err)
|
|
}
|
|
}
|
|
|
|
func TestOIDCDiscoveryFillsJwksAndIssuerWhenEndpointsAreExplicit(t *testing.T) {
|
|
withOIDCSSRFWhitelist(t, "127.0.0.1")
|
|
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
_ = json.NewEncoder(w).Encode(oidcDiscoveryDocument{
|
|
Issuer: "https://idp.example",
|
|
AuthorizationEndpoint: serverURL(r, "/authorize"),
|
|
TokenEndpoint: serverURL(r, "/token"),
|
|
UserInfoEndpoint: serverURL(r, "/userinfo"),
|
|
JwksURI: serverURL(r, "/keys"),
|
|
})
|
|
}))
|
|
defer server.Close()
|
|
|
|
svc := &userService{}
|
|
cfg := &config.OIDCAuthConfig{
|
|
DiscoveryURL: server.URL,
|
|
AuthorizationEndpoint: server.URL + "/authorize",
|
|
TokenEndpoint: server.URL + "/token",
|
|
}
|
|
if err := svc.populateOIDCEndpoints(context.Background(), cfg); err != nil {
|
|
t.Fatalf("populateOIDCEndpoints: %v", err)
|
|
}
|
|
if cfg.JwksURI != server.URL+"/keys" {
|
|
t.Fatalf("JwksURI = %q, want discovery jwks_uri", cfg.JwksURI)
|
|
}
|
|
if cfg.IssuerURL != "https://idp.example" {
|
|
t.Fatalf("IssuerURL = %q, want discovery issuer", cfg.IssuerURL)
|
|
}
|
|
}
|
|
|
|
func TestOIDCDiscoveryRejectsInternalJwksURI(t *testing.T) {
|
|
withOIDCSSRFWhitelist(t, "127.0.0.1")
|
|
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
_ = json.NewEncoder(w).Encode(oidcDiscoveryDocument{
|
|
Issuer: "https://idp.example",
|
|
AuthorizationEndpoint: serverURL(r, "/authorize"),
|
|
TokenEndpoint: serverURL(r, "/token"),
|
|
JwksURI: "http://169.254.169.254/latest/meta-data/",
|
|
})
|
|
}))
|
|
defer server.Close()
|
|
|
|
svc := &userService{}
|
|
cfg := &config.OIDCAuthConfig{DiscoveryURL: server.URL}
|
|
err := svc.populateOIDCEndpoints(context.Background(), cfg)
|
|
if err == nil || !strings.Contains(err.Error(), "OIDC jwks endpoint failed SSRF validation") {
|
|
t.Fatalf("populateOIDCEndpoints error = %v, want jwks SSRF validation failure", err)
|
|
}
|
|
}
|
|
|
|
func TestOIDCTokenExchangeBlocksRedirectToInternalURL(t *testing.T) {
|
|
withOIDCSSRFWhitelist(t, "127.0.0.1")
|
|
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
http.Redirect(w, r, "http://169.254.169.254/latest/meta-data/", http.StatusFound)
|
|
}))
|
|
defer server.Close()
|
|
|
|
svc := &userService{}
|
|
cfg := &config.OIDCAuthConfig{TokenEndpoint: server.URL, ClientID: "client-id", ClientSecret: "client-secret"}
|
|
_, err := svc.exchangeOIDCCode(context.Background(), cfg, "code", "https://app.example/callback")
|
|
if err == nil || !strings.Contains(err.Error(), secutils.ErrSSRFRedirectBlocked.Error()) {
|
|
t.Fatalf("exchangeOIDCCode error = %v, want redirect SSRF block", err)
|
|
}
|
|
}
|
|
|
|
func TestOIDCErrorsDoNotEchoSecrets(t *testing.T) {
|
|
withOIDCSSRFWhitelist(t, "127.0.0.1")
|
|
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
_, _ = w.Write([]byte("client-secret bearer-token"))
|
|
}))
|
|
defer server.Close()
|
|
|
|
svc := &userService{}
|
|
cfg := &config.OIDCAuthConfig{TokenEndpoint: server.URL, ClientID: "client-id", ClientSecret: "client-secret"}
|
|
_, err := svc.exchangeOIDCCode(context.Background(), cfg, "code", "https://app.example/callback")
|
|
if err == nil {
|
|
t.Fatalf("exchangeOIDCCode returned nil error")
|
|
}
|
|
if strings.Contains(err.Error(), "client-secret") {
|
|
t.Fatalf("exchangeOIDCCode error leaked secret: %v", err)
|
|
}
|
|
|
|
_, err = svc.fetchOIDCUserInfo(context.Background(), server.URL, "bearer-token")
|
|
if err == nil {
|
|
t.Fatalf("fetchOIDCUserInfo returned nil error")
|
|
}
|
|
if strings.Contains(err.Error(), "bearer-token") {
|
|
t.Fatalf("fetchOIDCUserInfo error leaked bearer token: %v", err)
|
|
}
|
|
}
|
|
|
|
func serverURL(r *http.Request, path string) string {
|
|
return "http://" + r.Host + path
|
|
}
|