|
| 1 | +# MSC3383: Include destination in X-Matrix Auth Header |
| 2 | + |
| 3 | +Currently, a federation request can't be |
| 4 | +[validated](https://spec.matrix.org/v1.2/server-server-api/#request-authentication) |
| 5 | +mid-flight without some |
| 6 | +convoluted workarounds, because federation requests don't contain the |
| 7 | +`server_name` of the destination. The `Host` header does not necessarily contain |
| 8 | +the `server_name` of the destination server, if delegation via `.well-known` is |
| 9 | +being used. It's currently possible to get the `server_name` by making a |
| 10 | +request to `/_matrix/key/v2/server/{keyId}`, and then resolving the delegation |
| 11 | +for the contained `server_name` back to the `Host` included in the original |
| 12 | +request. |
| 13 | + |
| 14 | +This hasn't been a problem so far, as the `server_name` of the destination is |
| 15 | +usually known when validating the `Authorization` header, it's the |
| 16 | +`server_name` of the matrix server that's doing the validation. But there's two |
| 17 | +scenarios where this might not be the case: Forward proxies (that act as an API |
| 18 | +gateway for enforcing additional rules), or Matrix Homeservers implementing |
| 19 | +vhosting and have multiple `server_name`s pointing to the same `Host`. |
| 20 | + |
| 21 | +### Example: rule enforcing forward proxy |
| 22 | + |
| 23 | +Let's assume we have an organization running a matrix server in a protected |
| 24 | +network, that doesn't have direct internet access. The organization only allows |
| 25 | +access to the internet through a forward proxy enforcing additional security |
| 26 | +measures. |
| 27 | + |
| 28 | +For matrix federation, it's supposed to verify the matrix servers are on a list |
| 29 | +of allowed `server_name`s. As explained in the introduction, the `server_name` |
| 30 | +is not contained in the request, so verifying these requests is not possible |
| 31 | +without the workaround described in the introduction. |
| 32 | + |
| 33 | +Alternatively to that, it'd be also possible to keep a map for allowed |
| 34 | +`server_name`s to `Host` headers, but that needs to be updated regularly, to |
| 35 | +make sure it doesn't get stale. |
| 36 | + |
| 37 | +Both of these workarounds are more complicated than they need to be. If the |
| 38 | +`server_name` was included in the `Authorization` header, these workarounds |
| 39 | +could be completely avoided. |
| 40 | + |
| 41 | +## Proposal |
| 42 | + |
| 43 | +In addition to the currently present `origin`, `key` and `sig` fields, the |
| 44 | +`Authorization` header of the scheme `X-Matrix` used in the Matrix S2S API MUST |
| 45 | +also include a `destination` field, which contains the `server_name` of the |
| 46 | +Matrix Homeserver that the request is being sent to. |
| 47 | + |
| 48 | +A Matrix Homeserver receiving a request over the S2S API SHOULD gracefully |
| 49 | +handle requests that do not include the `destination` field in the |
| 50 | +`Authorization` header for backwards compatibility. |
| 51 | + |
| 52 | +When a matrix homeserver receives a request over the S2S API for a |
| 53 | +`destination` that is not the `server_name` (or one of the `server_name`s in |
| 54 | +case of vhosting) of itself, it should deny that request with an HTTP status |
| 55 | +code 401 - Unauthorized. |
| 56 | + |
| 57 | +## Potential issues |
| 58 | + |
| 59 | +Server implementations could theoretically be affected by this change, |
| 60 | +depending on how the header is parsed, which would cause failures in verifying |
| 61 | +the authenticity of the requests. This would be fatal, as it would mean that |
| 62 | +federation would stop working. Luckily, from an initial assessment, it seems |
| 63 | +that all major implementations work here, the parsing implementations in |
| 64 | +Synapse, Dendrite, Conduit, Sydent and SyTest looks like it'd gracefully handle |
| 65 | +this addition without any trouble. The other way around is also a concern: Newer |
| 66 | +implementation might be confronted with federating with an old implementation, |
| 67 | +which does not send the `destination` in it's auth headers. This is explicitly |
| 68 | +mentioned in the proposal though, advocating for graceful handling of these |
| 69 | +situations if possible. |
| 70 | + |
| 71 | +## Alternatives |
| 72 | + |
| 73 | +For the forward proxy scenario, it'd be possible to use the |
| 74 | +`/_matrix/key/v2/server` endpoint for fetching the `server_name` when receiving |
| 75 | +a request. After that though, the `server_name` has to be resolved back to a |
| 76 | +`Host`, for making sure that the domain owner of `server_name` actually intends |
| 77 | +for requests for `server_name` to go to the host in `Host`. This is |
| 78 | +unnecessarily complex and prone to error, which is why it'd be better to have |
| 79 | +that included. |
| 80 | + |
| 81 | +For the vhosting scenario, it'd be possible to have a different hostname to |
| 82 | +delegate to for each vhost. That means that wildcard DNS records and |
| 83 | +certificates have to be used though, to make it manageable to allow anyone |
| 84 | +pointing a `server_name` against a certain service. This is a limitation that |
| 85 | +might be problematic in certain setups, which is why I'd be better to not force |
| 86 | +that. |
| 87 | + |
| 88 | +## Security considerations |
| 89 | + |
| 90 | +I can't think of anything required in this section for this MSC, but I'm open |
| 91 | +to input. |
0 commit comments