22
22
# The prefix any e2e channel must have
23
23
ENCRYPTED_PREFIX = "private-encrypted-"
24
24
SERVER_TO_USER_PREFIX = "#server-to-user-"
25
+ MAX_PAYLOAD_SIZE_BYTES = 10240
26
+ MAX_CHANNELS = 100
27
+ MAX_CHANNEL_NAME_SIZE = 200
25
28
26
29
channel_name_re = re .compile (r'\A[-a-zA-Z0-9_=@,.;]+\Z' )
27
30
server_to_user_channel_re = re .compile (rf'\A{ SERVER_TO_USER_PREFIX } [-a-zA-Z0-9_=@,.;]+\Z' )
@@ -96,7 +99,7 @@ def validate_user_id(user_id):
96
99
if length == 0 :
97
100
raise ValueError ("User id is empty" )
98
101
99
- if length > 200 :
102
+ if length > MAX_CHANNEL_NAME_SIZE :
100
103
raise ValueError ("User id too long: '{}'" .format (user_id ))
101
104
102
105
if not channel_name_re .match (user_id ):
@@ -108,7 +111,7 @@ def validate_user_id(user_id):
108
111
def validate_channel (channel ):
109
112
channel = ensure_text (channel , "channel" )
110
113
111
- if len (channel ) > 200 :
114
+ if len (channel ) > MAX_CHANNEL_NAME_SIZE :
112
115
raise ValueError ("Channel too long: %s" % channel )
113
116
114
117
if channel .startswith (SERVER_TO_USER_PREFIX ):
@@ -128,7 +131,7 @@ def validate_channels(channels):
128
131
channels , (collections .Sized , collections .Iterable )):
129
132
raise TypeError ("Expected a single or a list of channels" )
130
133
131
- if len (channels ) > 100 :
134
+ if len (channels ) > MAX_CHANNELS :
132
135
raise ValueError ("Too many channels" )
133
136
134
137
channels = [validate_channel (ch ) for ch in channels ]
@@ -140,7 +143,7 @@ def validate_channels(channels):
140
143
141
144
def validate_event_name (event_name ):
142
145
event_name = ensure_text (event_name , "event_name" )
143
- if len (event_name ) > 200 :
146
+ if len (event_name ) > MAX_CHANNEL_NAME_SIZE :
144
147
raise ValueError ("event_name too long" )
145
148
return event_name
146
149
@@ -152,7 +155,7 @@ def validate_data(data, json_encoder=None):
152
155
"""
153
156
154
157
data = data_to_string (data , json_encoder )
155
- if len (data ) > 10240 :
158
+ if len (data ) > MAX_PAYLOAD_SIZE_BYTES :
156
159
raise ValueError ("Too much data" )
157
160
return data
158
161
0 commit comments