8000 fix: ensure JWT segments have the right types (#1162) · TJB-1/google-auth-library-python@fc843cd · GitHub
[go: up one dir, main page]

Skip to content

Commit fc843cd

Browse files
authored
fix: ensure JWT segments have the right types (googleapis#1162)
* fix: ensure JWT header is a dict before accessing its methods
1 parent 370293e commit fc843cd

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

google/auth/jwt.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,12 @@ def _unverified_decode(token):
133133
token (Union[str, bytes]): The encoded JWT.
134134
135135
Returns:
136-
Tuple[str, str, str, str]: header, payload, signed_section, and
136+
Tuple[Mapping, Mapping, str, str]: header, payload, signed_section, and
137137
signature.
138138
139139
Raises:
140-
ValueError: if there are an incorrect amount of segments in the token.
140+
ValueError: if there are an incorrect amount of segments in the token or
141+
segments of the wrong type.
141142
"""
142143
token = _helpers.to_bytes(token)
143144

@@ -152,6 +153,16 @@ def _unverified_decode(token):
152153
header = _decode_jwt_segment(encoded_header)
153154
payload = _decode_jwt_segment(encoded_payload)
154155

156+
if not isinstance(header, Mapping):
157+
raise ValueError(
158+
"Header segment should be a JSON object: {0}".format(encoded_header)
159+
)
160+
161+
if not isinstance(payload, Mapping):
162+
raise ValueError(
163+
"Payload segment should be a JSON object: {0}".format(encoded_payload)
164+
)
165+
155166
return header, payload, signed_section, signature
156167

157168

system_tests/secrets.tar.enc

0 Bytes
Binary file not shown.

tests/test_jwt.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,29 @@ def test_decode_valid(token_factory):
126126
assert payload["metadata"]["meta"] == "data"
127127

128128

129+
def test_decode_header_object(token_factory):
130+
payload = token_factory()
131+
# Create a malformed JWT token with a number as a header instead of a
132+
# dictionary (3 == base64d(M7==))
133+
payload = b"M7." + b".".join(payload.split(b".")[1:])
134+
135+
with pytest.raises(ValueError) as excinfo:
136+
jwt.decode(payload, certs=PUBLIC_CERT_BYTES)
137+
assert excinfo.match(r"Header segment should be a JSON object: " + str(b"M7"))
138+
139+
140+
def test_decode_payload_object(signer):
141+
# Create a malformed JWT token with a payload containing both "iat" and
142+
# "exp" strings, although not as fields of a dictionary
143+ 76D7
payload = jwt.encode(signer, "iatexp")
144+
145+
with pytest.raises(ValueError) as excinfo:
146+
jwt.decode(payload, certs=PUBLIC_CERT_BYTES)
147+
assert excinfo.match(
148+
r"Payload segment should be a JSON object: " + str(b"ImlhdGV4cCI")
149+
)
150+
151+
129152
def test_decode_valid_es256(token_factory):
130153
payload = jwt.decode(
131154
token_factory(use_es256_signer=True), certs=EC_PUBLIC_CERT_BYTES

0 commit comments

Comments
 (0)
0