From def694dc66e0be3309f4191a9842ecdc51d1ab48 Mon Sep 17 00:00:00 2001 From: Titus Date: Sat, 28 Nov 2026 18:02:08 +0100 Subject: [PATCH 1/2] write_folder refuses two file names in one type folder that fold to one name When two element ids of one type differ only in case (Ab1 and ab1, unnamed or both named Bob), the collision rule gave them file names that also differ only in case, and a case-insensitive disk (NTFS, APFS) kept one file for both: the folder held one element where the world had two. An unnamed id bob--x beside an element x named Bob was written as bob--x.json twice. Once the collision rule has chosen every name in a type folder, two names equal under the folder format's name fold, NFC then simple case folding (_fold_name, the archive reader's), now raise ValueError before anything is written, naming the type, both ids in code point order and both file names. The import doors and export_world write through write_folder. No name the rule chose before changes. The cases are shared byte for byte with the npm SDK (tests/fixtures/case-collide.cases.json, its sha256 held in the test). Co-Authored-By: Claude Opus 5.5 --- .gitattributes | 2 + CHANGELOG.md | 2 + src/onlyworlds/folder.py | 31 ++- tests/fixtures/case-collide.cases.json | 344 +++++++++++++++++++++++++ tests/test_case_collide.py | 92 +++++++ 5 files changed, 469 insertions(+), 2 deletions(-) create mode 100644 tests/fixtures/case-collide.cases.json create mode 100644 tests/test_case_collide.py diff --git a/.gitattributes b/.gitattributes index 362ca03..cd5b07e 100644 --- a/.gitattributes +++ b/.gitattributes @@ -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 diff --git a/CHANGELOG.md b/CHANGELOG.md index c7e092a..b3e58b6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ The package version runs on its own semver; the schema it is generated from is ` ## Unreleased +- **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 `.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. diff --git a/src/onlyworlds/folder.py b/src/onlyworlds/folder.py index 44f1f83..1d1b4c3 100644 --- a/src/onlyworlds/folder.py +++ b/src/onlyworlds/folder.py @@ -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 # --------------------------------------------------------------------------- @@ -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 ``.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 @@ -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] = {} diff --git a/tests/fixtures/case-collide.cases.json b/tests/fixtures/case-collide.cases.json new file mode 100644 index 0000000..104edd7 --- /dev/null +++ b/tests/fixtures/case-collide.cases.json @@ -0,0 +1,344 @@ +{ + "about": "Pairs of elements a world folder writer refuses, and pairs it writes: the npm SDK's folder writer (folderFiles, so worldFolderFiles and convertKanka) and the Python package's write_folder. The same bytes in both repositories; both suites hold its sha256. After the collision rule (folder spec 3.2) has named every file in one type folder, two final file names that are one name under OW-18's fold (NFC, then simple case folding, both at Unicode 15.1: the SDK's foldName, the Python package's _fold_name) are refused, since a case-insensitive disk may keep one file for both: the refusal names the type, the two ids in code point order and their file names as JSON text, in the words given here (a TypeError in the SDK, a ValueError in Python), and no file is written. A written case gives every file's path but world.json, in code point order. Cases written by hand.", + "version": 1, + "refused": [ + { + "elements": [ + { + "type": "character", + "id": "ab1", + "name": "" + }, + { + "type": "character", + "id": "Ab1", + "name": "" + } + ], + "message": "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." + }, + { + "elements": [ + { + "type": "character", + "id": "ab1", + "name": "Bob" + }, + { + "type": "character", + "id": "Ab1", + "name": "Bob" + } + ], + "message": "character element ids \"Ab1\" and \"ab1\" give the file names \"bob--Ab1.json\" and \"bob--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." + }, + { + "elements": [ + { + "type": "character", + "id": "x", + "name": "Bob" + }, + { + "type": "character", + "id": "bob--x", + "name": "" + } + ], + "message": "character element ids \"bob--x\" and \"x\" give the file names \"bob--x.json\" and \"bob--x.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." + }, + { + "elements": [ + { + "type": "location", + "id": "xé", + "name": "" + }, + { + "type": "location", + "id": "XÉ", + "name": "" + } + ], + "message": "location element ids \"XÉ\" and \"xé\" give the file names \"XÉ.json\" and \"xé.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." + }, + { + "elements": [ + { + "type": "character", + "id": "ä1", + "name": "" + }, + { + "type": "character", + "id": "Ä1", + "name": "" + } + ], + "message": "character element ids \"Ä1\" and \"ä1\" give the file names \"Ä1.json\" and \"ä1.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." + }, + { + "elements": [ + { + "type": "character", + "id": "ä1", + "name": "Bob" + }, + { + "type": "character", + "id": "Ä1", + "name": "Bob" + } + ], + "message": "character element ids \"Ä1\" and \"ä1\" give the file names \"bob--Ä1.json\" and \"bob--ä1.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." + }, + { + "elements": [ + { + "type": "character", + "id": "σ1", + "name": "" + }, + { + "type": "character", + "id": "Σ1", + "name": "" + } + ], + "message": "character element ids \"Σ1\" and \"σ1\" give the file names \"Σ1.json\" and \"σ1.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." + }, + { + "elements": [ + { + "type": "character", + "id": "σ1", + "name": "Bob" + }, + { + "type": "character", + "id": "Σ1", + "name": "Bob" + } + ], + "message": "character element ids \"Σ1\" and \"σ1\" give the file names \"bob--Σ1.json\" and \"bob--σ1.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." + }, + { + "elements": [ + { + "type": "character", + "id": "dž1", + "name": "" + }, + { + "type": "character", + "id": "Dž1", + "name": "" + } + ], + "message": "character element ids \"Dž1\" and \"dž1\" give the file names \"Dž1.json\" and \"dž1.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." + }, + { + "elements": [ + { + "type": "character", + "id": "dž1", + "name": "Bob" + }, + { + "type": "character", + "id": "Dž1", + "name": "Bob" + } + ], + "message": "character element ids \"Dž1\" and \"dž1\" give the file names \"bob--Dž1.json\" and \"bob--dž1.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." + }, + { + "elements": [ + { + "type": "character", + "id": "K1", + "name": "" + }, + { + "type": "character", + "id": "k1", + "name": "" + } + ], + "message": "character element ids \"k1\" and \"K1\" give the file names \"k1.json\" and \"K1.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." + }, + { + "elements": [ + { + "type": "character", + "id": "K1", + "name": "Bob" + }, + { + "type": "character", + "id": "k1", + "name": "Bob" + } + ], + "message": "character element ids \"k1\" and \"K1\" give the file names \"bob--k1.json\" and \"bob--K1.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." + }, + { + "elements": [ + { + "type": "character", + "id": "é1", + "name": "" + }, + { + "type": "character", + "id": "é1", + "name": "" + } + ], + "message": "character element ids \"é1\" and \"é1\" give the file names \"é1.json\" and \"é1.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." + }, + { + "elements": [ + { + "type": "character", + "id": "é1", + "name": "Bob" + }, + { + "type": "character", + "id": "é1", + "name": "Bob" + } + ], + "message": "character element ids \"é1\" and \"é1\" give the file names \"bob--é1.json\" and \"bob--é1.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." + } + ], + "written": [ + { + "elements": [ + { + "type": "character", + "id": "X0000000-1111", + "name": "Bob" + }, + { + "type": "character", + "id": "x0000000-1111", + "name": "Bob" + } + ], + "paths": [ + "elements/character/bob--000-1111.json", + "elements/character/bob--x0000000-1111.json" + ] + }, + { + "elements": [ + { + "type": "character", + "id": "Ab1", + "name": "" + }, + { + "type": "location", + "id": "ab1", + "name": "" + } + ], + "paths": [ + "elements/character/Ab1.json", + "elements/location/ab1.json" + ] + }, + { + "elements": [ + { + "type": "character", + "id": "Ab1", + "name": "Bob" + }, + { + "type": "character", + "id": "ab1", + "name": "Alice" + } + ], + "paths": [ + "elements/character/alice--ab1.json", + "elements/character/bob--Ab1.json" + ] + }, + { + "elements": [ + { + "type": "character", + "id": "İ1", + "name": "" + }, + { + "type": "character", + "id": "i1", + "name": "" + } + ], + "paths": [ + "elements/character/i1.json", + "elements/character/İ1.json" + ] + }, + { + "elements": [ + { + "type": "character", + "id": "İ1", + "name": "Bob" + }, + { + "type": "character", + "id": "i1", + "name": "Bob" + } + ], + "paths": [ + "elements/character/bob--i1.json", + "elements/character/bob--İ1.json" + ] + }, + { + "elements": [ + { + "type": "character", + "id": "ß1", + "name": "" + }, + { + "type": "character", + "id": "ss1", + "name": "" + } + ], + "paths": [ + "elements/character/ss1.json", + "elements/character/ß1.json" + ] + }, + { + "elements": [ + { + "type": "character", + "id": "ß1", + "name": "Bob" + }, + { + "type": "character", + "id": "ss1", + "name": "Bob" + } + ], + "paths": [ + "elements/character/bob--ss1.json", + "elements/character/bob--ß1.json" + ] + } + ] +} diff --git a/tests/test_case_collide.py b/tests/test_case_collide.py new file mode 100644 index 0000000..c9225f9 --- /dev/null +++ b/tests/test_case_collide.py @@ -0,0 +1,92 @@ +"""``write_folder`` refuses two elements of one type whose file names, once the collision rule (folder spec 3.2) has +chosen them, are one name under OW-18's fold (NFC, then simple case folding): a case-insensitive disk may keep one +file for both. The shared +cases (``tests/fixtures/case-collide.cases.json``, the same bytes as the npm SDK's +``test/fixtures/case-collide.cases.json``; both suites hold its sha256): each refused case is a ValueError in the +case's words and nothing is written, in either input order; each written case gives its paths.""" + +from __future__ import annotations + +import hashlib +import json +import re +from collections.abc import Iterable, Mapping +from pathlib import Path +from typing import Any + +import pytest + +from onlyworlds import write_folder + +FIXTURE = Path(__file__).resolve().parent / "fixtures" / "case-collide.cases.json" +FIXTURE_SHA256 = "e3992a35b1389188edd6c95aec24a727920644d01fff802eb243c01f0ff9f427" +W = {"id": "w-1", "name": "W"} + + +def _data() -> dict[str, Any]: + data: dict[str, Any] = json.loads(FIXTURE.read_bytes()) + return data + + +def _by_type(elements: Iterable[Mapping[str, str]]) -> dict[str, list[dict[str, str]]]: + out: dict[str, list[dict[str, str]]] = {} + for e in elements: + out.setdefault(e["type"], []).append({"id": e["id"], "name": e["name"]}) + return out + + +def _files(root: Path) -> list[str]: + return sorted(p.relative_to(root).as_posix() for p in root.rglob("*") if p.is_file()) + + +def _label(case: Mapping[str, Any]) -> str: + return ",".join(json.dumps(e["id"]) for e in case["elements"]) + + +def test_the_cases_file_is_the_pinned_bytes() -> None: + assert hashlib.sha256(FIXTURE.read_bytes()).hexdigest() == FIXTURE_SHA256 + data = _data() + assert data["version"] == 1 + assert len(data["refused"]) == 14 + assert len(data["written"]) == 7 + + +@pytest.mark.parametrize("reverse", [False, True], ids=["given", "reversed"]) +@pytest.mark.parametrize("case", _data()["refused"], ids=_label) +def test_a_refused_pair_writes_nothing(tmp_path: Path, case: dict[str, Any], reverse: bool) -> None: + out = tmp_path / "out" + elements = case["elements"][::-1] if reverse else case["elements"] + with pytest.raises(ValueError, match=f"^{re.escape(case['message'])}$"): + write_folder(out, W, _by_type(elements)) + assert list(tmp_path.iterdir()) == [], "the target is not made, and no temp folder is left" + + +@pytest.mark.parametrize("reverse", [False, True], ids=["given", "reversed"]) +@pytest.mark.parametrize("case", _data()["written"], ids=_label) +def test_a_written_pair_lands_at_its_paths(tmp_path: Path, case: dict[str, Any], reverse: bool) -> None: + out = tmp_path / "out" + elements = case["elements"][::-1] if reverse else case["elements"] + report = write_folder(out, W, _by_type(elements)) + assert _files(out) == sorted([*case["paths"], "world.json"]) + assert report.total == len(case["elements"]), "one file per element" + for e in case["elements"]: + hits = [p for p in case["paths"] if p.startswith(f"elements/{e['type']}/")] + assert e["id"] in {json.loads((out / p).read_bytes())["id"] for p in hits} + + +def test_a_pair_among_other_elements_leaves_an_empty_target_as_it_was(tmp_path: Path) -> None: + out = tmp_path / "out" + out.mkdir() + elements = { + "ability": [{"id": "a-1", "name": "Fly"}], + "character": [ + {"id": "c-1", "name": "Ann"}, + {"id": "Ab1", "name": ""}, + {"id": "c-2", "name": "Cy"}, + {"id": "ab1", "name": ""}, + ], + } + with pytest.raises(ValueError, match=f"^{re.escape(_data()['refused'][0]['message'])}$"): + write_folder(out, W, elements) + assert out.is_dir() and list(out.iterdir()) == [] + assert list(tmp_path.iterdir()) == [out] From f963687dd7f60b4a156e26c03eefe572de89fa1f Mon Sep 17 00:00:00 2001 From: Titus Date: Sat, 28 Nov 2026 18:14:52 +0100 Subject: [PATCH 2/2] 0.158.6: two file names that fold to one are refused Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 2 ++ src/onlyworlds/_version.py | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b3e58b6..06adc66 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ 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) diff --git a/src/onlyworlds/_version.py b/src/onlyworlds/_version.py index 229da94..2c37e0f 100644 --- a/src/onlyworlds/_version.py +++ b/src/onlyworlds/_version.py @@ -1 +1 @@ -__version__ = "0.158.5" +__version__ = "0.158.6"