2017-12-19 17:00:44 +00:00
|
|
|
// Copyright 2017 Andrew Morgan <andrew@amorgan.xyz>
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2018-02-08 11:02:48 +00:00
|
|
|
"fmt"
|
2017-12-19 17:00:44 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"path/filepath"
|
2018-02-08 11:02:48 +00:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
2017-12-19 17:00:44 +00:00
|
|
|
|
2018-06-18 09:43:15 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2018-05-30 12:43:13 +00:00
|
|
|
yaml "gopkg.in/yaml.v2"
|
2017-12-19 17:00:44 +00:00
|
|
|
)
|
|
|
|
|
2020-08-10 13:18:04 +00:00
|
|
|
type AppServiceAPI struct {
|
|
|
|
Matrix *Global `yaml:"-"`
|
|
|
|
Derived *Derived `yaml:"-"` // TODO: Nuke Derived from orbit
|
|
|
|
|
2020-08-13 11:16:37 +00:00
|
|
|
InternalAPI InternalAPIOptions `yaml:"internal_api"`
|
2020-08-10 13:18:04 +00:00
|
|
|
|
|
|
|
Database DatabaseOptions `yaml:"database"`
|
|
|
|
|
2021-03-05 10:40:27 +00:00
|
|
|
// DisableTLSValidation disables the validation of X.509 TLS certs
|
|
|
|
// on appservice endpoints. This is not recommended in production!
|
|
|
|
DisableTLSValidation bool `yaml:"disable_tls_validation"`
|
|
|
|
|
2020-08-10 13:18:04 +00:00
|
|
|
ConfigFiles []string `yaml:"config_files"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *AppServiceAPI) Defaults() {
|
2020-08-13 11:16:37 +00:00
|
|
|
c.InternalAPI.Listen = "http://localhost:7777"
|
|
|
|
c.InternalAPI.Connect = "http://localhost:7777"
|
2021-03-08 14:50:37 +00:00
|
|
|
c.Database.Defaults(5)
|
2020-08-10 13:18:04 +00:00
|
|
|
c.Database.ConnectionString = "file:appservice.db"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *AppServiceAPI) Verify(configErrs *ConfigErrors, isMonolith bool) {
|
2020-08-13 11:16:37 +00:00
|
|
|
checkURL(configErrs, "app_service_api.internal_api.listen", string(c.InternalAPI.Listen))
|
|
|
|
checkURL(configErrs, "app_service_api.internal_api.bind", string(c.InternalAPI.Connect))
|
2020-08-10 13:18:04 +00:00
|
|
|
checkNotEmpty(configErrs, "app_service_api.database.connection_string", string(c.Database.ConnectionString))
|
|
|
|
}
|
|
|
|
|
2017-12-19 17:00:44 +00:00
|
|
|
// ApplicationServiceNamespace is the namespace that a specific application
|
|
|
|
// service has management over.
|
|
|
|
type ApplicationServiceNamespace struct {
|
|
|
|
// Whether or not the namespace is managed solely by this application service
|
|
|
|
Exclusive bool `yaml:"exclusive"`
|
|
|
|
// A regex pattern that represents the namespace
|
|
|
|
Regex string `yaml:"regex"`
|
2018-06-18 09:43:15 +00:00
|
|
|
// The ID of an existing group that all users of this application service will
|
|
|
|
// be added to. This field is only relevant to the `users` namespace.
|
|
|
|
// Note that users who are joined to this group through an application service
|
|
|
|
// are not to be listed when querying for the group's members, however the
|
|
|
|
// group should be listed when querying an application service user's groups.
|
|
|
|
// This is to prevent making spamming all users of an application service
|
|
|
|
// trivial.
|
|
|
|
GroupID string `yaml:"group_id"`
|
2018-02-08 11:02:48 +00:00
|
|
|
// Regex object representing our pattern. Saves having to recompile every time
|
|
|
|
RegexpObject *regexp.Regexp
|
2017-12-19 17:00:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ApplicationService represents a Matrix application service.
|
|
|
|
// https://matrix.org/docs/spec/application_service/unstable.html
|
|
|
|
type ApplicationService struct {
|
|
|
|
// User-defined, unique, persistent ID of the application service
|
|
|
|
ID string `yaml:"id"`
|
|
|
|
// Base URL of the application service
|
|
|
|
URL string `yaml:"url"`
|
|
|
|
// Application service token provided in requests to a homeserver
|
|
|
|
ASToken string `yaml:"as_token"`
|
|
|
|
// Homeserver token provided in requests to an application service
|
|
|
|
HSToken string `yaml:"hs_token"`
|
|
|
|
// Localpart of application service user
|
|
|
|
SenderLocalpart string `yaml:"sender_localpart"`
|
2018-05-30 12:43:13 +00:00
|
|
|
// Information about an application service's namespaces. Key is either
|
|
|
|
// "users", "aliases" or "rooms"
|
2018-02-08 11:02:48 +00:00
|
|
|
NamespaceMap map[string][]ApplicationServiceNamespace `yaml:"namespaces"`
|
2018-06-18 09:43:15 +00:00
|
|
|
// Whether rate limiting is applied to each application service user
|
|
|
|
RateLimited bool `yaml:"rate_limited"`
|
|
|
|
// Any custom protocols that this application service provides (e.g. IRC)
|
|
|
|
Protocols []string `yaml:"protocols"`
|
2017-12-19 17:00:44 +00:00
|
|
|
}
|
|
|
|
|
2018-07-16 12:30:04 +00:00
|
|
|
// IsInterestedInRoomID returns a bool on whether an application service's
|
|
|
|
// namespace includes the given room ID
|
|
|
|
func (a *ApplicationService) IsInterestedInRoomID(
|
|
|
|
roomID string,
|
|
|
|
) bool {
|
|
|
|
if namespaceSlice, ok := a.NamespaceMap["rooms"]; ok {
|
|
|
|
for _, namespace := range namespaceSlice {
|
2021-06-07 08:13:40 +00:00
|
|
|
if namespace.RegexpObject != nil && namespace.RegexpObject.MatchString(roomID) {
|
2018-07-16 12:30:04 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsInterestedInUserID returns a bool on whether an application service's
|
|
|
|
// namespace includes the given user ID
|
|
|
|
func (a *ApplicationService) IsInterestedInUserID(
|
|
|
|
userID string,
|
|
|
|
) bool {
|
|
|
|
if namespaceSlice, ok := a.NamespaceMap["users"]; ok {
|
|
|
|
for _, namespace := range namespaceSlice {
|
|
|
|
if namespace.RegexpObject.MatchString(userID) {
|
2020-04-14 14:31:27 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// OwnsNamespaceCoveringUserId returns a bool on whether an application service's
|
|
|
|
// namespace is exclusive and includes the given user ID
|
|
|
|
func (a *ApplicationService) OwnsNamespaceCoveringUserId(
|
|
|
|
userID string,
|
|
|
|
) bool {
|
|
|
|
if namespaceSlice, ok := a.NamespaceMap["users"]; ok {
|
|
|
|
for _, namespace := range namespaceSlice {
|
|
|
|
if namespace.Exclusive && namespace.RegexpObject.MatchString(userID) {
|
2018-07-16 12:30:04 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsInterestedInRoomAlias returns a bool on whether an application service's
|
|
|
|
// namespace includes the given room alias
|
|
|
|
func (a *ApplicationService) IsInterestedInRoomAlias(
|
|
|
|
roomAlias string,
|
|
|
|
) bool {
|
|
|
|
if namespaceSlice, ok := a.NamespaceMap["aliases"]; ok {
|
|
|
|
for _, namespace := range namespaceSlice {
|
|
|
|
if namespace.RegexpObject.MatchString(roomAlias) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-07-17 14:36:04 +00:00
|
|
|
// loadAppServices iterates through all application service config files
|
2018-02-08 11:02:48 +00:00
|
|
|
// and loads their data into the config object for later access.
|
2020-08-10 13:18:04 +00:00
|
|
|
func loadAppServices(config *AppServiceAPI, derived *Derived) error {
|
|
|
|
for _, configPath := range config.ConfigFiles {
|
2018-06-18 09:43:15 +00:00
|
|
|
// Create a new application service with default options
|
|
|
|
appservice := ApplicationService{
|
|
|
|
RateLimited: true,
|
|
|
|
}
|
2017-12-19 17:00:44 +00:00
|
|
|
|
|
|
|
// Create an absolute path from a potentially relative path
|
|
|
|
absPath, err := filepath.Abs(configPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the application service's config file
|
|
|
|
configData, err := ioutil.ReadFile(absPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load the config data into our struct
|
|
|
|
if err = yaml.UnmarshalStrict(configData, &appservice); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append the parsed application service to the global config
|
2020-08-10 13:18:04 +00:00
|
|
|
derived.ApplicationServices = append(
|
|
|
|
derived.ApplicationServices, appservice,
|
2018-05-30 12:43:13 +00:00
|
|
|
)
|
2017-12-19 17:00:44 +00:00
|
|
|
}
|
|
|
|
|
2018-02-08 11:02:48 +00:00
|
|
|
// Check for any errors in the loaded application services
|
2020-08-10 13:18:04 +00:00
|
|
|
return checkErrors(config, derived)
|
2018-02-08 11:02:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// setupRegexps will create regex objects for exclusive and non-exclusive
|
|
|
|
// usernames, aliases and rooms of all application services, so that other
|
|
|
|
// methods can quickly check if a particular string matches any of them.
|
2021-03-05 14:57:42 +00:00
|
|
|
func setupRegexps(asAPI *AppServiceAPI, derived *Derived) (err error) {
|
2018-02-08 11:02:48 +00:00
|
|
|
// Combine all exclusive namespaces for later string checking
|
2018-05-30 12:43:13 +00:00
|
|
|
var exclusiveUsernameStrings, exclusiveAliasStrings []string
|
2018-02-08 11:02:48 +00:00
|
|
|
|
|
|
|
// If an application service's regex is marked as exclusive, add
|
2018-05-30 12:43:13 +00:00
|
|
|
// its contents to the overall exlusive regex string. Room regex
|
|
|
|
// not necessary as we aren't denying exclusive room ID creation
|
2020-08-10 13:18:04 +00:00
|
|
|
for _, appservice := range derived.ApplicationServices {
|
2021-03-05 14:57:42 +00:00
|
|
|
// The sender_localpart can be considered an exclusive regex for a single user, so let's do that
|
|
|
|
// to simplify the code
|
|
|
|
var senderUserIDSlice = []string{fmt.Sprintf("@%s:%s", appservice.SenderLocalpart, asAPI.Matrix.ServerName)}
|
|
|
|
usersSlice, found := appservice.NamespaceMap["users"]
|
|
|
|
if !found {
|
|
|
|
usersSlice = []ApplicationServiceNamespace{}
|
|
|
|
appservice.NamespaceMap["users"] = usersSlice
|
|
|
|
}
|
|
|
|
appendExclusiveNamespaceRegexs(&senderUserIDSlice, usersSlice)
|
|
|
|
|
2018-02-08 11:02:48 +00:00
|
|
|
for key, namespaceSlice := range appservice.NamespaceMap {
|
|
|
|
switch key {
|
|
|
|
case "users":
|
|
|
|
appendExclusiveNamespaceRegexs(&exclusiveUsernameStrings, namespaceSlice)
|
|
|
|
case "aliases":
|
|
|
|
appendExclusiveNamespaceRegexs(&exclusiveAliasStrings, namespaceSlice)
|
|
|
|
}
|
2021-06-07 08:13:40 +00:00
|
|
|
|
|
|
|
if err = compileNamespaceRegexes(namespaceSlice); err != nil {
|
|
|
|
return fmt.Errorf("invalid regex in appservice %q, namespace %q: %w", appservice.ID, key, err)
|
|
|
|
}
|
2018-02-08 11:02:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Join the regexes together into one big regex.
|
|
|
|
// i.e. "app1.*", "app2.*" -> "(app1.*)|(app2.*)"
|
2018-05-30 12:43:13 +00:00
|
|
|
// Later we can check if a username or alias matches any exclusive regex and
|
|
|
|
// deny access if it isn't from an application service
|
2018-02-08 11:02:48 +00:00
|
|
|
exclusiveUsernames := strings.Join(exclusiveUsernameStrings, "|")
|
2018-05-30 12:43:13 +00:00
|
|
|
exclusiveAliases := strings.Join(exclusiveAliasStrings, "|")
|
2018-02-08 11:02:48 +00:00
|
|
|
|
2018-05-30 12:43:13 +00:00
|
|
|
// If there are no exclusive regexes, compile string so that it will not match
|
|
|
|
// any valid usernames/aliases/roomIDs
|
2018-02-27 11:42:10 +00:00
|
|
|
if exclusiveUsernames == "" {
|
|
|
|
exclusiveUsernames = "^$"
|
|
|
|
}
|
2018-05-30 12:43:13 +00:00
|
|
|
if exclusiveAliases == "" {
|
|
|
|
exclusiveAliases = "^$"
|
|
|
|
}
|
2018-02-27 11:42:10 +00:00
|
|
|
|
2018-05-30 12:43:13 +00:00
|
|
|
// Store compiled Regex
|
2020-08-10 13:18:04 +00:00
|
|
|
if derived.ExclusiveApplicationServicesUsernameRegexp, err = regexp.Compile(exclusiveUsernames); err != nil {
|
2018-05-30 12:43:13 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-08-10 13:18:04 +00:00
|
|
|
if derived.ExclusiveApplicationServicesAliasRegexp, err = regexp.Compile(exclusiveAliases); err != nil {
|
2018-05-30 12:43:13 +00:00
|
|
|
return err
|
|
|
|
}
|
2018-02-08 11:02:48 +00:00
|
|
|
|
2018-05-30 12:43:13 +00:00
|
|
|
return nil
|
2018-02-08 11:02:48 +00:00
|
|
|
}
|
|
|
|
|
2018-05-30 12:43:13 +00:00
|
|
|
// appendExclusiveNamespaceRegexs takes a slice of strings and a slice of
|
2018-02-08 11:02:48 +00:00
|
|
|
// namespaces and will append the regexes of only the exclusive namespaces
|
|
|
|
// into the string slice
|
|
|
|
func appendExclusiveNamespaceRegexs(
|
|
|
|
exclusiveStrings *[]string, namespaces []ApplicationServiceNamespace,
|
|
|
|
) {
|
2021-06-07 08:13:40 +00:00
|
|
|
for _, namespace := range namespaces {
|
2018-02-08 11:02:48 +00:00
|
|
|
if namespace.Exclusive {
|
|
|
|
// We append parenthesis to later separate each regex when we compile
|
|
|
|
// i.e. "app1.*", "app2.*" -> "(app1.*)|(app2.*)"
|
|
|
|
*exclusiveStrings = append(*exclusiveStrings, "("+namespace.Regex+")")
|
|
|
|
}
|
2021-06-07 08:13:40 +00:00
|
|
|
}
|
|
|
|
}
|
2018-02-08 11:02:48 +00:00
|
|
|
|
2021-06-07 08:13:40 +00:00
|
|
|
// compileNamespaceRegexes turns strings into regex objects and complains
|
|
|
|
// if some of there are bad
|
|
|
|
func compileNamespaceRegexes(namespaces []ApplicationServiceNamespace) (err error) {
|
|
|
|
for index, namespace := range namespaces {
|
2018-02-08 11:02:48 +00:00
|
|
|
// Compile this regex into a Regexp object for later use
|
2021-06-07 08:13:40 +00:00
|
|
|
r, err := regexp.Compile(namespace.Regex)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("regex at namespace %d: %w", index, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
namespaces[index].RegexpObject = r
|
2018-02-08 11:02:48 +00:00
|
|
|
}
|
2021-06-07 08:13:40 +00:00
|
|
|
|
|
|
|
return nil
|
2018-02-08 11:02:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// checkErrors checks for any configuration errors amongst the loaded
|
|
|
|
// application services according to the application service spec.
|
2020-08-10 13:18:04 +00:00
|
|
|
func checkErrors(config *AppServiceAPI, derived *Derived) (err error) {
|
2018-02-08 11:02:48 +00:00
|
|
|
var idMap = make(map[string]bool)
|
|
|
|
var tokenMap = make(map[string]bool)
|
|
|
|
|
2018-06-18 09:43:15 +00:00
|
|
|
// Compile regexp object for checking groupIDs
|
|
|
|
groupIDRegexp := regexp.MustCompile(`\+.*:.*`)
|
|
|
|
|
2018-05-30 12:43:13 +00:00
|
|
|
// Check each application service for any config errors
|
2020-08-10 13:18:04 +00:00
|
|
|
for _, appservice := range derived.ApplicationServices {
|
2018-06-18 09:43:15 +00:00
|
|
|
// Namespace-related checks
|
|
|
|
for key, namespaceSlice := range appservice.NamespaceMap {
|
|
|
|
for _, namespace := range namespaceSlice {
|
|
|
|
if err := validateNamespace(&appservice, key, &namespace, groupIDRegexp); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-16 15:16:01 +00:00
|
|
|
// Check if the url has trailing /'s. If so, remove them
|
|
|
|
appservice.URL = strings.TrimRight(appservice.URL, "/")
|
|
|
|
|
2018-05-30 12:43:13 +00:00
|
|
|
// Check if we've already seen this ID. No two application services
|
|
|
|
// can have the same ID or token.
|
2018-02-08 11:02:48 +00:00
|
|
|
if idMap[appservice.ID] {
|
2020-08-10 13:18:04 +00:00
|
|
|
return ConfigErrors([]string{fmt.Sprintf(
|
2018-07-05 16:34:59 +00:00
|
|
|
"Application service ID %s must be unique", appservice.ID,
|
2018-04-10 12:21:20 +00:00
|
|
|
)})
|
2018-02-08 11:02:48 +00:00
|
|
|
}
|
2018-05-30 12:43:13 +00:00
|
|
|
// Check if we've already seen this token
|
2018-02-08 11:02:48 +00:00
|
|
|
if tokenMap[appservice.ASToken] {
|
2020-08-10 13:18:04 +00:00
|
|
|
return ConfigErrors([]string{fmt.Sprintf(
|
2018-07-05 16:34:59 +00:00
|
|
|
"Application service Token %s must be unique", appservice.ASToken,
|
2018-04-10 12:21:20 +00:00
|
|
|
)})
|
2018-02-08 11:02:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add the id/token to their respective maps if we haven't already
|
|
|
|
// seen them.
|
|
|
|
idMap[appservice.ID] = true
|
2018-06-01 15:34:52 +00:00
|
|
|
tokenMap[appservice.ASToken] = true
|
2018-05-30 12:43:13 +00:00
|
|
|
|
2018-06-18 09:43:15 +00:00
|
|
|
// TODO: Remove once rate_limited is implemented
|
|
|
|
if appservice.RateLimited {
|
|
|
|
log.Warn("WARNING: Application service option rate_limited is currently unimplemented")
|
|
|
|
}
|
|
|
|
// TODO: Remove once protocols is implemented
|
|
|
|
if len(appservice.Protocols) > 0 {
|
|
|
|
log.Warn("WARNING: Application service option protocols is currently unimplemented")
|
|
|
|
}
|
2018-02-08 11:02:48 +00:00
|
|
|
}
|
|
|
|
|
2020-08-10 13:18:04 +00:00
|
|
|
return setupRegexps(config, derived)
|
2018-06-18 09:43:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// validateNamespace returns nil or an error based on whether a given
|
|
|
|
// application service namespace is valid. A namespace is valid if it has the
|
|
|
|
// required fields, and its regex is correct.
|
|
|
|
func validateNamespace(
|
|
|
|
appservice *ApplicationService,
|
|
|
|
key string,
|
|
|
|
namespace *ApplicationServiceNamespace,
|
|
|
|
groupIDRegexp *regexp.Regexp,
|
|
|
|
) error {
|
2018-02-08 11:02:48 +00:00
|
|
|
// Check that namespace(s) are valid regex
|
2018-06-18 09:43:15 +00:00
|
|
|
if !IsValidRegex(namespace.Regex) {
|
2020-08-10 13:18:04 +00:00
|
|
|
return ConfigErrors([]string{fmt.Sprintf(
|
2018-06-18 09:43:15 +00:00
|
|
|
"Invalid regex string for Application Service %s", appservice.ID,
|
|
|
|
)})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if GroupID for the users namespace is in the correct format
|
|
|
|
if key == "users" && namespace.GroupID != "" {
|
|
|
|
// TODO: Remove once group_id is implemented
|
|
|
|
log.Warn("WARNING: Application service option group_id is currently unimplemented")
|
|
|
|
|
|
|
|
correctFormat := groupIDRegexp.MatchString(namespace.GroupID)
|
|
|
|
if !correctFormat {
|
2020-08-10 13:18:04 +00:00
|
|
|
return ConfigErrors([]string{fmt.Sprintf(
|
2018-06-18 09:43:15 +00:00
|
|
|
"Invalid user group_id field for application service %s.",
|
|
|
|
appservice.ID,
|
|
|
|
)})
|
2018-02-08 11:02:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-18 09:43:15 +00:00
|
|
|
return nil
|
2017-12-19 17:00:44 +00:00
|
|
|
}
|
2018-02-08 11:02:48 +00:00
|
|
|
|
|
|
|
// IsValidRegex returns true or false based on whether the
|
|
|
|
// given string is valid regex or not
|
|
|
|
func IsValidRegex(regexString string) bool {
|
|
|
|
_, err := regexp.Compile(regexString)
|
|
|
|
|
|
|
|
return err == nil
|
|
|
|
}
|