Skip to content

fix(core): enforce project boundaries at import and storage sinks - #1931

Merged
phernandez merged 7 commits into
mainfrom
codex/security-import-materialization-root
Oct 29, 2026
Merged

phernandez merged 7 commits into
mainfrom
codex/security-import-materialization-root

Conversation

@phernandez

@phernandez phernandez commented Oct 28, 2026 •

Copy link
Copy Markdown
Member

Why

Importer destinations, local accepted-note materialization, and regular-file moves can escape the selected project through traversal, absolute paths, or existing symlink ancestors. Twelve bounded real-filesystem regressions reproduced writes or moves outside the project. A complete directory-call audit reproduced two additional bypasses in Memory JSON and Claude project imports, which now use the guarded importer helper. Fixes #1908 (K34/K41/K42).

What Changed

Add one explicit FileService project-path guard and call it at importer folder/write boundaries, all local materialization storage reads/writes/deletes and cleanup alias checks, and regular-file move source/destination boundaries. Reject before creating directories or changing files.

Implementation Details

Reuse the existing portable relative-path validator and resolved local ancestry check. Absolute spellings remain supported within the chosen local root, including resolved aliases. Object storage validates relative logical keys without consulting the host filesystem. Validate EntityService move paths before existence preflight and rollback probes; remove its redundant mkdir so the guarded storage move owns parent creation. Storage paths permit literal tildes because these boundaries do not expand home markers. Native local POSIX storage also preserves literal-colon names; remote keys retain the portable drive restriction. The shared resolved-root helper also translates resolution failures, including Python 3.12 symlink-loop RuntimeError, into validation errors before storage mapping. General directory creation stays available for choosing arbitrary local project roots.

Stacked on #1930; only these destination boundaries and their regressions are new here.

Testing

  • Before the fix: all twelve traversal/absolute/symlink regressions failed across the four real sinks.
  • uv run pytest tests/importers/test_import_directory_containment.py tests/services/test_destination_containment.py tests/importers/test_importer_base.py tests/services/test_file_service.py tests/cloud/test_note_content_materialization.py -q --no-cov: 75 passed.
  • Controls preserve in-root absolute paths and remote logical keys; rejected operations preserve source bytes and create no outside directory.
  • A real EntityService symlink-move regression reproduced an outside parent directory created before rejection; 25 move/import/sink tests pass after removing the caller mkdir.
  • Review follow-up: a real outside-existing-destination probe and a literal-tilde file move both failed before correction. uv run pytest tests/services/test_non_markdown_moves.py tests/services/test_destination_containment.py tests/importers/test_import_directory_containment.py tests/services/test_file_service.py tests/utils/test_validate_project_path.py -q --no-cov: 74 passed.
  • Native-colon follow-up: two real local file moves failed before correction; 83 focused storage, move, importer and validation tests pass, including remote-key drive restrictions.
  • Python 3.12 follow-up: four real symlink-loop move cases failed before the shared resolver correction. The 3.12 destination suite passes 21 tests; 87 focused move/storage/import/validation tests pass on 3.14.
  • Canonical-file follow-up: nine real materialization/reuse/cleanup/alias/storage-access regressions failed before adding the shared path guard at every adapter access. uv run pytest tests/services/test_materialization_containment.py tests/services/test_destination_containment.py tests/cloud/test_note_content_materialization.py -q --no-cov: 59 passed.
  • just fast-check: passed, including repository type checking.
  • just doctor: passed sandboxed write/index/search self-test.

Risks / Follow-ups

This enforces the selected project boundary while preserving arbitrary project-root selection. It does not claim to prevent a concurrent local process swapping symlinks after validation. Symlink regressions skip Windows where privileges may be needed. Hosted adapters overriding these file operations retain their own storage authorization responsibilities.

@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-29T00:14:35.122286Z c6f0eb1 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: 2146f0d32a

ℹ️ 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".

Comment thread src/basic_memory/importers/base.py Resolved
@phernandez
phernandez force-pushed the codex/security-project-move-root branch from 678b3a2 to 51e7300 Compare October 28, 2026 21:27
@phernandez
phernandez force-pushed the codex/security-import-materialization-root branch from 2146f0d to a29f24d Compare October 28, 2026 21:27
@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: 47f7789fc6

