From 93350e11afaf97004a0cc743b257d89a2330f9e6 Mon Sep 17 00:00:00 2001 From: Titus Date: Mon, 30 Nov 2026 19:56:14 +0100 Subject: [PATCH] 0.160.1: a name that holds half of a surrogate pair is refused The world file's name check refuses a segment that is not well-formed Unicode, and write_folder's id check, world_file_bytes and write_world_file follow it, as the npm SDK does from 4.227.1. No archive name can carry such a name. Before, write_folder wrote it to an NTFS disk and could not encode it on a disk whose names are UTF-8. The new test fails 6 of its 7 before the change; the packer's case runs on Windows only. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 4 +++ src/onlyworlds/_version.py | 2 +- src/onlyworlds/worldfile.py | 5 ++++ tests/test_lone_surrogate.py | 57 ++++++++++++++++++++++++++++++++++++ 4 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 tests/test_lone_surrogate.py diff --git a/CHANGELOG.md b/CHANGELOG.md index f7e13c3..e8b98a4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ The package version runs on its own semver; the schema it is generated from is ` ## Unreleased +## 0.160.1 (2026-11-30) + +- **Fixed: a name that holds half of a surrogate pair is refused.** The world file's name check, and so `write_folder`, `export_world` and the import doors through its id check, and `world_file_bytes` and `write_world_file`, now refuse a name or an id that is not well-formed Unicode, in the words "is not well-formed Unicode: it holds half of a surrogate pair" (folder format, ruled after serial 150), as the npm SDK does from 4.227.1. No archive name can carry such a name. Before, `write_folder` wrote it to an NTFS disk, and on a disk whose names are UTF-8 it could not encode the name at all. + ## 0.160.0 (2026-11-30) - **The schema pin is v0.34.1-dist.150** (canonical 00.34.01 unchanged). The folder format is 0.3.26, so `FORMAT_VERSION` and a new folder's `format_version` say `0.3.26`; a folder stamped 0.3.25 is read as before. 0.3.26 writes down how a writer names and refuses element files, rules `write_folder` already keeps: the id tail, an id whose `.json` is not one file name, and two file names that are one once folded. The schema distribution now carries their cases in `fixtures/folder-writer/` (9 id tails; 24 ids refused and 5 written; 16 sets of elements refused and 10 written), and the tests run those files, which the npm SDK's tests read too, in place of their own copies. diff --git a/src/onlyworlds/_version.py b/src/onlyworlds/_version.py index 36947cd..dc85b91 100644 --- a/src/onlyworlds/_version.py +++ b/src/onlyworlds/_version.py @@ -1 +1 @@ -__version__ = "0.160.0" +__version__ = "0.160.1" diff --git a/src/onlyworlds/worldfile.py b/src/onlyworlds/worldfile.py index 4ea3e31..675c353 100644 --- a/src/onlyworlds/worldfile.py +++ b/src/onlyworlds/worldfile.py @@ -205,11 +205,16 @@ def _path_fault(name: str) -> str | None: ) #: ``<>:"|?*`` and the control characters (Unicode Cc: U+0000 to U+001F, U+007F to U+009F). _BAD_CHAR = re.compile('[<>:"|?*\x00-\x1f\x7f-\x9f]') +#: Half of a surrogate pair: a name that is not well-formed Unicode, which no archive name (UTF-8) can carry; written +#: out it would become another name (U+FFFD). A Python str never holds a whole pair as two halves. +_LONE_SURROGATE = re.compile("[\ud800-\udfff]") def _segment_fault(seg: str) -> str | None: """OW-22 for one segment: why it would not land on every disk, or None. The words name no segment, so the folder writer's id check (``_unsafe_id_why``, on ``.json``) says the same reason as an archive's.""" + if _LONE_SURROGATE.search(seg): + return "is not well-formed Unicode: it holds half of a surrogate pair" if _BAD_CHAR.search(seg): return "holds a reserved or control character" if seg.endswith((".", " ")): diff --git a/tests/test_lone_surrogate.py b/tests/test_lone_surrogate.py new file mode 100644 index 0000000..b246471 --- /dev/null +++ b/tests/test_lone_surrogate.py @@ -0,0 +1,57 @@ +"""A name that is not well-formed Unicode, one that holds half of a surrogate pair, is refused by the archive's name +check (folder spec 4.7, ruled after serial 150): no archive can carry it, and written out as UTF-8 it becomes another +name. ``write_folder``'s id check follows with no word of its own, since it asks the name check of ``.json``. The +same words as the npm SDK's ``test/lone-surrogate.test.mjs``.""" + +from __future__ import annotations + +import json +import sys +from pathlib import Path + +import pytest + +from onlyworlds import write_folder +from onlyworlds.folder import _unsafe_id_why +from onlyworlds.worldfile import WorldFileError, _segment_fault, world_file_bytes + +HIGH = chr(0xD800) +LOW = chr(0xDC00) +FFFD = chr(0xFFFD) +WHY = "is not well-formed Unicode: it holds half of a surrogate pair" +W = {"id": "w-1", "name": "W"} + + +@pytest.mark.parametrize("name", [f"abc{HIGH}.json", f"abc{LOW}.json", f"{LOW}{HIGH}.json", f"a{HIGH}b.json"]) +def test_the_name_check_refuses_half_of_a_surrogate_pair(name: str) -> None: + assert _segment_fault(name) == WHY + + +def test_a_whole_astral_character_passes(tmp_path: Path) -> None: + face = chr(0x1F600) + assert _segment_fault(f"abc{face}.json") is None + write_folder(tmp_path / "w", W, {"character": [{"id": f"abc{face}", "name": ""}]}) + assert [p.name for p in (tmp_path / "w" / "elements" / "character").iterdir()] == [f"abc{face}.json"] + + +def test_the_id_check_follows_the_name_check(tmp_path: Path) -> None: + lone = f"abc{HIGH}" + assert _unsafe_id_why(lone) == f"as {json.dumps(lone + '.json', ensure_ascii=False)} it {WHY}" + with pytest.raises(ValueError, match=WHY): + write_folder(tmp_path / "a", W, {"character": [{"id": lone, "name": ""}]}) + assert not (tmp_path / "a").exists() + write_folder(tmp_path / "b", W, {"character": [{"id": f"abc{FFFD}", "name": ""}]}) + assert [p.name for p in (tmp_path / "b" / "elements" / "character").iterdir()] == [f"abc{FFFD}.json"] + + +@pytest.mark.skipif(sys.platform != "win32", reason="only a disk with UTF-16 names (NTFS) can hold such a file name") +def test_the_packer_refuses_a_folder_holding_such_a_file_name(tmp_path: Path) -> None: + folder = tmp_path / "w" + (folder / "elements" / "character").mkdir(parents=True) + (folder / "world.json").write_text('{"id": "w-1", "name": "W"}\n', encoding="utf-8") + (folder / "elements" / "character" / f"abc{HIGH}.json").write_bytes(b'{"id": "abc\\ud800"}\n') + (folder / "elements" / "character" / f"abc{FFFD}.json").write_text( + json.dumps({"id": f"abc{FFFD}"}, ensure_ascii=False) + "\n", encoding="utf-8" + ) + with pytest.raises(WorldFileError, match=WHY): + world_file_bytes(folder)