Skip to content

fix(cli): validate normalized upload destinations before dispatch - #1928

Merged
phernandez merged 5 commits into
mainfrom
codex/security-upload-destination
Oct 28, 2026
Merged

phernandez merged 5 commits into
mainfrom
codex/security-upload-destination

Conversation

@phernandez

@phernandez phernandez commented Oct 28, 2026 •

Copy link
Copy Markdown
Member

Why

Legacy upload normalizes literal POSIX backslashes into separators without validating the resulting destination. A real temporary filename safe\..\..\other-project\leak.md became safe/../../other-project/leak.md; the HTTP client dispatched its PUT to /webdav/other-project/leak.md. Fixes #1905.

What Changed

Validate every destination using the shared portable relative-path contract before sizing, dry-run reporting, or any authenticated request. Reject the batch when a destination carries traversal, a drive, or an absolute root.

Implementation Details

Reject literal backslash filenames instead of remapping them to another remote key. Convert native directory separators with Path.as_posix(), then use the shared portable path-value validator with filename-preserving behavior. This preserves distinct edge-space and Unicode filename spellings. Skip archive validation because archives never reach dispatch. Cover both directory discovery and explicitly selected files. Regression tests run real files and a FastAPI HTTP receiver through ASGITransport, without mocking upload behavior.

Stacked on #1927; only upload destination validation and its tests are new here.

Testing

  • Before the fix: both public upload regressions failed; the directory case reached the wrong project route.
  • uv run pytest tests/cli/cloud/test_upload_destination.py tests/cli/cloud/test_upload_symlinks.py tests/cli/cloud/test_upload_path.py tests/cli/test_upload.py -q --no-cov: 30 passed.
  • Additional review regression: both ordinary backslash filename cases failed before the fix; 10 focused upload tests pass after it.
  • Review follow-up: whitespace-only files/components and skipped backslash archives failed before the fix; 56 upload/path tests pass. The shared validator is reused here without trimming names.
  • Literal-colon follow-up: five real HTTP regressions failed before the fix. 62 upload/path tests, 23 focused colon-policy tests, and 332 combined utility/filter/ZIP/upload/sink tests pass on the updated stack.
  • just fast-check: passed, including repository type checking.
  • just doctor: passed sandboxed write/index/search self-test.

Risks / Follow-ups

A client-side route escape is reproduced, not a hosted authorization bypass: server-side project authorization is still a separate boundary. Single-file and directory uploads reject unsupported literal backslashes. Native upload filenames explicitly allow literal colons, including one-letter prefixes, because these names are discovered relative to the selected tree and URL-encoded. Path-reference callers retain the default Windows-drive restriction. Filename spaces and Unicode spelling are preserved; three added real HTTP regressions failed before the spelling fix.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-28T23:07:04.109662Z 456dd50 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4d0756ca95

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@phernandez

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 8abb40565a

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@phernandez

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@phernandez

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c655134892

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Base automatically changed from codex/security-csv-preview-budget to main October 28, 2026 22:24
@phernandez
phernandez changed the base branch from codex/security-csv-preview-budget to main October 28, 2026 22:24
@phernandez

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@phernandez

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 47300a226b

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@phernandez

Copy link
Copy Markdown
Member Author

@codex review

@phernandez
phernandez force-pushed the codex/security-upload-destination branch from c75ddfc to 456dd50 Compare October 28, 2026 23:05
@phernandez

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Bravo.

Reviewed commit: 456dd50e6c

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@phernandez
phernandez merged commit 8803fbd into main Oct 28, 2026
52 checks passed
phernandez added a commit that referenced this pull request Oct 28, 2026
## Why

Configured project-root containment applies during creation but was
bypassed by move_project, which persisted arbitrary replacement paths.
Fixes #1896.

## What Changed

Require a move destination to resolve strictly beneath the configured
root before any config or database mutation. Reject sibling-prefix
paths, unrelated paths, the shared root itself, and symlink escapes.
Preserve unconstrained local moves.

## Implementation Details

Use the same resolved ancestry rule as project creation. Legitimate
constrained destinations are stored in resolved form. The existing
update_project API caller updates only active status; path moves run
through move_project.

Stacked on #1928 (the ignore-rule PR is held separately for a
compatibility decision); only move containment and its regression tests
are new here.

## Testing

- Before the fix: all four escape regressions failed because config and
database paths were updated.
- `uv run pytest tests/services/test_project_move_root.py
tests/services/test_project_service.py
tests/services/test_project_service_operations.py -q --no-cov`: 78
passed, 2 existing skips.
- Real config/database tests verify rejection leaves both unchanged and
valid descendants still work.
- `just fast-check`: passed, including repository type checking.
- `just doctor`: passed sandboxed write/index/search self-test.

## Risks / Follow-ups

This addresses a configured local-root boundary, not a demonstrated
hosted tenant escape. Symlink creation tests skip Windows. It does not
add protection against a concurrent local process replacing symlinks
after validation.

---------

Signed-off-by: phernandez <[email protected]>
phernandez added a commit that referenced this pull request Oct 29, 2026
## Why

