Skip to content
This repository was archived by the owner on Mar 13, 2022. It is now read-only.

Commit 8f0ccdb

Browse files
bpicoloroycaihw
authored andcommitted
Add additional checks + test case fixes
1 parent 4e5e565 commit 8f0ccdb

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

config/kube_config.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -268,12 +268,23 @@ def _load_oid_token(self, provider):
268268
if 'config' not in provider:
269269
return
270270

271-
parts = provider['config']['id-token'].split('.')
271+
reserved_characters = frozenset(["=", "+", "/"])
272+
token = provider['config']['id-token']
272273

274+
if any(char in token for char in reserved_characters):
275+
# Invalid jwt, as it contains url-unsafe chars
276+
return None
277+
278+
parts = token.split('.')
273279
if len(parts) != 3: # Not a valid JWT
274280
return None
275281

276282
padding = (4 - len(parts[1]) % 4) * '='
283+
if len(padding) == 3:
284+
# According to spec, 3 padding characters cannot occur
285+
# in a valid jwt
286+
# https://tools.ietf.org/html/rfc7515#appendix-C
287+
return None
277288

278289
if PY3:
279290
jwt_attributes = json.loads(

config/kube_config_test.py

+16-8
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ def _base64(string):
4949
return base64.standard_b64encode(string.encode()).decode()
5050

5151

52-
def _unpadded_base64(string):
53-
return base64.b64encode(string.encode()).decode().rstrip('=')
52+
def _urlsafe_unpadded_b64encode(string):
53+
return base64.urlsafe_b64encode(string.encode()).decode().rstrip('=')
5454

5555

5656
def _format_expiry_datetime(dt):
@@ -100,14 +100,22 @@ def _raise_exception(st):
100100

101101
TEST_OIDC_TOKEN = "test-oidc-token"
102102
TEST_OIDC_INFO = "{\"name\": \"test\"}"
103-
TEST_OIDC_BASE = _unpadded_base64(
104-
TEST_OIDC_TOKEN) + "." + _unpadded_base64(TEST_OIDC_INFO)
105-
TEST_OIDC_LOGIN = TEST_OIDC_BASE + "." + TEST_CLIENT_CERT_BASE64
103+
TEST_OIDC_BASE = ".".join([
104+
_urlsafe_unpadded_b64encode(TEST_OIDC_TOKEN),
105+
_urlsafe_unpadded_b64encode(TEST_OIDC_INFO)
106+
])
107+
TEST_OIDC_LOGIN = ".".join([
108+
TEST_OIDC_BASE,
109+
_urlsafe_unpadded_b64encode(TEST_CLIENT_CERT_BASE64)
110+
])
106111
TEST_OIDC_TOKEN = "Bearer %s" % TEST_OIDC_LOGIN
107112
TEST_OIDC_EXP = "{\"name\": \"test\",\"exp\": 536457600}"
108-
TEST_OIDC_EXP_BASE = _unpadded_base64(
109-
TEST_OIDC_TOKEN) + "." + _unpadded_base64(TEST_OIDC_EXP)
110-
TEST_OIDC_EXPIRED_LOGIN = TEST_OIDC_EXP_BASE + "." + TEST_CLIENT_CERT_BASE64
113+
TEST_OIDC_EXP_BASE = _urlsafe_unpadded_b64encode(
114+
TEST_OIDC_TOKEN) + "." + _urlsafe_unpadded_b64encode(TEST_OIDC_EXP)
115+
TEST_OIDC_EXPIRED_LOGIN = ".".join([
116+
TEST_OIDC_EXP_BASE,
117+
_urlsafe_unpadded_b64encode(TEST_CLIENT_CERT)
118+
])
111119
TEST_OIDC_CA = _base64(TEST_CERTIFICATE_AUTH)
112120

113121

0 commit comments

Comments
 (0)