Repository navigation
Description
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 honorSOURCE_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
sha256sumcommand :) - 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
[-]Towards reproducible builds for our PyPI release wheels[/-][+]RFC: Towards reproducible builds for our PyPI release wheels[/+]ogrisel commentedon Jan 18, 2024
In addition to wheels, we might also want to obtain reproducible conda-forge release artifacts. This is being discussed here:
ogrisel commentedon Jan 19, 2024
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.jsonformat 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.thomasjpfan commentedon Jan 29, 2024
Does this include pinning our build dependencies in the
pyproject.tomlfor the release branch?ogrisel commentedon Jan 29, 2024
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
cibuildwheelto not rely onpyproject.tomlto setup its build environment but instead use one we can control with some form of lock file.betatim commentedon Jan 30, 2024
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
ogrisel commentedon Feb 6, 2025
There is ongoing work to survey SBOM generation tools for Python projects here:
ogrisel commentedon Oct 17, 2025
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:
The value for
CIBW_MANYLINUX_AARCH64_IMAGEwas 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 inpyproject.toml. Maybe we should pin all build dependencies in thepyproject.tomlfile prior to building a release? Not sure if this is a good idea or not.ogrisel commentedon Oct 17, 2025
It's probably a better idea to rely on
CIBW_DEPENDENCY_VERSIONSinstead: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,setuptoolsand co on non-linux builds:rgommers commentedon Oct 17, 2025
I think upper bounds in
pyproject.tomlare 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.
ogrisel commentedon Oct 17, 2025
Yes, I am reaching the same conclusion. To implement this, we will probably need to extend
cibuildwheelto inject the possibility to reuse a pinned build environment instead of dynamically setting up build isolation from the build dependency constraints of thepyproject.tomlfile.rgommers commentedon Oct 17, 2025
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.
betatim commentedon Oct 17, 2025
Is it worth proposing adding something to
cibuildwheelto 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).ogrisel commentedon Oct 17, 2025
I would be in favor of first trying to come with a proof of concept with the regular
cibuildwheeland then open an issue on thecibuildwheelrepo to streamline the process with we what we learned in our PoC.ogrisel commentedon Jun 22, 2026
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.
agriyakhetarpal commentedon Jun 22, 2026
It might even be worth writing a PEP to define what a locked
build-system.requirestable 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 readssetup_requiresand runssetup.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.get_requires_for_build_wheelreturning 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 ofPIP_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-depsas of 26.0)tool.uv.build-constraint-dependencies--dependency-constraints-txt, so a third way of spelling things (and this signals some fragmentation already :/)pylock.toml(as PEP 817/825 are already discussing adding a variant field topylock.toml).About how this would look like in
pyproject.toml: the most rudimentary option isrequires = "pylock.toml", but I think that is a no-go becauserequireshas 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 therequireskey, i.e., something like the following (note: can be iterated upon – this is only a rough sketch I had):or
In that sense, a PEP can address three things: what the table in
pyproject.tomlwould look like when standardised, what the semantics of the lock are, and what the hook clause would look like.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?
--no-build-isolation(see Validate build dependencies when using--no-build-isolationpypa/pip#9794), so this is more a case of making that range-based validation to be more exactThere 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.
rgommers commentedon Jun 22, 2026
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.tomlcontains abstract requirements, in a non-distribution-mechanism-specific way. Locks are concrete resolution results, and are specific to a particular ecosystem/channel --> separate files.