-
Notifications
You must be signed in to change notification settings - Fork 40
OpenConceptLab/ocl_online#51 | Block Anonymous API usage except from approved clients #845
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
snyaggarwal
wants to merge
2
commits into
master
Choose a base branch
from
ocl_online/issues#51
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+288
−2
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| import json | ||
|
|
||
| from django.conf import settings | ||
| from django.contrib.auth.models import AnonymousUser | ||
| from django.http import HttpResponse | ||
| from django.test import RequestFactory, SimpleTestCase, override_settings | ||
|
|
||
| from core.middlewares.middlewares import RequireAuthenticationMiddleware | ||
|
|
||
|
|
||
| @override_settings( | ||
| REQUIRE_AUTHENTICATION=True, | ||
| APPROVED_ANONYMOUS_CLIENTS=['test-client'], | ||
| APPROVED_ANONYMOUS_API_KEYS=['test-api-key'], | ||
| APPROVED_ANONYMOUS_IPS=['10.0.0.1'], | ||
| ) | ||
| class RequireAuthenticationMiddlewareTest(SimpleTestCase): | ||
| """Verify anonymous authentication enforcement and approved bypasses.""" | ||
|
|
||
| def setUp(self): | ||
| """Create a request factory and middleware instance for each test.""" | ||
| self.factory = RequestFactory() | ||
| self.middleware = RequireAuthenticationMiddleware(lambda request: HttpResponse('ok')) | ||
|
|
||
| def make_request(self, path='/orgs/', method='get', user=None, **meta): | ||
| """Build a request object with a controllable authenticated user state.""" | ||
| request_method = getattr(self.factory, method.lower()) | ||
| request = request_method(path, **meta) | ||
| request.user = user or AnonymousUser() | ||
| return request | ||
|
|
||
| def test_allows_authenticated_request(self): | ||
| """Authenticated requests should bypass the anonymous access gate.""" | ||
| user = type('AuthenticatedUser', (), {'is_authenticated': True})() | ||
|
|
||
| response = self.middleware(self.make_request(user=user)) | ||
|
|
||
| self.assertEqual(response.status_code, 200) | ||
|
|
||
| def test_blocks_anonymous_request_for_protected_path(self): | ||
| """Anonymous traffic to protected API paths should receive a 403 response.""" | ||
| response = self.middleware(self.make_request('/orgs/OCL/')) | ||
|
|
||
| self.assertEqual(response.status_code, 403) | ||
| self.assertEqual( | ||
| json.loads(response.content), | ||
| { | ||
| 'detail': 'Authentication required. Anonymous API access is disabled.', | ||
| 'upgrade_url': 'https://app.openconceptlab.org/pricing', | ||
| } | ||
| ) | ||
|
|
||
| def test_allows_anonymous_request_for_approved_client_header(self): | ||
| """Approved X-OCL-CLIENT values should retain anonymous access.""" | ||
| response = self.middleware(self.make_request('/orgs/OCL/', HTTP_X_OCL_CLIENT='test-client')) | ||
|
|
||
| self.assertEqual(response.status_code, 200) | ||
|
|
||
| def test_blocks_anonymous_request_for_unapproved_client_header(self): | ||
| """Unknown X-OCL-CLIENT values should still be rejected.""" | ||
| response = self.middleware(self.make_request('/orgs/OCL/', HTTP_X_OCL_CLIENT='unknown-client')) | ||
|
|
||
| self.assertEqual(response.status_code, 403) | ||
|
|
||
| def test_blocks_anonymous_request_for_whitespace_client_header(self): | ||
| """Whitespace-only client header values should be rejected after normalization.""" | ||
| response = self.middleware(self.make_request('/orgs/OCL/', HTTP_X_OCL_CLIENT=' ')) | ||
|
|
||
| self.assertEqual(response.status_code, 403) | ||
|
|
||
| def test_allows_anonymous_request_for_approved_api_key_header(self): | ||
| """Allowlisted anonymous API keys should bypass the gate.""" | ||
| response = self.middleware(self.make_request('/orgs/OCL/', HTTP_X_API_KEY='test-api-key')) | ||
|
|
||
| self.assertEqual(response.status_code, 200) | ||
|
|
||
| def test_allows_anonymous_request_for_approved_authorization_token(self): | ||
| """Allowlisted bearer or token credentials should bypass the gate.""" | ||
| response = self.middleware(self.make_request('/orgs/OCL/', HTTP_AUTHORIZATION='Token test-api-key')) | ||
|
|
||
| self.assertEqual(response.status_code, 200) | ||
|
|
||
| def test_blocks_anonymous_request_for_query_string_api_key(self): | ||
| """Query string API keys should not bypass the authentication gate.""" | ||
| response = self.middleware(self.make_request('/orgs/OCL/?api_key=test-api-key')) | ||
|
|
||
| self.assertEqual(response.status_code, 403) | ||
|
|
||
| def test_allows_anonymous_request_for_approved_ip(self): | ||
| """Allowlisted source IPs should keep anonymous access.""" | ||
| response = self.middleware(self.make_request('/orgs/OCL/', REMOTE_ADDR='10.0.0.1')) | ||
|
|
||
| self.assertEqual(response.status_code, 200) | ||
|
|
||
| def test_blocks_anonymous_request_for_forwarded_ip_only(self): | ||
| """Forwarded IP headers alone should not bypass the authentication gate.""" | ||
| response = self.middleware( | ||
| self.make_request('/orgs/OCL/', HTTP_X_FORWARDED_FOR='10.0.0.1', REMOTE_ADDR='203.0.113.5') | ||
| ) | ||
|
|
||
| self.assertEqual(response.status_code, 403) | ||
|
|
||
| def test_allows_options_request(self): | ||
| """CORS preflight requests should not be blocked.""" | ||
| response = self.middleware(self.make_request('/orgs/OCL/', method='options')) | ||
|
|
||
| self.assertEqual(response.status_code, 200) | ||
|
|
||
| def test_allows_elb_health_checker_request(self): | ||
| """Infrastructure health checks should bypass the gate.""" | ||
| response = self.middleware( | ||
| self.make_request('/orgs/OCL/', HTTP_USER_AGENT='ELB-HealthChecker/2.0') | ||
| ) | ||
|
|
||
| self.assertEqual(response.status_code, 200) | ||
|
|
||
| def test_allows_exempt_exact_paths(self): | ||
| """Public root-level utility endpoints should remain anonymous.""" | ||
| for path in ['/', '/version/', '/changelog/', '/feedback/', '/toggles/', '/locales/', '/events/']: | ||
| with self.subTest(path=path): | ||
| response = self.middleware(self.make_request(path)) | ||
| self.assertEqual(response.status_code, 200) | ||
|
|
||
| def test_allows_exempt_path_prefixes(self): | ||
| """Auth, docs, admin, and FHIR prefixes should remain anonymous.""" | ||
| paths = [ | ||
| '/healthcheck/', | ||
| '/users/api-token/', | ||
| '/users/login/', | ||
| '/users/logout/', | ||
| '/users/signup/', | ||
| '/users/password/reset/', | ||
| '/users/oidc/code-exchange/', | ||
| '/oidc/authenticate/', | ||
| '/fhir/', | ||
| '/swagger/', | ||
| '/swagger.json', | ||
| '/redoc/', | ||
| '/admin/login/', | ||
| ] | ||
|
|
||
| for path in paths: | ||
| with self.subTest(path=path): | ||
| response = self.middleware(self.make_request(path)) | ||
| self.assertEqual(response.status_code, 200) | ||
|
|
||
| def test_allows_exempt_dynamic_user_paths(self): | ||
| """Public user verification and following endpoints should remain anonymous.""" | ||
| paths = [ | ||
| '/users/alice/verify/token-123/', | ||
| '/users/alice/sso-migrate/', | ||
| '/users/alice/following/', | ||
| ] | ||
|
|
||
| for path in paths: | ||
| with self.subTest(path=path): | ||
| response = self.middleware(self.make_request(path)) | ||
| self.assertEqual(response.status_code, 200) | ||
|
|
||
|
|
||
| @override_settings(REQUIRE_AUTHENTICATION=False) | ||
| class RequireAuthenticationSettingsTest(SimpleTestCase): | ||
| """Verify auth-enforcement middleware configuration toggles cleanly.""" | ||
|
|
||
| def test_authentication_middleware_not_inserted_when_disabled(self): | ||
| """RequireAuthenticationMiddleware should be absent when the feature is disabled.""" | ||
| self.assertNotIn('core.middlewares.middlewares.RequireAuthenticationMiddleware', settings.MIDDLEWARE) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.