285 lines
10 KiB
Go
285 lines
10 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net/http"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"pageswiki/internal/store"
|
|
)
|
|
|
|
func (s *Server) authorizeResolvedEndpoint(r *http.Request, principal *store.Principal, policy endpointPolicy) (bool, error) {
|
|
domainUUID := s.domainUUIDForRequest(r)
|
|
switch policy.Resolver {
|
|
case resolveNone:
|
|
return false, nil
|
|
case resolveHandler:
|
|
return true, nil
|
|
case resolveRaw:
|
|
address := strings.TrimPrefix(r.URL.Path, "/v/raw/")
|
|
switch {
|
|
case strings.HasPrefix(address, "path:"):
|
|
return true, s.authorizePrincipalPath(domainUUID, principal.UUID, policy.Capability, strings.TrimPrefix(address, "path:"))
|
|
case strings.HasPrefix(address, "uuid:"):
|
|
return true, s.authorizePrincipalObject(domainUUID, principal.UUID, policy.Capability, strings.TrimPrefix(address, "uuid:"))
|
|
case strings.HasPrefix(address, "rev:"):
|
|
return true, s.authorizePrincipalRevision(domainUUID, principal.UUID, policy.Capability, strings.TrimPrefix(address, "rev:"))
|
|
case strings.HasPrefix(address, "asset:"):
|
|
// Asset HEAD authorization is address-aware and remains in its handler.
|
|
return true, nil
|
|
default:
|
|
return true, store.ErrAccessDenied
|
|
}
|
|
case resolveText:
|
|
address := strings.TrimPrefix(r.URL.Path, "/v/text/")
|
|
if !strings.HasPrefix(address, "rev:") {
|
|
return true, store.ErrAccessDenied
|
|
}
|
|
return true, s.authorizePrincipalRevision(domainUUID, principal.UUID, policy.Capability, strings.TrimPrefix(address, "rev:"))
|
|
case resolvePDF:
|
|
return true, s.authorizePrincipalRevision(domainUUID, principal.UUID, policy.Capability, r.URL.Query().Get("rev"))
|
|
case resolvePreview:
|
|
name := filepath.Base(strings.TrimPrefix(r.URL.Path, "/v/preview-cache/"))
|
|
hash := strings.TrimSuffix(strings.TrimSuffix(name, ".jpg"), ".svg")
|
|
return true, s.authorizePrincipalDocument(domainUUID, principal.UUID, policy.Capability, hash)
|
|
case resolveQueryPath:
|
|
return true, s.authorizePrincipalPath(domainUUID, principal.UUID, policy.Capability, r.URL.Query().Get("path"))
|
|
case resolveViewPath:
|
|
return true, s.authorizePrincipalPath(domainUUID, principal.UUID, policy.Capability, semanticViewPath(r.URL.Path))
|
|
case resolvePrint:
|
|
address := strings.TrimPrefix(r.URL.Path, "/_/view/print/")
|
|
return true, s.authorizePrincipalAddress(domainUUID, principal.UUID, policy.Capability, address)
|
|
case resolveObjectURL:
|
|
return true, s.authorizePrincipalObject(domainUUID, principal.UUID, policy.Capability, objectIDFromRoute(r.URL.Path))
|
|
case resolveRevision:
|
|
return true, s.authorizePrincipalRevision(domainUUID, principal.UUID, policy.Capability, strings.TrimPrefix(r.URL.Path, "/r/view/"))
|
|
case resolveDiff:
|
|
parts := strings.SplitN(strings.TrimPrefix(r.URL.Path, "/r/diff/"), "/", 2)
|
|
if len(parts) != 2 {
|
|
return true, store.ErrAccessDenied
|
|
}
|
|
if err := s.authorizePrincipalRevision(domainUUID, principal.UUID, policy.Capability, parts[0]); err != nil {
|
|
return true, err
|
|
}
|
|
return true, s.authorizePrincipalRevision(domainUUID, principal.UUID, policy.Capability, parts[1])
|
|
default:
|
|
return true, store.ErrAccessDenied
|
|
}
|
|
}
|
|
|
|
func (s *Server) authorizePrincipalAddress(domainUUID, principalUUID, capability, address string) error {
|
|
switch {
|
|
case strings.HasPrefix(address, "path:"):
|
|
return s.authorizePrincipalPath(domainUUID, principalUUID, capability, strings.TrimPrefix(address, "path:"))
|
|
case strings.HasPrefix(address, "uuid:"):
|
|
return s.authorizePrincipalObject(domainUUID, principalUUID, capability, strings.TrimPrefix(address, "uuid:"))
|
|
case strings.HasPrefix(address, "rev:"):
|
|
return s.authorizePrincipalRevision(domainUUID, principalUUID, capability, strings.TrimPrefix(address, "rev:"))
|
|
default:
|
|
return store.ErrAccessDenied
|
|
}
|
|
}
|
|
|
|
func (s *Server) requestCanReadPath(r *http.Request, path string) bool {
|
|
return s.pathReadFilter(r)(path)
|
|
}
|
|
|
|
func (s *Server) authorizeObjectForRequest(r *http.Request, capability, objectUUID string) error {
|
|
domainUUID := s.domainUUIDForRequest(r)
|
|
mode, err := s.store.GetConfigInDomain(domainUUID, "authorization_mode")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if mode != "enforcing" {
|
|
return nil
|
|
}
|
|
principal, err := s.store.PrincipalForSession(domainUUID, s.authTokenFromRequest(r))
|
|
if err != nil {
|
|
return store.ErrAccessDenied
|
|
}
|
|
return s.authorizePrincipalObject(domainUUID, principal.UUID, capability, objectUUID)
|
|
}
|
|
|
|
func normalizeAuthorizationPath(value string) string {
|
|
value = strings.TrimSpace(value)
|
|
if value == "" || !strings.HasPrefix(value, "/") {
|
|
return ""
|
|
}
|
|
if value != "/" {
|
|
value = strings.TrimRight(value, "/")
|
|
}
|
|
return value
|
|
}
|
|
|
|
// authorizedMutationSnapshot returns exactly the bindings whose source and
|
|
// destination grants were checked. The mutation must pass this opaque snapshot
|
|
// to the checked store method, never enumerate the tree for a second time.
|
|
func (s *Server) authorizedMutationSnapshot(r *http.Request, sourceCapability, destinationCapability, source, destination string, recursive bool) (*store.PathMutationSnapshot, error) {
|
|
source = normalizeAuthorizationPath(source)
|
|
if source == "" {
|
|
return nil, store.ErrAccessDenied
|
|
}
|
|
if destinationCapability != "" {
|
|
destination = normalizeAuthorizationPath(destination)
|
|
if destination == "" {
|
|
return nil, store.ErrAccessDenied
|
|
}
|
|
}
|
|
domainUUID := s.domainUUIDForRequest(r)
|
|
mode, err := s.store.GetConfigInDomain(domainUUID, "authorization_mode")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var principal *store.Principal
|
|
if mode == "enforcing" {
|
|
principal, err = s.store.PrincipalForSession(domainUUID, s.authTokenFromRequest(r))
|
|
if err != nil {
|
|
return nil, store.ErrAccessDenied
|
|
}
|
|
}
|
|
snapshot, err := s.store.SnapshotPathsInDomain(domainUUID, source, recursive)
|
|
if err != nil {
|
|
// Do not expose the existence/size of a scope whose root is unreadable.
|
|
if principal != nil && (errors.Is(err, store.ErrPathNotFound) || errors.Is(err, store.ErrPathMutationTooLarge)) && s.authorizePrincipalPath(domainUUID, principal.UUID, sourceCapability, source) != nil {
|
|
return nil, store.ErrAccessDenied
|
|
}
|
|
return nil, err
|
|
}
|
|
if principal == nil {
|
|
return snapshot, nil
|
|
}
|
|
for _, sourcePath := range snapshot.Paths() {
|
|
if err := s.authorizePrincipalPath(domainUUID, principal.UUID, sourceCapability, sourcePath); err != nil {
|
|
return nil, err
|
|
}
|
|
if destinationCapability != "" {
|
|
destinationPath := destination + strings.TrimPrefix(sourcePath, snapshot.Root())
|
|
if err := s.authorizePrincipalPath(domainUUID, principal.UUID, destinationCapability, destinationPath); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
}
|
|
return snapshot, nil
|
|
}
|
|
|
|
func (s *Server) authorizePathTreeForRequest(r *http.Request, capability, root string, recursive bool) error {
|
|
_, err := s.authorizedMutationSnapshot(r, capability, "", root, "", recursive)
|
|
return err
|
|
}
|
|
|
|
func (s *Server) authorizePathMoveForRequest(r *http.Request, sourceCapability, destinationCapability, source, destination string, recursive bool) error {
|
|
_, err := s.authorizedMutationSnapshot(r, sourceCapability, destinationCapability, source, destination, recursive)
|
|
return err
|
|
}
|
|
|
|
func (s *Server) authorizePrincipalDocument(domainUUID, principalUUID, capability, hash string) error {
|
|
if hash == "" {
|
|
return store.ErrAccessDenied
|
|
}
|
|
references, err := s.store.DocumentReferencesInDomain(domainUUID, hash)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, reference := range references {
|
|
if s.authorizePrincipalPath(domainUUID, principalUUID, capability, reference.Path) == nil {
|
|
return nil
|
|
}
|
|
}
|
|
return store.ErrAccessDenied
|
|
}
|
|
|
|
func (s *Server) authorizePrincipalPath(domainUUID, principalUUID, capability, path string) error {
|
|
if path == "" || !strings.HasPrefix(path, "/") {
|
|
return store.ErrAccessDenied
|
|
}
|
|
if store.IsUserSpacePath(path) {
|
|
allowed, err := s.store.UserSpaceCanReadAuthenticated(context.Background(), domainUUID, principalUUID, path)
|
|
if err != nil || !allowed {
|
|
return store.ErrAccessDenied
|
|
}
|
|
}
|
|
return s.store.Authorize(store.AuthorizationRequest{DomainUUID: domainUUID, PrincipalUUID: principalUUID, Capability: capability, TargetType: "path", TargetID: path})
|
|
}
|
|
|
|
func (s *Server) authorizePrincipalObject(domainUUID, principalUUID, capability, objectUUID string) error {
|
|
if objectUUID == "" {
|
|
return store.ErrAccessDenied
|
|
}
|
|
request := store.AuthorizationRequest{DomainUUID: domainUUID, PrincipalUUID: principalUUID, Capability: capability, TargetType: "object", TargetID: objectUUID}
|
|
inScope, err := s.store.ObjectInDomainAuthorityScope(domainUUID, objectUUID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !inScope {
|
|
return s.store.AuthorizeExactObject(request)
|
|
}
|
|
links, err := s.store.ListPathLinksInDomain(domainUUID, objectUUID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, link := range links {
|
|
if !store.IsUserSpacePath(link.Path) {
|
|
continue
|
|
}
|
|
allowed, audienceErr := s.store.UserSpaceCanReadAuthenticated(context.Background(), domainUUID, principalUUID, link.Path)
|
|
if audienceErr != nil || !allowed {
|
|
return store.ErrAccessDenied
|
|
}
|
|
}
|
|
if err := s.store.CheckObjectLocalityAccessCaps(domainUUID, principalUUID, capability, objectUUID); err != nil {
|
|
return err
|
|
}
|
|
if err := s.store.Authorize(request); err == nil {
|
|
return nil
|
|
} else if !errors.Is(err, store.ErrAccessDenied) {
|
|
return err
|
|
}
|
|
if !s.store.ObjectBoundInDomain(domainUUID, objectUUID) {
|
|
return store.ErrAccessDenied
|
|
}
|
|
for _, link := range links {
|
|
if s.authorizePrincipalPath(domainUUID, principalUUID, capability, link.Path) == nil {
|
|
return nil
|
|
}
|
|
}
|
|
return store.ErrAccessDenied
|
|
}
|
|
|
|
func (s *Server) authorizePrincipalRevision(domainUUID, principalUUID, capability, revisionUUID string) error {
|
|
if revisionUUID == "" {
|
|
return store.ErrAccessDenied
|
|
}
|
|
revision, err := s.store.GetRevisionMetadata(revisionUUID)
|
|
if err != nil {
|
|
return store.ErrAccessDenied
|
|
}
|
|
return s.authorizePrincipalObject(domainUUID, principalUUID, capability, revision.ObjectUUID)
|
|
}
|
|
|
|
func objectIDFromRoute(path string) string {
|
|
for _, prefix := range []string{"/o/e/uuid:", "/o/r/uuid:", "/o/uuid:", "/l/uuid:", "/r/history/"} {
|
|
if strings.HasPrefix(path, prefix) {
|
|
return strings.TrimPrefix(path, prefix)
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func semanticViewPath(route string) string {
|
|
for _, prefix := range []string{"/v/preview", "/v/plain", "/v/markdown", "/v/browser", "/v/slides"} {
|
|
if strings.HasPrefix(route, prefix) {
|
|
path := strings.TrimPrefix(route, prefix)
|
|
if path == "" {
|
|
return "/"
|
|
}
|
|
if !strings.HasPrefix(path, "/") {
|
|
return "/" + path
|
|
}
|
|
return path
|
|
}
|
|
}
|
|
return ""
|
|
}
|