Skip to content

Commit 3ae55df

Browse files
committed
fix(matrix): authenticate channel probes
1 parent e44dde2 commit 3ae55df

2 files changed

Lines changed: 32 additions & 10 deletions

File tree

‎extensions/matrix/src/matrix/probe.test.ts‎

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,39 @@ describe("probeMatrix", () => {
4040
});
4141
});
4242

43-
it("trims provided userId before client creation", async () => {
44-
await probeMatrix({
43+
it("authenticates a configured userId instead of trusting the local client identity", async () => {
44+
createMatrixClientMock.mockImplementation(async (params: { userId?: string }) => {
45+
return {
46+
getUserId: vi.fn(async () => {
47+
if (params.userId) {
48+
return params.userId;
49+
}
50+
throw Object.assign(new Error("Invalid access token"), {
51+
statusCode: 401,
52+
});
53+
}),
54+
};
55+
});
56+
57+
const result = await probeMatrix({
4558
homeserver: "https://matrix.example.org",
46-
accessToken: "tok",
59+
accessToken: "expired-token",
4760
userId: " @bot:example.org ",
4861
timeoutMs: 500,
4962
});
5063

5164
expect(createMatrixClientMock).toHaveBeenCalledWith({
5265
homeserver: "https://matrix.example.org",
53-
userId: "@bot:example.org",
54-
accessToken: "tok",
66+
userId: undefined,
67+
accessToken: "expired-token",
5568
persistStorage: false,
5669
localTimeoutMs: 500,
5770
});
71+
expect(result).toMatchObject({
72+
ok: false,
73+
status: 401,
74+
error: "Invalid access token",
75+
});
5876
});
5977

6078
it("passes accountId through to client creation", async () => {
@@ -68,7 +86,7 @@ describe("probeMatrix", () => {
6886

6987
expect(createMatrixClientMock).toHaveBeenCalledWith({
7088
homeserver: "https://matrix.example.org",
71-
userId: "@bot:example.org",
89+
userId: undefined,
7290
accessToken: "tok",
7391
persistStorage: false,
7492
localTimeoutMs: 500,
@@ -112,7 +130,7 @@ describe("probeMatrix", () => {
112130

113131
expect(createMatrixClientMock).toHaveBeenCalledWith({
114132
homeserver: "https://matrix.example.org",
115-
userId: "@bot:example.org",
133+
userId: undefined,
116134
accessToken: "tok",
117135
deviceId: "ABCDEF",
118136
persistStorage: false,

‎extensions/matrix/src/matrix/probe.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ export async function probeMatrix(params: {
5252
const inputUserId = normalizeOptionalString(params.userId);
5353
const client = await createMatrixClient({
5454
homeserver: params.homeserver,
55-
userId: inputUserId,
55+
// A seeded userId makes getUserId() a local getter; probes must force whoami auth.
56+
userId: undefined,
5657
accessToken: params.accessToken,
5758
deviceId: params.deviceId,
5859
persistStorage: false,
@@ -62,8 +63,11 @@ export async function probeMatrix(params: {
6263
ssrfPolicy: params.ssrfPolicy,
6364
dispatcherPolicy: params.dispatcherPolicy,
6465
});
65-
// The client wrapper resolves user ID via whoami when needed.
66-
return { ...result, ok: true, userId: (await client.getUserId()) ?? null };
66+
const userId = await client.getUserId();
67+
if (inputUserId && inputUserId !== userId) {
68+
return { ...result, error: "Matrix access token user does not match configured userId" };
69+
}
70+
return { ...result, ok: true, userId };
6771
},
6872
(error) => ({
6973
ok: false,

0 commit comments

Comments
 (0)