|
| 1 | +// +build !dendrite_blacklist |
| 2 | + |
| 3 | +// Rationale for being included in Dendrite's blacklist: https://github.com/matrix-org/complement/pull/199#issuecomment-904852233 |
| 4 | +package csapi_tests |
| 5 | + |
| 6 | +import ( |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/matrix-org/complement/internal/b" |
| 10 | + "github.com/matrix-org/complement/internal/client" |
| 11 | + "github.com/matrix-org/complement/internal/match" |
| 12 | + "github.com/matrix-org/complement/internal/must" |
| 13 | +) |
| 14 | + |
| 15 | +const aliceUserID = "@alice:hs1" |
| 16 | +const alicePublicName = "Alice Cooper" |
| 17 | +const alicePrivateName = "Freddy" |
| 18 | + |
| 19 | +var justAliceByPublicName = []match.JSON{ |
| 20 | + match.JSONKeyArrayOfSize("results", 1), |
| 21 | + match.JSONKeyEqual("results.0.display_name", alicePublicName), |
| 22 | + match.JSONKeyEqual("results.0.user_id", aliceUserID), |
| 23 | +} |
| 24 | + |
| 25 | +var noResults = []match.JSON{ |
| 26 | + match.JSONKeyArrayOfSize("results", 0), |
| 27 | +} |
| 28 | + |
| 29 | +func setupUsers(t *testing.T) (*client.CSAPI, *client.CSAPI, *client.CSAPI, func(*testing.T)) { |
| 30 | + // Originally written to reproduce https://github.com/matrix-org/synapse/issues/5677 |
| 31 | + // In that bug report, |
| 32 | + // - Bob knows about Alice, and |
| 33 | + // - Alice has revealed a private name to another friend X, |
| 34 | + // - Bob can see that private name when he shouldn't be able to. |
| 35 | + // |
| 36 | + // I've tweaked the names to be more traditional: |
| 37 | + // - Eve knows about Alice, |
| 38 | + // - Alice reveals a private name to another friend Bob |
| 39 | + // - Eve shouldn't be able to see that private name via the directory. |
| 40 | + deployment := Deploy(t, b.BlueprintAlice) |
| 41 | + cleanup := func(t *testing.T) { |
| 42 | + deployment.Destroy(t) |
| 43 | + } |
| 44 | + |
| 45 | + alice := deployment.Client(t, "hs1", aliceUserID) |
| 46 | + bob := deployment.RegisterUser(t, "hs1", "bob", "bob-has-a-very-secret-pw") |
| 47 | + eve := deployment.RegisterUser(t, "hs1", "eve", "eve-has-a-very-secret-pw") |
| 48 | + |
| 49 | + // Alice sets her profile displayname. This ensures that her |
| 50 | + // public name, private name and userid localpart are all |
| 51 | + // distinguishable, even case-insensitively. |
| 52 | + alice.MustDoFunc( |
| 53 | + t, |
| 54 | + "PUT", |
| 55 | + []string{"_matrix", "client", "r0", "profile", alice.UserID, "displayname"}, |
| 56 | + client.WithJSONBody(t, map[string]interface{}{ |
| 57 | + "displayname": alicePublicName, |
| 58 | + }), |
| 59 | + ) |
| 60 | + |
| 61 | + // Alice creates a public room (so when Eve searches, she can see that Alice exists) |
| 62 | + alice.CreateRoom(t, map[string]interface{}{"visibility": "public"}) |
| 63 | + return alice, bob, eve, cleanup |
| 64 | +} |
| 65 | + |
| 66 | +func checkExpectations(t *testing.T, bob, eve *client.CSAPI) { |
| 67 | + t.Run("Eve can find Alice by profile display name", func(t *testing.T) { |
| 68 | + res := eve.MustDoFunc( |
| 69 | + t, |
| 70 | + "POST", |
| 71 | + []string{"_matrix", "client", "r0", "user_directory", "search"}, |
| 72 | + client.WithJSONBody(t, map[string]interface{}{ |
| 73 | + "search_term": alicePublicName, |
| 74 | + }), |
| 75 | + ) |
| 76 | + must.MatchResponse(t, res, match.HTTPResponse{JSON: justAliceByPublicName}) |
| 77 | + }) |
| 78 | + |
| 79 | + t.Run("Eve can find Alice by mxid", func(t *testing.T) { |
| 80 | + res := eve.MustDoFunc( |
| 81 | + t, |
| 82 | + "POST", |
| 83 | + []string{"_matrix", "client", "r0", "user_directory", "search"}, |
| 84 | + client.WithJSONBody(t, map[string]interface{}{ |
| 85 | + "search_term": aliceUserID, |
| 86 | + }), |
| 87 | + ) |
| 88 | + must.MatchResponse(t, res, match.HTTPResponse{JSON: justAliceByPublicName}) |
| 89 | + }) |
| 90 | + |
| 91 | + t.Run("Eve cannot find Alice by room-specific name that Eve is not privy to", func(t *testing.T) { |
| 92 | + res := eve.MustDoFunc( |
| 93 | + t, |
| 94 | + "POST", |
| 95 | + []string{"_matrix", "client", "r0", "user_directory", "search"}, |
| 96 | + client.WithJSONBody(t, map[string]interface{}{ |
| 97 | + "search_term": alicePrivateName, |
| 98 | + }), |
| 99 | + ) |
| 100 | + must.MatchResponse(t, res, match.HTTPResponse{JSON: noResults}) |
| 101 | + }) |
| 102 | + |
| 103 | + t.Run("Bob can find Alice by profile display name", func(t *testing.T) { |
| 104 | + res := bob.MustDoFunc( |
| 105 | + t, |
| 106 | + "POST", |
| 107 | + []string{"_matrix", "client", "r0", "user_directory", "search"}, |
| 108 | + client.WithJSONBody(t, map[string]interface{}{ |
| 109 | + "search_term": alicePublicName, |
| 110 | + }), |
| 111 | + ) |
| 112 | + must.MatchResponse(t, res, match.HTTPResponse{ |
| 113 | + JSON: justAliceByPublicName, |
| 114 | + }) |
| 115 | + }) |
| 116 | + |
| 117 | + t.Run("Bob can find Alice by mxid", func(t *testing.T) { |
| 118 | + res := bob.MustDoFunc( |
| 119 | + t, |
| 120 | + "POST", |
| 121 | + []string{"_matrix", "client", "r0", "user_directory", "search"}, |
| 122 | + client.WithJSONBody(t, map[string]interface{}{ |
| 123 | + "search_term": aliceUserID, |
| 124 | + }), |
| 125 | + ) |
| 126 | + must.MatchResponse(t, res, match.HTTPResponse{ |
| 127 | + JSON: justAliceByPublicName, |
| 128 | + }) |
| 129 | + }) |
| 130 | +} |
| 131 | + |
| 132 | +func TestRoomSpecificUsernameChange(t *testing.T) { |
| 133 | + alice, bob, eve, cleanup := setupUsers(t) |
| 134 | + defer cleanup(t) |
| 135 | + |
| 136 | + // Bob creates a new room and invites Alice. |
| 137 | + privateRoom := bob.CreateRoom(t, map[string]interface{}{ |
| 138 | + "visibility": "private", |
| 139 | + "invite": []string{alice.UserID}, |
| 140 | + }) |
| 141 | + |
| 142 | + // Alice waits until she sees the invite, then accepts. |
| 143 | + alice.SyncUntilInvitedTo(t, privateRoom) |
| 144 | + alice.JoinRoom(t, privateRoom, nil) |
| 145 | + |
| 146 | + // Alice reveals her private name to Bob |
| 147 | + alice.MustDoFunc( |
| 148 | + t, |
| 149 | + "PUT", |
| 150 | + []string{"_matrix", "client", "r0", "rooms", privateRoom, "state", "m.room.member", alice.UserID}, |
| 151 | + client.WithJSONBody(t, map[string]interface{}{ |
| 152 | + "displayname": alicePrivateName, |
| 153 | + "membership": "join", |
| 154 | + }), |
| 155 | + ) |
| 156 | + |
| 157 | + checkExpectations(t, bob, eve) |
| 158 | +} |
| 159 | + |
| 160 | +func TestRoomSpecificUsernameAtJoin(t *testing.T) { |
| 161 | + alice, bob, eve, cleanup := setupUsers(t) |
| 162 | + defer cleanup(t) |
| 163 | + |
| 164 | + // Bob creates a new room and invites Alice. |
| 165 | + privateRoom := bob.CreateRoom(t, map[string]interface{}{ |
| 166 | + "visibility": "private", |
| 167 | + "invite": []string{alice.UserID}, |
| 168 | + }) |
| 169 | + |
| 170 | + // Alice waits until she sees the invite, then accepts. |
| 171 | + // When she accepts, she does so with a specific displayname. |
| 172 | + alice.SyncUntilInvitedTo(t, privateRoom) |
| 173 | + alice.JoinRoom(t, privateRoom, nil) |
| 174 | + |
| 175 | + // Alice reveals her private name to Bob |
| 176 | + alice.MustDoFunc( |
| 177 | + t, |
| 178 | + "PUT", |
| 179 | + []string{"_matrix", "client", "r0", "rooms", privateRoom, "state", "m.room.member", alice.UserID}, |
| 180 | + client.WithJSONBody(t, map[string]interface{}{ |
| 181 | + "displayname": alicePrivateName, |
| 182 | + "membership": "join", |
| 183 | + }), |
| 184 | + ) |
| 185 | + |
| 186 | + checkExpectations(t, bob, eve) |
| 187 | +} |
0 commit comments