Skip to content

engine: heph import add (STEP/mesh into imports/) - #36

Merged
Code4me2 merged 3 commits into
mainfrom
cursor/heph-import-add-20d9
Aug 31, 2026
Merged

Code4me2 merged 3 commits into
mainfrom
cursor/heph-import-add-20d9

Conversation

@Code4me2

@Code4me2 Code4me2 commented Aug 31, 2026 •

Copy link
Copy Markdown
Member

Closes #35 (parent #14). Engine/CLI only — no web/, no #29 scan-to-part, no second kernel, no surface reconstruction.

What this is

Kernel import already exists (import_step, import_mesh / import_point_cloud). The hole was project ingress: an operator could not admit a vendor STEP or scan into imports/ without hand-copying. heph reference add is the document counterpart; this is the geometry one.

INTERFACE.md §15.37 is unchanged: browser / viewport import stays deferred. One sentence in docs/cli.md records that.

Verbs

  • heph import add FILE copies into project imports/ (path confinement, O_NOFOLLOW, original untouched). --json emits {name, kind: step|mesh|points, sha256, path, units?}.
  • STEP (.step / .stp, AP203/AP214): no --units. STL/PLY/OBJ/OFF/XYZ require --units {mm,cm,m,in} (never inferred). Unknown suffix is a named refusal.
  • Optional --part NAME seeds parts/<name>.py through create_part (refuse if exists, no force):
    • STEP → part.geometry = import_step("copied-name")
    • mesh → import_mesh(..., units=...) then mesh_to_solid with the declared unit
  • heph import list lists imports/ the way heph reference list lists references.

Confinement

  • Destination walk is O_NOFOLLOW; a planted dest symlink is refused.
  • Dest is never opened with O_TRUNC. Payload goes to an exclusive temp inode under imports/, then rename onto the dest name, so a planted hardlink cannot write through to a file outside the project.

Tests

core/tests/test_cli_import.py (13 passed): happy STEP (exported Box) + mesh, missing file, traversal/symlink, planted dest hardlink (outside bytes unchanged), mesh without units, existing part name, suffix/units/--part seed contracts.

Open in Web Open in Cursor 

cursoragent and others added 2 commits August 31, 2026 20:41
Admit a vendor STEP or scan into project imports/ without hand-copying.
Optional --part seeds create_part with import_step or import_mesh +
mesh_to_solid. Browser import stays deferred (INTERFACE.md §15.37).

Closes #35.

Co-authored-by: Vel <Code4me2@users.noreply.github.com>
write_part is on ProjectStore, not the raw opstore. Ingress path
refusals now print [path_confinement] so the CLI names the same
Stage 8A/12A reason the walk already raised.

Co-authored-by: Vel <Code4me2@users.noreply.github.com>
@Code4me2
Code4me2 marked this pull request as ready for review August 31, 2026 20:44
@Code4me2

Copy link
Copy Markdown
Member Author

