Cross-site request forgery on the Control API allows remote command execution through a browser form POST
Description
The HTTP origin-handling middleware shared by MediaMTX's API, metrics and pprof servers (handlerOrigin) only decided whether to add an Access-Control-Allow-Origin header to the response. It never rejected a request coming from a disallowed origin, which was always passed on to the handler. This is not enough to protect state-changing endpoints, since browsers do not run a CORS preflight for "simple requests" such as a POST with a text/plain body sent from an HTML form (enctype="text/plain"), and CORS only restricts what a page can read back, not whether the request is executed.
The POST /v3/config/paths/add/{name} endpoint decodes the JSON configuration from the request body without checking Content-Type, so such a form can carry a valid configuration. Additionally, the default authInternalUsers entry (user: any) grants the api permission to 127.0.0.1 and ::1 without credentials, and a browser running on the operator's machine satisfies this condition. As a result, when the Control API is enabled (api: yes), any web page visited by a user on the MediaMTX host can submit a cross-site form that creates a path with runOnInit (or runOnDemand, runOnReady, etc.), which is executed as the user running MediaMTX, leading to arbitrary command execution on the host (root in the official container image and in the documented systemd unit). Other write endpoints could be reached the same way when the request is a CORS-safelisted one, and the configuration could be read cross-origin when the API was allowing the * origin (the default until v1.21.0).
The default value of apiAllowOrigins, metricsAllowOrigins and pprofAllowOrigins was changed from * to an empty list in PR #6165, and the validation and the emission of Access-Control-Allow-Origin were corrected in PRs #6198, #6200 and #6201, all released in v1.21.0; these changes prevent cross-origin reads of the responses but do not prevent the request from being executed.
This was fixed in PR #6238, which makes the origin middleware reject with 403 Forbidden any POST request that browsers flag as cross-site (Sec-Fetch-Site: same-site or cn header matches the configured allowed origins.
The fix relies on the Sec-Fetch-Site request header, which is sent automatically by browsers but is not available in very old ones: it is supported by Chrome and Edge since version 76 (2019), Firefox since version 90 (2021) and Safari since version 16.4 (2023). When a browser doesn't send this header, the fix cannot recognize a cross-site form submission, so the issue is not fixed for users that browse the web with an outdated browser on the same machine where MediaMTX and its Control API are running. Users are advised to upgrade their browser to a recent version, and, if this is not possible, to disable the Control API (api: no) or to protect it with credentials by restricting the any user in authInternalUsers to the required permissions only.