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

Commit ba8b9e0

Browse files
bpicoloroycaihw
authored andcommitted
Add additional checks + test case fixes
1 parent 8bee2f8 commit ba8b9e0

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
@@ -259,12 +259,23 @@ def _load_oid_token(self, provider):
259259
if 'config' not in provider:
260260
return
261261

262-
parts = provider['config']['id-token'].split('.')
262+
reserved_characters = frozenset(["=", "+", "/"])
263+
token = provider['config']['id-token']
263264

265+
if any(char in token for char in reserved_characters):
266+
# Invalid jwt, as it contains url-unsafe chars
267+
return None
268+
269+
parts = token.split('.')
264270
if len(parts) != 3: # Not a valid JWT
265271
return None
266272

267273
padding = (4 - len(parts[1]) % 4) * '='
274+
if len(padding) == 3:
275+
# According to spec, 3 padding characters cannot occur
276+
# in a valid jwt
277+
# https://tools.ietf.org/html/rfc7515#appendix-C
278+
return None
268279

269280
if PY3:
270281
jwt_attributes = json.loads(

config/kube_config_test.py

+16-8
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ def _base64(string):
4747
return base64.standard_b64encode(string.encode()).decode()
4848

4949

50-
def _unpadded_base64(string):
51-
return base64.b64encode(string.encode()).decode().rstrip('=')
50+
def _urlsafe_unpadded_b64encode(string):
51+
return base64.urlsafe_b64encode(string.encode()).decode().rstrip('=')
5252

5353

5454
def _format_expiry_datetime(dt):
@@ -98,14 +98,22 @@ def _raise_exception(st):
9898

9999
TEST_OIDC_TOKEN = "test-oidc-token"
100100
TEST_OIDC_INFO = "{\"name\": \"test\"}"
101-
TEST_OIDC_BASE = _unpadded_base64(
102-
TEST_OIDC_TOKEN) + "." + _unpadded_base64(TEST_OIDC_INFO)
103-
TEST_OIDC_LOGIN = TEST_OIDC_BASE + "." + TEST_CLIENT_CERT_BASE64
101+
TEST_OIDC_BASE = ".".join([
102+
_urlsafe_unpadded_b64encode(TEST_OIDC_TOKEN),
103+
_urlsafe_unpadded_b64encode(TEST_OIDC_INFO)
104+
])
105+
TEST_OIDC_LOGIN = ".".join([
106+
TEST_OIDC_BASE,
107+
_urlsafe_unpadded_b64encode(TEST_CLIENT_CERT_BASE64)
108+
])
104109
TEST_OIDC_TOKEN = "Bearer %s" % TEST_OIDC_LOGIN
105110
TEST_OIDC_EXP = "{\"name\": \"test\",\"exp\": 536457600}"
106-
TEST_OIDC_EXP_BASE = _unpadded_base64(
107-
TEST_OIDC_TOKEN) + "." + _unpadded_base64(TEST_OIDC_EXP)
108-
TEST_OIDC_EXPIRED_LOGIN = TEST_OIDC_EXP_BASE + "." + TEST_CLIENT_CERT_BASE64
111+
TEST_OIDC_EXP_BASE = _urlsafe_unpadded_b64encode(
112+
TEST_OIDC_TOKEN) + "." + _urlsafe_unpadded_b64encode(TEST_OIDC_EXP)
113+
TEST_OIDC_EXPIRED_LOGIN = ".".join([
114+
TEST_OIDC_EXP_BASE,
115+
_urlsafe_unpadded_b64encode(TEST_CLIENT_CERT)
116+
])
109117
TEST_OIDC_CA = _base64(TEST_CERTIFICATE_AUTH)
110118

111119

0 commit comments

Comments
 (0)