Skip to content

Commit 1334c64

Browse files
committed
fix: spec compliant token verification
Signed-off-by: Debdut Chakraborty <[email protected]>
1 parent 55598e9 commit 1334c64

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

src/bridge.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1582,13 +1582,23 @@ export class Bridge {
15821582
// Bridge isn't ready yet
15831583
return false;
15841584
}
1585-
if (
1586-
req.query.access_token !== this.registration.getHomeserverToken() &&
1587-
req.get("authorization") !== `Bearer ${this.registration.getHomeserverToken()}`
1588-
) {
1589-
return false;
1585+
1586+
const tokenFromQuery = req.query.access_token;
1587+
1588+
const tokenFromHeader = req.get("authorization").substring(7); // "Bearer ".length === 7
1589+
1590+
const tokenFromRegistration = this.registration.getHomeserverToken();
1591+
1592+
// https://github.com/matrix-org/matrix-spec/blob/7935a0728a12e768283dba0145fb95154d2f7f6b/content/application-service-api.md?plain=1#L149
1593+
// "Application services should ensure both match if both are provided."
1594+
if (tokenFromQuery && tokenFromHeader) {
1595+
return tokenFromQuery === tokenFromRegistration && tokenFromHeader === tokenFromRegistration;
15901596
}
1591-
return true;
1597+
1598+
// prefer header then query
1599+
// https://github.com/matrix-org/matrix-spec/blob/7935a0728a12e768283dba0145fb95154d2f7f6b/content/application-service-api.md?plain=1#L146-L147
1600+
// Spec does not enforce both to be sent. "encouraged" being the key word.
1601+
return tokenFromHeader === tokenFromRegistration || tokenFromQuery === tokenFromRegistration;
15921602
}
15931603

15941604
/**
@@ -1740,3 +1750,5 @@ async function loadDatabase<T extends BridgeStore>(path: string, Cls: new (db: D
17401750
throw Error('nedb could not be imported. You will need to add this package as a peer dependency.');
17411751
}
17421752
}
1753+
1754+
// vi: et sw=4 ts=4

0 commit comments

Comments
 (0)