Skip to content

Commit cd1dbfc

Browse files
reivilibresandhose
authored andcommitted
Don't include access tokens where the referenced device has vanished
1 parent 9c1e120 commit cd1dbfc

File tree

1 file changed

+26
-4
lines changed
  • crates/syn2mas/src/synapse_reader

1 file changed

+26
-4
lines changed

crates/syn2mas/src/synapse_reader/mod.rs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,12 @@ impl<'conn> SynapseReader<'conn> {
494494
/// Reads unrefreshable access tokens from the Synapse database.
495495
/// This does not include access tokens used for puppetting users, as those
496496
/// are not supported by MAS.
497+
///
498+
/// This also excludes access tokens whose referenced device ID does not
499+
/// exist, except for deviceless access tokens.
500+
/// (It's unclear what mechanism led to these, but since Synapse has no
501+
/// foreign key constraints and is not consistently atomic about this,
502+
/// it should be no surprise really)
497503
pub fn read_unrefreshable_access_tokens(
498504
&mut self,
499505
) -> impl Stream<Item = Result<SynapseAccessToken, Error>> + '_ {
@@ -502,7 +508,15 @@ impl<'conn> SynapseReader<'conn> {
502508
SELECT
503509
at0.user_id, at0.device_id, at0.token, at0.valid_until_ms, at0.last_validated
504510
FROM access_tokens at0
511+
INNER JOIN devices USING (user_id, device_id)
505512
WHERE at0.puppets_user_id IS NULL AND at0.refresh_token_id IS NULL
513+
514+
UNION
515+
516+
SELECT
517+
at0.user_id, at0.device_id, at0.token, at0.valid_until_ms, at0.last_validated
518+
FROM access_tokens at0
519+
WHERE at0.puppets_user_id IS NULL AND at0.refresh_token_id IS NULL AND at0.device_id IS NULL
506520
",
507521
)
508522
.fetch(&mut *self.txn)
@@ -526,6 +540,7 @@ impl<'conn> SynapseReader<'conn> {
526540
SELECT
527541
rt0.user_id, rt0.device_id, at0.token AS access_token, rt0.token AS refresh_token, at0.valid_until_ms, at0.last_validated
528542
FROM refresh_tokens rt0
543+
INNER JOIN devices USING (device_id)
529544
INNER JOIN access_tokens at0 ON at0.refresh_token_id = rt0.id AND at0.user_id = rt0.user_id AND at0.device_id = rt0.device_id
530545
LEFT JOIN access_tokens at1 ON at1.refresh_token_id = rt0.next_token_id
531546
WHERE NOT at1.used OR at1.used IS NULL
@@ -619,7 +634,10 @@ mod test {
619634
assert_debug_snapshot!(devices);
620635
}
621636

622-
#[sqlx::test(migrator = "MIGRATOR", fixtures("user_alice", "access_token_alice"))]
637+
#[sqlx::test(
638+
migrator = "MIGRATOR",
639+
fixtures("user_alice", "devices_alice", "access_token_alice")
640+
)]
623641
async fn test_read_access_token(pool: PgPool) {
624642
let mut conn = pool.acquire().await.expect("failed to get connection");
625643
let mut reader = SynapseReader::new(&mut conn, false)
@@ -638,7 +656,7 @@ mod test {
638656
/// Tests that puppetting access tokens are ignored.
639657
#[sqlx::test(
640658
migrator = "MIGRATOR",
641-
fixtures("user_alice", "access_token_alice_with_puppet")
659+
fixtures("user_alice", "devices_alice", "access_token_alice_with_puppet")
642660
)]
643661
async fn test_read_access_token_puppet(pool: PgPool) {
644662
let mut conn = pool.acquire().await.expect("failed to get connection");
@@ -657,7 +675,7 @@ mod test {
657675

658676
#[sqlx::test(
659677
migrator = "MIGRATOR",
660-
fixtures("user_alice", "access_token_alice_with_refresh_token")
678+
fixtures("user_alice", "devices_alice", "access_token_alice_with_refresh_token")
661679
)]
662680
async fn test_read_access_and_refresh_tokens(pool: PgPool) {
663681
let mut conn = pool.acquire().await.expect("failed to get connection");
@@ -686,7 +704,11 @@ mod test {
686704

687705
#[sqlx::test(
688706
migrator = "MIGRATOR",
689-
fixtures("user_alice", "access_token_alice_with_unused_refresh_token")
707+
fixtures(
708+
"user_alice",
709+
"devices_alice",
710+
"access_token_alice_with_unused_refresh_token"
711+
)
690712
)]
691713
async fn test_read_access_and_unused_refresh_tokens(pool: PgPool) {
692714
let mut conn = pool.acquire().await.expect("failed to get connection");

0 commit comments

Comments
 (0)