|
| 1 | +# MSC2778: Providing authentication method for appservice users |
| 2 | + |
| 3 | +Appservices within Matrix are increasingly attempting to support End-to-End Encryption. As such, they |
| 4 | +need a way to generate devices for their users so that they can participate in E2E rooms. In order to |
| 5 | +do so, this proposal suggests implementing an appservice extension to the |
| 6 | +[`POST /login` endpoint](https://matrix.org/docs/spec/client_server/r0.6.0#post-matrix-client-r0-login). |
| 7 | + |
| 8 | +Appservice users do not usually need to log in as they do not need their own access token, and do not |
| 9 | +traditionally need a "device". However, E2E encryption demands that at least one user in a room has a |
| 10 | +Matrix device which means bridge users need to be able to generate a device on demand. In the past, |
| 11 | +bridge developers have used the bridge bot's device for all bridge users in the room, but this causes |
| 12 | +problems should the bridge wish to only join ghosts to a room (e.g. for DMs). |
| 13 | + |
| 14 | +Another advantage this provides is that an appservice can now be used to generate access tokens for |
| 15 | +any user in its namespace without having to set a password for that user, which may be useful where |
| 16 | +maintaining password(s) in the configuration is undesirable. |
| 17 | + |
| 18 | +## Proposal |
| 19 | + |
| 20 | +A new `type` is to be added to `POST /login`: `m.login.application_service` |
| 21 | + |
| 22 | +The `/login` endpoint may now take an `access_token` in the same way that other |
| 23 | +authenticated endpoints do. No additional parameters should be specified in the request body. |
| 24 | + |
| 25 | +Example request |
| 26 | + |
| 27 | +```json |
| 28 | +{ |
| 29 | + "type": "m.login.application_service", |
| 30 | + "identifier": { |
| 31 | + "type": "m.id.user", |
| 32 | + "user": "_bridge_alice" |
| 33 | + } |
| 34 | +} |
| 35 | +``` |
| 36 | + |
| 37 | +Note: Implementations MUST use the `identifier.type`=`m.id.user` method of specifying the |
| 38 | +localpart. The deprecated top-level `user` field **cannot** use this login flow type. This |
| 39 | +is deliberate so as to coax developers into using the new identifier format when implementing |
| 40 | +new flows. |
| 41 | + |
| 42 | +The response body should be unchanged from the existing `/login` specification. |
| 43 | + |
| 44 | +If one of the following conditions are true: |
| 45 | + |
| 46 | +- The access token is not provided |
| 47 | +- The access token does not correspond to an appservice |
| 48 | +- Or the user has not previously been registered |
| 49 | + |
| 50 | +Then the servers MUST reject with HTTP 403, with an `errcode` of `"M_FORBIDDEN"`. |
| 51 | + |
| 52 | +If the access token DOES correspond to an appservice but the user is not inside its namespace, |
| 53 | +then the `errcode` must be `"M_EXCLUSIVE"`. |
| 54 | + |
| 55 | +Homeservers should ignore the `access_token` parameter if a type other than |
| 56 | +`m.login.application_service` has been provided. |
| 57 | + |
| 58 | +Appservices creating **new** users can still use the `/register` endpoint to generate an `access_token` / `device_id` |
| 59 | +but for existing users, the `/login` endpoint can be used instead. |
| 60 | + |
| 61 | +## Potential issues |
| 62 | + |
| 63 | +This proposal means that there will be more calls to make when setting up a appservice user, when |
| 64 | +using encryption. While this could be done during the registration step, this would prohibit creating |
| 65 | +new devices should the appservice intentionally or inadvertently have lost the client-side device data. |
| 66 | + |
| 67 | +## Alternatives |
| 68 | + |
| 69 | +### 1. Include the token in the `/login` request body |
| 70 | + |
| 71 | +One minor tweak to the current proposal could be to include the token as part of the auth data, rather than |
| 72 | +being part of the header/params to the request. An argument could be made for either, but since the specification |
| 73 | +expects the appservice to pass the token this way in all requests, including `/register`, it seems wise to keep |
| 74 | +it that way. |
| 75 | + |
| 76 | +### 2. Use implementation specific "shared secret" authentication |
| 77 | + |
| 78 | +Some community members have used homeserver implementation details such as a "shared secret" authentication method to |
| 79 | +log into the accounts without having to use the /login process at all. Synapse provides such a function, |
| 80 | +but also means the appservice can now authenticate as any user on the homeserver. This is undesirable from a |
| 81 | +security standpoint. |
| 82 | + |
| 83 | +### 3. Keep using `/register` solely |
| 84 | + |
| 85 | +A third option could be to create a new endpoint that simply creates a new device for an appservice user on demand. |
| 86 | +Given the rest of the matrix eco-system does this with /login, and /login is already extensible with `type`, it would |
| 87 | +create more work for all parties involved for little benefit. |
| 88 | + |
| 89 | +Finally, `POST /register` does already return a `device_id` and `access_token` so appservices |
| 90 | +could store this information rather than calling `POST /login` at all. This does however present a few problems: |
| 91 | + |
| 92 | +- Quite a few appservices which only support unencrypted messaging do not use/store the `device_id`/`access_token` from a register call. |
| 93 | + In the event that an appservice eventually gains the ability to support encryption, they would be unable to fetch a new `device_id`/ |
| 94 | + `access_token` for any existing users (as `/register` would fail for an existing user). |
| 95 | +- If user tokens were lost or exposed, there is no way to programattically create new access tokens for these users. |
| 96 | +- Finally, if a user was registered externally and the appservice would like to masquerade as it, it would be unable to fetch |
| 97 | + an access token for that user. |
| 98 | + |
| 99 | +While `POST /register` does work, it is impactical as the sole method of fetching an access token. |
| 100 | + |
| 101 | +## Security considerations |
| 102 | + |
| 103 | +Appservices could use this new functionality to generate devices for any userId that are within its namespace e.g. setting the |
| 104 | +user namespace regex to `@.*:example.com` would allow appservice to control anyone on the homeserver. While this sounds scary, in practice |
| 105 | +this is not a problem because: |
| 106 | + |
| 107 | +- Appservice namespaces are maintained by the homeserver admin. If the namespace were to change, then it's reasonable |
| 108 | + to assume that the server admin is aware. There is no defense mechanism to stop a malicious server admin from creating new |
| 109 | + devices for a given user's account as they could also do so by simply modifying the database. |
| 110 | + |
| 111 | +- While an appservice *could* try to masquerade as a user maliciously without the server admin expecting it, it would still |
| 112 | + be bound by the restrictions of the namespace. Server admins are expected to be aware of the implications of adding new |
| 113 | + appservices to their server so the burden of responsibility lies with the server admin. |
| 114 | + |
| 115 | +- Appservices already can /sync as any user using the `as_token` and send any messages as any user in the namespace, the only |
| 116 | + difference is that without a dedicated access token they are unable to receive device messages. While in theory this |
| 117 | + does make them unable to see encrypted messages, this is not designed to be a security mechanism. |
| 118 | + |
| 119 | +In conclusion this MSC only automates the creation of new devices for users inside an AS namespace, which is something |
| 120 | +a server admin could already do. Appservices should always be treated with care and so with these facts in mind the MSC should |
| 121 | +be considered secure. |
| 122 | + |
| 123 | +## Unstable prefix |
| 124 | + |
| 125 | +Implementations should use `uk.half-shot.msc2778.login.application_service` for `type` given in the |
| 126 | +`POST /login` until this lands in a released version of the specification. |
| 127 | + |
| 128 | +## Implementations |
| 129 | + |
| 130 | +The proposal has been implemented by a homeserver, a bridge SDK and two bridges: |
| 131 | + |
| 132 | +- [synapse](https://github.com/matrix-org/synapse/pull/8320) |
| 133 | +- [mautrix-python](https://github.com/tulir/mautrix-python/commit/12d7c48ca7c15fd3ff61608369af1cf69e289aeb) |
| 134 | +- [mautrix-whatsapp](https://github.com/tulir/mautrix-whatsapp/commit/ead8a869c84d07fadc7cfcf3d522452c99faaa36) |
| 135 | +- [matrix-appservice-bridge](https://github.com/matrix-org/matrix-appservice-bridge/pull/231/files#diff-5e93f1b51d50a44fcf0ca46ea1793c1cR851-R864) |
0 commit comments