package fbhttp import ( "encoding/json" "io/fs" "net/http" "net/http/httptest" "os" "os/exec" "path/filepath" "strings" "sync" "testing" "time" "github.com/filebrowser/filebrowser/v2/diskcache" "github.com/filebrowser/filebrowser/v2/frontend" "github.com/filebrowser/filebrowser/v2/img" "github.com/filebrowser/filebrowser/v2/settings" "github.com/filebrowser/filebrowser/v2/users" "golang.org/x/oauth2" ) // Optional integration: two real File Browser servers, one shared mock issuer, // separate host-only Secure cookies, and a second independent browser device. func TestInternalSessionBrowser(t *testing.T) { if os.Getenv("FILEBROWSER_INTERNAL_QA") != "1" { t.Skip("optional browser integration") } var mu sync.Mutex name, version, status := "Alice", "A-1", "authenticated" denied, offline := false, false control := randomSecret() provider := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.URL.Path == "/internal/session/frame" { w.Header().Set("Content-Type", "text/html") w.Write([]byte("Session notification fixture")) return } mu.Lock() defer mu.Unlock() if r.URL.Path == "/__qa/change" { if r.Header.Get("X-QA-Control") != control { w.WriteHeader(403) return } var p struct { Name string Version string Status string Denied bool Offline bool } if json.NewDecoder(r.Body).Decode(&p) != nil { w.WriteHeader(400) return } name, version, status, denied, offline = p.Name, p.Version, p.Status, p.Denied, p.Offline w.WriteHeader(204) return } client, secret, ok := r.BasicAuth() if !ok || (client != "one" && client != "two") || secret != "qa-secret" || r.Header.Get("Cookie") != "" { t.Error("unexpected internal credentials or browser cookies") w.WriteHeader(401) return } r.ParseForm() if !strings.HasPrefix(r.Form.Get("session_handle"), client+"-") { w.WriteHeader(400) json.NewEncoder(w).Encode(map[string]string{"error": "invalid_handle"}) return } if offline { w.WriteHeader(503) return } if r.URL.Path == "/internal/session/logout" { if r.Form.Get("session_state") != version { w.WriteHeader(409) return } status = "signed_out" version = "signed-out" w.WriteHeader(204) return } if r.URL.Path != "/internal/session/resolve" { t.Error("unexpected OAuth exchange or redirect") w.WriteHeader(404) return } who, revision, condition := name, version, status if strings.Contains(r.Form.Get("session_handle"), "device-two") { who, revision, condition = "Alice", "device-two-A", "authenticated" } if denied && client == "two" { condition = "access_denied" } state := internalState{Status: condition, State: revision} if condition == "authenticated" { roles := []string{"User"} if who == "Alice" { roles = []string{"FS Admin"} } state.Authenticated = true state.ClientID = client state.Expires = time.Now().Add(time.Hour).Unix() state.User = &ssoClaims{Subject: who, Username: who, Roles: roles} } json.NewEncoder(w).Encode(state) })) defer provider.Close() assets, err := fs.Sub(frontend.Assets(), "dist") if err != nil { t.Fatal(err) } urls := []string{} for _, id := range []string{"one", "two"} { root := t.TempDir() for _, who := range []string{"Alice", "Bob"} { os.Mkdir(filepath.Join(root, who), 0750) os.WriteFile(filepath.Join(root, who, who+"-private.txt"), []byte(who+" PRIVATE"), 0600) } os.Mkdir(filepath.Join(root, "Alice", "Alice-only"), 0750) os.WriteFile(filepath.Join(root, "Alice", "Alice-only", "nested-Alice.txt"), []byte("ALICE ONLY"), 0600) st := scopedUserStorage(t, root, users.Permissions{Download: true, Create: true, Rename: true, Modify: true, Delete: true, Share: true}, []byte("key")) st.Users = st.Users.(*customFSUser).Store options, _ := st.Settings.Get() options.Defaults = settings.UserDefaults{Scope: ".", Locale: "en", Perm: users.Permissions{Download: true, Create: true, Rename: true, Modify: true, Delete: true, Share: true}} st.Settings.Save(options) cfg := &settings.Server{Root: root, SSOIssuer: provider.URL, SSOClientID: id} s := &SSO{issuer: provider.URL, store: st, server: cfg, client: provider.Client(), oauth: oauth2.Config{ClientID: id, ClientSecret: "qa-secret"}, avatars: map[string]avatarCache{}, uploads: map[string]uploadBinding{}} for _, who := range []string{"Alice", "Bob"} { i, e := s.provisionUser(ssoClaims{Subject: who, Username: who}) if e != nil { t.Fatal(e) } if e = st.Users.Update(&users.User{ID: i.ID, Scope: who}, "Scope"); e != nil { t.Fatal(e) } } uploads, e := NewUploadCache("") if e != nil { t.Fatal(e) } defer uploads.Close() h, e := NewHandler(img.New(1), diskcache.NewNoOp(), uploads, st, cfg, assets) if e != nil { t.Fatal(e) } app := httptest.NewTLSServer(s.Wrap(h)) defer app.Close() url := strings.Replace(app.URL, "127.0.0.1", id+".localhost", 1) s.origin = url urls = append(urls, url) } command := exec.Command("node", os.Getenv("FILEBROWSER_INTERNAL_QA_SCRIPT")) command.Env = append(os.Environ(), "QA_APP_ONE="+urls[0], "QA_APP_TWO="+urls[1], "QA_ISSUER="+provider.URL, "QA_CONTROL="+control) out, err := command.CombinedOutput() if err != nil { t.Fatalf("%v\n%s", err, out) } t.Log(string(out)) }