Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@ tests/fixtures/pins-known-to.cases.json -text
tests/fixtures/most-linked.fixture.json -text
# The folder writers' refused and written ids, the same bytes as the npm SDK's copy (both tests hold its sha256).
tests/fixtures/unsafe-ids.cases.json -text
# The folder writers' refused and written pairs of case-only file names, the same bytes as the npm SDK's copy (both tests hold its sha256).
tests/fixtures/case-collide.cases.json -text
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ The package version runs on its own semver; the schema it is generated from is `

## Unreleased

## 0.158.6 (2026-11-28)

- **Fixed: two elements whose file names differ only in case are refused, where one of them was lost.** `write_folder` names a file from the element's id, and when two ids of one type differ only in case (`Ab1` and `ab1`, unnamed or both named `Bob`) it gave them file names that also differ only in case. A case-insensitive disk (Windows, macOS) keeps one file for both, so the folder held one element where the world had two. It now raises `ValueError` before anything is written, once the collision rule has chosen every name in a type folder, when two of them are one name under the folder format's name fold (NFC, then simple case folding, as the archive reader compares names): `character element ids "Ab1" and "ab1" give the file names "Ab1.json" and "ab1.json", which are one name once folded (NFC, then simple case folding), so a case-insensitive disk (NTFS, APFS) may keep one file for both.` The same refusal holds an unnamed id `bob--x` beside an element `x` named `Bob`, which were both written as `bob--x.json` on any disk. So are ids that differ as `Ä1` and `ä1`, `Σ1` and `σ1`, `Dž1` and `dž1`, the Kelvin sign `K1` and `k1`, or by normalization alone; `İ1` and `i1`, and `ß1` and `ss1`, stay two files. Ids in two types, and names whose slugs differ, are written as before, and every other folder keeps its bytes. The import doors and `export_world` write through `write_folder` and refuse alike. The npm SDK's folder writer refuses the same pairs in the same words (`tests/fixtures/case-collide.cases.json`, the same bytes in both).

## 0.158.5 (2026-11-28)

- **Fixed: a folder writer no longer writes outside its folder for an element id that holds a path; such an id is refused.** An element whose name gives no slug is written as `<id>.json`, so an id such as `../../x`, from an export or a server, wrote a file outside the folder it was asked to write. `write_folder` now refuses, whatever the name and before anything is written, an element id that is empty (it would be the hidden file `.json`), holds `/` or `\`, holds a colon (on Windows `D:x.json` is a path on drive D), holds a control character (U+0000 to U+001F), or is `.` or `..`: `ValueError: character element id "../../x" cannot be a file name: it holds a path separator, / or \.` Behind that rule, a second guard refuses any file whose path would not lie inside its folder: in `write_folder` (inside its type folder, both in the temp folder it writes and in the folder that temp folder becomes), in `write_bundle` (the OKF export, whose files are named from ids) and for the LegendKeeper import's board records. Every writer that names files by id goes through these: `write_folder`, `export_world`, `export_okf` and the Kanka, Campfire, LegendKeeper, Notion and World Anvil imports. The world's id names no path and is not checked. The cases are in `tests/fixtures/unsafe-ids.cases.json`, which the npm SDK's folder writer is held to as well.
Expand Down
2 changes: 1 addition & 1 deletion src/onlyworlds/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.158.5"
__version__ = "0.158.6"
31 changes: 29 additions & 2 deletions src/onlyworlds/folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,29 @@ def resolve_filenames(elements: Iterable[tuple[str, str | None]]) -> dict[str, s
return out


def _refuse_case_collisions(etype: str, names: Mapping[str, str]) -> None:
"""Raise ValueError when two of one type folder's final file names are one name under OW-18's fold
(``_fold_name``: NFC, then simple case folding; the npm SDK's ``foldName``): a case-insensitive disk may
keep one file for both. The collision rule's own buckets still lowercase.
The ids are walked in code point order and the first pair found is named, the lower id first, so both
packages name the same pair (tests/fixtures/case-collide.cases.json)."""
from .worldfile import _fold_name # worldfile imports this module

seen: dict[str, str] = {}
for element_id in sorted(names):
filename = names[element_id]
first = seen.setdefault(_fold_name(filename), element_id)
if first != element_id:
a, b, file_a, file_b = (
json.dumps(s, ensure_ascii=False) for s in (first, element_id, names[first], filename)
)
raise ValueError(
f"{etype} element ids {a} and {b} give the file names {file_a} and {file_b}, which are one name once "
"folded (NFC, then simple case folding), so a case-insensitive disk (NTFS, APFS) may keep one file for "
"both."
)


# ---------------------------------------------------------------------------
# Writer
# ---------------------------------------------------------------------------
Expand Down Expand Up @@ -337,8 +360,11 @@ def write_folder(
under two types is an error, and so is an id that cannot name a file (one that
is empty, holds ``/``, ``\\``, ``:`` or a control character, or is ``.`` or ``..``:
as ``<id>.json`` it would leave its type folder or name a hidden file), whatever
the name, and a file name whose path would not lie inside its type folder, all
before anything is written. The world's id names no path.
the name, a file name whose path would not lie inside its type folder, and two
elements of one type whose file names, once the collision rule has chosen them,
are one name folded, NFC then simple case folding (ids such as ``Ab1`` and ``ab1``:
a case-insensitive disk may keep one file for both), all before anything is written. The world's
id names no path.

``root`` must not exist, or be an empty directory: this writes a folder's
birth, never over another folder (stale files from renamed or deleted
Expand Down Expand Up @@ -383,6 +409,7 @@ def write_folder(
f"{etype} element id {json.dumps(eid, ensure_ascii=False)} gives the file name "
f"{json.dumps(filename, ensure_ascii=False)}, which does not lie inside its type folder."
)
_refuse_case_collisions(etype, names)
names_by_type[etype] = names

counts: dict[str, int] = {}
Expand Down
Loading
Loading