Skip to content

RFC: Towards reproducible builds for our PyPI release wheels #28151

Open

Description

@ogrisel

Given the popularity of our project, our release automation might be considered an interesting target to conduct supply chain attacks to make our binaries ship spyware or ransomware to some of our users.

One way to detect such attacks would be to:

  • make sure we produce reproducible builds;
  • rebuild our wheels from independent build environments and check that we obtain the same hash as for binaries obtained by our release CI to make sure that our release CI environment has not been tampered to inject malware in our binaries;
  • optionally make it possible to publish GPG signed statements that some released artifact digests were successfully byte-for-byte reproduced from source independently.

The first step would to make our wheels as reproducible as possible would be to define deterministic values for the SOURCE_DATE_EPOCH (and maybe PYTHONHASHSEED, that cannot hurt) environment variables.

HHowever,this would not be enough.

To get this fully work as expected, we would also need to guarantee that:

  • we use recent enough versions of pip/setuptools/wheel/auditwheel/delocate
    that honor SOURCE_DATE_EPOCH;

  • a full description of the build environment (e.g. versions and sha256 digests of the
    compilers and other build dependencies) is archived in our source repo for a given tag
    of scikit-learn. Ideally, all those build dependencies should themselves be
    byte-for-byte reproducible from their own public source code repo.

Currently some build dependencies such as NumPy and Cython come from the pyproject.toml file which only specifies a minimum version. This means that we may end up with a newer versions of these dependencies than the one used to build the wheels for a given tag. cibuildwheel itself is not pinned, hence neither the dependencies it installs in its managed venvs (pip, setuptools, wheel, auditwheel, delocate).

Furthermore, we do not archive or pin the versions and sha256 digests of the compilers yet. For Linux, this depends on the manylinux docker image used by cibuildwheel, which at the time of writing, is not guaranteed to be reproducible, even when using the same docker image tag. For windows and macOS, the compilers come from the VM image used on our CI which we do not archive neither their version numbers nor the hash of their binaries.

Ideally all this information should be in our source code at the time of the release (reachable via a checkout of our commit tag).

Finally, we might need to set a specific umask:

Not sure about how to get deterministic file permission metadata for macOS and Windows wheels.

EDIT: now that we use meson, this problem with umask might have gone away, but we need to check.

EDIT2: I tried and I think we still have a sensitivity to umask after the switch to the meson build system.

Finally, once our builds are made 100% reproducible, we would need to document:

  • document instructions (and provide official scripts) to allow anyone to easily re-build the binaries independently from source;
  • make it process easy to automate on private infrastructure (distinct from our usual public CI);
  • publish official reproducibility results on a public site, ideally not only on our github pages hosted website and maybe sign those with GPG for instance;
  • coordinate with people from https://scientific-python.org or even the PSF or pypi.org admins to define and follow community-wide best practices.

This is just for scikit-learn itself. But for this kind of supply chain audit to be meaningful, we would need to make sure that all the tools in the build pipeline of scikit-learn are themselves reproducible and regularly and independently reproduced, including:

  • compilers;
  • runtime libraries such as the libc;
  • build dependencies (numpy, Cython, meson-python, ninja, cibuildweel);
  • wheel binary editing tools such as auditwheel/delocate/delvewheel/repairwheel;
  • the sha256sum command :)
  • the whole manylinux docker image and probably docker since it is required to build manylinux wheels.

We would also need to snapshot the provenance info before running the tests (in case pytest or any test dependency are themselves supply chain attacked). For instance, the test environment was effectively used to hide the attack on the xz binaries.

Note that a large fraction of debian is already reproducible but we would need to trace everything in our build process to check that this is the case.

Doing all of this will require significant maintainers time investment, but we can probably start from low hanging fruits such as setting SOURCE_DATE_EPOCH in our release CI scripts.

