Can hg serve enforce deny‑by‑default anonymous read access without external auth?
0 reputation · 02 Nov 2020, 16:19 UTC
0 reputation · 02 Nov 2020, 16:19 UTC
Ensure that anonymous users cannot pull or clone a repository served by Mercurial’s built‑in hg serve unless explicit permission is granted.
By default hg serve allows anonymous read access when no [web] section defines allow_read or deny_read. The server itself does not perform authentication; it expects the front‑end to set REMOTE_USER. It is unclear whether a configuration exists that makes the server reject anonymous read by default without listing every allowed user or relying on an external reverse proxy to block requests.
hg serve be configured to deny anonymous read access by default without specifying an explicit allow_read list?deny_read = * in the web configuration effectively block all anonymous pull/clone attempts?[web] settings are required to achieve a deny‑by‑default posture for anonymous users?31775 reputation · 02 Nov 2020, 17:55 UTC
Yes. Mercurial’s built‑in hg serve can reject all anonymous read requests by setting deny_read = * in the repository’s [web] section. The rule applies to any request where REMOTE_USER is empty or not supplied, which is the case for anonymous clients. Authorized users must be listed in allow_read to override the denial.
The [web] section evaluates deny_read before allow_read. When deny_read = * matches every username (including the empty string for anonymous), the request is denied unless an explicit allow_read entry matches the REMOTE_USER value. Mercurial itself does not perform authentication; it relies on the front‑end or proxy to set REMOTE_USER. If no authentication layer is present, all reads are blocked.
Edit or create the repository’s .hg/hgrc file and add:
[web]
deny_read = *
allow_read = alice, bob
Start the server:
hg serve -R /path/to/repo
Ensure your authentication front‑end (e.g., Apache, Nginx, or a custom proxy) sets the REMOTE_USER environment variable to the authenticated username before invoking hg serve. If REMOTE_USER is unset or empty, the request is treated as anonymous and will be denied.
REMOTE_USER): hg clone http://localhost:8000/repo → permission denied.export REMOTE_USER=alice then hg clone http://localhost:8000/repo → succeeds.export REMOTE_USER=charlie then clone → permission denied.Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.