fix(assets): repair double-encoded UTF-8 in asset names - #2966
fix(assets): repair double-encoded UTF-8 in asset names#2966vpetersson wants to merge 3 commits into
Conversation
- add `repair_mojibake` helper that safely undoes the classic `UTF-8 bytes decoded as Latin-1` corruption (e.g. `Formulários` → `Formulários`); no-op on correctly-encoded text - apply it in `Asset.save` so every write path (web form, all API versions, legacy import) stores a clean name - one-time data migration repairs names stored before the guardrail Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
This PR adds a defensive repair for asset names that arrive as classic double-encoded UTF-8 mojibake (UTF-8 bytes decoded as Latin-1), ensuring newly saved assets store cleaned names and providing a one-time migration to fix previously stored corrupted rows.
Changes:
- Add
repair_mojibake()helper and apply it inAsset.save()as a write-side guardrail. - Add a data migration to repair existing
Asset.namevalues stored before the guardrail. - Add unit tests covering the helper, model save behavior, and the migration logic.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
tests/test_mojibake_repair.py |
Adds tests for mojibake repair helper, save-time guardrail, and migration repair pass. |
src/anthias_server/app/models.py |
Introduces repair_mojibake() and applies it in Asset.save() to clean names on write. |
src/anthias_server/app/migrations/0007_repair_mojibake_asset_names.py |
Adds a one-time data migration to repair pre-existing garbled asset names. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Soften the safety claim in repair_mojibake / migration docstrings: the
Latin-1 + valid-UTF-8 test is a strong heuristic, not a proof. A
genuinely Latin-1 name whose bytes are also valid UTF-8 (e.g. `©` →
`©`) is indistinguishable from mojibake and gets rewritten; document
the accepted trade-off instead of overclaiming "can't corrupt good
data".
- Return the original string when the repair produces no change, to
match the stated "only rewrites when different" contract.
- Stream the migration's rows with .only('asset_id', 'name').iterator()
instead of caching the whole assets table in memory.
- Add a regression test pinning the documented `©` false positive.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Asset.save() repaired self.name unconditionally, so a metadata-only or reachability save (update_fields excluding 'name', e.g. v2 views' save(update_fields=['metadata'])) would mutate the in-memory name without writing the column — diverging the instance from the stored row and leaving any DB-side garble unrepaired. Repair only when name is actually persisted (update_fields is None or includes 'name'), and add a regression test for the metadata-only path. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
|
Closing this one — the implementation is careful (conservative helper, idempotent, well tested), but the approach isn't worth carrying:
If garbled names ever show up for real, the safe version is repair-on-display — render the corrected name in the UI list and the viewer's |



Issues Fixed
Asset names that arrive double-encoded from an upstream uploader (the classic UTF-8 bytes decoded as Latin-1 mojibake — e.g.
Formuláriosstored asFormulários) were saved verbatim. The garbled name then shows up both in the web UI asset list and in the viewer'sShowing asset …log line.Description
Anthias never produces this corruption itself — Django's multipart parser and DRF both decode as UTF-8, and the viewer's
stderris UTF-8 — but it stored whatever a misbehaving client sent in the request body.repair_mojibakehelper. It only rewrites a string when every character is in the Latin-1 range and those bytes form a valid, different UTF-8 string — the unambiguous signature of double-encoded UTF-8. Correctly-stored names (Formulários,Café,日本語) raise on one of the two steps and pass through untouched, so good data can't be corrupted. Idempotent.Asset.save, so every create/update path (web form, all four API versions, legacy Screenly import) stores a clean name.Checklist