Skip to content

Commit 1fde43b

Browse files
committed
Don't apply a session limit when genuinely replacing a session
1 parent f13d141 commit 1fde43b

File tree

6 files changed

+51
-2
lines changed

6 files changed

+51
-2
lines changed

crates/handlers/src/compat/login.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,8 @@ async fn token_login(
576576
Device::generate(rng)
577577
};
578578

579-
repo.app_session()
579+
let session_replaced = repo
580+
.app_session()
580581
.finish_sessions_to_replace_device(clock, &browser_session.user, &device)
581582
.await?;
582583

@@ -586,6 +587,7 @@ async fn token_login(
586587
.evaluate_compat_login(mas_policy::CompatLoginInput {
587588
user: &browser_session.user,
588589
login_type: CompatLoginType::WebSso,
590+
session_replaced,
589591
session_counts,
590592
requester,
591593
})
@@ -702,7 +704,8 @@ async fn user_password_login(
702704
Device::generate(&mut rng)
703705
};
704706

705-
repo.app_session()
707+
let session_replaced = repo
708+
.app_session()
706709
.finish_sessions_to_replace_device(clock, &user, &device)
707710
.await?;
708711

@@ -712,6 +715,7 @@ async fn user_password_login(
712715
.evaluate_compat_login(mas_policy::CompatLoginInput {
713716
user: &user,
714717
login_type: CompatLoginType::Password,
718+
session_replaced,
715719
session_counts,
716720
requester: policy_requester,
717721
})

crates/handlers/src/compat/login_sso_complete.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ pub async fn get(
123123
.evaluate_compat_login(mas_policy::CompatLoginInput {
124124
user: &session.user,
125125
login_type: CompatLoginType::WebSso,
126+
// TODO should we predict a replacement?
127+
session_replaced: false,
126128
session_counts,
127129
requester: mas_policy::Requester {
128130
ip_address: activity_tracker.ip(),
@@ -251,6 +253,8 @@ pub async fn post(
251253
user: &session.user,
252254
login_type: CompatLoginType::WebSso,
253255
session_counts,
256+
// TODO should we predict a replacement?
257+
session_replaced: false,
254258
requester: mas_policy::Requester {
255259
ip_address: activity_tracker.ip(),
256260
user_agent,

crates/policy/src/model.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,9 @@ pub struct CompatLoginInput<'a> {
197197
/// How many sessions the user has.
198198
pub session_counts: SessionCounts,
199199

200+
/// Whether a session will be replaced by this login
201+
pub session_replaced: bool,
202+
200203
// TODO is this actually what we care about? Don't we care a bit more about whether we're in an
201204
// interactive context or a non-interactive context? (SSO type has both phases :()
202205
pub login_type: CompatLoginType,

policies/compat_login/compat_login.rego

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ violation contains {
3636
# TODO not strictly correct...
3737
input.login_type == "m.login.sso"
3838

39+
# Only apply if this login doesn't replace a session
40+
# (As then this login is not actually increasing the number of devices)
41+
not input.session_replaced
42+
3943
# For web-based 'compat SSO' login, a violation occurs when the soft limit has already been
4044
# reached or exceeded.
4145
# We use the soft limit because the user will be able to interactively remove
@@ -53,6 +57,10 @@ violation contains {
5357
# This is not a web-based interactive login
5458
input.login_type == "m.login.password"
5559

60+
# Only apply if this login doesn't replace a session
61+
# (As then this login is not actually increasing the number of devices)
62+
not input.session_replaced
63+
5664
# For `m.login.password` login, a violation occurs when the hard limit has already been
5765
# reached or exceeded.
5866
# We don't use the soft limit because the user won't be able to interactively remove

policies/compat_login/compat_login_test.rego

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,59 +14,84 @@ test_session_limiting_sso if {
1414
compat_login.allow with input.user as user
1515
with input.session_counts as {"total": 1}
1616
with input.login_type as "m.login.sso"
17+
with input.session_replaced as false
1718
with data.session_limit as {"soft_limit": 32, "hard_limit": 64}
1819

1920
compat_login.allow with input.user as user
2021
with input.session_counts as {"total": 31}
2122
with input.login_type as "m.login.sso"
23+
with input.session_replaced as false
2224
with data.session_limit as {"soft_limit": 32, "hard_limit": 64}
2325

2426
not compat_login.allow with input.user as user
2527
with input.session_counts as {"total": 32}
2628
with input.login_type as "m.login.sso"
29+
with input.session_replaced as false
2730
with data.session_limit as {"soft_limit": 32, "hard_limit": 64}
2831

2932
not compat_login.allow with input.user as user
3033
with input.session_counts as {"total": 42}
3134
with input.login_type as "m.login.sso"
35+
with input.session_replaced as false
3236
with data.session_limit as {"soft_limit": 32, "hard_limit": 64}
3337

3438
not compat_login.allow with input.user as user
3539
with input.session_counts as {"total": 65}
3640
with input.login_type as "m.login.sso"
41+
with input.session_replaced as false
3742
with data.session_limit as {"soft_limit": 32, "hard_limit": 64}
3843

3944
# No limit configured
4045
compat_login.allow with input.user as user
4146
with input.session_counts as {"total": 1}
4247
with input.login_type as "m.login.sso"
48+
with input.session_replaced as false
4349
with data.session_limit as null
4450
}
4551

4652
test_session_limiting_password if {
4753
compat_login.allow with input.user as user
4854
with input.session_counts as {"total": 1}
4955
with input.login_type as "m.login.password"
56+
with input.session_replaced as false
5057
with data.session_limit as {"soft_limit": 32, "hard_limit": 64}
5158

5259
compat_login.allow with input.user as user
5360
with input.session_counts as {"total": 63}
5461
with input.login_type as "m.login.password"
62+
with input.session_replaced as false
5563
with data.session_limit as {"soft_limit": 32, "hard_limit": 64}
5664

5765
not compat_login.allow with input.user as user
5866
with input.session_counts as {"total": 64}
5967
with input.login_type as "m.login.password"
68+
with input.session_replaced as false
6069
with data.session_limit as {"soft_limit": 32, "hard_limit": 64}
6170

6271
not compat_login.allow with input.user as user
6372
with input.session_counts as {"total": 65}
6473
with input.login_type as "m.login.password"
74+
with input.session_replaced as false
6575
with data.session_limit as {"soft_limit": 32, "hard_limit": 64}
6676

6777
# No limit configured
6878
compat_login.allow with input.user as user
6979
with input.session_counts as {"total": 1}
7080
with input.login_type as "m.login.password"
81+
with input.session_replaced as false
7182
with data.session_limit as null
7283
}
84+
85+
test_no_session_limiting_upon_replacement if {
86+
not compat_login.allow with input.user as user
87+
with input.session_counts as {"total": 65}
88+
with input.login_type as "m.login.password"
89+
with input.session_replaced as false
90+
with data.session_limit as {"soft_limit": 32, "hard_limit": 64}
91+
92+
not compat_login.allow with input.user as user
93+
with input.session_counts as {"total": 65}
94+
with input.login_type as "m.login.sso"
95+
with input.session_replaced as false
96+
with data.session_limit as {"soft_limit": 32, "hard_limit": 64}
97+
}

policies/schema/compat_login_input.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"login_type",
88
"requester",
99
"session_counts",
10+
"session_replaced",
1011
"user"
1112
],
1213
"properties": {
@@ -22,6 +23,10 @@
2223
}
2324
]
2425
},
26+
"session_replaced": {
27+
"description": "Whether a session will be replaced by this login",
28+
"type": "boolean"
29+
},
2530
"login_type": {
2631
"$ref": "#/definitions/CompatLoginType"
2732
},

0 commit comments

Comments
 (0)