Skip to content

Commit

Permalink
fix(akamai): handle non-string user data in base64 decoding (canonica…
Browse files Browse the repository at this point in the history
…l#5751)

Previously, user data that was base64-encoded was decoded as a string,
causing non-string data (e.g., gzipped user data) to raise an exception.
This change ensures that the output of b64decode is not decoded to a
string, passing along the decoded bytes unchanged.
  • Loading branch information
jessealter authored Oct 3, 2024
1 parent 545e9ac commit ce80781
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
2 changes: 1 addition & 1 deletion cloudinit/sources/DataSourceAkamai.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ def _fetch_metadata(self, use_v6: bool = False) -> bool:
)
self.userdata_raw = str(userdata)
try:
self.userdata_raw = b64decode(self.userdata_raw).decode()
self.userdata_raw = b64decode(self.userdata_raw)
except binascii.Error as e:
LOG.warning("Failed to base64 decode userdata due to %s", e)
except url_helper.UrlError as e:
Expand Down
41 changes: 35 additions & 6 deletions tests/unittests/sources/test_akamai.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,14 +261,43 @@ def test_get_network_context_managers(
assert rv6 == ev6

@pytest.mark.parametrize(
"use_v6",
"use_v6,userdata,decoded_userdata,case",
(
False,
True,
(
False,
"dGVzdGluZyBlbmNvZGVkIHVzZXJkYXRh",
b"testing encoded userdata",
"base64-encoded ASCII plaintext, IPv4 request",
),
(
True,
"dGVzdGluZyBlbmNvZGVkIHVzZXJkYXRh",
b"testing encoded userdata",
"base64-encoded ASCII plaintext, IPv6 request",
),
(
False,
"H4sIAAAAAAACAytJLS7hAgDGNbk7BQAAAA==",
b"\x1f\x8b\x08\x00\x00\x00\x00\x00\x02\x03+I-.\xe1\x02\x00\xc65\xb9;\x05\x00\x00\x00",
"base64-encoded gzipped data",
),
(
False,
"dGVzdDHwn4yKdGVzdDI=",
"test1\N{WATER WAVE}test2".encode("utf-8"),
"base64-encoded Unicode text",
),
),
)
@mock.patch("cloudinit.url_helper.readurl")
def test_fetch_metadata(self, readurl, use_v6: bool):
def test_fetch_metadata(
self,
readurl,
use_v6: bool,
userdata: str,
decoded_userdata: str,
case: str,
):
"""
Tests that making requests sends the expected requests in the expected
order
Expand All @@ -280,7 +309,7 @@ def test_fetch_metadata(self, readurl, use_v6: bool):
# to GET /v1/instance; truncated for brevity
'{"id": 123}',
# to GET /v1/user-data
"",
userdata,
]

# if we're asked to force using v6, we should see the hostname of the
Expand All @@ -292,7 +321,7 @@ def test_fetch_metadata(self, readurl, use_v6: bool):

assert readurl.call_count == 3
assert ds.metadata == {"id": 123}
assert ds.userdata_raw == ""
assert ds.userdata_raw == decoded_userdata, f"Failed to decode {case}"

assert readurl.mock_calls == [
mock.call(
Expand Down
2 changes: 2 additions & 0 deletions tools/.github-cla-signers
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,10 @@ izzyleung
j5awry
jacobsalmela
jamesottinger
jberner12
jcmoore3
Jehops
jessealter
jf
jfroche
jgrassler
Expand Down

0 comments on commit ce80781

Please sign in to comment.