Skip to content

Commit

Permalink
Only when server.Options.Capabilities.MaximumMessageExpiryInterval is…
Browse files Browse the repository at this point in the history
… set to math.MaxInt64 for no expiry.
  • Loading branch information
werbenhu committed Oct 13, 2023
1 parent 3b0fd36 commit 46b986f
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 49 deletions.
2 changes: 1 addition & 1 deletion README-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ server := mqtt.New(&mqtt.Options{

关于决定默认配置的值,在这里进行一些说明:

- 默认情况下,server.Options.Capabilities.MaximumMessageExpiryInterval 的值被设置为 86400(24小时),以防止在使用默认配置时网络上暴露服务器而受到恶意DOS攻击(如果不配置到期时间将允许无限数量的保留retained/待发送inflight消息累积)。如果您在一个受信任的环境中运行,或者您有更大的保留期容量,您可以选择覆盖此设置(设置为 0 或 math.MaxInt64 以取消到期限制)。
- 默认情况下,server.Options.Capabilities.MaximumMessageExpiryInterval 的值被设置为 86400(24小时),以防止在使用默认配置时网络上暴露服务器而受到恶意DOS攻击(如果不配置到期时间将允许无限数量的保留retained/待发送inflight消息累积)。如果您在一个受信任的环境中运行,或者您有更大的保留期容量,您可以选择覆盖此设置(设置为math.MaxInt64 以取消到期限制)。

## 事件钩子(Event Hooks)

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ Review the mqtt.Options, mqtt.Capabilities, and mqtt.Compatibilities structs for

Some choices were made when deciding the default configuration that need to be mentioned here:

- By default, the value of `server.Options.Capabilities.MaximumMessageExpiryInterval` is set to 86400 (24 hours), in order to prevent exposing the broker to DOS attacks on hostile networks when using the out-of-the-box configuration (as an infinite expiry would allow an infinite number of retained/inflight messages to accumulate). If you are operating in a trusted environment, or you have capacity for a larger retention period, uou may wish to override this (set to `0` or `math.MaxInt64` for no expiry).
- By default, the value of `server.Options.Capabilities.MaximumMessageExpiryInterval` is set to 86400 (24 hours), in order to prevent exposing the broker to DOS attacks on hostile networks when using the out-of-the-box configuration (as an infinite expiry would allow an infinite number of retained/inflight messages to accumulate). If you are operating in a trusted environment, or you have capacity for a larger retention period, uou may wish to override this (set to `math.MaxInt64` for no expiry).

## Event Hooks
A universal event hooks system allows developers to hook into various parts of the server and client life cycle to add and modify functionality of the broker. These universal hooks are used to provide everything from authentication, persistent storage, to debugging tools.
Expand Down
21 changes: 9 additions & 12 deletions clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,21 +331,18 @@ func (cl *Client) ResendInflightMessages(force bool) error {
func (cl *Client) ClearInflights(now, maximumExpiry int64) []uint16 {
deleted := []uint16{}

if maximumExpiry == 0 || maximumExpiry == math.MaxInt64 {
// If the maximum message expiry interval is set to 0 or math.MaxInt64, do not process expired inflight messages.
return deleted
}

for _, tk := range cl.State.Inflight.GetAll(false) {
if (tk.Expiry > 0 && tk.Expiry < now) || tk.Created < now-maximumExpiry {
if ok := cl.State.Inflight.Delete(tk.PacketID); ok {
cl.ops.hooks.OnQosDropped(cl, tk)
atomic.AddInt64(&cl.ops.info.Inflight, -1)
deleted = append(deleted, tk.PacketID)
// If the maximum message expiry interval is set to math.MaxInt64, do not process expired inflight messages.
if maximumExpiry != math.MaxInt64 {
for _, tk := range cl.State.Inflight.GetAll(false) {
if (tk.Expiry > 0 && tk.Expiry < now) || tk.Created < now-maximumExpiry {
if ok := cl.State.Inflight.Delete(tk.PacketID); ok {
cl.ops.hooks.OnQosDropped(cl, tk)
atomic.AddInt64(&cl.ops.info.Inflight, -1)
deleted = append(deleted, tk.PacketID)
}
}
}
}

return deleted
}

Expand Down
5 changes: 0 additions & 5 deletions clients_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,11 +318,6 @@ func TestClientClearInflights(t *testing.T) {
require.Equal(t, 2, cl.State.Inflight.Len())

cl.State.Inflight.Set(packets.Packet{PacketID: 8, Created: n - 8})
deleted = cl.ClearInflights(n, 0)
require.Len(t, deleted, 0)
require.ElementsMatch(t, []uint16{}, deleted)
require.Equal(t, 3, cl.State.Inflight.Len())

deleted = cl.ClearInflights(n, math.MaxInt64)
require.Len(t, deleted, 0)
require.ElementsMatch(t, []uint16{}, deleted)
Expand Down
36 changes: 14 additions & 22 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1596,34 +1596,26 @@ func (s *Server) clearExpiredClients(dt int64) {

// clearExpiredRetainedMessage deletes retained messages from topics if they have expired.
func (s *Server) clearExpiredRetainedMessages(now int64) {

if s.Options.Capabilities.MaximumMessageExpiryInterval == 0 ||
s.Options.Capabilities.MaximumMessageExpiryInterval == math.MaxInt64 {
// If the maximum message expiry interval is set to 0 or math.MaxInt64, do not process expired messages.
return
}

for filter, pk := range s.Topics.Retained.GetAll() {
if (pk.Expiry > 0 && pk.Expiry < now) || pk.Created < now-s.Options.Capabilities.MaximumMessageExpiryInterval {
s.Topics.Retained.Delete(filter)
s.hooks.OnRetainedExpired(filter)
// If the maximum message expiry interval is set to math.MaxInt64, do not process expired messages.
if s.Options.Capabilities.MaximumMessageExpiryInterval != math.MaxInt64 {
for filter, pk := range s.Topics.Retained.GetAll() {
if (pk.Expiry > 0 && pk.Expiry < now) || pk.Created < now-s.Options.Capabilities.MaximumMessageExpiryInterval {
s.Topics.Retained.Delete(filter)
s.hooks.OnRetainedExpired(filter)
}
}
}
}

// clearExpiredInflights deletes any inflight messages which have expired.
func (s *Server) clearExpiredInflights(now int64) {

if s.Options.Capabilities.MaximumMessageExpiryInterval == 0 ||
s.Options.Capabilities.MaximumMessageExpiryInterval == math.MaxInt64 {
// If the maximum message expiry interval is set to 0 or math.MaxInt64, do not process expired messages.
return
}

for _, client := range s.Clients.GetAll() {
if deleted := client.ClearInflights(now, s.Options.Capabilities.MaximumMessageExpiryInterval); len(deleted) > 0 {
for _, id := range deleted {
s.hooks.OnQosDropped(client, packets.Packet{PacketID: id})
// If the maximum message expiry interval is set math.MaxInt64, do not process expired messages.
if s.Options.Capabilities.MaximumMessageExpiryInterval != math.MaxInt64 {
for _, client := range s.Clients.GetAll() {
if deleted := client.ClearInflights(now, s.Options.Capabilities.MaximumMessageExpiryInterval); len(deleted) > 0 {
for _, id := range deleted {
s.hooks.OnQosDropped(client, packets.Packet{PacketID: id})
}
}
}
}
Expand Down
8 changes: 0 additions & 8 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3265,10 +3265,6 @@ func TestServerClearExpiredInflights(t *testing.T) {
cl.State.Inflight.Set(packets.Packet{PacketID: 8, Expiry: n - 8})
s.clearExpiredInflights(n)
require.Len(t, cl.State.Inflight.GetAll(false), 3)

s.Options.Capabilities.MaximumMessageExpiryInterval = 0
s.clearExpiredInflights(n)
require.Len(t, cl.State.Inflight.GetAll(false), 3)
}

func TestServerClearExpiredRetained(t *testing.T) {
Expand All @@ -3291,10 +3287,6 @@ func TestServerClearExpiredRetained(t *testing.T) {
s.Topics.Retained.Add("o/p/q", packets.Packet{Created: n - 8})
s.clearExpiredRetainedMessages(n)
require.Len(t, s.Topics.Retained.GetAll(), 3)

s.Options.Capabilities.MaximumMessageExpiryInterval = 0
s.clearExpiredRetainedMessages(n)
require.Len(t, s.Topics.Retained.GetAll(), 3)
}

func TestServerClearExpiredClients(t *testing.T) {
Expand Down

0 comments on commit 46b986f

Please sign in to comment.