Skip to content

Commit 22cac86

Browse files
Correctly parse domains from userIds (#1068)
* Only split userId on the first colon * Add changelog * Update changelog to also account for IPv6 * Fix split in permissions * Update changelog to reflect permissions fix as well * update changelog * lint --------- Co-authored-by: Twilight Sparkle <[email protected]>
1 parent 7583ad1 commit 22cac86

File tree

6 files changed

+61
-16
lines changed

6 files changed

+61
-16
lines changed

changelog.d/1068.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix bugs in handling server names that includes colons.

src/CommentProcessor.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Appservice } from "matrix-bot-sdk";
1+
import { Appservice, UserID } from "matrix-bot-sdk";
22
import markdown from "markdown-it";
33
import emoji from "node-emoji";
44
import { MatrixMessageContent, MatrixEvent } from "./MatrixEvent";
@@ -142,7 +142,8 @@ export class CommentProcessor {
142142
for (const [full, userId] of mentionMatches) {
143143
if (this.as.isNamespacedUser(userId)) {
144144
// XXX: Prefix hack
145-
const githubId = userId.split(":")[0].substr("@_github_".length);
145+
const parsedUserID = new UserID(userId);
146+
const githubId = parsedUserID.localpart.slice("@_github_".length);
146147
if (!githubId) {
147148
continue;
148149
}

src/Connections/GenericHook.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { Logger } from "matrix-appservice-bridge";
99
import { MessageSenderClient } from "../MatrixSender";
1010
import markdownit from "markdown-it";
1111
import { MatrixEvent } from "../MatrixEvent";
12-
import { Appservice, Intent, StateEvent } from "matrix-bot-sdk";
12+
import { Appservice, Intent, StateEvent, UserID } from "matrix-bot-sdk";
1313
import { ApiError, ErrCode } from "../api";
1414
import { BaseConnection } from "./BaseConnection";
1515
import { BridgeConfigGenericWebhooks } from "../config/sections";
@@ -455,7 +455,7 @@ export class GenericHookConnection
455455
if (!this.config.userIdPrefix) {
456456
return this.intent.userId;
457457
}
458-
const [, domain] = this.intent.userId.split(":");
458+
const { domain } = new UserID(this.intent.userId);
459459
const name =
460460
this.state.name &&
461461
this.state.name

src/IntentUtils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Logger } from "matrix-appservice-bridge";
2-
import { Appservice, Intent, MatrixClient } from "matrix-bot-sdk";
2+
import { Appservice, Intent, MatrixClient, UserID } from "matrix-bot-sdk";
33
import axios from "axios";
44

55
const log = new Logger("IntentUtils");
@@ -44,7 +44,7 @@ export async function getIntentForUser(
4444
as: Appservice,
4545
prefix?: string,
4646
) {
47-
const domain = as.botUserId.split(":")[1];
47+
const { domain } = new UserID(as.botUserId);
4848
const intent = as.getIntentForUserId(
4949
`@${prefix ?? ""}${user.login}:${domain}`,
5050
);

src/config/permissions.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,10 @@ impl BridgePermissions {
100100
service: String,
101101
permission: String,
102102
) -> napi::Result<bool> {
103-
let parts: Vec<&str> = mxid.split(':').collect();
104103
let permission_int = permission_level_to_int(permission)?;
105-
let domain = if parts.len() > 1 {
106-
parts[1].to_string()
107-
} else {
108-
parts[0].to_string()
104+
let domain: String = match mxid.split_once(':') {
105+
Some((.., d)) => d.to_string(),
106+
None => return Ok(false),
109107
};
110108
for actor_permission in self.config.iter() {
111109
// Room_id
@@ -128,12 +126,10 @@ impl BridgePermissions {
128126

129127
#[napi]
130128
pub fn check_action_any(&self, mxid: String, permission: String) -> napi::Result<bool> {
131-
let parts: Vec<&str> = mxid.split(':').collect();
132129
let permission_int = permission_level_to_int(permission)?;
133-
let domain = if parts.len() > 1 {
134-
parts[1].to_string()
135-
} else {
136-
parts[0].to_string()
130+
let domain: String = match mxid.split_once(':') {
131+
Some((.., d)) => d.to_string(),
132+
None => return Ok(false),
137133
};
138134
for actor_permission in self.config.iter() {
139135
if !self.match_actor(actor_permission, &domain, &mxid) {

tests/config/permissions.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,53 @@ describe("Config/BridgePermissions", () => {
8080
.to.be.true;
8181
});
8282

83+
it("handles domain actors with ports", () => {
84+
const bridgePermissionNoPort = genBridgePermissions(
85+
"bar",
86+
"my-service",
87+
"login",
88+
);
89+
expect(
90+
bridgePermissionNoPort.checkAction(
91+
"@foo:bar:9999",
92+
"my-service",
93+
"login",
94+
),
95+
).to.be.false;
96+
expect(
97+
bridgePermissionNoPort.checkAction("@foo:bar", "my-service", "login"),
98+
).to.be.true;
99+
const bridgePermissionWithPort = genBridgePermissions(
100+
"bar:9999",
101+
"my-service",
102+
"login",
103+
);
104+
expect(
105+
bridgePermissionWithPort.checkAction(
106+
"@foo:bar:9999",
107+
"my-service",
108+
"login",
109+
),
110+
).to.be.true;
111+
expect(
112+
bridgePermissionWithPort.checkAction(
113+
"@foo:bar:999",
114+
"my-service",
115+
"login",
116+
),
117+
).to.be.false;
118+
expect(
119+
bridgePermissionWithPort.checkAction(
120+
"@foo:bar:",
121+
"my-service",
122+
"login",
123+
),
124+
).to.be.false;
125+
expect(
126+
bridgePermissionWithPort.checkAction("@foo:bar", "my-service", "login"),
127+
).to.be.false;
128+
});
129+
83130
it("will return true for a wildcard actor", () => {
84131
const bridgePermissions = genBridgePermissions(
85132
"*",

0 commit comments

Comments
 (0)