79% vs 51%: independent neobanks lag on app security. Read the report

Deep Dive

Deep Dive

Mercurial Client and Server RCE, Fixed in 7.2.6 (HG-SA-2026-001)

Malicious repositories, bundles and .hgsub files can run code on Mercurial clients and servers; upgrade to 7.2.6.

TL;DR

  • Mercurial 7.2.6 fixes four advisories that allow arbitrary code execution on clients and remote code execution on servers. A crafted repository, bundle or .hgsub file is enough to start the chain.
  • HG-SA-2026-001 and HG-SA-2026-004 affect every version before 7.2.6. HG-SA-2026-002 affects 7.1 to 7.2.5, and HG-SA-2026-003 affects 2.6 to 7.2.5.
  • Mercurial calls 7.2.6 a critical security release. It publishes no CVSS score. No in-the-wild exploitation or public exploit code is reported, and no Mercurial advisory is in CISA KEV as of 2026-12-03.
  • Upgrade to 7.2.6, or to the patched package of your distribution. "How do I fix it?" lists the configuration changes that reduce exposure while you upgrade.

Am I affected?

All of these must hold:

  • You use Mercurial before 7.2.6.
  • At least one of these exposure paths applies:
    • You clone, pull, unbundle or apply patches from a source you do not fully trust, or you run hg archive, hg cat or hg diff on revisions from such a source.
    • You run a Mercurial server that untrusted people can push to.
    • You use Mercurial subrepositories with content from an untrusted source.

The exposure is about the data you process, not only the version. A developer who only works with repositories they control is still exposed to a malicious bundle or patch that arrives through a review or a mirror. A server that accepts pushes from untrusted users is exposed even if nobody checks out the malicious revision.

Advisory What it allows Affected versions Fixed version
HG-SA-2026-001 Arbitrary code execution through .hg/ paths and symlinks exported by hg archive, hg cat, hg export and accepted on exchange All versions before 7.2.6 7.2.6
HG-SA-2026-002 Integrity check bypass through in-memory cache poisoning during unbundle and use 7.1 to 7.2.5 7.2.6
HG-SA-2026-003 Writing through a symlink during a parallel update through a race in symlink auditing 2.6 to 7.2.5 7.2.6
HG-SA-2026-004 Configuration injection through a malformed .hgsub subrepository URL, including on Windows All versions before 7.2.6 7.2.6

Check the installed version:

hg --version
python -m pip show mercurial
apt policy mercurial

Check whether a repository already contains an illegal .hg path:

hg manifest --all | grep -iE '(^|/)\.hg\.?(/|$)'

A match means the repository holds a malicious changeset that was accepted before the upgrade. Mercurial 7.2.6 will abort with path contains illegal component: .hg/... when it encounters one.

How do I fix it?

Upgrade to Mercurial 7.2.6. The upstream project says no new releases are planned for older series, but it is providing backported patches to distributions on request.

With pip:

python -m pip install --upgrade mercurial==7.2.6

With a distribution package, upgrade through the distribution once it ships 7.2.6:

sudo apt update && sudo apt install --only-upgrade mercurial
sudo dnf upgrade mercurial

With Homebrew:

brew upgrade mercurial

On a server, upgrade the Mercurial package and turn off web.archivesubrepos even after the upgrade. Mercurial says it should stay off until a future security release of much narrower reach.

If you cannot upgrade immediately, apply the configuration changes from the release notes:

  1. Set storage.revlog.validate-delta-base=yes to reject bundles that poison the delta cache. Pulls and unbundles will be slower.
  2. Set experimental.merge.checkpathconflicts=yes and disable parallel updates with worker.enabled=no or worker.numcpus=1 before 4.6. Updates will be slower.
  3. Disable subrepositories with subrepos.allowed=no, or at least subrepos.hg:allowed=no, unless you need them.
  4. Set ui.clonebundles=no when cloning from a trusted server whose clone bundles are hosted elsewhere.
  5. Do not run hg archive or hg cat --output on revisions from a source you do not trust.
  6. Apply patches from untrusted changesets with hg import, not with patch.

These settings reduce exposure, but they do not replace the upgrade. An archive made before 7.2.6 stays dangerous after the upgrade: do not run hg inside an archive of an untrusted repository made by an earlier version.

What is the root cause?

The four advisories share one theme: Mercurial audited paths on checkout but did not apply the same audit to every export, exchange and background operation. A path or a configuration value that looked inert in one command became executable in another.

HG-SA-2026-001: exported .hg/ paths and symlink writes. Mercurial prevented a changeset with .hg/hgrc or a symlink write from being checked out on disk. It did not prevent hg archive, hg cat --output, hg export or hg diff from exporting those paths, and it accepted them on pull, unbundle, stream clone and local clone. An archive that contains .hg/hgrc is read as a repository by any Mercurial version, so running hg inside it executes the hooks and extensions in that file.

