package core
import (
"context"
"encoding/json/v2"
"errors"
"fmt"
"os"
"regexp"
"slices"
"strconv"
"strings"
"sync"
"time"
validation "ganigeorgiev.com/pocketbase/ozzo-validation/v4"
"ganigeorgiev.com/pocketbase/ozzo-validation/v4/is"
"ganigeorgiev.com/pocketbase/pocketbase/core/validators"
"ganigeorgiev.com/pocketbase/pocketbase/tools/cron"
"ganigeorgiev.com/pocketbase/pocketbase/tools/hook"
"ganigeorgiev.com/pocketbase/pocketbase/tools/mailer"
"ganigeorgiev.com/pocketbase/pocketbase/tools/security"
"ganigeorgiev.com/pocketbase/pocketbase/tools/types"
)
const (
paramsTable = "_params"
paramsKeySettings = "settings"
systemHookIdSettings = "__pbSettingsSystemHook__"
)
func (app *BaseApp) registerSettingsHooks() {
saveFunc := func(me *ModelEvent) error {
if err := me.Next(); err != nil {
return err
}
if me.Model.PK() == paramsKeySettings {
return errors.Join(
me.App.Settings().PostScan(),
me.App.ReloadSettings(),
)
}
return nil
}
app.OnModelAfterCreateSuccess(paramsTable).Bind(&hook.Handler[*ModelEvent]{
Id: systemHookIdSettings,
Func: saveFunc,
Priority: -999,
})
app.OnModelAfterUpdateSuccess(paramsTable).Bind(&hook.Handler[*ModelEvent]{
Id: systemHookIdSettings,
Func: saveFunc,
Priority: -999,
})
app.OnModelDelete(paramsTable).Bind(&hook.Handler[*ModelEvent]{
Id: systemHookIdSettings,
Func: func(me *ModelEvent) error {
if me.Model.PK() == paramsKeySettings {
return errors.New("the app params settings cannot be deleted")
}
return me.Next()
},
Priority: -999,
})
app.OnCollectionUpdate().Bind(&hook.Handler[*CollectionEvent]{
Id: systemHookIdSettings,
Func: func(e *CollectionEvent) error {
oldCollection, err := e.App.FindCachedCollectionByNameOrId(e.Collection.Id)
if err != nil {
return fmt.Errorf("failed to retrieve old cached collection: %w", err)
}
err = e.Next()
if err != nil {
return err
}
if oldCollection.Name != e.Collection.Name {
var hasChange bool
rules := e.App.Settings().RateLimits.Rules
for i := 0; i < len(rules); i++ {
if strings.HasPrefix(rules[i].Label, oldCollection.Name+":") {
rules[i].Label = strings.Replace(rules[i].Label, oldCollection.Name+":", e.Collection.Name+":", 1)
hasChange = true
}
}
if hasChange {
e.App.Settings().RateLimits.Rules = rules
err = e.App.Save(e.App.Settings())
if err != nil {
return err
}
}
}
return nil
},
Priority: 99,
})
}
var (
_ Model = (*Settings)(nil)
_ PostValidator = (*Settings)(nil)
_ DBExporter = (*Settings)(nil)
)
type settings struct {
SuperuserIPs []string `form:"superuserIPs" json:"superuserIPs"`
SMTP SMTPConfig `form:"smtp" json:"smtp"`
Backups BackupsConfig `form:"backups" json:"backups"`
S3 S3Config `form:"s3" json:"s3"`
Meta MetaConfig `form:"meta" json:"meta"`
RateLimits RateLimitsConfig `form:"rateLimits" json:"rateLimits"`
TrustedProxy TrustedProxyConfig `form:"trustedProxy" json:"trustedProxy"`
Batch BatchConfig `form:"batch" json:"batch"`
Logs LogsConfig `form:"logs" json:"logs"`
}
type Settings struct {
settings
mu sync.RWMutex
isNew bool
}
func newDefaultSettings() *Settings {
return &Settings{
isNew: true,
settings: settings{
Meta: MetaConfig{
AccentColor: "#1055c9",
AppName: "Acme",
AppURL: "http://localhost:8090",
HideControls: false,
SenderName: "Support",
SenderAddress: "support@example.com",
},
Logs: LogsConfig{
MaxDays: 5,
LogIP: true,
},
SMTP: SMTPConfig{
Enabled: false,
Host: "smtp.example.com",
Port: 587,
Username: "",
Password: "",
TLS: false,
},
Backups: BackupsConfig{
CronMaxKeep: 3,
},
Batch: BatchConfig{
Enabled: false,
MaxRequests: 50,
Timeout: 3,
},
RateLimits: RateLimitsConfig{
Enabled: false,
Rules: []RateLimitRule{
{Label: "*:auth", MaxRequests: 2, Duration: 3},
{Label: "*:create", MaxRequests: 20, Duration: 5},
{Label: "/api/batch", MaxRequests: 3, Duration: 1},
{Label: "/api/", MaxRequests: 300, Duration: 10},
},
},
},
}
}
func (s *Settings) TableName() string {
return paramsTable
}
func (s *Settings) LastSavedPK() any {
return paramsKeySettings
}
func (s *Settings) PK() any {
return paramsKeySettings
}
func (s *Settings) IsNew() bool {
s.mu.RLock()
defer s.mu.RUnlock()
return s.isNew
}
func (s *Settings) MarkAsNew() {
s.mu.Lock()
defer s.mu.Unlock()
s.isNew = true
}
func (s *Settings) MarkAsNotNew() {
s.mu.Lock()
defer s.mu.Unlock()
s.isNew = false
}
func (s *Settings) PostScan() error {
s.MarkAsNotNew()
return nil
}
func (s *Settings) String() string {
s.mu.RLock()
defer s.mu.RUnlock()
raw, _ := s.MarshalJSON()
return string(raw)
}
func (s *Settings) DBExport(app App) (map[string]any, error) {
s.mu.RLock()
defer s.mu.RUnlock()
now := types.NowDateTime()
result := map[string]any{
"id": s.PK(),
}
if s.IsNew() {
result["created"] = now
}
result["updated"] = now
encoded, err := json.Marshal(s.settings, json.Deterministic(true))
if err != nil {
return nil, err
}
encryptionKey := os.Getenv(app.EncryptionEnv())
if encryptionKey != "" {
encryptVal, encryptErr := security.Encrypt(encoded, encryptionKey)
if encryptErr != nil {
return nil, encryptErr
}
result["value"] = encryptVal
} else {
result["value"] = encoded
}
return result, nil
}
func (s *Settings) PostValidate(ctx context.Context, app App) error {
s.mu.RLock()
defer s.mu.RUnlock()
return validation.ValidateStructWithContext(ctx, s,
validation.Field(&s.SuperuserIPs, validation.Each(validation.Required, validation.By(validators.IPOrSubnet))),
validation.Field(&s.Meta),
validation.Field(&s.Logs),
validation.Field(&s.SMTP),
validation.Field(&s.S3),
validation.Field(&s.Backups),
validation.Field(&s.Batch),
validation.Field(&s.RateLimits),
validation.Field(&s.TrustedProxy),
)
}
func (s *Settings) Merge(other *Settings) error {
other.mu.RLock()
defer other.mu.RUnlock()
raw, err := json.Marshal(other.settings)
if err != nil {
return err
}
s.mu.Lock()
defer s.mu.Unlock()
return json.Unmarshal(raw, &s)
}
func (s *Settings) Clone() (*Settings, error) {
clone := &Settings{
isNew: s.isNew,
}
if err := clone.Merge(s); err != nil {
return nil, err
}
return clone, nil
}
func (s *Settings) MarshalJSON() ([]byte, error) {
s.mu.RLock()
copy := s.settings
s.mu.RUnlock()
copy.SMTP.hidePassword = true
sensitiveFields := []*string{
©.SMTP.Password,
©.S3.Secret,
©.Backups.S3.Secret,
}
for _, v := range sensitiveFields {
if v != nil && *v != "" {
*v = ""
}
}
return json.Marshal(copy, json.Deterministic(true))
}
type SMTPConfig struct {
hidePassword bool
Enabled bool `form:"enabled" json:"enabled"`
Port int `form:"port" json:"port"`
Host string `form:"host" json:"host"`
Username string `form:"username" json:"username"`
Password string `form:"password" json:"password"`
AuthMethod string `form:"authMethod" json:"authMethod"`
TLS bool `form:"tls" json:"tls"`
LocalName string `form:"localName" json:"localName"`
}
func (c SMTPConfig) Validate() error {
return validation.ValidateStruct(&c,
validation.Field(
&c.Host,
validation.When(c.Enabled, validation.Required),
is.Host,
),
validation.Field(
&c.Port,
validation.When(c.Enabled, validation.Required),
validation.Min(0),
),
validation.Field(
&c.AuthMethod,
validation.In(mailer.SMTPAuthLogin, mailer.SMTPAuthPlain),
),
validation.Field(&c.LocalName, is.Host),
)
}
func (c SMTPConfig) MarshalJSON() ([]byte, error) {
type alias SMTPConfig
if c.hidePassword {
v := struct {
alias
Password string `json:"password,omitempty"`
}{alias(c), ""}
return json.Marshal(v)
}
return json.Marshal(alias(c))
}
type S3Config struct {
Enabled bool `form:"enabled" json:"enabled"`
Bucket string `form:"bucket" json:"bucket"`
Region string `form:"region" json:"region"`
Endpoint string `form:"endpoint" json:"endpoint"`
AccessKey string `form:"accessKey" json:"accessKey"`
Secret string `form:"secret" json:"secret,omitempty"`
ForcePathStyle bool `form:"forcePathStyle" json:"forcePathStyle"`
}
func (c S3Config) Validate() error {
return validation.ValidateStruct(&c,
validation.Field(&c.Endpoint, is.URL, validation.When(c.Enabled, validation.Required)),
validation.Field(&c.Bucket, validation.When(c.Enabled, validation.Required)),
validation.Field(&c.Region, validation.When(c.Enabled, validation.Required)),
validation.Field(&c.AccessKey, validation.When(c.Enabled, validation.Required)),
validation.Field(&c.Secret, validation.When(c.Enabled, validation.Required)),
)
}
type BatchConfig struct {
Enabled bool `form:"enabled" json:"enabled"`
MaxRequests int `form:"maxRequests" json:"maxRequests"`
Timeout int64 `form:"timeout" json:"timeout"`
MaxBodySize int64 `form:"maxBodySize" json:"maxBodySize"`
}
func (c BatchConfig) Validate() error {
return validation.ValidateStruct(&c,
validation.Field(&c.MaxRequests, validation.When(c.Enabled, validation.Required), validation.Min(0)),
validation.Field(&c.Timeout, validation.When(c.Enabled, validation.Required), validation.Min(0)),
validation.Field(&c.MaxBodySize, validation.Min(0)),
)
}
type BackupsConfig struct {
Cron string `form:"cron" json:"cron"`
CronMaxKeep int `form:"cronMaxKeep" json:"cronMaxKeep"`
S3 S3Config `form:"s3" json:"s3"`
}
func (c BackupsConfig) Validate() error {
return validation.ValidateStruct(&c,
validation.Field(&c.S3),
validation.Field(&c.Cron, validation.By(checkCronExpression)),
validation.Field(
&c.CronMaxKeep,
validation.When(c.Cron != "", validation.Required),
validation.Min(1),
),
)
}
func checkCronExpression(value any) error {
v, _ := value.(string)
if v == "" {
return nil
}
_, err := cron.NewSchedule(v)
if err != nil {
return validation.NewError("validation_invalid_cron", err.Error())
}
return nil
}
type MetaConfig struct {
AccentColor string `form:"accentColor" json:"accentColor"`
AppName string `form:"appName" json:"appName"`
AppURL string `form:"appURL" json:"appURL"`
SenderName string `form:"senderName" json:"senderName"`
SenderAddress string `form:"senderAddress" json:"senderAddress"`
HideControls bool `form:"hideControls" json:"hideControls"`
}
func (c MetaConfig) Validate() error {
return validation.ValidateStruct(&c,
validation.Field(&c.AccentColor, validation.Length(7, 7), is.HexColor),
validation.Field(&c.AppName, validation.Required, validation.Length(1, 255)),
validation.Field(&c.AppURL, validation.Required, is.URL),
validation.Field(&c.SenderName, validation.Required, validation.Length(1, 255)),
validation.Field(&c.SenderAddress, is.EmailFormat, validation.Required),
)
}
type LogsConfig struct {
MaxDataSize int64 `form:"maxDataSize" json:"maxDataSize"`
MaxDays int `form:"maxDays" json:"maxDays"`
MinLevel int `form:"minLevel" json:"minLevel"`
LogIP bool `form:"logIP" json:"logIP"`
LogAuthId bool `form:"logAuthId" json:"logAuthId"`
}
func (c LogsConfig) Validate() error {
return validation.ValidateStruct(&c,
validation.Field(&c.MaxDataSize, validation.Min(0), validation.Max(maxSafeJSONInt)),
validation.Field(&c.MaxDays, validation.Min(0), validation.Max(maxSafeJSONInt)),
validation.Field(&c.MinLevel, validation.Max(maxSafeJSONInt)),
)
}
type TrustedProxyConfig struct {
Headers []string `form:"headers" json:"headers"`
UseLeftmostIP bool `form:"useLeftmostIP" json:"useLeftmostIP"`
}
func (c TrustedProxyConfig) Validate() error {
return nil
}
type RateLimitsConfig struct {
Rules []RateLimitRule `form:"rules" json:"rules"`
ExcludedIPs []string `form:"excludedIPs" json:"excludedIPs"`
Enabled bool `form:"enabled" json:"enabled"`
}
func (c *RateLimitsConfig) FindRateLimitRule(searchLabels []string, optOnlyAudience ...string) (RateLimitRule, bool) {
var prefixRules []int
for i, label := range searchLabels {
for j := range c.Rules {
if label == c.Rules[j].Label &&
(len(optOnlyAudience) == 0 || slices.Contains(optOnlyAudience, c.Rules[j].Audience)) {
return c.Rules[j], true
}
if i == 0 && strings.HasSuffix(c.Rules[j].Label, "/") {
prefixRules = append(prefixRules, j)
}
}
if len(prefixRules) > 0 {
for j := range prefixRules {
if strings.HasPrefix(label+"/", c.Rules[prefixRules[j]].Label) &&
(len(optOnlyAudience) == 0 || slices.Contains(optOnlyAudience, c.Rules[prefixRules[j]].Audience)) {
return c.Rules[prefixRules[j]], true
}
}
}
}
return RateLimitRule{}, false
}
func (c RateLimitsConfig) Validate() error {
return validation.ValidateStruct(&c,
validation.Field(
&c.Rules,
validation.When(c.Enabled, validation.Required),
validation.By(checkUniqueRuleLabel),
),
validation.Field(
&c.ExcludedIPs,
validation.Each(validation.Required, validation.By(validators.IPOrSubnet)),
),
)
}
func checkUniqueRuleLabel(value any) error {
rules, ok := value.([]RateLimitRule)
if !ok {
return validators.ErrUnsupportedValueType
}
existing := make([]string, 0, len(rules))
for i, rule := range rules {
fullKey := rule.Label + "@@" + rule.Audience
var conflicts bool
for _, key := range existing {
if strings.HasPrefix(key, fullKey) || strings.HasPrefix(fullKey, key) {
conflicts = true
break
}
}
if conflicts {
return validation.Errors{
strconv.Itoa(i): validation.Errors{
"label": validation.NewError("validation_conflicting_rate_limit_rule", "Rate limit rule configuration with label {{.label}} already exists or conflicts with another rule.").
SetParams(map[string]any{"label": rule.Label}),
},
}
} else {
existing = append(existing, fullKey)
}
}
return nil
}
var rateLimitRuleLabelRegex = regexp.MustCompile(`^(\w+\ \/[\w\/-]*|\/[\w\/-]*|^\w+\:\w+|\*\:\w+|\w+)$`)
const (
RateLimitRuleAudienceAll = ""
RateLimitRuleAudienceGuest = "@guest"
RateLimitRuleAudienceAuth = "@auth"
)
type RateLimitRule struct {
Label string `form:"label" json:"label"`
Audience string `form:"audience" json:"audience"`
Duration int64 `form:"duration" json:"duration"`
MaxRequests int `form:"maxRequests" json:"maxRequests"`
}
func (c RateLimitRule) Validate() error {
return validation.ValidateStruct(&c,
validation.Field(&c.Label, validation.Required, validation.Match(rateLimitRuleLabelRegex)),
validation.Field(&c.MaxRequests, validation.Required, validation.Min(1)),
validation.Field(&c.Duration, validation.Required, validation.Min(1)),
validation.Field(&c.Audience,
validation.In(RateLimitRuleAudienceAll, RateLimitRuleAudienceGuest, RateLimitRuleAudienceAuth),
),
)
}
func (c RateLimitRule) DurationTime() time.Duration {
return time.Duration(c.Duration) * time.Second
}
func (c RateLimitRule) String() string {
raw, err := json.Marshal(c)
if err != nil {
return c.Label
}
return string(raw)
}