Skip to content

Commit db4b23b

Browse files
enforce max-keys
1 parent 2275bed commit db4b23b

File tree

5 files changed

+33
-2
lines changed

5 files changed

+33
-2
lines changed

default.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1094,7 +1094,7 @@ metadata:
10941094
# how many keys can a client subscribe to?
10951095
# set to 0 to disable subscriptions or -1 to allow unlimited subscriptions.
10961096
max-subs: 100
1097-
# how many keys can a given target store? set to -1 to allow unlimited keys.
1097+
# how many keys can a user store about themselves? set to -1 to allow unlimited keys.
10981098
max-keys: 1000
10991099

11001100
# experimental support for mobile push notifications

irc/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,7 @@ type Config struct {
728728
Enabled bool
729729
MaxSubs int `yaml:"max-subs"`
730730
MaxKeys int `yaml:"max-keys"`
731-
MaxValueBytes int `yaml:"max-value-length"`
731+
MaxValueBytes int `yaml:"max-value-length"` // todo: currently unenforced!!
732732
}
733733

734734
WebPush struct {

irc/getters.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -935,6 +935,17 @@ func (channel *Channel) ClearMetadata() MetadataStore {
935935
return oldMap
936936
}
937937

938+
func (channel *Channel) CountMetadata() int {
939+
channel.stateMutex.RLock()
940+
defer channel.stateMutex.RUnlock()
941+
942+
if channel.metadata == nil {
943+
return 0
944+
}
945+
946+
return len(channel.metadata)
947+
}
948+
938949
func (client *Client) GetMetadata(key string) (string, error) {
939950
client.stateMutex.RLock()
940951
defer client.stateMutex.RUnlock()
@@ -982,3 +993,14 @@ func (client *Client) ClearMetadata() MetadataStore {
982993

983994
return oldMap
984995
}
996+
997+
func (client *Client) CountMetadata() int {
998+
client.stateMutex.RLock()
999+
defer client.stateMutex.RUnlock()
1000+
1001+
if client.metadata == nil {
1002+
return 0
1003+
}
1004+
1005+
return len(client.metadata)
1006+
}

irc/handlers.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3167,6 +3167,14 @@ func metadataHandler(server *Server, client *Client, msg ircmsg.Message, rb *Res
31673167
return
31683168
}
31693169

3170+
maxKeys := server.Config().Metadata.MaxKeys
3171+
isSelf := targetClient != nil && client == targetClient
3172+
3173+
if isSelf && maxKeys > 0 && t.CountMetadata() >= maxKeys {
3174+
rb.Add(nil, server.name, "FAIL", "METADATA", "LIMIT_REACHED", client.nick, client.t("You have too many keys set on yourself"))
3175+
return
3176+
}
3177+
31703178
server.logger.Debug("metadata", "setting", key, value, "on", target)
31713179

31723180
t.SetMetadata(key, value)

irc/metadata.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type MetadataHaver = interface {
2323
DeleteMetadata(key string)
2424
ListMetadata() MetadataStore
2525
ClearMetadata() MetadataStore
26+
CountMetadata() int
2627
}
2728

2829
func notifySubscribers(server *Server, session *Session, target string, key string, value string) {

0 commit comments

Comments
 (0)