HG-SA-2026-002: cache poisoning. A bug introduced in Mercurial 7.1 let a crafted bundle poison an in-memory cache with content that had not been validated. The same process could then reuse that content as if it had been validated, which affects what is checked out or what validation hooks see. The known vector is unbundling combined with use in the same process, such as hg clone, hg pull -u or hg unbundle -u.

HG-SA-2026-003: symlink audit race. Mercurial audits every file-system write for symlink traversal. When several workers performed writes, a small race window remained. A malicious changeset could extend that window and write through a symlink, either into .hg/hgrc for code execution or outside the working copy.

HG-SA-2026-004: .hgsub configuration injection. A subrepository URL in .hgsub could contain a line break. When Mercurial created a secondary repository in the background, it wrote the URL into that repository's configuration without neutralizing the line break. The rest of the URL became configuration of its own, and configuration injection can load hooks or extensions. Operations as simple as hg log --patch, hg diff --change or hg cat --rev can trigger it without checking out the malicious revision.

How is it exploited?

The lead advisory, HG-SA-2026-001, is the clearest chain. The attacker needs the victim to process a crafted repository, bundle or archive.

  1. The attacker creates a changeset that contains a .hg/hgrc path, or a symlink that points outside the working copy.
  2. The victim clones, pulls or unbundles the repository, or receives an archive or a patch from it.
  3. Mercurial accepts or exports the path because the checkout audit does not cover that operation.
  4. The victim extracts the archive or runs hg inside it. The .hg/hgrc is read as repository configuration.
  5. The configuration loads a hook or an extension, and the attacker's command runs with the victim's privileges.

A server that accepts untrusted pushes is a second path. The server can accept a changeset with an illegal path, relay it to users, or run a read-only operation that triggers the configuration injection from HG-SA-2026-004. Mercurial says an unpatched server can also serve a malicious payload that executes on clients.

The advisories do not publish exploit code. The release notes give the mechanism and the configuration that blocks each vector, and the public fix commits show the checks that were added.

How does the attack flow?

An untrusted repository or bundle carries a path or a configuration value that Mercurial audits on checkout but not on export or exchange, and the victim's next Mercurial command runs the attacker's configuration.

flowchart LR A["Untrusted repository or bundle"] --> B["Mercurial accepts or exports the path"] B --> C["No audit on archive, export or exchange"] C --> D["Victim extracts or runs hg"] D --> E[".hg/hgrc or injected config"] E --> F["Hook or extension runs"] F --> G["Code execution on client or server"]

What can an attacker do?

An attacker can run code on a developer's machine or on a Mercurial server. On a client, the code runs with the developer's privileges and can read the files the developer can read, including source code and credentials, and can push to repositories the developer can reach. On a server, the code runs with the server's privileges and can read every hosted repository and the credentials the server holds.

The attacker can also write outside the working copy through a symlink, which lets a malicious changeset tamper with files the repository should never touch. The .hgsub vector does not need the malicious revision to be checked out, so a read-only command on untrusted content can be enough.

The conditions are the ones in "Am I affected?": an affected Mercurial version, and content from a source you do not fully trust, or a server that accepts untrusted pushes. A developer who only processes repositories they created and control is not exposed to the described chains.

Why does the fix work?

Mercurial 7.2.6 adds one canonical-path check that every repository path must pass, and it applies that check on exchange as well as on checkout. It also neutralizes line breaks before writing paths and subrepository URLs into configuration.

The central check is is_canonical_repo_path in mercurial/pathutil.py:

_non_canonical_component_re = re.compile(br'(?:^|/)\.{0,2}(?:/|$)|[\n\x00]')

def is_canonical_repo_path(path: bytes) -> bool:
    return _non_canonical_component_re.search(path) is None

The regular expression rejects an empty, . or .. component, a leading slash, a trailing slash and any line feed or NUL byte. mercurial/changegroup.py now calls it on every file name in an incoming manifest:

                     for f, n in mfest.items():
+                        if not pathutil.is_canonical_repo_path(f):
+                            msg = _(b"path contains illegal component: %s")
+                            raise error.InputError(msg % f)
                         needfiles.setdefault(f, set()).add(n)

The archive and export paths now audit every file name before writing it, and the path auditor rejects the same non-canonical components, so a changeset that was accepted before the upgrade can no longer be exported silently. The archive writer also creates directories explicitly and writes symlinks last, which prevents a write-through-symlink attack during extraction:

+        # We create symlink last to prevent symlink to be abused by
+        # write-through.
+        symlinks.sort(
+            key=lambda x: (x[0].count(b"/"), len(x[0]), x[0]),
+            reverse=True,
+        )
+        for f, perm in symlinks:
+            write(f, perm, True, ctx[f].data)

