Skip to content

Use handle-based exclusive creation in FileHelper - #5354

Merged
mnriem merged 4 commits into
github:mainfrom
mnriem:mnriem-file-helper-creation-handles
Nov 10, 2026
Merged

mnriem merged 4 commits into
github:mainfrom
mnriem:mnriem-file-helper-creation-handles

Conversation

@mnriem

@mnriem mnriem commented Nov 9, 2026

Copy link
Copy Markdown
Collaborator

Description

Use handle-based parent traversal for FileHelper.create_bytes() and create_text(), so exclusive creation uses the policy-resolved directory throughout the operation.

POSIX creation opens resolved parents without following new links and creates the file relative to the final directory descriptor. Windows creation pins the resolved hierarchy using the existing handle primitive, extracted into a shared private module.

Preserves supported contained parent links and link chains, trusted root aliases, deny-mode rejection, existing-leaf rejection, existing-parent requirements, mode/umask semantics, and byte/text encoding behavior. Adds focused creation coverage and updates the filesystem contract. Read, deletion, and overwrite behavior are unchanged.

Testing

Set up this checkout's .venv and editable install using the local development guide. Run commands from the checkout root.

  • Tested locally with .venv/bin/specify --help (Windows PowerShell: .\.venv\Scripts\specify.exe --help)
  • Ran relevant existing tests with .venv/bin/python -m pytest tests -q (Windows PowerShell: .\.venv\Scripts\python.exe -m pytest tests -q); use affected test paths for focused checks and LC_ALL=en_US.UTF-8 on macOS
  • Tested with a sample project (if applicable)

Commands and results:

  • .venv/bin/specify --help — passed.
  • LC_ALL=en_US.UTF-8 .venv/bin/python -m pytest tests/test_file_helper.py tests/specify_cli/workflows/test_catalog_cache.py tests/specify_cli/test_file_policy.py -q — 476 passed, 92 skipped.
  • git diff --check — passed.

Validation ran on macOS. Native Windows tests are included but were not executed on this host. A separate sample-project run was not performed; the affected filesystem operations are exercised directly by the focused tests.

AI Disclosure

  • I did not use AI assistance for this contribution
  • I did use AI assistance (fill in the disclosure below)

AI disclosure: Implemented with GitHub Copilot using GPT-6.1 Sol (model ID: gpt-6.1-sol) in autopilot/autonomous mode. AI assistance covered implementation, tests, documentation, validation execution, and this PR description on behalf of @mnriem.

Anchor byte and text creation to policy-resolved parent directories while preserving contained links, root aliases, modes, and encoding. Reuse the Windows handle primitive and cover creation behavior with focused tests.

Assisted-by: GitHub Copilot (model: gpt-6.1-sol, autonomous)
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI adaptive review requested due to automatic review settings November 9, 2026 23:25

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The extracted CreateFileW prototype omits a required DWORD argument, causing TypeError failures in Windows exclusive creation and workflow/step catalog cache access.

3 open issues
What changed in this PR

Adds handle-based parent traversal to Spec Kit’s FileHelper exclusive-create operations to guard against parent-path replacement.

Changes:

  • Use descriptor-relative creation on POSIX and pinned parent handles on Windows.
  • Share the Windows handle primitive with catalog caching.
  • Expand creation regression coverage and document filesystem guarantees and limits.
File Description
tests/​test_file_helper.py Adds parent-swap, encoding, mode, and handle-lifetime coverage.
src/​specify_cli/​workflows/​_catalog_cache.py Imports the shared Windows handle primitive.
src/​specify_cli/​file_helper.py Anchors parent traversal for exclusive byte/text creation.
src/​specify_cli/​_file_handles.py Houses shared Windows handle management.
design/​file-helper.md Documents anchored creation and remaining safety limits.

Review effort: Max (auto)

Comment thread src/specify_cli/_file_handles.py Resolved
Comment thread src/specify_cli/file_helper.py Resolved
Comment thread src/specify_cli/file_helper.py Resolved
Restore the complete CreateFileW prototype, use search-only POSIX directory handles where available, and close previous parent descriptors during traversal. Add regression coverage for the Windows prototype, directory permissions, and bounded descriptor use.

