Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/crypto module routing #193

Merged
merged 4 commits into from
Aug 13, 2024
Merged

Fix/crypto module routing #193

merged 4 commits into from
Aug 13, 2024

Conversation

seba-aln
Copy link
Contributor

@seba-aln seba-aln commented Aug 8, 2024

fix: Fix for routing crypto module if custom one was defined

@seba-aln seba-aln force-pushed the fix/crypto_module_routing branch from 5e7fb41 to ad3fd90 Compare August 8, 2024 12:47
@seba-aln seba-aln marked this pull request as ready for review August 8, 2024 13:01
pubnub/crypto.py Outdated
@@ -69,7 +68,7 @@ def extract_random_iv(self, message, use_random_iv):

def get_initialization_vector(self, use_random_iv):
if self.pubnub_configuration.use_random_initialization_vector or use_random_iv:
return "{0:016}".format(random.randint(0, 9999999999999999))
return secrets.token_hex(16)[0:16]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there's something suspicious about this line... you're generating 16 random bytes, represented as a text string, then throw away half of it (0:16 is half of the 32 character long hex string), and return the text string that can be only made up of 0-9a-f, which has even fewer possible combinations...

why not use just secrets.token_bytes(16) if you need 16 bytes and leave it at that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes you are right. I replaced it with a different method that gives me full 16 character long high entropy string

if self.pubnub.config.crypto_module:
stringified_message = self.pubnub.config.crypto_module.encrypt(stringified_message)
elif self.pubnub.config.cipher_key is not None: # The legacy way
stringified_message = '"' + self.pubnub.config.crypto.encrypt(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems risky to keep doing '"' + x + '"' everywhere, are you sure x can't contain a " character? also here above in line 62 you don't add the " - is it required? I'm just pointing it out because it's different than in other places

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch. Fixed missing quote concat. the method to "stringify" message escapes double quote so it shouldn't exist by itself in the wild

@seba-aln seba-aln force-pushed the fix/crypto_module_routing branch from 7b52a90 to e98bb65 Compare August 12, 2024 10:39
@@ -68,7 +68,7 @@ def extract_random_iv(self, message, use_random_iv):

def get_initialization_vector(self, use_random_iv):
if self.pubnub_configuration.use_random_initialization_vector or use_random_iv:
return secrets.token_hex(16)[0:16]
return secrets.token_urlsafe(16)[:16]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why clip to :16 characters again? I think I don't know enough about the underlying implementation to understand why we need strings here instead of bytes, but I'm fairly sure that reducing the output of 16 random BYTES from token_urlsafe to 16 url safe characters makes this again much less random. If we don't need 16 random bytes that's fine, but it the requirement is to have 16 (secure)random bytes, then I don't think this satisfies it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It has 16 bytes but character wise it's length is 22. And in this old implementation we deal with strings. So while it's the old, deprecated implementation I don't want to update it more than necessary

@seba-aln seba-aln requested a review from wkal-pubnub August 12, 2024 11:14
Copy link

@wkal-pubnub wkal-pubnub left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

approve with the comment that the IV should be properly done as bytes, not strings.

@seba-aln seba-aln merged commit fd04298 into master Aug 13, 2024
9 checks passed
@seba-aln seba-aln deleted the fix/crypto_module_routing branch August 13, 2024 10:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants