mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-08-19 17:40:59 +00:00
fix(subscription): bound outbound response body (#5493)
This commit is contained in:
@@ -3,6 +3,7 @@ package service
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
@@ -18,6 +19,24 @@ import (
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/util/link"
|
||||
)
|
||||
|
||||
// maxOutboundSubscriptionBytes caps a single outbound subscription response.
|
||||
// It is larger than the 2 MiB user-facing subscription cap because an outbound
|
||||
// subscription may aggregate many upstream outbounds into one document.
|
||||
const maxOutboundSubscriptionBytes int64 = 8 << 20
|
||||
|
||||
var errOutboundSubscriptionBodyTooLarge = errors.New("outbound subscription response body exceeds size limit")
|
||||
|
||||
func readBoundedOutboundSubscriptionBody(r io.Reader) ([]byte, error) {
|
||||
body, err := io.ReadAll(io.LimitReader(r, maxOutboundSubscriptionBytes+1))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if int64(len(body)) > maxOutboundSubscriptionBytes {
|
||||
return nil, fmt.Errorf("%w (limit: %d bytes)", errOutboundSubscriptionBodyTooLarge, maxOutboundSubscriptionBytes)
|
||||
}
|
||||
return body, nil
|
||||
}
|
||||
|
||||
// OutboundSubscriptionService manages remote outbound subscriptions.
|
||||
type OutboundSubscriptionService struct {
|
||||
settingService SettingService
|
||||
@@ -281,7 +300,7 @@ func (s *OutboundSubscriptionService) fetchAndStore(sub *model.OutboundSubscript
|
||||
s.recordError(sub, err)
|
||||
return nil, err
|
||||
}
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
body, err := readBoundedOutboundSubscriptionBody(resp.Body)
|
||||
if err != nil {
|
||||
s.recordError(sub, err)
|
||||
return nil, err
|
||||
|
||||
Reference in New Issue
Block a user