Skip to content

Commit bf81d3a

Browse files
reivilibresandhose
authored andcommitted
Don't include access tokens where the referenced device has vanished
1 parent 59d04c2 commit bf81d3a

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
@@ -493,6 +493,12 @@ impl<'conn> SynapseReader<'conn> {
493493
/// Reads unrefreshable access tokens from the Synapse database.
494494
/// This does not include access tokens used for puppetting users, as those
495495
/// are not supported by MAS.
496+
///
497+
/// This also excludes access tokens whose referenced device ID does not
498+
/// exist, except for deviceless access tokens.
499+
/// (It's unclear what mechanism led to these, but since Synapse has no
500+
/// foreign key constraints and is not consistently atomic about this,
501+
/// it should be no surprise really)
496502
pub fn read_unrefreshable_access_tokens(
497503
&mut self,
498504
) -> impl Stream<Item = Result<SynapseAccessToken, Error>> + '_ {
@@ -501,7 +507,15 @@ impl<'conn> SynapseReader<'conn> {
501507
SELECT
502508
at0.user_id, at0.device_id, at0.token, at0.valid_until_ms, at0.last_validated
503509
FROM access_tokens at0
510+
INNER JOIN devices USING (user_id, device_id)
504511
WHERE at0.puppets_user_id IS NULL AND at0.refresh_token_id IS NULL
512+
513+
UNION
514+
515+
SELECT
516+
at0.user_id, at0.device_id, at0.token, at0.valid_until_ms, at0.last_validated
517+
FROM access_tokens at0
518+
WHERE at0.puppets_user_id IS NULL AND at0.refresh_token_id IS NULL AND at0.device_id IS NULL
505519
",
506520
)
507521
.fetch(&mut *self.txn)
@@ -525,6 +539,7 @@ impl<'conn> SynapseReader<'conn> {
525539
SELECT
526540
rt0.user_id, rt0.device_id, at0.token AS access_token, rt0.token AS refresh_token, at0.valid_until_ms, at0.last_validated
527541
FROM refresh_tokens rt0
542+
INNER JOIN devices USING (device_id)
528543
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
529544
LEFT JOIN access_tokens at1 ON at1.refresh_token_id = rt0.next_token_id
530545
WHERE NOT at1.used OR at1.used IS NULL
@@ -618,7 +633,10 @@ mod test {
618633
assert_debug_snapshot!(devices);
619634
}
620635

621-
#[sqlx::test(migrator = "MIGRATOR", fixtures("user_alice", "access_token_alice"))]
636+
#[sqlx::test(
637+
migrator = "MIGRATOR",
638+
fixtures("user_alice", "devices_alice", "access_token_alice")
639+
)]
622640
async fn test_read_access_token(pool: PgPool) {
623641
let mut conn = pool.acquire().await.expect("failed to get connection");
624642
let mut reader = SynapseReader::new(&mut conn, false)
@@ -637,7 +655,7 @@ mod test {
637655
/// Tests that puppetting access tokens are ignored.
638656
#[sqlx::test(
639657
migrator = "MIGRATOR",
640-
fixtures("user_alice", "access_token_alice_with_puppet")
658+
fixtures("user_alice", "devices_alice", "access_token_alice_with_puppet")
641659
)]
642660
async fn test_read_access_token_puppet(pool: PgPool) {
643661
let mut conn = pool.acquire().await.expect("failed to get connection");
@@ -656,7 +674,7 @@ mod test {
656674

657675
#[sqlx::test(
658676
migrator = "MIGRATOR",
659-
fixtures("user_alice", "access_token_alice_with_refresh_token")
677+
fixtures("user_alice", "devices_alice", "access_token_alice_with_refresh_token")
660678
)]
661679
async fn test_read_access_and_refresh_tokens(pool: PgPool) {
662680
let mut conn = pool.acquire().await.expect("failed to get connection");
@@ -685,7 +703,11 @@ mod test {
685703

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

0 commit comments

Comments
 (0)