Assisted-by: GitHub Copilot (model: gpt-6.1-sol, autonomous)
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI adaptive review requested due to automatic review settings November 9, 2026 23:52
Use Darwin's native O_SEARCH value when Python does not expose the flag. Cover write/search-only parents with missing exported search flags and preserve no-follow directory traversal.

Assisted-by: GitHub Copilot (model: gpt-6.1-sol, autonomous)
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The exclusive-creation fallback requires directory read permission when search-only flags are unavailable, breaking supported write-and-traverse-only cases and their existing permission tests.

2 open issues
1 previously missed issue

In code that hasn't changed since last review

Low severity Clarify race disclaimer covers all path-based operations

design/​file-helper.md:223

“Those path-based operations” can be read as limiting the race disclaimer to writes and deletion. Reads, overwrite_text, and symlink creation still use validated paths that can be redirected by a concurrent parent swap. Keep the disclaimer covering all path-based operations, except for the exclusive-creation parent anchoring described below.

3 resolved since last review

Review effort: Max (auto)

Comment thread src/specify_cli/file_helper.py Resolved
Comment thread design/file-helper.md Resolved
Copilot AI adaptive review requested due to automatic review settings November 10, 2026 00:11
Document Windows directory-listing and attribute access for pinned parents, and keep the path-based-operation disclaimer explicit for reads, directory creation, writes, overwrites, symlink creation, and deletion.

Assisted-by: GitHub Copilot (model: gpt-6.1-sol, autonomous)
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@mnriem

mnriem commented Nov 10, 2026 •

Copy link
Copy Markdown
Collaborator Author

Addressed the review feedback in e506d29, 6083180, and 95e7a69: restored the complete Windows CreateFileW prototype, bounded POSIX directory-descriptor use, preserved write/search-only access on older macOS Python builds, documented Windows directory-listing and attribute-access requirements, and clarified the limits for all remaining path-based operations. The latest head is 95e7a69. Local focused checks passed (530 tests, 92 skipped), the affected workflow/catalog checks passed (1,960 tests, 29 skipped), and Ruff and whitespace checks passed. Upstream CI on 95e7a69 passed all nine Linux/macOS/Windows Python jobs, including native Windows tests and macOS Python 3.11, and the lint/dependency-audit check passed. On behalf of @mnriem: this response and the changes were generated with GitHub Copilot using GPT-6.1 Sol (gpt-6.1-sol) in autopilot/autonomous mode, covering implementation, tests, documentation, validation execution, and review-response text.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

A Windows junction-conversion race can redirect file creation outside the intended root despite the pinned parent handle.

1 open issue
2 previously missed issues

In code that hasn't changed since last review

High severity Full-path creation follows a parent changed to a junction

src/​specify_cli/​file_helper.py:223

On Windows, an empty pinned parent can still be converted in place into a junction: FSCTL_SET_REPARSE_POINT accepts FILE_WRITE_ATTRIBUTES, which withholding delete sharing does not block. If this happens after the handle's attribute check, this full-path open follows the junction and creates the file outside the root. Use handle-relative creation with reparse refusal, and add a native Windows regression that converts the empty parent immediately before creation.

Medium severity Parent pinning requires unnecessary directory-listing permission

src/​specify_cli/​_file_handles.py:44

Reusing this handle for FileHelper.create_*() adds a Windows permission requirement: 0x1 is FILE_LIST_DIRECTORY. A non-privileged user can have traversal and file-creation rights without directory-listing rights; the previous exclusive os.open() could create there, but this parent handle now fails with access denied. Use pinning access rights that do not require listing while still enforcing delete-sharing checks, and add a native Windows ACL regression case.

1 resolved since last review

Review effort: Max (auto)

Copilot AI adaptive review requested due to automatic review settings November 10, 2026 00:32

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Human assessment is needed to judge the diagnostic impact of losing full destination-path context in parent-traversal and leaf-open errors.

No open issues 🎉

1 resolved since last review

Review effort: Max (auto)

@mnriem
mnriem merged commit 199bebb into github:main Nov 10, 2026
16 checks passed
@mnriem
mnriem deleted the mnriem-file-helper-creation-handles branch November 10, 2026 13:50
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.

2 participants