|
2 | 2 | import datetime |
3 | 3 | import json |
4 | 4 | import os |
| 5 | +import socket |
5 | 6 | import tempfile |
6 | 7 | import unittest |
| 8 | +from unittest.mock import patch |
7 | 9 |
|
8 | 10 | from cryptography.hazmat.primitives.asymmetric import rsa |
9 | 11 | from tornado.testing import AsyncHTTPTestCase |
|
17 | 19 | patched_jwks_client, |
18 | 20 | ) |
19 | 21 |
|
| 22 | +# The fake "idp.example.com" JWKS host used by these tests doesn't |
| 23 | +# resolve. TabPyApp resolves the JWKS host at startup to guard against |
| 24 | +# SSRF, so tests need a fake public resolution result in place of a real |
| 25 | +# DNS lookup. |
| 26 | +_PUBLIC_JWKS_ADDRINFO = [ |
| 27 | + (socket.AF_INET, socket.SOCK_STREAM, 6, "", ("93.184.216.34", 443)), |
| 28 | +] |
| 29 | + |
20 | 30 |
|
21 | 31 | class BaseTestOAuthHandler(AsyncHTTPTestCase): |
22 | 32 | def get_app(self): |
23 | | - self.app = TabPyApp(self.config_file.name) |
| 33 | + with patch( |
| 34 | + "tabpy.tabpy_server.app.app.socket.getaddrinfo", |
| 35 | + return_value=_PUBLIC_JWKS_ADDRINFO, |
| 36 | + ): |
| 37 | + self.app = TabPyApp(self.config_file.name) |
24 | 38 | return self.app._create_tornado_web_app() |
25 | 39 |
|
26 | 40 | @classmethod |
@@ -99,6 +113,35 @@ def test_expired_jwt_returns_401(self): |
99 | 113 | response = self.fetch("/info", headers=headers) |
100 | 114 | self.assertEqual(response.code, 401) |
101 | 115 |
|
| 116 | + def test_401_response_body_is_uniform_across_jwt_failure_reasons(self): |
| 117 | + """ |
| 118 | + The specific reason a JWT was rejected (expired, wrong issuer, |
| 119 | + wrong audience, unresolvable signing key, etc.) must never appear |
| 120 | + in the response body -- that would let an unauthenticated caller |
| 121 | + enumerate why a token failed. Every failure reason must produce |
| 122 | + the exact same generic body. |
| 123 | + """ |
| 124 | + past = datetime.datetime.now(datetime.timezone.utc) - datetime.timedelta(hours=1) |
| 125 | + expired_token = self._make_token( |
| 126 | + {"iat": past - datetime.timedelta(minutes=5), "exp": past} |
| 127 | + ) |
| 128 | + wrong_issuer_token = self._make_token({"iss": "https://wrong-idp.example.com/"}) |
| 129 | + wrong_audience_token = self._make_token({"aud": "wrong-audience"}) |
| 130 | + |
| 131 | + bodies = [] |
| 132 | + for token in (expired_token, wrong_issuer_token, wrong_audience_token): |
| 133 | + headers = {"Authorization": f"Bearer {token}"} |
| 134 | + with self._patched_jwks_client(): |
| 135 | + response = self.fetch("/info", headers=headers) |
| 136 | + self.assertEqual(response.code, 401) |
| 137 | + bodies.append(response.body) |
| 138 | + |
| 139 | + missing_token_response = self.fetch("/info") |
| 140 | + self.assertEqual(missing_token_response.code, 401) |
| 141 | + bodies.append(missing_token_response.body) |
| 142 | + |
| 143 | + self.assertEqual(len(set(bodies)), 1) |
| 144 | + |
102 | 145 | def test_info_advertises_oauth_jwt_method(self): |
103 | 146 | token = self._make_token() |
104 | 147 | headers = {"Authorization": f"Bearer {token}"} |
|
0 commit comments