Skip to content

Commit 19cfdb1

Browse files
committed
only client to server comm must be masked
1 parent f0f492f commit 19cfdb1

2 files changed

Lines changed: 12 additions & 10 deletions

File tree

ws4py/client/__init__.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,21 +122,21 @@ def close_connection(self):
122122
def send(self, payload, binary=False):
123123
if isinstance(payload, basestring):
124124
if not binary:
125-
self.write_to_connection(self.stream.text_message(payload).single())
125+
self.write_to_connection(self.stream.text_message(payload).single(mask=True))
126126
else:
127-
self.write_to_connection(self.stream.binary_message(payload).single())
127+
self.write_to_connection(self.stream.binary_message(payload).single(mask=True))
128128

129129
elif type(payload) == types.GeneratorType:
130130
bytes = payload.next()
131131
first = True
132132
for chunk in payload:
133133
if not binary:
134-
self.write_to_connection(self.stream.text_message(bytes).fragment(first=first))
134+
self.write_to_connection(self.stream.text_message(bytes).fragment(first=first, mask=True))
135135
else:
136-
self.write_to_connection(self.stream.binary_message(payload).fragment(first=first))
136+
self.write_to_connection(self.stream.binary_message(payload).fragment(first=first, mask=True))
137137
bytes = chunk
138138
first = False
139139
if not binary:
140-
self.write_to_connection(self.stream.text_message(bytes).fragment(last=True))
140+
self.write_to_connection(self.stream.text_message(bytes).fragment(last=True, mask=True))
141141
else:
142-
self.write_to_connection(self.stream.text_message(bytes).fragment(last=True))
142+
self.write_to_connection(self.stream.text_message(bytes).fragment(last=True, mask=True))

ws4py/messaging.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,15 @@ def __init__(self, opcode, data='', encoding='utf-8'):
3232

3333
self.encoding = encoding
3434

35-
def single(self):
35+
def single(self, mask=False):
3636
"""
3737
Returns a frame bytes with the fin bit set and a random mask.
3838
"""
39+
mask = os.urandom(4) if mask else None
3940
return Frame(body=self.data or '', opcode=self.opcode,
40-
masking_key=os.urandom(4), fin=1).build()
41+
masking_key=mask, fin=1).build()
4142

42-
def fragment(self, first=False, last=False):
43+
def fragment(self, first=False, last=False, mask=False):
4344
"""
4445
Returns a frame bytes as part of a fragmented message.
4546
@@ -49,8 +50,9 @@ def fragment(self, first=False, last=False):
4950
"""
5051
fin = 1 if last is True else 0
5152
opcode = self.opcode if first is True else OPCODE_CONTINUATION
53+
mask = os.urandom(4) if mask else None
5254
return Frame(body=self.data or '', opcode=opcode,
53-
masking_key=os.urandom(4), fin=fin).build()
55+
masking_key=mask, fin=fin).build()
5456

5557
@property
5658
def completed(self):

0 commit comments

Comments
 (0)