mirror of
https://github.com/zulip/zulip.git
synced 2025-11-15 19:31:58 +00:00
zephyr: Fix bogus uses of force_bytes that break on Python 3.
An expression like `force_bytes(chr(...))`, on Python 3 where the `force_bytes` finds itself with something to do because `chr` returns a text string, gives the UTF-8 encoding of the given value as a Unicode codepoint. Here, we don't want that -- rather we want the given value as a single byte. We can do that with `struct.pack`. This fixes an issue where the "Link with Webathena" flow was producing invalid credential caches when run on Python 3, breaking the Zephyr mirror for any user who went through it anew.
This commit is contained in:
@@ -41,17 +41,17 @@ import six
|
|||||||
def der_encode_length(length):
|
def der_encode_length(length):
|
||||||
# type: (int) -> bytes
|
# type: (int) -> bytes
|
||||||
if length <= 127:
|
if length <= 127:
|
||||||
return force_bytes(chr(length))
|
return struct.pack('!B', length)
|
||||||
out = b""
|
out = b""
|
||||||
while length > 0:
|
while length > 0:
|
||||||
out = force_bytes(chr(length & 0xff)) + out
|
out = struct.pack('!B', length & 0xff) + out
|
||||||
length >>= 8
|
length >>= 8
|
||||||
out = force_bytes(chr(len(out) | 0x80)) + out
|
out = struct.pack('!B', len(out) | 0x80) + out
|
||||||
return out
|
return out
|
||||||
|
|
||||||
def der_encode_tlv(tag, value):
|
def der_encode_tlv(tag, value):
|
||||||
# type: (int, bytes) -> bytes
|
# type: (int, bytes) -> bytes
|
||||||
return force_bytes(chr(tag)) + der_encode_length(len(value)) + value
|
return struct.pack('!B', tag) + der_encode_length(len(value)) + value
|
||||||
|
|
||||||
def der_encode_integer_value(val):
|
def der_encode_integer_value(val):
|
||||||
# type: (int) -> bytes
|
# type: (int) -> bytes
|
||||||
@@ -71,7 +71,7 @@ def der_encode_integer_value(val):
|
|||||||
# We can stop once sign-extension matches the remaining value.
|
# We can stop once sign-extension matches the remaining value.
|
||||||
while val != sign:
|
while val != sign:
|
||||||
byte = val & 0xff
|
byte = val & 0xff
|
||||||
out = force_bytes(chr(byte)) + out
|
out = struct.pack('!B', byte) + out
|
||||||
sign = -1 if byte & 0x80 == 0x80 else 0
|
sign = -1 if byte & 0x80 == 0x80 else 0
|
||||||
val >>= 8
|
val >>= 8
|
||||||
return out
|
return out
|
||||||
|
|||||||
Reference in New Issue
Block a user