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

{Core} Stop using legacy base64.decodebytes #30464

Merged
merged 1 commit into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/azure-cli-core/azure/cli/core/keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ def is_valid_ssh_rsa_public_key(openssh_pubkey):
# http://stackoverflow.com/questions/2494450/ssh-rsa-public-key-validation-using-a-regular-expression # pylint: disable=line-too-long
# A "good enough" check is to see if the key starts with the correct header.
import struct
from base64 import decodebytes as base64_decode
import base64

parts = openssh_pubkey.split()
if len(parts) < 2:
return False
key_type = parts[0]
key_string = parts[1]

data = base64_decode(key_string.encode()) # pylint:disable=deprecated-method
data = base64.b64decode(key_string)
Copy link
Member Author

@jiasli jiasli Dec 4, 2024

Choose a reason for hiding this comment

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

base64.b64decode() takes str input:

https://docs.python.org/3/library/base64.html#base64.b64decode

base64.b64decode(s, altchars=None, validate=False)
Decode the Base64 encoded bytes-like object or ASCII string s and return the decoded bytes.

int_len = 4
str_len = struct.unpack('>I', data[:int_len])[0] # this should return 7
return data[int_len:int_len + str_len] == key_type.encode()
Expand Down
4 changes: 2 additions & 2 deletions src/azure-cli/azure/cli/command_modules/lab/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,14 +499,14 @@ def _is_valid_ssh_rsa_public_key(openssh_pubkey):
# http://stackoverflow.com/questions/2494450/ssh-rsa-public-key-validation-using-a-regular-expression
# A "good enough" check is to see if the key starts with the correct header.
import struct
from base64 import decodebytes as base64_decode
import base64
parts = openssh_pubkey.split()
if len(parts) < 2:
return False
key_type = parts[0]
key_string = parts[1]

data = base64_decode(key_string.encode()) # pylint:disable=deprecated-method
data = base64.b64decode(key_string)
int_len = 4
str_len = struct.unpack('>I', data[:int_len])[0] # this should return 7
return data[int_len:int_len + str_len] == key_type.encode()
Expand Down