Project creation and project moves duplicated resolved-root checks, and
note moves maintained separate portable-path syntax checks. The user
requested shared path helpers across these security boundaries.

## What Changed

Reuse resolve_path_within_root for project creation and project moves,
requiring strict descendants of a configured root. Reuse
valid_project_path_value for note-move syntax. Uploads already use that
portable validator in #1928; storage/import/materialization guards
already share resolved containment in #1931.

## Implementation Details

The shared helpers live in the preceding PRs. This PR removes the
remaining project and note-move copies and adds controls for root
permissions, sibling-prefix and symlink escapes, filename spelling, and
literal tildes. Filename normalization remains an explicit operation
policy; reference validation keeps its existing default restrictions.
Object-storage keys do not resolve against the host filesystem.

Stacked on #1931. The ignore-rule PR (#1929) is held separately and is
not a dependency.

## Testing

- `uv run pytest tests/utils/test_path_containment.py
tests/utils/test_validate_project_path.py
tests/utils/test_remote_project_paths.py
tests/runtime/test_note_move_destination.py
tests/services/test_destination_containment.py
tests/services/test_project_move_root.py
tests/services/test_project_root_containment.py
tests/cli/cloud/test_upload_destination.py -q --no-cov`: 118 passed on
the final stack.
- `just fast-check`: passed, including repository type checking.
- `just doctor`: passed sandboxed write/index/search self-test.

## Risks / Follow-ups

Note moves apply the shared portable restrictions, including control
characters and Windows-normalized traversal segments. File storage
explicitly permits literal tildes because it never expands home markers.
Native POSIX storage preserves literal-colon names; remote keys and
reference validation retain their drive restrictions. Archive syntax
remains a separately stricter format contract.

---------

Signed-off-by: phernandez <[email protected]>
phernandez added a commit that referenced this pull request Oct 29, 2026
## Why

Nested directory ignore rules were compared to individual components, so
explicitly excluded paths remained selected for indexing and export.
Review also reproduced fnmatch differences for separators, globstars,
and caret-negated classes. Fixes #1913.

## What Changed

Use pathspec for trailing-slash directory rules in the shared
should_ignore_path helper. All consumers, including indexing, uploads,
and ZIP planning, use the same matching behavior. Keep existing
non-directory matching and unordered exclusion-set behavior.

## Implementation Details

Declare pathspec >=1.1.1,<2 as a runtime dependency and lock it at
1.1.1. Compile complete directory-rule collections with the pure-Python
backend and a bounded cache of 32 ignore sets. A large rule set compiles
once across files rather than churning a per-rule cache. Preserve raw
directory-rule escapes when loading ignore files. Distinguish
directories from files with a trailing slash when matching. Remote and
ZIP callers pass their known file kind explicitly so a conflicting local
directory cannot silently hide a virtual file.

Two narrow normalizations retain Git behavior that differs from this
pathspec release: whole components containing two or more stars use
globstar semantics, and terminal directory globstars must consume a
directory. Literal leading !/# names retain their existing exclusion-set
meaning. The custom component-matching algorithm is removed. Compile
patterns independently inside each cached collection so invalid lines
cannot disable valid directory or file exclusions; invalid pathspec
patterns and invalid regex ranges are skipped consistently with Git
tolerance.

Based on main after #1928; this PR is independent of the
project/storage/YAML stack.

## Testing

- Before correction: nested rules, separator boundaries, zero-level and
terminal globstars, longer-star components, and caret-negated classes
reproduced failures.
- `uv run pytest tests/cli/test_nested_ignore_rules.py
tests/cli/test_ignore_utils.py
tests/importers/test_project_zip_import.py
tests/cli/cloud/test_upload_destination.py
tests/cli/cloud/test_webdav_transfer.py -q --no-cov`: 165 passed.
- Virtual-file regression: a remote file named `keys` was omitted when
the local side held a directory at `keys` and the rules included
`keys/`; the real transfer-plan regression failed before the fix and
passes afterward, while `keys/secret.md` remains excluded.
- Cache regression: 513 rules across three real files constructed 1,539
regex patterns before the fix and 513 after. The regression counts
actual pathspec calls with cProfile, without replacing its
implementation or timing thresholds.
- Invalid-rule regressions: four combinations of unusable directory
rules and valid directory/file exclusions failed before isolation and
pass after it.
- Real Git comparison across 180,069 generated file/directory cases:
zero differences. Includes directory pruning, all-star components, and
caret/exclamation-negated character classes.
- `just fast-check`: passed, including repository type checking.
- `just package-check`: passed all consolidated package checks.
- `just doctor`: passed sandboxed write/index/search self-test.

## Risks / Follow-ups

This adds a runtime dependency. It uses pathspec's supported pattern
syntax with the explicit globstar compatibility cases above; it does not
promise every Git extension. Ordered ignore-file negation is unchanged
and remains outside this fix because the existing loaders return
unordered exclusion sets. Non-directory matching is unchanged.

---------

Signed-off-by: phernandez <[email protected]>
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.

Normalize legacy upload paths before authorizing and dispatching

1 participant