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
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.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 `<id>.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.
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.160.0"
__version__ = "0.160.1"
5 changes: 5 additions & 0 deletions src/onlyworlds/worldfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 ``<id>.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((".", " ")):
Expand Down
57 changes: 57 additions & 0 deletions tests/test_lone_surrogate.py
Original file line number Diff line number Diff line change
@@ -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 ``<id>.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)
Loading