Skip to content

Check the weight index of dot-named Sentence Transformers module folders - #14952

Merged
danielhanchen merged 1 commit into
mainfrom
dh/s3-weight-index-dots-folder
Oct 23, 2026
Merged

danielhanchen merged 1 commit into
mainfrom
dh/s3-weight-index-dots-folder

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Follows up #14527.

What was wrong

escaping_weight_references skipped every modules.json module path made only of dots and slashes (if not sub.strip("./\\"): continue). #14527 moved the escape check ahead of that skip, which catches .., ../.., / and \, but ..., ./... and ..../ are ordinary folder names: they do not normalize to .., so they passed the escape check and were then skipped, and the index inside them was never read. Sentence Transformers accepts such a path (_validate_relative_path only rejects rooted paths and .. components) and opens Path(model_dir, sub); transformers then joins each shard name onto that folder (os.path.join(path, subfolder, f) in get_checkpoint_shard_files). So an index in .../ naming ../../outside.safetensors loads a file from outside the model folder without being flagged.

Fix

  • Only a module path that normalizes to . ("", ., ./) is treated as the model root and skipped.
  • The path is judged both with backslash as a separator (Windows) and as a literal name character (POSIX, where Sentence Transformers opens it as written); if either reading escapes, the module path is flagged.
  • Otherwise the index is read from the folder the loader actually opens, and its entries are checked against each reading.
  • The earlier escape check is unchanged.

Tests

  • New cases in test_account_path_confinement.py: ..., ./..., ..../, ...., 0_Transformer/... and .\... (POSIX only) are flagged when their index escapes and pass when it does not; 0_Transformer, . and ./ keep their verdict.
  • The new dot-folder cases fail on main (5 failed) and pass here; the file gives 47 passed.
  • 9 other test files that use file_security: 716 passed; 2 failures there also fail on main (a module missing from the test environment).

Not run: Windows (backslash cases checked on Linux only), a real Sentence Transformers load of a ... folder (loader behaviour read from the 6.2.0 source and the transformers shard resolver), a live Studio server.

A modules.json path such as '...', './...' or '..../' is an ordinary
folder, not the model root, but escaping_weight_references skipped any
path made of only dots and slashes, so its index was never read. Skip
only paths that normalize to '.', and judge the index against both the
Windows and POSIX spelling of the folder.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 23, 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-23T08:22:51.824602Z 1e2db96 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

Copy link
Copy Markdown

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

Reviewed commit: 1e2db96dba

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

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

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen merged commit f172aec into main Oct 23, 2026
2 of 37 checks passed
@danielhanchen
danielhanchen deleted the dh/s3-weight-index-dots-folder branch October 23, 2026 09:05
shimmyshimmer pushed a commit to shimmyshimmer/unsloth-staging-4 that referenced this pull request Nov 4, 2026
…e root (unslothai#16212)

## Problem

`escaping_weight_references` reads each Sentence Transformers module
folder named in `modules.json` and flags weight-index entries that point
outside the model folder. A module path that normalizes to `.` as text
is treated as the model root, whose index was already read, so it is
skipped.

On POSIX the kernel follows a symlink before it applies `..`, so
`link/..` opens the folder beside the link's target, not the model root.
A local model folder laid out like this passes the gate:

```text
model/
  modules.json                     path: "link/.."
  link -> model/deep/child
  deep/model.safetensors.index.json   weight_map: {"w": "../../outside.safetensors"}
```

Sentence Transformers passes `link/..` as the subfolder, and
transformers' `get_checkpoint_shard_files` returns
`model/link/../../../outside.safetensors`, which opens a file next to
the model folder. On current main the gate returns `[]` for this folder.

## Change

When a module path's `..` climbs through a link, so its real path
differs from its spelled path (`link/..`, `link/../x`), the module's
index is read and each name is judged from where the module really
lands: rooted / drive-qualified names, a `..` that leaves the folder, or
a shard link that resolves outside are flagged. Module paths without
such a `..` (every real Sentence Transformers layout: `""`,
`0_Transformer`, `1_Pooling`) keep the existing check unchanged.

## Tests

- New
`test_account_path_confinement.py::test_a_module_path_that_reads_as_the_root_but_lands_elsewhere_has_its_index_read`
builds the layout above. Main: 1 failed (the gate returns `[]`). This
branch: 1 passed, and the same layout with a shard that stays in `deep/`
still passes. Each arm imported its own `file_security.py`.
- New
`test_a_module_path_climbing_through_a_link_into_a_subfolder_is_judged_where_it_lands`
covers `link/../x` with a shard `out/../s.safetensors` through a link:
fails on the earlier head of this branch, passes now. transformers
4.57.6 `get_checkpoint_shard_files(subfolder="link/../x")` opens the
outside file for that layout.
- A standalone check through the real transformers
`get_checkpoint_shard_files` (opens the outside file, then asks the
gate) passes on this branch.
- `test_account_path_confinement.py` + `test_file_security.py`: 109
passed.
- `test_consent_gate.py`, `test_embedding_model_security_gate.py`,
`test_security_gate_consistency.py`, `test_load_downloads.py`,
`test_training_cached_start.py`, `test_offline_embedding_minimal.py`,
`test_export_absolute_paths.py`: the same results on main and this
branch. The one failure on both is
`test_registered_absolute_export_folder_is_discoverable`, from a module
missing in this environment.

- `test_a_dot_named_module_folder_index_outside_the_model_is_flagged`
(from unslothai#14952) now skips on Windows: Win32 strips trailing dots, so its
`...` folders cannot be created there (it failed 5 cases on
windows-latest before this PR touched it). Cross-platform staging:
ubuntu 60 passed, macOS 59 passed / 1 skipped, Windows 37 passed / 22
skipped, 0 failed.

## Not covered

- Windows was not run. There `ntpath` collapses `..` before it follows
links, so `link/..` is the model root and the new branch does not run.
- No GPU needed. The change only reads small JSON index files.
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