From 55a17b2ed3c506e240e35ae8bff432e08e588787 Mon Sep 17 00:00:00 2001 From: Thomas Kriechbaumer Date: Sun, 15 May 2016 10:40:21 -0700 Subject: [PATCH] fix encoding error on *too* random bytes This should fix things like: def __init__(self, val): self.val = val.decode("string_escape") ValueError: Trailing \ in string pathod/language/base.py:108: ValueError test/pathod/test_language_websocket.py:83: in fr return netlib.websockets.Frame.from_bytes(tutils.render(wf, settings)) test/pathod/tutils.py:123: in render r = r.resolve(settings) pathod/language/websockets.py:179: in resolve Key(base.TokValueLiteral(os.urandom(4))) --- pathod/language/websockets.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pathod/language/websockets.py b/pathod/language/websockets.py index ea7c870e5..09443a951 100644 --- a/pathod/language/websockets.py +++ b/pathod/language/websockets.py @@ -1,4 +1,6 @@ import os +import random +import string import netlib.websockets import pyparsing as pp from . import base, generators, actions, message @@ -175,8 +177,10 @@ class WebsocketFrame(message.Message): Mask(True) ) if not self.knone and self.mask and self.mask.value and not self.key: + allowed_chars = string.ascii_letters + string.digits + k = ''.join([allowed_chars[random.randrange(0, len(allowed_chars))] for i in range(4)]) tokens.append( - Key(base.TokValueLiteral(os.urandom(4))) + Key(base.TokValueLiteral(k)) ) return self.__class__( [i.resolve(settings, self) for i in tokens]