forked from doubtfire-lms/doubtfire-api
-
Notifications
You must be signed in to change notification settings - Fork 123
Fix/session binding validation #61
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
theiris6
wants to merge
6
commits into
thoth-tech:app-attack-fixes
Choose a base branch
from
theiris6:fix/session-binding-validation
base: app-attack-fixes
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.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4ca2475
Fix: Add session binding checks to prevent token reuse
theiris6 83662ee
fix(auth): add session binding check to prevent token reuse
theiris6 c3c91d1
app/helpers/authentication_helpers.rb:
theiris6 845d331
AuthToken.column_names
theiris6 eb3def3
Fix/typo aadd
theiris6 c1577c6
- Disgard changes in 20240920052508_convert_task_def_filenames
theiris6 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| # AuthToken Column Documentation | ||
| # This file documents the new columns added to auth_tokens table for security enhancements | ||
|
|
||
| # Original columns: | ||
| # id: primary key | ||
| # auth_token_expiry: datetime when token expires | ||
| # user_id: user who owns this token | ||
| # authentication_token: the actual token string | ||
| # token_type: type of token (general, api, etc.) | ||
| # session_ip: original IP address that created this token | ||
| # session_user_agent: original User-Agent that created this token | ||
|
|
||
| # New columns for session binding: | ||
| # last_seen_ip: most recent IP address that used this token | ||
| # last_seen_ua: most recent User-Agent that used this token | ||
| # ip_history: JSON array of all IPs that have used this token | ||
| # suspicious_activity_detected_at: timestamp when first suspicious change was detected | ||
|
|
||
| # New columns for session fixation/hijacking prevention: | ||
| # invalidation_requested_at: timestamp when logout was requested | ||
| # last_activity_at: timestamp of most recent activity with this token | ||
|
|
||
| # Security implementation notes: | ||
| # - When validating tokens, check invalidation_requested_at to prevent session fixation | ||
| # - Use ip_history to track and limit the number of different IPs that can use a token | ||
| # - suspicious_activity_detected_at starts a grace period for user to reauthenticate | ||
| # - The combined approach provides flexible session binding while preventing session stealing across users |
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,4 @@ | ||
| Rails.application.reloader.to_prepare do | ||
| load Rails.root.join("app/helpers/authentication_helpers.rb") | ||
| Rails.logger.info "Reloaded AuthenticationHelpers at #{Time.zone.now}" | ||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| # Be sure to restart your server when you modify this file. | ||
|
|
||
| # Configuration for authentication session security | ||
| Doubtfire::Application.config.session_security = { | ||
| binding_enabled: true, # Enable/disable session binding completely | ||
| ip_binding_strictness: :flexible, # :strict, :flexible, or :disabled | ||
| max_allowed_ip_changes: 3, # Maximum number of different IPs allowed per token | ||
| suspicious_change_timeout: 5.minutes, # Period to allow suspicious changes before requiring re-auth | ||
| token_max_lifetime: 8.hours, # Maximum lifetime of a token, regardless of activity | ||
| auth_enforcement_window: 15.seconds # Time window to check for forced session persistence | ||
| } | ||
| Rails.logger.info "Loading session security config at #{Time.zone.now}" |
15 changes: 15 additions & 0 deletions
15
db/migrate/20250419030255_add_session_binding_to_auth_tokens.rb
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,15 @@ | ||
| class AddSessionBindingToAuthTokens < ActiveRecord::Migration[7.1] | ||
| def change | ||
| add_column :auth_tokens, :last_seen_ip, :string unless column_exists?(:auth_tokens, :last_seen_ip) | ||
| add_column :auth_tokens, :last_seen_ua, :string unless column_exists?(:auth_tokens, :last_seen_ua) | ||
|
|
||
| # Use TEXT for JSON data in MySQL | ||
| add_column :auth_tokens, :ip_history, :text unless column_exists?(:auth_tokens, :ip_history) | ||
| add_column :auth_tokens, :suspicious_activity_detected_at, :datetime unless column_exists?(:auth_tokens, :suspicious_activity_detected_at) | ||
| add_column :auth_tokens, :invalidation_requested_at, :datetime unless column_exists?(:auth_tokens, :invalidation_requested_at) | ||
| add_column :auth_tokens, :last_activity_at, :datetime unless column_exists?(:auth_tokens, :last_activity_at) | ||
|
|
||
| # Add index for performance | ||
| add_index :auth_tokens, :invalidation_requested_at unless index_exists?(:auth_tokens, :invalidation_requested_at) | ||
| end | ||
| end |
26 changes: 26 additions & 0 deletions
26
db/migrate/20250428135250_add_session_binding_columns_to_auth_tokens.rb
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,26 @@ | ||
| class AddSessionBindingColumnsToAuthTokens < ActiveRecord::Migration[7.1] | ||
| def change | ||
| # Columns for session binding improvements | ||
| # Only add columns if they don't already exist | ||
| add_column :auth_tokens, :last_seen_ip, :string unless column_exists?(:auth_tokens, :last_seen_ip) | ||
| add_column :auth_tokens, :last_seen_ua, :string unless column_exists?(:auth_tokens, :last_seen_ua) | ||
|
|
||
| # For arrays, use JSON in MySQL/MariaDB since they don't support native arrays | ||
| # Detect database type and use appropriate column type | ||
| if ActiveRecord::Base.connection.adapter_name.downcase.include?('mysql') | ||
| add_column :auth_tokens, :ip_history, :text unless column_exists?(:auth_tokens, :ip_history) | ||
| else | ||
| # PostgreSQL supports arrays | ||
| add_column :auth_tokens, :ip_history, :string, array: true, default: [] unless column_exists?(:auth_tokens, :ip_history) | ||
| end | ||
|
|
||
| # Columns for session fixation/hijacking prevention | ||
| add_column :auth_tokens, :suspicious_activity_detected_at, :datetime unless column_exists?(:auth_tokens, :suspicious_activity_detected_at) | ||
| add_column :auth_tokens, :invalidation_requested_at, :datetime unless column_exists?(:auth_tokens, :invalidation_requested_at) | ||
| add_column :auth_tokens, :last_activity_at, :datetime unless column_exists?(:auth_tokens, :last_activity_at) | ||
|
|
||
| # Add index to improve query performance for token validation | ||
| # Add index if it doesn't exist | ||
| add_index :auth_tokens, :invalidation_requested_at unless index_exists?(:auth_tokens, :invalidation_requested_at) | ||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| class AddTimestampsToAuthTokens < ActiveRecord::Migration[7.1] | ||
| def change | ||
| add_timestamps :auth_tokens, default: -> { 'CURRENT_TIMESTAMP' }, null: false | ||
| end | ||
| end |
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.
As discussed user_agent does not include PII or raise any data protection concerns - that is great