263 lines
9.4 KiB
Go
263 lines
9.4 KiB
Go
package fbhttp
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/filebrowser/filebrowser/v2/settings"
|
|
"github.com/filebrowser/filebrowser/v2/users"
|
|
"golang.org/x/oauth2"
|
|
)
|
|
|
|
func TestInternalSessionTwoApplicationsAndAccountBoundaries(t *testing.T) {
|
|
user, state, status := "A", "version-A", "authenticated"
|
|
mode := ""
|
|
resolves, logouts := 0, 0
|
|
provider := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Header.Get("Cookie") != "" {
|
|
t.Error("forwarded browser cookies")
|
|
}
|
|
client, secret, ok := r.BasicAuth()
|
|
if !ok || (client != "one" && client != "two") || secret != "secret" {
|
|
w.WriteHeader(401)
|
|
return
|
|
}
|
|
r.ParseForm()
|
|
handle := r.Form.Get("session_handle")
|
|
if r.URL.Path == "/internal/session/logout" {
|
|
logouts++
|
|
if r.Form.Get("session_state") != state {
|
|
w.WriteHeader(409)
|
|
return
|
|
}
|
|
status = "signed_out"
|
|
w.WriteHeader(204)
|
|
return
|
|
}
|
|
resolves++
|
|
if handle == "foreign-handle-000000000000" {
|
|
w.WriteHeader(400)
|
|
json.NewEncoder(w).Encode(map[string]string{"error": "invalid_handle"})
|
|
return
|
|
}
|
|
if mode == "client-auth" {
|
|
w.WriteHeader(401)
|
|
return
|
|
}
|
|
if mode == "unavailable" {
|
|
w.WriteHeader(503)
|
|
return
|
|
}
|
|
if mode == "malformed" {
|
|
w.Write([]byte("not-json"))
|
|
return
|
|
}
|
|
current, version, condition := user, state, status
|
|
if strings.HasPrefix(handle, "device-two-") {
|
|
current, version, condition = "A", "device-two-A", "authenticated"
|
|
}
|
|
response := internalState{Status: condition, State: version, Factors: []string{"totp"}}
|
|
if condition == "authenticated" {
|
|
response.Authenticated = true
|
|
response.ClientID = client
|
|
response.Expires = time.Now().Add(time.Hour).Unix()
|
|
response.User = &ssoClaims{Subject: current, Username: current, Roles: []string{"User"}}
|
|
}
|
|
switch mode {
|
|
case "wrong-client":
|
|
response.ClientID = "other"
|
|
case "expired":
|
|
response.Expires = time.Now().Add(-time.Minute).Unix()
|
|
case "empty-sub":
|
|
response.User.Subject = ""
|
|
case "leaked-user":
|
|
response.Status = "signed_out"
|
|
response.Authenticated = false
|
|
}
|
|
json.NewEncoder(w).Encode(response)
|
|
}))
|
|
defer provider.Close()
|
|
type application struct {
|
|
s *SSO
|
|
h http.Handler
|
|
}
|
|
apps := []application{}
|
|
mutations := []string{}
|
|
for _, client := range []string{"one", "two"} {
|
|
root := t.TempDir()
|
|
st := scopedUserStorage(t, root, users.Permissions{Create: true, Download: true}, []byte("key"))
|
|
s := &SSO{issuer: provider.URL, origin: "https://" + client + ".test", store: st, server: &settings.Server{Root: root}, client: provider.Client(), oauth: oauth2.Config{ClientID: client, ClientSecret: "secret"}, avatars: map[string]avatarCache{}, uploads: map[string]uploadBinding{}}
|
|
h := s.Wrap(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
i, ok := r.Context().Value(identityKey{}).(sessionIdentity)
|
|
if !ok {
|
|
t.Error("missing identity")
|
|
w.WriteHeader(401)
|
|
return
|
|
}
|
|
if r.Method == "POST" {
|
|
mutations = append(mutations, i.Sub)
|
|
}
|
|
w.Header().Set("X-Subject", i.Sub)
|
|
w.WriteHeader(204)
|
|
}))
|
|
apps = append(apps, application{s, h})
|
|
}
|
|
request := func(app int, method, path, expected, handle string) *httptest.ResponseRecorder {
|
|
r := httptest.NewRequest(method, path, nil)
|
|
r.AddCookie(&http.Cookie{Name: internalCookie, Value: handle})
|
|
r.Header.Set("X-FB-Request", "1")
|
|
if expected != "" {
|
|
r.Header.Set("X-FB-Session-State", expected)
|
|
}
|
|
w := httptest.NewRecorder()
|
|
apps[app].h.ServeHTTP(w, r)
|
|
return w
|
|
}
|
|
handle := "device-one-handle-000000000"
|
|
for i := range apps {
|
|
w := request(i, "GET", "/api/private", "", handle)
|
|
if w.Code != 204 || w.Header().Get("X-Subject") != "A" {
|
|
t.Fatalf("app %d did not resolve A: %d", i, w.Code)
|
|
}
|
|
}
|
|
user, state = "B", "version-B"
|
|
for i := range apps {
|
|
w := request(i, "GET", "/api/private", "", handle)
|
|
if w.Code != 204 || w.Header().Get("X-Subject") != "B" {
|
|
t.Fatalf("app %d kept old identity", i)
|
|
}
|
|
}
|
|
if w := request(0, "GET", "/api/private", "", "device-two-handle-00000000"); w.Header().Get("X-Subject") != "A" {
|
|
t.Fatal("another device was switched")
|
|
}
|
|
if w := request(0, "POST", "/api/private", "version-A", handle); w.Code != 409 || len(mutations) != 0 {
|
|
t.Fatal("old form changed data as B")
|
|
}
|
|
if w := request(0, "POST", "/api/private", "version-B", handle); w.Code != 204 || len(mutations) != 1 || mutations[0] != "B" {
|
|
t.Fatal("current form rejected")
|
|
}
|
|
for _, condition := range []string{"access_denied", "signed_out", "factor_required", "factor_enrollment_required", "rebind_required"} {
|
|
status = condition
|
|
w := request(0, "GET", "/auth/session", "", handle)
|
|
if w.Code != 200 || !strings.Contains(w.Body.String(), `"status":"`+condition+`"`) || strings.Contains(w.Body.String(), `"user"`) {
|
|
t.Fatalf("bad state %s: %d %s", condition, w.Code, w.Body.String())
|
|
}
|
|
if p := request(0, "POST", "/api/private", "version-B", handle); p.Code == 204 {
|
|
t.Fatal("inactive identity retained access")
|
|
}
|
|
}
|
|
status = "authenticated"
|
|
for _, failure := range []string{"client-auth", "unavailable", "malformed", "wrong-client", "expired", "empty-sub", "leaked-user"} {
|
|
mode = failure
|
|
w := request(0, "GET", "/auth/session", "", handle)
|
|
if w.Code != 503 || len(w.Result().Cookies()) != 0 {
|
|
t.Fatalf("%s must fail closed without deleting cookie: %d", failure, w.Code)
|
|
}
|
|
}
|
|
mode = ""
|
|
if w := request(0, "GET", "/auth/session", "", "foreign-handle-000000000000"); w.Code != 200 || !strings.Contains(w.Body.String(), "rebind_required") {
|
|
t.Fatal("foreign handle accepted")
|
|
}
|
|
before := resolves
|
|
request(0, "GET", "/api/private", "", handle)
|
|
request(0, "GET", "/api/private", "", handle)
|
|
if resolves-before != 2 {
|
|
t.Fatal("positive identity was cached between requests")
|
|
}
|
|
if w := request(0, "POST", "/auth/logout", "version-A", handle); w.Code != 409 || logouts != 0 {
|
|
t.Fatal("stale logout reached provider")
|
|
}
|
|
if w := request(0, "POST", "/auth/logout", "version-B", handle); w.Code != 204 {
|
|
t.Fatal("global logout failed")
|
|
}
|
|
if w := request(1, "GET", "/api/private", "", handle); w.Code != 401 {
|
|
t.Fatal("second app retained access after logout")
|
|
}
|
|
if w := request(0, "GET", "/api/private", "", "device-two-handle-00000000"); w.Code != 204 {
|
|
t.Fatal("logout affected another device")
|
|
}
|
|
status, user, state = "authenticated", "A", "version-A"
|
|
if w := request(0, "POST", "/api/tus/upload.bin", state, handle); w.Code != 204 {
|
|
t.Fatal("upload did not start")
|
|
}
|
|
user, state = "B", "version-B"
|
|
if w := request(0, "PATCH", "/api/tus/upload.bin", state, handle); w.Code != 409 {
|
|
t.Fatal("B continued A's transfer")
|
|
}
|
|
user, state = "A", "version-A2"
|
|
if w := request(0, "PATCH", "/api/tus/upload.bin", state, handle); w.Code != 409 {
|
|
t.Fatal("old transfer crossed a session-state boundary")
|
|
}
|
|
}
|
|
|
|
func TestNoSSOIsExplicitAndDoesNotNeedProvider(t *testing.T) {
|
|
root := t.TempDir()
|
|
st := scopedUserStorage(t, root, users.Permissions{}, []byte("key"))
|
|
server := &settings.Server{Root: root}
|
|
n, err := NewNoSSO(st, server)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
h := n.Wrap(handle(withAdmin(func(w http.ResponseWriter, r *http.Request, d *data) (int, error) { return http.StatusNoContent, nil }), "", st, server))
|
|
for _, method := range []string{"GET", "POST"} {
|
|
r := httptest.NewRequest(method, "/api/private", nil)
|
|
r.Header.Set("X-FB-Request", "1")
|
|
w := httptest.NewRecorder()
|
|
h.ServeHTTP(w, r)
|
|
if w.Code != 204 {
|
|
t.Fatalf("passwordless mode: %d", w.Code)
|
|
}
|
|
}
|
|
w := httptest.NewRecorder()
|
|
h.ServeHTTP(w, httptest.NewRequest("GET", "/auth/session", nil))
|
|
if w.Code != 200 || !strings.Contains(w.Body.String(), `"authMode":"no-sso"`) || !strings.Contains(w.Body.String(), `"admin":true`) {
|
|
t.Fatal(w.Body.String())
|
|
}
|
|
r := httptest.NewRequest("POST", "/api/private", nil)
|
|
r.Header.Set("Origin", "https://evil.test")
|
|
w = httptest.NewRecorder()
|
|
h.ServeHTTP(w, r)
|
|
if w.Code != 403 {
|
|
t.Fatal("cross-origin passwordless write allowed")
|
|
}
|
|
u, err := st.Users.Get(root, false, "local:no-sso")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
n2, err := NewNoSSO(st, server)
|
|
if err != nil || n2.identity.ID != u.ID {
|
|
t.Fatal("no-sso created duplicate local user")
|
|
}
|
|
}
|
|
|
|
func TestLongOperationRevalidatesIdentity(t *testing.T) {
|
|
provider := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
json.NewEncoder(w).Encode(internalState{Status: "signed_out"})
|
|
}))
|
|
defer provider.Close()
|
|
s := &SSO{issuer: provider.URL, client: provider.Client()}
|
|
g := &identityGuard{s: s, identity: sessionIdentity{Sub: "A", State: "state", Handle: "long-operation-handle-00000"}, last: time.Now().Add(-3 * time.Second)}
|
|
if g.check(context.Background()) == nil {
|
|
t.Fatal("long connection retained revoked access")
|
|
}
|
|
}
|
|
|
|
func TestParallelLoginTransactionsHaveSeparateBrowserCookies(t *testing.T) {
|
|
s := &SSO{base: "", cookiePath: "/", secure: true, attempts: map[string]loginAttempt{}, oauth: oauth2.Config{ClientID: "client", Endpoint: oauth2.Endpoint{AuthURL: "https://sso.test/oauth/authorize"}}}
|
|
first, second := httptest.NewRecorder(), httptest.NewRecorder()
|
|
s.login(first, httptest.NewRequest("GET", "/auth/login", nil))
|
|
s.login(second, httptest.NewRequest("GET", "/auth/login?silent=1", nil))
|
|
a, b := first.Result().Cookies()[0], second.Result().Cookies()[0]
|
|
if a.Name == b.Name || a.Value == b.Value || len(s.attempts) != 2 {
|
|
t.Fatal("parallel flows overwrite each other")
|
|
}
|
|
if !a.HttpOnly || !a.Secure || a.SameSite != http.SameSiteLaxMode {
|
|
t.Fatal("unprotected transaction cookie")
|
|
}
|
|
}
|