-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(feature-flags): Add a setting to prevent certain teams from abusi…
…ng decide endpoint (#24902) Adds a DECIDE_SHORT_CIRCUITED_TEAM_IDS property settings/web.py which is then used by the /decide endpoint to disable access to the endpoint.
- Loading branch information
Showing
3 changed files
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -140,6 +140,17 @@ def get_decide(request: HttpRequest): | |
team = user.teams.get(id=project_id) | ||
|
||
if team: | ||
if team.id in settings.DECIDE_SHORT_CIRCUITED_TEAM_IDS: | ||
return cors_response( | ||
request, | ||
generate_exception_response( | ||
"decide", | ||
f"Team with ID {team.id} cannot access the /decide endpoint." | ||
f"Please contact us at [email protected]", | ||
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, | ||
), | ||
) | ||
|
||
token = cast(str, token) # we know it's not None if we found a team | ||
structlog.contextvars.bind_contextvars(team_id=team.id) | ||
|
||
|
This file contains 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 |
---|---|---|
|
@@ -29,6 +29,7 @@ | |
Plugin, | ||
PluginConfig, | ||
PluginSourceFile, | ||
Project, | ||
) | ||
from posthog.models.cohort.cohort import Cohort | ||
from posthog.models.feature_flag.feature_flag import FeatureFlagHashKeyOverride | ||
|
@@ -2647,6 +2648,39 @@ def test_missing_token(self, *args): | |
response = self._post_decide({"distinct_id": "example_id", "api_key": None, "project_id": self.team.id}) | ||
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) | ||
|
||
def test_short_circuited_team(self, *args): | ||
short_circuited_team_token = "short_circuited_team_token" | ||
|
||
_, short_circuited_team = Project.objects.create_with_team( | ||
organization=self.organization, | ||
team_fields={ | ||
"api_token": short_circuited_team_token, | ||
"test_account_filters": [ | ||
{ | ||
"key": "email", | ||
"value": "@posthog.com", | ||
"operator": "not_icontains", | ||
"type": "person", | ||
} | ||
], | ||
"has_completed_onboarding_for": {"product_analytics": True}, | ||
}, | ||
) | ||
with self.settings(DECIDE_SHORT_CIRCUITED_TEAM_IDS=[short_circuited_team.id]): | ||
response = self._post_decide( | ||
{ | ||
"distinct_id": "example_id", | ||
"api_key": short_circuited_team_token, | ||
"project_id": short_circuited_team.id, | ||
} | ||
) | ||
self.assertEqual(response.status_code, status.HTTP_422_UNPROCESSABLE_ENTITY) | ||
response_data = response.json() | ||
self.assertEqual( | ||
response_data["detail"], | ||
f"Team with ID {short_circuited_team.id} cannot access the /decide endpoint.Please contact us at [email protected]", | ||
) | ||
|
||
def test_invalid_payload_on_decide_endpoint(self, *args): | ||
invalid_payloads = [ | ||
base64.b64encode(b"1-1").decode("utf-8"), | ||
|
This file contains 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