For HG-SA-2026-004, mercurial/utils/urlutil.py adds a check before a path is written into a [paths] section:

def check_safe_for_config(path: bytes) -> None:
    if b'\n' in path or b'\r' in path:
        msg = _(b'path contains a line break: %r')
        msg %= pycompat.bytestr(path)
        raise error.Abort(msg)

mercurial/subrepo.py adds the same line-break check before it writes a subrepository path into configuration. The fix is complete for the four advisories, but the release notes still warn that patches produced by hg export and hg diff can write through symlinks in some situations. Applying them safely is up to the tool, and GNU patch 2.8.1 accepts such writes, so use hg import for untrusted patches.

How do I check if I was compromised?

Mercurial has not published indicators of compromise. The useful checks are repository and configuration reviews.

Look for an illegal .hg path in every repository you pulled from an untrusted source:

hg manifest --all | grep -iE '(^|/)\.hg\.?(/|$)'

Inspect the repository configuration for hooks and extensions you did not add:

hg config hooks
hg config extensions

Check archives made before the upgrade for a .hg/hgrc entry:

tar -tf untrusted-archive.tar | grep -E '(^|/)\.hg(/|$)'
unzip -l untrusted-archive.zip | grep -E '(^|/)\.hg(/|$)'

On a server, review the push log for changesets that contain .hg paths. Mercurial 7.2.6 rejects them, so an accepted push before the upgrade is the lead. Also review the server's own hgrc for unexpected hooks or extensions.

A failed attempt may leave no trace. The .hgsub vector can run during a read-only command, and the configuration injection may not be logged. Treat an unexpected hook, a new extension, a repository that aborts with path contains illegal component, or a server process that starts a shell as a lead, not as proof.

Timeline

  • 2026-11-30: Mercurial 7.2.5 is released, and the project tells users to upgrade to it as an interim step before 7.2.6.
  • 2026-12-03: Mercurial 7.2.6 is released, with the PyPI package uploaded at 14:26 UTC and the four fixes.
  • 2026-12-03: The HG-SA-2026-001 to HG-SA-2026-004 advisories are published with the release.

Mercurial published the interim release and the critical fix three days apart
Figure 1: The interim 7.2.5 release and the 7.2.6 security release. Source: Mercurial release notes and PyPI upload times, checked December 3, 2026

FAQ

Is Mercurial 7.2.6 exploited in the wild?

No exploitation is reported. Mercurial calls the release critical and says all users should upgrade immediately, but the announcement does not report attacks. No Mercurial advisory is in CISA KEV as of 2026-12-03, and no public exploit code is published.

Which Mercurial versions are affected?

HG-SA-2026-001 and HG-SA-2026-004 affect every version before 7.2.6. HG-SA-2026-002 affects 7.1 to 7.2.5. HG-SA-2026-003 affects 2.6 to 7.2.5. If you run any version before 7.2.6, upgrade.

Do I need to upgrade if I only clone from my own server?

You still need to upgrade. Your server can relay a malicious changeset that it accepted from someone else, and a bundle or patch can arrive through a mirror or a review. The fix is cheap and the exposure depends on the data you process, not only on who owns the server.

Can I keep my version and change the configuration?

The release notes list configuration changes that reduce exposure, such as storage.revlog.validate-delta-base=yes, subrepos.allowed=no and worker.enabled=no. They slow some operations and do not cover every vector, so treat them as interim measures. The upgrade is the durable fix.

Does the fix cover patches produced by hg export?

Not completely. The release notes say patches from hg export and hg diff can still write through symlinks in some situations, and GNU patch 2.8.1 accepts those writes. Apply untrusted patches with hg import, which detects the write and refuses it.

Is there a CVE for these issues?

Mercurial publishes the four issues under its own HG-SA identifiers. No CVE is listed in the 7.2.6 announcement or the security page as of 2026-12-03.

References

  • Mercurial 7.2.6 release announcement, published 2026-12-03: https://mercurial-scm.org/news/2026/0009-release-7.2.6 (checked December 3, 2026)
  • Mercurial 7.2 release notes, security section, published 2026-12-03: https://mercurial-scm.org/relnotes/7.2.html (checked December 3, 2026)
  • Mercurial security advisories: https://mercurial-scm.org/security.html (checked December 3, 2026)
  • Mercurial 7.2.6 package on PyPI, uploaded 2026-12-03: https://pypi.org/project/mercurial/7.2.6/ (checked December 3, 2026)
  • Mercurial package download statistics: https://pypistats.org/packages/mercurial (checked December 3, 2026)
  • CISA Known Exploited Vulnerabilities catalog: https://www.cisa.gov/known-exploited-vulnerabilities-catalog (catalog 2026.11.30, checked December 3, 2026)

Free scan

Scan your own app for free

or book a demo

Keep reading

Related articles

Browse the blog