Repository navigation
Lapse OAuth grants left idle for 90 days #3161
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
Merged
+182
−7
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
c96be10
Lapse OAuth grants left idle for 90 days
jeremy b8df79a
Enforce the idle limit in the rotation itself, and lapse grants writt…
jeremy d0efc0e
Measure idleness from the grant's last rotation, not a stored deadline
jeremy 50499f8
Recheck each lapsed grant under its lock before the sweep deletes it
jeremy d96cc01
Measure the idle limit in fixed seconds
jeremy 9bf0974
Hand the sweep the grant as it was read, so the race pin needs the lock
jeremy 1a9a232
Merge oauth-refresh-replay: its migrations can be run again after sto…
jeremy b7e4bb4
Merge oauth-refresh-replay
jeremy f5c405b
Merge oauth-refresh-replay into oauth-grant-idle-expiry
jeremy 09158fd
Merge oauth-refresh-replay into oauth-grant-idle-expiry
jeremy 64bcc77
Merge oauth-refresh-replay into oauth-grant-idle-expiry
jeremy 585770e
Merge oauth-refresh-replay into oauth-grant-idle-expiry
jeremy 1c794ea
Merge oauth-refresh-replay into oauth-grant-idle-expiry
jeremy c4617f7
Merge oauth-refresh-replay into oauth-grant-idle-expiry
jeremy d36a630
Merge oauth-refresh-replay into oauth-grant-idle-expiry
jeremy 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
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
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
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,119 @@ | ||
| require "test_helper" | ||
|
|
||
| # A grant lapses once its refresh token goes unused for the idle window, as | ||
| # bc3's refresh tokens do: each refresh restarts the clock, and a lapsed grant | ||
| # takes a fresh authorization, with consent. | ||
| class OauthGrantIdleExpiryTest < ActionDispatch::IntegrationTest | ||
| setup do | ||
| @client = oauth_clients(:mcp_client) | ||
| freeze_time | ||
| @grant = identities(:david).access_tokens.create!(oauth_client: @client, permission: :read) | ||
| end | ||
|
|
||
| test "a grant refreshes up to the end of the idle window" do | ||
| later_by Identity::AccessToken::REFRESH_IDLE_LIMIT | ||
| refresh @grant.refresh_token | ||
|
|
||
| assert_response :success | ||
| end | ||
|
|
||
| test "a grant idle past the window lapses" do | ||
| later_by Identity::AccessToken::REFRESH_IDLE_LIMIT + 1.second | ||
| refresh @grant.refresh_token | ||
|
|
||
| assert_response :bad_request | ||
| assert_equal "invalid_grant", response.parsed_body["error"] | ||
| end | ||
|
|
||
| test "each refresh restarts the idle clock" do | ||
| later_by Identity::AccessToken::REFRESH_IDLE_LIMIT - 1.day | ||
| refresh @grant.refresh_token | ||
| assert_response :success | ||
|
|
||
| later_by Identity::AccessToken::REFRESH_IDLE_LIMIT - 1.day | ||
| refresh response.parsed_body["refresh_token"] | ||
|
|
||
| assert_response :success | ||
| end | ||
|
|
||
| test "a lapsed grant leaves Connected Apps" do | ||
| sign_in_as :david | ||
|
|
||
| later_by Identity::AccessToken::REFRESH_IDLE_LIMIT + 1.second | ||
| get my_connected_apps_path | ||
|
|
||
| assert_response :success | ||
| assert_no_match @client.name, response.body | ||
| end | ||
|
|
||
| test "the sweep removes lapsed grants and keeps live ones" do | ||
| live = identities(:david).access_tokens.create!(oauth_client: @client, permission: :read) | ||
| personal = identities(:david).access_tokens.create!(permission: :read, description: "PAT") | ||
|
|
||
| later_by Identity::AccessToken::REFRESH_IDLE_LIMIT - 1.day | ||
| live.refresh | ||
|
|
||
| later_by 1.day + 1.second | ||
| Identity::AccessToken.cleanup | ||
|
|
||
| assert_not Identity::AccessToken.exists?(@grant.id) | ||
| assert Identity::AccessToken.exists?(live.id) | ||
| assert Identity::AccessToken.exists?(personal.id) | ||
| end | ||
|
|
||
| test "the sweep spares a grant renewed after it was selected" do | ||
| later_by Identity::AccessToken::REFRESH_IDLE_LIMIT + 1.second | ||
| # The sweep holds the grant as it was read when selected, before the renewal. | ||
| selection = stub | ||
| selection.stubs(:find_each).yields(Identity::AccessToken.find(@grant.id)) | ||
| Identity::AccessToken.stubs(:lapsed).returns(selection) | ||
|
|
||
| Identity::AccessToken.where(id: @grant.id).update_all(updated_at: Time.current) | ||
| Identity::AccessToken.cleanup | ||
|
|
||
| assert Identity::AccessToken.exists?(@grant.id) | ||
| end | ||
|
|
||
| test "rotation itself refuses a lapsed grant, so one racing the deadline can't renew it" do | ||
| later_by Identity::AccessToken::REFRESH_IDLE_LIMIT + 1.second | ||
| presented = @grant.refresh_token | ||
|
|
||
| assert_nil @grant.refresh | ||
| assert_equal presented, @grant.reload.refresh_token | ||
| end | ||
|
|
||
| test "the idle clock is the grant's last rotation, so a rotation by older code mid-deploy restarts it too" do | ||
| later_by Identity::AccessToken::REFRESH_IDLE_LIMIT - 1.day | ||
| rotated_by_older_code = Identity::AccessToken.generate_unique_secure_token | ||
| Identity::AccessToken.where(id: @grant.id).update_all(refresh_token: rotated_by_older_code, updated_at: Time.current) | ||
|
|
||
| later_by Identity::AccessToken::REFRESH_IDLE_LIMIT - 1.day | ||
| refresh rotated_by_older_code | ||
|
|
||
| assert_response :success | ||
| end | ||
|
|
||
| test "a lapsed grant takes its entry point off the profile page too" do | ||
| sign_in_as :david | ||
|
|
||
| get user_path(users(:david)) | ||
| assert_match my_connected_apps_path, response.body | ||
|
|
||
| later_by Identity::AccessToken::REFRESH_IDLE_LIMIT + 1.second | ||
| get user_path(users(:david)) | ||
| assert_no_match my_connected_apps_path, response.body | ||
| end | ||
|
|
||
| private | ||
| # Elapsed time in the app's zone, as the model counts it: travel would add | ||
| # calendar days in the local zone, an hour off across a DST change. | ||
| def later_by(duration) | ||
| travel_to Time.current + duration | ||
| end | ||
|
|
||
| def refresh(refresh_token) | ||
| untenanted do | ||
| post oauth_token_path, params: { grant_type: "refresh_token", refresh_token: refresh_token, client_id: @client.client_id } | ||
| end | ||
| end | ||
| end |
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.