Files
Warp/server/internal/appservice/registry.go
T
tajniak81andClaude Opus 5.5 a94794b866
CI / Test (ubuntu-latest) (push) Canceled after 0s
CI / Test (windows-latest) (push) Canceled after 0s
CI / Storage tests on PostgreSQL (push) Canceled after 0s
CI / Complement (federation off) (push) Canceled after 0s
CI / golangci-lint (push) Canceled after 0s
CI / Smoke test the binary (push) Canceled after 0s
Phase 9: application services and admin completion
Implements PLAN.md Phase 9. internal/appservice reads registration files
(appservice.config_files), matches namespaces the way Synapse does
(anchored at the start), and delivers events to each service as
transactions read from the roomserver's event stream at a cursor kept in
appservice_state. A transaction in flight is recorded, so after a restart
it is resent under the same ID; failures back off from 2s to 5m, and a
successful ping cuts the backoff short.

In the Client-Server API an as_token acts as the service's sender or, with
?user_id=, as any existing user in its namespace. Services register and
log in their users with m.login.application_service; exclusive user and
alias namespaces are refused to everybody else with M_EXCLUSIVE. Unknown
aliases and users in a namespace are asked of the service first. The
ping endpoint reports M_URL_NOT_SET, M_BAD_STATUS, M_CONNECTION_FAILED
and M_CONNECTION_TIMEOUT. Services' own requests, and their users' when
rate_limited is false, skip the rate limiter.

Users can change their password (POST /account/password) and deactivate
their account (POST /account/deactivate), both behind the password.
Deactivation revokes tokens, deletes devices, optionally erases the
profile and leaves every room through a new userapi.AccountObserver.

The admin API is complete: user detail, the admin flag, deactivation and
reactivation, password resets, devices, media quotas
(media.max_user_storage_bytes plus per-user overrides), registration
tokens with use limits and expiry (registration.require_token), server
notices (internal/servernotices, server_notices.*), an editor for the
federation allow/deny lists persisted in server_settings, a delivery view
of the application services, and an audit log of every change.

Migration 0011 adds the registration token, server notice room, media
quota, audit log and appservice cursor tables. Policy lists are now
returned sorted. The Complement entrypoint loads the registrations
Complement mounts in /complement/appservice.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-17 12:58:28 +02:00

181 lines
5.2 KiB
Go

package appservice
import (
"crypto/sha256"
"fmt"
)
// Service is one registered application service.
type Service struct {
*Registration
// sender is the full user ID of SenderLocalpart.
sender string
}
// SenderUserID is the service's own user.
func (s *Service) SenderUserID() string { return s.sender }
// OwnsUser reports whether the service may act as a user: its own sender,
// or anybody in its user namespaces, exclusive or not.
func (s *Service) OwnsUser(userID string) bool {
return userID == s.sender || matchesAny(s.Namespaces.Users, userID)
}
// ClaimsUserExclusively reports whether a user ID is in one of the
// service's exclusive user namespaces, which nobody else may register.
func (s *Service) ClaimsUserExclusively(userID string) bool {
return matchesExclusive(s.Namespaces.Users, userID)
}
// OwnsAlias reports whether an alias is in the service's alias
// namespaces.
func (s *Service) OwnsAlias(alias string) bool { return matchesAny(s.Namespaces.Aliases, alias) }
// ClaimsAliasExclusively reports whether an alias is in an exclusive alias
// namespace of the service.
func (s *Service) ClaimsAliasExclusively(alias string) bool {
return matchesExclusive(s.Namespaces.Aliases, alias)
}
// InterestedInUser reports whether events by or about a user concern the
// service.
func (s *Service) InterestedInUser(userID string) bool { return s.OwnsUser(userID) }
// InterestedInRoom reports whether a room ID is in the service's room
// namespaces.
func (s *Service) InterestedInRoom(roomID string) bool { return matchesAny(s.Namespaces.Rooms, roomID) }
func matchesAny(list []Namespace, id string) bool {
for _, ns := range list {
if ns.Matches(id) {
return true
}
}
return false
}
func matchesExclusive(list []Namespace, id string) bool {
for _, ns := range list {
if ns.Exclusive && ns.Matches(id) {
return true
}
}
return false
}
// Registry is every application service this server knows, looked up by
// ID and by token.
type Registry struct {
services []*Service
byID map[string]*Service
// byToken is keyed by the SHA-256 of the as_token, not the token: a
// map lookup's timing depends on the key, and the key an attacker
// controls should not be the secret itself.
byToken map[[sha256.Size]byte]*Service
}
// NewRegistry builds a registry, refusing two services with one ID or one
// token, or a token shared with another service's hs_token.
func NewRegistry(serverName string, regs []*Registration) (*Registry, error) {
r := &Registry{
byID: map[string]*Service{},
byToken: map[[sha256.Size]byte]*Service{},
}
hsTokens := map[string]string{}
for _, reg := range regs {
hsTokens[reg.HSToken] = reg.ID
}
for _, reg := range regs {
if _, dup := r.byID[reg.ID]; dup {
return nil, fmt.Errorf("application service %q is registered twice", reg.ID)
}
key := sha256.Sum256([]byte(reg.ASToken))
if other, dup := r.byToken[key]; dup {
return nil, fmt.Errorf("application services %q and %q have the same as_token", other.ID, reg.ID)
}
if other, ok := hsTokens[reg.ASToken]; ok {
return nil, fmt.Errorf("the as_token of application service %q is the hs_token of %q", reg.ID, other)
}
svc := &Service{Registration: reg, sender: "@" + reg.SenderLocalpart + ":" + serverName}
r.services = append(r.services, svc)
r.byID[reg.ID] = svc
r.byToken[key] = svc
}
return r, nil
}
// Services lists every service, in the order they were registered.
func (r *Registry) Services() []*Service {
if r == nil {
return nil
}
return r.services
}
// Empty reports whether no service is registered.
func (r *Registry) Empty() bool { return r == nil || len(r.services) == 0 }
// ByID returns a service by ID, or nil.
func (r *Registry) ByID(id string) *Service {
if r == nil {
return nil
}
return r.byID[id]
}
// ByToken returns the service an as_token belongs to, or nil.
func (r *Registry) ByToken(token string) *Service {
if r == nil || token == "" {
return nil
}
return r.byToken[sha256.Sum256([]byte(token))]
}
// ExclusiveUserOwner returns the service that claims a user ID
// exclusively, or nil. The sender of a service counts as claimed by it.
func (r *Registry) ExclusiveUserOwner(userID string) *Service {
for _, svc := range r.Services() {
if svc.sender == userID || svc.ClaimsUserExclusively(userID) {
return svc
}
}
return nil
}
// ExclusiveAliasOwner returns the service that claims an alias
// exclusively, or nil.
func (r *Registry) ExclusiveAliasOwner(alias string) *Service {
for _, svc := range r.Services() {
if svc.ClaimsAliasExclusively(alias) {
return svc
}
}
return nil
}
// UserOwners lists the services with a URL whose user namespaces match a
// user: the ones to ask whether it exists.
func (r *Registry) UserOwners(userID string) []*Service {
var out []*Service
for _, svc := range r.Services() {
if svc.URL != "" && matchesAny(svc.Namespaces.Users, userID) {
out = append(out, svc)
}
}
return out
}
// AliasOwners lists the services with a URL whose alias namespaces match
// an alias.
func (r *Registry) AliasOwners(alias string) []*Service {
var out []*Service
for _, svc := range r.Services() {
if svc.URL != "" && svc.OwnsAlias(alias) {
out = append(out, svc)
}
}
return out
}