Skip to content

fix(builder): reject backend-path entries that leave the source tree - #1299

Open
v01dst wants to merge 2 commits into
pypa:mainfrom
v01dst:fix/1267-backend-path-outside-source-tree
Open

v01dst wants to merge 2 commits into
pypa:mainfrom
v01dst:fix/1267-backend-path-outside-source-tree

Conversation

@v01dst

@v01dst v01dst commented Nov 19, 2026

Copy link
Copy Markdown

Why I made this PR

backend-path entries have to stay inside the source tree (PEP 517), but build only checked that the entry was a directory. Two things slipped through:

  • An entry that leaves the source tree (.., an absolute path) was handed to pyproject-hooks, which raised a bare ValueError. The except BuildException pattern shown in the API guide does not catch it, and the CLI prints a traceback before its error line.
  • An entry that is a symlink to a directory outside the source tree was accepted, and build then imported the backend from outside the tree. That is backend-path accepts symlinks outside the source tree #1267.

This narrows the scope of the original report to the backend-path case, following @henryiii's note on #1264. The other malformed inputs listed there are left as they are.

Description

_validate_backend_path checks every entry before the hook caller is created:

Entry Before After
.. ValueError: paths must be inside source tree BuildSystemTableValidationError
an absolute path ValueError: paths must be relative BuildSystemTableValidationError
a symlink to a directory outside the tree accepted, the backend was imported from outside the tree BuildSystemTableValidationError
a symlink to a directory inside the tree accepted accepted
a missing entry, or a file BuildSystemTableValidationError unchanged

BuildSystemTableValidationError is a BuildException, so the documented error handling catches it and the CLI prints one error line instead of a traceback.

The containment check resolves symlinks on both sides and compares the results with pathlib.Path.is_relative_to, so it does not need the cross-drive special case that os.path.commonpath needs on Windows. The directory check runs first, so an entry that cannot be resolved at all (a path with a null byte, a symlink loop) is still reported the way it was before.

Changelog

  • Added changelog fragment: docs/changelog/1299.bugfix.rst.

Checklist

  • I looked at the contributing guide (docs/development/contributing.rst)
  • Tests pass locally (pytest suite, ruff format and check, pyrefly)
  • The PR description is not significantly longer than the change
AI assistance (supporting detail, reviewed by the author)

The reproduction and the test cases were written with an AI coding assistant (OpenCode, MiniMax-M3.1-Flash). The author checked the behaviour of every path against the code on main before and after the change: a backend-path entry of .. raised ValueError: paths must be inside source tree from pyproject-hooks before the change and raises BuildSystemTableValidationError after it, and a backend-path entry that is a symlink to a directory outside the source tree was accepted before the change and is rejected after it. The test suite was run locally with Python 3.12 on Windows (362 passed, 3 skipped).

`backend-path` entries have to stay inside the source tree (PEP 517), but
`build` only checked that the entry was a directory.

An entry that leaves the source tree (`..`, an absolute path) was handed to
`pyproject-hooks`, which raised a bare `ValueError`. The `except
BuildException` pattern shown in the API guide does not catch it, and the CLI
prints a traceback before its error line.

An entry that is a symlink to a directory outside the source tree was
accepted, and `build` then imported the backend from outside the tree.

`_validate_backend_path` now checks every entry before the hook caller is
created. The containment check resolves symlinks on both sides and compares
the results with `pathlib.Path.is_relative_to`, so it does not need the
cross-drive special case that `os.path.commonpath` needs on Windows. The
directory check runs first, so an entry that cannot be resolved at all is
still reported the way it was before.

Fixes pypa#1267.
Refs pypa#1264.

Signed-off-by: v01dst <v01dst@users.noreply.github.com>
Signed-off-by: v01dst <v01dst@users.noreply.github.com>

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant