Don't consider ACL'd servers as permalink candidates

and fix the bug where it was picking 4 servers instead of 3. This was due to `<=` instead of `<` in the `MAX_SERVER_CANDIDATES` loop. Added tests for all the things.

Fixes https://github.com/vector-im/riot-web/issues/7752
Fixes https://github.com/vector-im/riot-web/issues/7682
This commit is contained in:
Travis Ralston
2018-12-05 18:00:09 -07:00
parent daa0ddd21d
commit f08a54ed1e
3 changed files with 232 additions and 16 deletions
+174 -12
View File
@@ -150,7 +150,39 @@ describe('matrix-to', function() {
expect(pickedServers[2]).toBe("third");
});
it('should work with IPv4 hostnames', function() {
it('should pick a maximum of 3 candidate servers', function() {
peg.get().getRoom = () => {
return {
getJoinedMembers: () => [
{
userId: "@alice:alpha",
powerLevel: 100,
},
{
userId: "@alice:bravo",
powerLevel: 0,
},
{
userId: "@alice:charlie",
powerLevel: 0,
},
{
userId: "@alice:delta",
powerLevel: 0,
},
{
userId: "@alice:echo",
powerLevel: 0,
},
],
};
};
const pickedServers = pickServerCandidates("!somewhere:example.org");
expect(pickedServers).toExist();
expect(pickedServers.length).toBe(3);
});
it('should not consider IPv4 hosts', function() {
peg.get().getRoom = () => {
return {
getJoinedMembers: () => [
@@ -163,11 +195,10 @@ describe('matrix-to', function() {
};
const pickedServers = pickServerCandidates("!somewhere:example.org");
expect(pickedServers).toExist();
expect(pickedServers.length).toBe(1);
expect(pickedServers[0]).toBe("127.0.0.1");
expect(pickedServers.length).toBe(0);
});
it('should work with IPv6 hostnames', function() {
it('should not consider IPv6 hosts', function() {
peg.get().getRoom = () => {
return {
getJoinedMembers: () => [
@@ -180,11 +211,10 @@ describe('matrix-to', function() {
};
const pickedServers = pickServerCandidates("!somewhere:example.org");
expect(pickedServers).toExist();
expect(pickedServers.length).toBe(1);
expect(pickedServers[0]).toBe("[::1]");
expect(pickedServers.length).toBe(0);
});
it('should work with IPv4 hostnames with ports', function() {
it('should not consider IPv4 hostnames with ports', function() {
peg.get().getRoom = () => {
return {
getJoinedMembers: () => [
@@ -197,11 +227,10 @@ describe('matrix-to', function() {
};
const pickedServers = pickServerCandidates("!somewhere:example.org");
expect(pickedServers).toExist();
expect(pickedServers.length).toBe(1);
expect(pickedServers[0]).toBe("127.0.0.1:8448");
expect(pickedServers.length).toBe(0);
});
it('should work with IPv6 hostnames with ports', function() {
it('should not consider IPv6 hostnames with ports', function() {
peg.get().getRoom = () => {
return {
getJoinedMembers: () => [
@@ -214,8 +243,7 @@ describe('matrix-to', function() {
};
const pickedServers = pickServerCandidates("!somewhere:example.org");
expect(pickedServers).toExist();
expect(pickedServers.length).toBe(1);
expect(pickedServers[0]).toBe("[::1]:8448");
expect(pickedServers.length).toBe(0);
});
it('should work with hostnames with ports', function() {
@@ -235,6 +263,140 @@ describe('matrix-to', function() {
expect(pickedServers[0]).toBe("example.org:8448");
});
it('should not consider servers explicitly denied by ACLs', function() {
peg.get().getRoom = () => {
return {
getJoinedMembers: () => [
{
userId: "@alice:evilcorp.com",
powerLevel: 100,
},
{
userId: "@bob:chat.evilcorp.com",
powerLevel: 0,
},
],
currentState: {
getStateEvents: (type, key) => {
if (type !== "m.room.server_acl" || key !== "") return null;
return {
getContent: () => {
return {
deny: ["evilcorp.com", "*.evilcorp.com"],
allow: ["*"],
};
},
};
},
},
};
};
const pickedServers = pickServerCandidates("!somewhere:example.org");
expect(pickedServers).toExist();
expect(pickedServers.length).toBe(0);
});
it('should not consider servers not allowed by ACLs', function() {
peg.get().getRoom = () => {
return {
getJoinedMembers: () => [
{
userId: "@alice:evilcorp.com",
powerLevel: 100,
},
{
userId: "@bob:chat.evilcorp.com",
powerLevel: 0,
},
],
currentState: {
getStateEvents: (type, key) => {
if (type !== "m.room.server_acl" || key !== "") return null;
return {
getContent: () => {
return {
deny: [],
allow: [], // implies "ban everyone"
};
},
};
},
},
};
};
const pickedServers = pickServerCandidates("!somewhere:example.org");
expect(pickedServers).toExist();
expect(pickedServers.length).toBe(0);
});
it('should consider servers not explicitly banned by ACLs', function() {
peg.get().getRoom = () => {
return {
getJoinedMembers: () => [
{
userId: "@alice:evilcorp.com",
powerLevel: 100,
},
{
userId: "@bob:chat.evilcorp.com",
powerLevel: 0,
},
],
currentState: {
getStateEvents: (type, key) => {
if (type !== "m.room.server_acl" || key !== "") return null;
return {
getContent: () => {
return {
deny: ["*.evilcorp.com"], // evilcorp.com is still good though
allow: ["*"],
};
},
};
},
},
};
};
const pickedServers = pickServerCandidates("!somewhere:example.org");
expect(pickedServers).toExist();
expect(pickedServers.length).toBe(1);
expect(pickedServers[0]).toEqual("evilcorp.com");
});
it('should consider servers not disallowed by ACLs', function() {
peg.get().getRoom = () => {
return {
getJoinedMembers: () => [
{
userId: "@alice:evilcorp.com",
powerLevel: 100,
},
{
userId: "@bob:chat.evilcorp.com",
powerLevel: 0,
},
],
currentState: {
getStateEvents: (type, key) => {
if (type !== "m.room.server_acl" || key !== "") return null;
return {
getContent: () => {
return {
deny: [],
allow: ["evilcorp.com"], // implies "ban everyone else"
};
},
};
},
},
};
};
const pickedServers = pickServerCandidates("!somewhere:example.org");
expect(pickedServers).toExist();
expect(pickedServers.length).toBe(1);
expect(pickedServers[0]).toEqual("evilcorp.com");
});
it('should generate an event permalink for room IDs with no candidate servers', function() {
peg.get().getRoom = () => null;
const result = makeEventPermalink("!somewhere:example.org", "$something:example.com");