Skip to content

Commit 4750aa9

Browse files
committed
Add additional checks + test case fixes
1 parent d54efa7 commit 4750aa9

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

config/kube_config.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,12 +252,23 @@ def _load_oid_token(self, provider):
252252
if 'config' not in provider:
253253
return
254254

255-
parts = provider['config']['id-token'].split('.')
255+
reserved_characters = frozenset(["=", "+", "/"])
256+
token = provider['config']['id-token']
256257

258+
if any(char in token for char in reserved_characters):
259+
# Invalid jwt, as it contains url-unsafe chars
260+
return None
261+
262+
parts = token.split('.')
257263
if len(parts) != 3: # Not a valid JWT
258264
return None
259265

260266
padding = (4 - len(parts[1]) % 4) * '='
267+
if len(padding) == 3:
268+
# According to spec, 3 padding characters cannot occur
269+
# in a valid jwt
270+
# https://tools.ietf.org/html/rfc7515#appendix-C
271+
return None
261272

262273
if PY3:
263274
jwt_attributes = json.loads(

config/kube_config_test.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ def _base64(string):
4343
return base64.encodestring(string.encode()).decode()
4444

4545

46-
def _unpadded_base64(string):
47-
return base64.b64encode(string.encode()).decode().rstrip('=')
46+
def _urlsafe_unpadded_b64encode(string):
47+
return base64.urlsafe_b64encode(string.encode()).decode().rstrip('=')
4848

4949

5050
def _format_expiry_datetime(dt):
@@ -91,14 +91,22 @@ def _raise_exception(st):
9191

9292
TEST_OIDC_TOKEN = "test-oidc-token"
9393
TEST_OIDC_INFO = "{\"name\": \"test\"}"
94-
TEST_OIDC_BASE = _unpadded_base64(
95-
TEST_OIDC_TOKEN) + "." + _unpadded_base64(TEST_OIDC_INFO)
96-
TEST_OIDC_LOGIN = TEST_OIDC_BASE + "." + TEST_CLIENT_CERT_BASE64
94+
TEST_OIDC_BASE = ".".join([
95+
_urlsafe_unpadded_b64encode(TEST_OIDC_TOKEN),
96+
_urlsafe_unpadded_b64encode(TEST_OIDC_INFO)
97+
])
98+
TEST_OIDC_LOGIN = ".".join([
99+
TEST_OIDC_BASE,
100+
_urlsafe_unpadded_b64encode(TEST_CLIENT_CERT_BASE64)
101+
])
97102
TEST_OIDC_TOKEN = "Bearer %s" % TEST_OIDC_LOGIN
98103
TEST_OIDC_EXP = "{\"name\": \"test\",\"exp\": 536457600}"
99-
TEST_OIDC_EXP_BASE = _unpadded_base64(
100-
TEST_OIDC_TOKEN) + "." + _unpadded_base64(TEST_OIDC_EXP)
101-
TEST_OIDC_EXPIRED_LOGIN = TEST_OIDC_EXP_BASE + "." + TEST_CLIENT_CERT_BASE64
104+
TEST_OIDC_EXP_BASE = _urlsafe_unpadded_b64encode(
105+
TEST_OIDC_TOKEN) + "." + _urlsafe_unpadded_b64encode(TEST_OIDC_EXP)
106+
TEST_OIDC_EXPIRED_LOGIN = ".".join([
107+
TEST_OIDC_EXP_BASE,
108+
_urlsafe_unpadded_b64encode(TEST_CLIENT_CERT)
109+
])
102110
TEST_OIDC_CA = _base64(TEST_CERTIFICATE_AUTH)
103111

104112

0 commit comments

Comments
 (0)