-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
feat: Improve impersonation logic #26891
Open
benjackwhite
wants to merge
11
commits into
master
Choose a base branch
from
feat/extended-impersonation
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.
+99
−14
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
bba8d6c
Improve auto logout
benjackwhite 8d89d89
Increases the values
benjackwhite 98e8738
Fixes
benjackwhite f1cf66e
Merge branch 'master' into feat/extended-impersonation
pauldambra bcf8240
Merge branch 'master' into feat/extended-impersonation
benjackwhite ff5b5cc
Merge branch 'master' into feat/extended-impersonation
benjackwhite ea8fa8e
Update UI snapshots for `chromium` (2)
github-actions[bot] 4cf837e
Update UI snapshots for `chromium` (2)
github-actions[bot] dcaf969
Update UI snapshots for `chromium` (2)
github-actions[bot] 00da3a5
Merge branch 'master' into feat/extended-impersonation
benjackwhite e515764
Merge branch 'master' into feat/extended-impersonation
benjackwhite 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
Binary file modified
BIN
+496 Bytes
(100%)
...snapshots__/scenes-app-insights--funnel-top-to-bottom-breakdown-edit--light.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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
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
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 |
---|---|---|
|
@@ -501,7 +501,8 @@ def test_logout(self): | |
self.assertNotIn("ph_current_project_name", response.cookies) | ||
|
||
|
||
@override_settings(IMPERSONATION_TIMEOUT_SECONDS=30) | ||
@override_settings(IMPERSONATION_TIMEOUT_SECONDS=100) | ||
@override_settings(IMPERSONATION_IDLE_TIMEOUT_SECONDS=20) | ||
class TestAutoLogoutImpersonateMiddleware(APIBaseTest): | ||
other_user: User | ||
|
||
|
@@ -538,21 +539,65 @@ def test_not_staff_user_cannot_login(self): | |
assert response.status_code == 200 | ||
assert self.client.get("/api/users/@me").json()["email"] == self.user.email | ||
|
||
def test_after_timeout_api_requests_401(self): | ||
now = datetime.now() | ||
def test_after_idle_timeout_api_requests_401(self): | ||
now = datetime(2024, 1, 1, 12, 0, 0) | ||
with freeze_time(now): | ||
self.login_as_other_user() | ||
res = self.client.get("/api/users/@me") | ||
assert res.status_code == 200 | ||
assert res.json()["email"] == "[email protected]" | ||
assert res.json()["is_impersonated_until"] == "2024-01-01T12:00:20+00:00" | ||
assert self.client.session.get("session_created_at") == now.timestamp() | ||
|
||
with freeze_time(now + timedelta(seconds=10)): | ||
# Move forward by 19 | ||
now = now + timedelta(seconds=19) | ||
with freeze_time(now): | ||
res = self.client.get("/api/users/@me") | ||
assert res.status_code == 200 | ||
assert res.json()["email"] == "[email protected]" | ||
assert res.json()["is_impersonated_until"] == "2024-01-01T12:00:39+00:00" | ||
|
||
with freeze_time(now + timedelta(seconds=35)): | ||
# Past idle timeout | ||
now = now + timedelta(seconds=21) | ||
|
||
with freeze_time(now): | ||
res = self.client.get("/api/users/@me") | ||
assert res.status_code == 401 | ||
|
||
def test_after_total_timeout_api_requests_401(self): | ||
now = datetime(2024, 1, 1, 12, 0, 0) | ||
with freeze_time(now): | ||
self.login_as_other_user() | ||
res = self.client.get("/api/users/@me") | ||
assert res.status_code == 200 | ||
assert res.json()["email"] == "[email protected]" | ||
assert res.json()["is_impersonated_until"] == "2024-01-01T12:00:20+00:00" | ||
assert self.client.session.get("session_created_at") == now.timestamp() | ||
|
||
for _ in range(4): | ||
# Move forward by 19 seconds 4 times for a total of 76 seconds | ||
now = now + timedelta(seconds=19) | ||
with freeze_time(now): | ||
res = self.client.get("/api/users/@me") | ||
assert res.status_code == 200 | ||
assert res.json()["email"] == "[email protected]" | ||
# Format exactly like the date above | ||
assert res.json()["is_impersonated_until"] == (now + timedelta(seconds=20)).strftime( | ||
"%Y-%m-%dT%H:%M:%S+00:00" | ||
) | ||
|
||
now = now + timedelta(seconds=19) | ||
with freeze_time(now): | ||
res = self.client.get("/api/users/@me") | ||
assert res.status_code == 200 | ||
assert res.json()["email"] == "[email protected]" | ||
# Even though below the idle timeout, we now see the total timeout as that is earlier | ||
assert res.json()["is_impersonated_until"] == "2024-01-01T12:01:40+00:00" | ||
|
||
# Now even less than the idle time will take us past the total timeout | ||
now = now + timedelta(seconds=10) | ||
|
||
with freeze_time(now): | ||
res = self.client.get("/api/users/@me") | ||
assert res.status_code == 401 | ||
|
||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thinking out loud
a mini google suggests the default session engine signs the cookie content so a naughty person can't send their own init time here
(and anyway it would only be useful to an attacker if they had control of an impersonated session which would mean even if you could edit this then it wouldn't matter since they have impersonation)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you're confusing session and cookies. The cookie just has a
sessionid
. The session is loaded in django from the DB. So here it is modifying the session DB entry, nothing from the user.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome, bad googling from me 👍