Repository navigation
Description
Note
This issue was authored by Claude (Fable 5.2) and has not yet been closely reviewed by @kdmccormick.
Found while reviewing #904 (the new Asset.path field), but it is not specific to that PR: nothing in openedx_content normalizes Unicode, so identifiers that look identical can be stored as different strings.
Background
Unicode can encode the same visible text in more than one way. For example, é is one code point in NFC (U+00E9) and two in NFD (e + U+0301, a combining accent). They render identically but are different strings.
Our case-sensitive identifier fields use the utf8mb4_bin collation on MySQL, which compares bytes. So café.png in NFC and café.png in NFD are two distinct values. Uploads from macOS, which has historically decomposed filenames, and copy-paste between tools make this a realistic way to end up with two indistinguishable names, or with a reference that does not resolve to the thing it visibly names.
There is no call to unicodedata.normalize anywhere in src/.
Affected fields
| Field | Validation | Exposure |
|---|---|---|
ComponentVersionMedia.path (models.py:301) |
ref_field() with no field-level validation; the API applies a light check (api.py:336-338: no leading /, no leading or trailing whitespace, no backslash) |
File path attached to a component version, looked up by exact string. NFC and NFD spellings are different files. |
Asset.path (added in #904) |
Path regex, no normalization | Same as above, but the path is also the Asset's permanent identity and cannot be renamed. |
PublishableEntity.entity_ref (publishable_entity.py:139) |
ref_field(), none |
Derived from the codes and paths, so it inherits whatever they allow. |
Component.component_code (models.py:201) and Container.container_code (models.py:211) |
code_field(unicode=True) |
See below. |
Collection.collection_code is ASCII-only (models.py:118) and is not affected. Titles are not identifiers.
code_field(unicode=True) handles this by accident, and incompletely
CODE_REGEX_UNICODE is ^[\w.-]+\Z (fields.py:125). Python's \w does not match combining marks, which has two effects:
- NFD spellings of accented Latin letters are rejected (
cafépasses in NFC, fails in NFD). That avoids the duplicate problem for those scripts, but not by design. - Scripts that need combining marks even in NFC are rejected outright. For example,
हिन्दीfails the regex in both forms.
The same pattern is also enforced in the database by code_field_check, where \w is interpreted by the database's regex engine rather than Python's. I believe MySQL's engine treats marks as word characters, in which case the Python validator and the DB constraint disagree; this needs checking.
One place normalizes implicitly
The backup zipper names entity files with Django's slugify(entity_ref, allow_unicode=True) (zipper.py:256), which applies NFKC. Two refs that differ only by normalization form produce the same slug. The existing collision check falls back to a hashed filename, so this works, but only as a side effect.
Minimal reproduction
Save as tests/openedx_content/test_unicode_normalization_repro.py and run pytest tests/openedx_content/test_unicode_normalization_repro.py --no-cov. It passes, which is the problem: one component version ends up with two files whose paths look the same.
"""Minimal reproduction: NFC and NFD spellings of one name are stored as two different identifiers."""
import unicodedata
from datetime import datetime, timezone
import pytest
from openedx_content import api
NFC = unicodedata.normalize("NFC", "static/café.png") # "é" is U+00E9
NFD = unicodedata.normalize("NFD", "static/café.png") # "é" is "e" + U+0301
@pytest.mark.django_db
def test_nfc_and_nfd_media_paths_are_distinct_files():
assert NFC != NFD # ...but they render identically
learning_package = api.create_learning_package(package_ref="repro", title="Repro")
html_type = api.get_or_create_component_type("xblock.v1", "html")
_component, version = api.create_component_and_version(
learning_package.id,
html_type,
"intro",
"Intro",
datetime.now(tz=timezone.utc),
media={NFC: b"first file", NFD: b"second file"},
)
# One component version now has two files whose paths look the same.
media = {cvm.path: cvm.media.read_file().read() for cvm in version.componentversionmedia_set.all()}
assert media == {NFC: b"first file", NFD: b"second file"}This was run on SQLite against the #904 branch (which contains main at 62e3ff2) and uses only APIs that exist on main. It has not been run on MySQL.
Suggested direction
- Pick one form (NFC is the web convention) and require it at the API boundary for identifier-like fields: reject values where
unicodedata.normalize("NFC", value) != value, and let callers such as upload views normalize before calling in. - Decide what
code_field(unicode=True)is meant to accept, and make the Python and database checks agree. - Check whether existing data contains non-NFC values in these fields before enforcing anything.
Asset.path is the most time-sensitive of these, since it is new and immutable; that part is being discussed on #904.
Activity