ℹ️ 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 force-pushed the codex/security-project-move-root branch from 51e7300 to 1f8f180 Compare October 28, 2026 21:44
@phernandez
phernandez force-pushed the codex/security-import-materialization-root branch from 47f7789 to 21d4111 Compare October 28, 2026 21:44
@phernandez

Copy link
Copy Markdown
Member Author

@codex review

@phernandez
phernandez force-pushed the codex/security-import-materialization-root branch from 21d4111 to f2939a7 Compare October 28, 2026 22:17
@phernandez
phernandez force-pushed the codex/security-project-move-root branch from 1f8f180 to b20b121 Compare October 28, 2026 22:17
@phernandez

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Nice work!

Reviewed commit: f2939a7c7c

ℹ️ 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 force-pushed the codex/security-import-materialization-root branch from f2939a7 to 4923a55 Compare October 28, 2026 22:23
@phernandez
phernandez force-pushed the codex/security-project-move-root branch from b20b121 to 23333cb Compare October 28, 2026 22:23
@phernandez

Copy link
Copy Markdown
Member Author

@codex review

@phernandez
phernandez force-pushed the codex/security-project-move-root branch from 23333cb to dbb46a2 Compare October 28, 2026 22:26
@phernandez
phernandez force-pushed the codex/security-import-materialization-root branch from 4923a55 to 46d3ab7 Compare October 28, 2026 22:26
@phernandez

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

Reviewed commit: 46d3ab7a8e

ℹ️ 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 force-pushed the codex/security-project-move-root branch from dbb46a2 to 982b178 Compare October 28, 2026 23:01
@phernandez
phernandez force-pushed the codex/security-import-materialization-root branch from 46d3ab7 to f610f1d Compare October 28, 2026 23:01
@phernandez

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. What shall we delve into next?

Reviewed commit: f610f1d4b4

ℹ️ 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 force-pushed the codex/security-project-move-root branch from 982b178 to 29935e1 Compare October 28, 2026 23:05
@phernandez
phernandez force-pushed the codex/security-import-materialization-root branch from f610f1d to 4c483a3 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. Swish!

Reviewed commit: 4c483a32c6

ℹ️ 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 force-pushed the codex/security-project-move-root branch from 29935e1 to f86424e Compare October 28, 2026 23:10
@phernandez

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

ℹ️ 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 force-pushed the codex/security-import-materialization-root branch from 416add7 to 0719993 Compare October 28, 2026 23:19
@phernandez
phernandez force-pushed the codex/security-project-move-root branch from f86424e to 1ed4f67 Compare October 28, 2026 23:19
@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: 0719993b75

ℹ️ 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: 0e347ea275

ℹ️ 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: 0f08aa8bf4

ℹ️ 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

Copy link
Copy Markdown

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

Reviewed commit: c5ceade03b

ℹ️ 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-project-move-root to main October 28, 2026 23:49
@phernandez
phernandez changed the base branch from codex/security-project-move-root to main October 28, 2026 23:49
@phernandez
phernandez force-pushed the codex/security-import-materialization-root branch from c5ceade to c6f0eb1 Compare October 29, 2026 00:12
@phernandez

Copy link
Copy Markdown
Member Author

@codex review

Comment thread src/basic_memory/utils.py Dismissed
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Chef's kiss.

Reviewed commit: c6f0eb17d0

ℹ️ 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

CodeQL alert 95 was inspected, including its SARIF data flows. The flagged sink is Path.resolve() in the shared containment validator. The resolved path must pass is_relative_to(resolved_root) before it is returned for any content I/O; this is the normalize-then-validate sequence recommended by the alert itself. Existing real traversal, sibling-prefix, absolute/symlink, move-preflight, materialization reuse, and cleanup regressions reject escapes and preserve outside bytes. Dismissed alert 95 as a sanitizer-model false positive: https://github.com/basicmachines-co/basic-memory/security/code-scanning/95 . No code or guards changed.

@phernandez
phernandez merged commit d68feae into main Oct 29, 2026
53 checks passed
@phernandez
phernandez deleted the codex/security-import-materialization-root branch October 29, 2026 00:30
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]>
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.

Enforce destination containment for imports and local materialization

2 participants