Capture accumulated application, UI, CLI, regression tests and design documentation changes, including task project mapping and default CalDAV list naming. Keep deployment data, backups, local credentials and build artifacts out of Git. Move the Compose enrollment token to an ignored local environment file.
89 lines
3.2 KiB
Go
89 lines
3.2 KiB
Go
package server
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"pageswiki/internal/db"
|
|
"pageswiki/internal/store"
|
|
)
|
|
|
|
func TestObjectAndRevisionAuthorizationFollowReadableBindings(t *testing.T) {
|
|
database, err := db.Open(t.TempDir() + "/endpoint-target.db")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { _ = database.Close() })
|
|
if err := db.Migrate(database); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
st := store.New(database)
|
|
if err := st.EnsureConfigTable(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
domain, err := st.GetDefaultDomain()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
principal, err := st.BootstrapOwner(domain.UUID, "scoped", "correct horse battery staple", "Scoped", "")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var roleUUID string
|
|
if err := database.QueryRow(`SELECT b.role_uuid FROM auth_role_bindings b JOIN domain_memberships m ON m.uuid=b.membership_uuid WHERE m.domain_uuid=? AND m.principal_uuid=? LIMIT 1`, domain.UUID, principal.UUID).Scan(&roleUUID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := database.Exec(`DELETE FROM auth_capability_grants WHERE role_uuid=?`, roleUUID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := database.Exec(`INSERT INTO auth_capability_grants(uuid,role_uuid,capability,target_type,target_id,created_at_ms) VALUES('scoped-read',?,'read','path','/allowed',1)`, roleUUID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
allowedObject, err := st.CreatePageAtPathInDomain(domain.UUID, "/allowed")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
allowedRevision, err := st.SaveRevision(allowedObject, "allowed", "test")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
deniedObject, err := st.CreatePageAtPathInDomain(domain.UUID, "/denied")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
server := &Server{store: st}
|
|
if err := server.authorizePrincipalObject(domain.UUID, principal.UUID, "read", allowedObject); err != nil {
|
|
t.Fatalf("object through readable binding: %v", err)
|
|
}
|
|
if err := server.authorizePrincipalRevision(domain.UUID, principal.UUID, "read", allowedRevision); err != nil {
|
|
t.Fatalf("revision through readable binding: %v", err)
|
|
}
|
|
if err := server.authorizePrincipalObject(domain.UUID, principal.UUID, "read", deniedObject); !errors.Is(err, store.ErrAccessDenied) {
|
|
t.Fatalf("unreadable object authorization = %v", err)
|
|
}
|
|
|
|
if _, err := st.CreatePageAtPathInDomain(domain.UUID, "/allowed/child"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
token, err := st.CreateAuthSession(domain.UUID, principal.UUID)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := st.SetConfigInDomain(domain.UUID, "authorization_mode", "enforcing"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
request := httptest.NewRequest(http.MethodPost, "http://localhost/_/api/v1/path/copy", nil)
|
|
request.Header.Set("Authorization", "Bearer "+token)
|
|
if err := server.authorizePathTreeForRequest(request, "read", "/allowed", true); !errors.Is(err, store.ErrAccessDenied) {
|
|
t.Fatalf("recursive operation crossed an ungranted child: %v", err)
|
|
}
|
|
if _, err := database.Exec(`INSERT INTO auth_capability_grants(uuid,role_uuid,capability,target_type,target_id,created_at_ms) VALUES('scoped-child-read',?,'read','path','/allowed/child',1)`, roleUUID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := server.authorizePathTreeForRequest(request, "read", "/allowed", true); err != nil {
|
|
t.Fatalf("fully granted recursive operation: %v", err)
|
|
}
|
|
}
|