Adversarial review (issue #35 / parent #14)

Reviewed 8466b6f (cursor/heph-import-add-20d9 off main f318dbd). Engine/CLI only: six files, +734/−1. Official core/tests/test_cli_import.py 12 passed. Additional probes run against a live heph from this tree.

This is not a merge.

Scope (does not trip the listed gates)

Gate Result
web/ paint Unchanged
Kernel rewrite / second kernel Only a public alias validate_import_path over the existing Stage 8A/12A walk
Units inferred Closed set via argparse choices; mesh/XYZ without --units is mesh_units_undeclared; STEP + --units is step_units_not_applicable
Existing part overwritten Pre-check + write_part(..., base_hash=None) (same create contract as heph part create); existing parts/plate.py left byte-identical, import not copied
INTERFACE.md §15.37 0-byte diff. Still: "No import. Egress is one-way"
G4 / G5 / G10 No web/, no tests/stage4, no tests/stage10
Fake reconstruction / #29 XYZ --part is point_cloud_has_no_solid (no copy, no parts/*.py). Mesh seed is import_mesh + existing mesh_to_solid(..., intent="measurement_target")

--part seeds the specified terms. heph import list walks regular files only and skips unknown suffixes.

BLOCK: dest hardlink write-through (path escape)

write_import_copy opens the leaf with O_CREAT \| O_TRUNC \| O_NOFOLLOW and then only checks S_ISREG. A hardlink is a regular file. O_NOFOLLOW does not save you. O_TRUNC writes through the inode.

Repro (this tree, just now):

imports/planted.step  --hardlink-->  /tmp/outside.step   (contents: ORIGINAL-OUTSIDE)
heph import add vendor.step --name planted.step
# exit 0
# /tmp/outside.step is now NEW-PAYLOAD-STEP-CONTENT

The planted-symlink case in test_add_refuses_traversal_and_symlink_escape is the right test and it passes. The sibling attack — same dest name, hardlink instead of symlink — mutates a file that is not under imports/.

That is the confinement claim (#35: path confinement, no escape; PR: "the destination is always a regular file — never a symlink — so a later import_step cannot escape through a link this verb planted"). The verb did not plant the link; it wrote through one that was already there. Same threat model as the symlink they already refuse.

Fix that actually confines the write (any one is enough):

  • write to a new name under imports/ (O_EXCL) then rename over the dest (replaces the directory entry; does not truncate the old inode), or
  • after fstat, refuse st_nlink > 1 as path_confinement, or
  • refuse an already-existing dest instead of O_TRUNC.

Add a hardlink case next to the existing symlink case. Until then the walk is not the Stage 8A/12A confinement it claims to be — that walk is a read. This is a write.

Non-blocking notes (not the verdict)

  1. Dest-name is the classifier. heph import add scan.stl --name scan.step admits STL bytes as kind: step with no --units. Classification-by-admitted-name is what the card asked for; an operator can still launder a mesh into the STEP door. Worth a named refusal or a sniff if you want the dest suffix to mean the bytes.
  2. Silent overwrite of an existing regular import. Second add --name same.step O_TRUNCs the first copy. Not a part overwrite; not specified as refuse-if-exists.
  3. FIFO dest. open(O_WRONLY) on a planted FIFO blocks until a reader appears — S_ISREG runs after open. Hang, not an escape. O_NONBLOCK or a pre-open fstatat(..., AT_SYMLINK_NOFOLLOW) would refuse it the way a symlink is refused.
  4. XYZ --part is implemented and correct (point_cloud_has_no_solid, no mutation). Not in the 12 tests.
  5. heph import list cannot emit units. Units are not stored on disk; list honestly omits them. Fine for a reference list analogue.

BLOCK — heph import add --name write-through of a dest hardlink mutates a file outside imports/.

A planted hardlink is a regular file; opening the dest with O_TRUNC
wrote through the inode to a file outside the project. Write an
exclusive temp under imports/ and rename onto the dest name so the
outside link keeps its bytes. Symlink dests stay refused (O_NOFOLLOW).

Co-authored-by: Vel <Code4me2@users.noreply.github.com>
@Code4me2

Copy link
Copy Markdown
Member Author

Re-review: dest hardlink write-through only (e81c56d)

Previous review BLOCKed 8466b6f: write_import_copy opened the dest leaf with O_CREAT|O_TRUNC|O_NOFOLLOW. A planted dest hardlink is a regular file; O_TRUNC mutated the shared inode outside imports/.

This is not a merge. Scope is that BLOCK only.

Checklist

Check Result
Dest opened with O_TRUNC Gone. Dest is never opened for write. Leaf is lstat (follow_symlinks=False) then os.replace.
Payload path Exclusive temp under dest parent: O_WRONLY|O_CREAT|O_EXCL|O_NOFOLLOW|O_CLOEXEC on .heph-import-{uuid}.tmp, write, then os.replace(tmp, leaf, src_dir_fd=fd, dst_dir_fd=fd). Same-dir renameat replaces the directory entry; the outside nlink keeps its bytes.
Planted dest hardlink test test_add_does_not_write_through_a_planted_hardlink: imports/planted.step hardlinked to outside/secret.step, then heph import add … --name planted.step. Asserts outside bytes stay OUTSIDE-ORIGINAL-BYTES. On rc 0, dest has the payload and a different inode.
Symlink dest refused test_add_refuses_traversal_and_symlink_escape still expects rc 1, dest remains a symlink, outside target not created. Walk stays O_NOFOLLOW; dest S_ISLNK is a named path_confinement refuse before replace.
web/ / INTERFACE.md Unchanged. e81c56d touches only cli_import.py + test_cli_import.py.

os.replace on a dest hardlink unlinks that name from the shared inode. It does not truncate it. A dest symlink is refused, not followed; even a TOCTOU swap-to-symlink would replace the link itself on Linux, not write through it.

The earlier BLOCK is closed.

MERGE — dest hardlink write-through is gone: exclusive temp under imports/ then rename, outside bytes unchanged, symlink dest still refused.

@Code4me2
Code4me2 merged commit 03da17a into main Aug 31, 2026
18 checks passed
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.

engine: heph import add — admit STEP/mesh into imports/ and optionally seed a part

2 participants