From 77784a2fce75abc58d91a69223b3f566d182920e Mon Sep 17 00:00:00 2001 From: Fulong Sun <2051806+SunFulong@users.noreply.github.com> Date: Sat, 13 Apr 2024 14:39:45 +0800 Subject: [PATCH 1/7] Random ID should beginning with 1 --- autobahn/util.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/autobahn/util.py b/autobahn/util.py index bfad2ed26..b660bc730 100644 --- a/autobahn/util.py +++ b/autobahn/util.py @@ -278,13 +278,13 @@ def __next__(self): # -# 8 byte mask with 53 LSBs set (WAMP requires IDs from [0, 2**53] +# 8 byte mask with 53 LSBs set (WAMP requires IDs from [1, 2**53] _WAMP_ID_MASK = struct.unpack(">Q", b"\x00\x1f\xff\xff\xff\xff\xff\xff")[0] def rid(): """ - Generate a new random integer ID from range **[0, 2**53]**. + Generate a new random integer ID from range **[1, 2**53]**. The generated ID is uniformly distributed over the whole range, doesn't have a period (no pseudo-random generator is used) and cryptographically strong. @@ -298,13 +298,13 @@ def rid(): :returns: A random integer ID. :rtype: int """ - return struct.unpack("@Q", os.urandom(8))[0] & _WAMP_ID_MASK + return struct.unpack("@Q", os.urandom(8))[0] & _WAMP_ID_MASK + 1 # noinspection PyShadowingBuiltins def id(): """ - Generate a new random integer ID from range **[0, 2**53]**. + Generate a new random integer ID from range **[1, 2**53]**. The generated ID is based on a pseudo-random number generator (Mersenne Twister, which has a period of 2**19937-1). It is NOT cryptographically strong, and @@ -319,7 +319,7 @@ def id(): :returns: A random integer ID. :rtype: int """ - return random.randint(0, 9007199254740992) + return random.randint(1, 9007199254740992) def newid(length=16): From eeb977ee5c989506c08ce026cde3fb3aacb17156 Mon Sep 17 00:00:00 2001 From: Fulong Sun <2051806+SunFulong@users.noreply.github.com> Date: Sat, 13 Apr 2024 23:35:53 +0800 Subject: [PATCH 2/7] Make (and) first --- autobahn/util.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autobahn/util.py b/autobahn/util.py index b660bc730..cd420d1fd 100644 --- a/autobahn/util.py +++ b/autobahn/util.py @@ -298,7 +298,7 @@ def rid(): :returns: A random integer ID. :rtype: int """ - return struct.unpack("@Q", os.urandom(8))[0] & _WAMP_ID_MASK + 1 + return (struct.unpack("@Q", os.urandom(8))[0] & _WAMP_ID_MASK) + 1 # noinspection PyShadowingBuiltins From 45b21dae36778a6c5221121d517d09df8f5ccf29 Mon Sep 17 00:00:00 2001 From: Fulong Sun <2051806+SunFulong@users.noreply.github.com> Date: Sun, 14 Apr 2024 00:03:48 +0800 Subject: [PATCH 3/7] Enforce byte-order to big-endian --- autobahn/util.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autobahn/util.py b/autobahn/util.py index cd420d1fd..7d87919af 100644 --- a/autobahn/util.py +++ b/autobahn/util.py @@ -298,7 +298,7 @@ def rid(): :returns: A random integer ID. :rtype: int """ - return (struct.unpack("@Q", os.urandom(8))[0] & _WAMP_ID_MASK) + 1 + return (struct.unpack(">Q", os.urandom(8))[0] & _WAMP_ID_MASK) + 1 # noinspection PyShadowingBuiltins From c12ce188fde8aabb36670eed5c526a8962972ce1 Mon Sep 17 00:00:00 2001 From: Fulong Sun <2051806+SunFulong@users.noreply.github.com> Date: Sun, 14 Apr 2024 00:22:04 +0800 Subject: [PATCH 4/7] Change MASK to let use native byte order --- autobahn/util.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/autobahn/util.py b/autobahn/util.py index 7d87919af..154a584b8 100644 --- a/autobahn/util.py +++ b/autobahn/util.py @@ -279,7 +279,7 @@ def __next__(self): # 8 byte mask with 53 LSBs set (WAMP requires IDs from [1, 2**53] -_WAMP_ID_MASK = struct.unpack(">Q", b"\x00\x1f\xff\xff\xff\xff\xff\xff")[0] +_WAMP_ID_MASK = 2 ** 53 - 1 def rid(): @@ -298,7 +298,7 @@ def rid(): :returns: A random integer ID. :rtype: int """ - return (struct.unpack(">Q", os.urandom(8))[0] & _WAMP_ID_MASK) + 1 + return (struct.unpack("@Q", os.urandom(8))[0] & _WAMP_ID_MASK) + 1 # noinspection PyShadowingBuiltins From f0840517eb84a9a82138e042250b08abb9063917 Mon Sep 17 00:00:00 2001 From: Fulong Sun <2051806+SunFulong@users.noreply.github.com> Date: Sun, 14 Apr 2024 01:05:17 +0800 Subject: [PATCH 5/7] Wrap value to 2**53 if 0 --- autobahn/util.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/autobahn/util.py b/autobahn/util.py index 154a584b8..c3d3f4786 100644 --- a/autobahn/util.py +++ b/autobahn/util.py @@ -279,7 +279,7 @@ def __next__(self): # 8 byte mask with 53 LSBs set (WAMP requires IDs from [1, 2**53] -_WAMP_ID_MASK = 2 ** 53 - 1 +_WAMP_ID_MASK = struct.unpack("@Q", b"\x00\x1f\xff\xff\xff\xff\xff\xff")[0] def rid(): @@ -298,7 +298,7 @@ def rid(): :returns: A random integer ID. :rtype: int """ - return (struct.unpack("@Q", os.urandom(8))[0] & _WAMP_ID_MASK) + 1 + return struct.unpack("@Q", os.urandom(8))[0] & _WAMP_ID_MASK or 2 ** 53 # noinspection PyShadowingBuiltins From b59b11445ff50b0e12f6f2906feeac7ec3343369 Mon Sep 17 00:00:00 2001 From: Fulong Sun <2051806+SunFulong@users.noreply.github.com> Date: Sun, 14 Apr 2024 02:14:56 +0800 Subject: [PATCH 6/7] Both MASK and random number using big-endian byte order --- autobahn/util.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/autobahn/util.py b/autobahn/util.py index c3d3f4786..a66b26f28 100644 --- a/autobahn/util.py +++ b/autobahn/util.py @@ -279,7 +279,7 @@ def __next__(self): # 8 byte mask with 53 LSBs set (WAMP requires IDs from [1, 2**53] -_WAMP_ID_MASK = struct.unpack("@Q", b"\x00\x1f\xff\xff\xff\xff\xff\xff")[0] +_WAMP_ID_MASK = struct.unpack(">Q", b"\x00\x1f\xff\xff\xff\xff\xff\xff")[0] def rid(): @@ -298,7 +298,7 @@ def rid(): :returns: A random integer ID. :rtype: int """ - return struct.unpack("@Q", os.urandom(8))[0] & _WAMP_ID_MASK or 2 ** 53 + return struct.unpack(">Q", os.urandom(8))[0] & _WAMP_ID_MASK or 2 ** 53 # noinspection PyShadowingBuiltins From f4480338ea13ced4bb3d5cd1043a2c41258751bf Mon Sep 17 00:00:00 2001 From: Fulong Sun <2051806+SunFulong@users.noreply.github.com> Date: Sun, 14 Apr 2024 02:30:33 +0800 Subject: [PATCH 7/7] Fix ID generator range error (#1637) --- autobahn/_version.py | 2 +- docs/changelog.rst | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/autobahn/_version.py b/autobahn/_version.py index 965c97f24..c8e3a5015 100644 --- a/autobahn/_version.py +++ b/autobahn/_version.py @@ -24,6 +24,6 @@ # ############################################################################### -__version__ = '24.4.1' +__version__ = '24.4.2' __build__ = '00000000-0000000' diff --git a/docs/changelog.rst b/docs/changelog.rst index 35681f6d7..a543df433 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -5,6 +5,11 @@ Changelog ========= +24.4.2 +------ + +- fix: Ensure ID generator in range [1, 2 ** 53] (#1637) + 24.4.1 ------