Activity

  1. added
    Needs Triage
    on Jan 17, 2024
  2. added
    Meta-issue
    and removed
    Needs Triage
    on Jan 17, 2024
  3. changed the title [-]Towards reproducible builds for our PyPI release wheels[/-] [+]RFC: Towards reproducible builds for our PyPI release wheels[/+] on Jan 17, 2024
  4. ogrisel commented on Jan 18, 2024

    @ogrisel
    MemberAuthor

    In addition to wheels, we might also want to obtain reproducible conda-forge release artifacts. This is being discussed here:

  5. ogrisel commented on Jan 19, 2024

    @ogrisel
    MemberAuthor

    The following blog posts by @sethmlarson can serve as interesting references:

    In particular, we could attempt to generate SBOM descriptions (Software Bill of Materials) using the .spdx.json format for each of our wheels (and for the source tarball) as a first step and then provide a script to automate byte for byte reproductions using the information provided in the SBOM files.

  6. thomasjpfan commented on Jan 29, 2024

    @thomasjpfan
    Member

    Does this include pinning our build dependencies in the pyproject.toml for the release branch?

  7. ogrisel commented on Jan 29, 2024

    @ogrisel
    MemberAuthor

    Does this include pinning our build dependencies in the pyproject.toml for the release branch?

    I don't think so because we don't want the generated package metadata to be pinned to a particular version of the dependencies.

    But we would need to find a way to tell cibuildwheel to not rely on pyproject.toml to setup its build environment but instead use one we can control with some form of lock file.

  8. betatim commented on Jan 30, 2024

    @betatim
    Member

    I had understood it so that there is a tool that creates a record of your build environment and a second (set of) tool(s) that can recreated the build environment from that record.

    Not sure if this would mean that you are free to create the build environment however you want (pinned or not) or if you need to use a particular tool/strategy to create it so that the tool that creates the record of the build environment can do its job.


    In general I think this would be a useful thing to have/work on. As Olivier said it will be quite a lot of work though. So picking some easier first steps that already provide some value is a good way to get started.

    A learning from mybinder.org users is that you need to exercise the "recreate environment from your record" step regularly to check that it works and continues to work. A bit like you can't just create a tape backup and then put the tape in a safe. You need to regularly try to restore your backup to check it still works, reposition the tape, copy to a newer tape, etc. The learning was that just because today you can successfully recreate the environment does not mean that you will be able to do so tomorrow. Yes, the chances are high but not 100%. So yet another thing to think about/work on :D

  9. ogrisel commented on Feb 6, 2025

    @ogrisel
    MemberAuthor

    There is ongoing work to survey SBOM generation tools for Python projects here:

  10. ogrisel commented on Oct 17, 2025

    @ogrisel
    MemberAuthor

    For the record, we attempted to reproduce the 1.7.2 cp313 linux_aarch64 release on our local machines with @lesteve.

    Here are the commands we used:

    git checkout 1.7.2
    export SOURCE_DATE_EPOCH=$(git log -1 --pretty=%ct)
    export PYTHONHASHSEED=0
    CIBW_BUILD=cp313-manylinux_aarch64 CIBW_MANYLINUX_AARCH64_IMAGE=quay.io/pypa/manylinux_2_28_aarch64:2025.08.15-1  cibuildwheel --archs aarch64 --platform linux --output-dir wheelhouse
    

    The value for CIBW_MANYLINUX_AARCH64_IMAGE was found by inspecting the 1.7.2 build logs that was fortunately not lost. To find the logs for a given branch, look at:

    https://github.com/scikit-learn/scikit-learn/actions/workflows/wheels.yml?query=branch%3A1.7.X

    Then look at the release date on pypi.org and then find the previous [cd build] labeled commit in that log.

    Unfortunately, this is not enough. According to diffoscope, our native extensions still differ with the release file, most probably because we do not pin the version of Cython in our build environment. The Cython version (3.1.3 in this case) is still visible in the build log. But I am not sure how we could tell cibuildwheel to install a specific version of Cython (or other build dependencies, in the build environment it creates from the info in pyproject.toml. Maybe we should pin all build dependencies in the pyproject.toml file prior to building a release? Not sure if this is a good idea or not.

  11. ogrisel commented on Oct 17, 2025

    @ogrisel
    MemberAuthor

    Maybe we should pin all build dependencies in the pyproject.toml file prior to building a release? Not sure if this is a good idea or not.

    It's probably a better idea to rely on CIBW_DEPENDENCY_VERSIONS instead:

    https://cibuildwheel.pypa.io/en/stable/options/#dependency-versions

    EDIT: actually this might not be suitable to pin build dependencies but only tools like pip, setuptools and co on non-linux builds:

    This option does not affect the tools used on the Linux build - those versions are bundled with the manylinux/musllinux image that cibuildwheel uses. To change dependency versions on Linux, use the manylinux-* / musllinux-* options.

  12. rgommers commented on Oct 17, 2025

    @rgommers
    Contributor

    I think upper bounds in pyproject.toml are one option indeed, but not always desirable. I do like using them to guard against expected breakage long-term, but not for exact pins that prevent even bug-fix releases to be used. E.g., see https://github.com/scipy/scipy/blob/maintenance/1.16.x/pyproject.toml#L27 where we use "Cython>=3.0.8,<3.2.0", and similar bounds for other dependencies.

    For full reproducibility I think what is needed is recording the exact versions for the original build, and then doing non-isolated builds in a venv with those exact same versions installed to reproduce later.

  13. ogrisel commented on Oct 17, 2025

    @ogrisel
    MemberAuthor

    For full reproducibility I think what is needed is recording the exact versions for the original build, and then doing non-isolated builds in a venv with those exact same versions installed to reproduce later.

    Yes, I am reaching the same conclusion. To implement this, we will probably need to extend cibuildwheel to inject the possibility to reuse a pinned build environment instead of dynamically setting up build isolation from the build dependency constraints of the pyproject.toml file.

  14. rgommers commented on Oct 17, 2025

    @rgommers
    Contributor

    I think it can be done without upstream changes, but perhaps it's a little clumsy indeed. The environment should be created in before-build hook I think, and the isolation can be flipped in the reproducer script by using CIBW_BUILD_FRONTEND.

  15. betatim commented on Oct 17, 2025

    @betatim
    Member

    Is it worth proposing adding something to cibuildwheel to make it easier for others (average Python user on the street) to reproduce builds? Or is there another project which is more relevant? Thinking about the fact that we want to have a world where more projects produce reproducible release artefacts and hopefully more people check the files in a release it would make sense to make it easy to do (or as easy as reasonably possible).

  16. ogrisel commented on Oct 17, 2025

    @ogrisel
    MemberAuthor

    I would be in favor of first trying to come with a proof of concept with the regular cibuildwheel and then open an issue on the cibuildwheel repo to streamline the process with we what we learned in our PoC.

  17. ogrisel commented on Jun 22, 2026

    @ogrisel
    MemberAuthor

    As explained in scikit-learn/scikit-learn-release#1, it should be possible to use pixi or uv to manage lock files and then configure cibuildwheel to use those lockfiles for its build environment instead of solely relying on the info in pyproject.toml. We could then archive the lockfiles (and even export them as pylock.toml and/or cyclone dx for third party auditing tools) in order to make the build environment bitwise traceable/reproducible, at least on linux where the system dependencies are delegated to manylinux.

  18. agriyakhetarpal commented on Jun 22, 2026

    @agriyakhetarpal
    Contributor

    It might even be worth writing a PEP to define what a locked build-system.requires table can look like in order to be consumed by PEP-517-implementing build tools. I would be pretty interested in this topic and even potentially co-authoring something, especially because of its deferment in PEP 751.

    Here is a related DPO thread that hasn't seen the light of day in a while: https://discuss.python.org/t/no-way-to-pin-build-dependencies/29833.

    I previously did some of my own discovery about this many weeks back, and I feel that this might be a good place and time to put down my notes (including gotchas, implementation details, ideas, etc.) from the time:

    • Since some PEP 517 hooks are dynamic, such as get_requires_for_build_wheel (or _sdist/_editable) and some build backends use them to inject build-time dependencies based on context at build time (such as meson-python, which installs patchelf and ninja only if they are not already present on the system), there are some cases where build-time dependencies are not expressible as PEP 508 markers. Similarly, setuptools also reads setup_requires and runs setup.py. This can (and probably will) cause problems when a lockfile generated on one machine may not match what the hook would ask for on another, and therefore is one of the more important design decisions a PEP would need to make.

      • To that effect, I would suggest that the best option would be to lock the full closure and still call the PEP 517 hook, but we should require the output received from it to be satisfied by the lock, and raise an error in case it doesn't.
      • One caveat here is that such a satisfaction check only governs what the hook requests. It is blind to what a backend discovers ambiently, as an empty get_requires_for_build_wheel returning is a bit ambiguous. That can either mean that nothing is needed, or that ninja is already found on PATH. The ambient case is a separate gap that I have listed below.
    • This would (mostly) be a change that would be required in the build frontends/installers, not the build backends. There has been a somewhat independent conclusion reached that build constraints must be decoupled from runtime constraints:

      • PIP_CONSTRAINT's effect on build environments has already been deprecated in favour of PIP_BUILD_CONSTRAINT. This is added behind a flag --use-feature=build-constraints (see Add build constraints pypa/pip#13534, which also adds --use-feature=inprocess-build-deps as of 26.0)
      • uv has a similar tool.uv.build-constraint-dependencies
      • pypa/build has --dependency-constraints-txt, so a third way of spelling things (and this signals some fragmentation already :/)
      • One question I have about this is the proposed PEP 817 variant provider plugins that are in progress. These plugins are invoked (and run arbitrary code) at both build and installation time, and based on my readings at the time, I didn't see a mention of locking the plugins in any fashion. So I think we might need to factor that aspect into the PEP based on how the timeline goes. My current feeling is that build-time plugin invocation is a build dependency and so in scope for the lock, whereas install-time plugin execution is out of scope here and belongs with variant handling in pylock.toml (as PEP 817/825 are already discussing adding a variant field to pylock.toml).
    • About how this would look like in pyproject.toml: the most rudimentary option is requires = "pylock.toml", but I think that is a no-go because requires has been an array of PEP 508 strings, and every backend's parser would reject it. I opine that needs a bit too much effort everywhere across the ecosystem. The better option is something where the lock gets generated from the requires key, i.e., something like the following (note: can be iterated upon – this is only a rough sketch I had):

      [build-system]
      requires = ["setuptools>=70", "numpy", "Cython>=3"]
      build-backend = "setuptools.build_meta"
      requires-lock = "pylock.build.toml" # we add another PEP 751 lock file (or it could be within an existing lockfile under a new table)

      or

      [build-system]
      requires = ["setuptools>=70", "numpy", "Cython>=3"]
      build-backend = "setuptools.build_meta"
      
      [build-system.lock]
      path = "pylock.build.toml"
      enforce-hashes = true
    • In that sense, a PEP can address three things: what the table in pyproject.toml would look like when standardised, what the semantics of the lock are, and what the hook clause would look like.

      • With a lock present, the build frontend populates an isolated environment solely from it.
      • The frontend still calls get_requires_for_build_wheel, but we require that every requirement the hook returns must be satisfied by the installed locked set before we install anything, i.e., we must fail and instruct the package author to regenerate the lockfile.
    • What happens when build isolation is disabled?

      • We don't want the lockfile to be silently ignored. It should still have an effect, which would be that the frontend should (or must?) still check that the user-owned environment satisifies every locked package (and its hash, wherever resolvable). The frontend moves from provisioning the installation to just verification in this case. pip, for example, already validates the build dependencies when using --no-build-isolation (see Validate build dependencies when using --no-build-isolation pypa/pip#9794), so this is more a case of making that range-based validation to be more exact
      • This should either be a hard error or a warning. It could even be a hard error by default, with the option to downgrade to a warning.
    • There are a few cases that the PEP can't still address without being accompanied by other PEPs. However, I would not see that as a deterrent to this form of Python-dependency-only locking. For example, if ninja is installed from Homebrew and is detected on PATH by the hook (or patchelf/cmake/pkg-config/or the unavoidable compilers, which are ambient by nature and never installed from PyPI), this falls outside what a Python lock can capture, and IMO the right place(s) for it to happen are via PEP 725/PEP 804 and (ideally) an exported SBOM (but as of the status quo, it defers external build dependencies to PEP 725 itself). In that regard, locking the build-time dependencies can never, even in principle, mean locking everything the build touches. That kind of thing I assume will be closed by other PEPs, and even having an exported SBOM for a start would be pretty useful for describing the process of a reproducible build and for gaining better visibility into external dependencies from a non-tooling stance.

  19. rgommers commented on Jun 22, 2026

    @rgommers
    Contributor

    That's all pretty ambitious:) I'd start with just installing the complete build dependencies in an environment and then building wheels with --no-build-isolation.

    One comment on a conceptual aspect: locking definitely does not belong inside pyproject.toml, but rather somewhere separate. pyproject.toml contains abstract requirements, in a non-distribution-mechanism-specific way. Locks are concrete resolution results, and are specific to a particular ecosystem/channel --> separate files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Meta-issueRFC

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions