Skip to content

Fail loudly when attribute data cannot be encoded - #2686

Open
kha333n wants to merge 1 commit into
lunarphp:1.xfrom
kha333n:fix/attribute-data-encode-failure
Open

Fail loudly when attribute data cannot be encoded#2686
kha333n wants to merge 1 commit into
lunarphp:1.xfrom
kha333n:fix/attribute-data-encode-failure

Conversation

@kha333n

@kha333n kha333n commented Aug 28, 2026

Copy link
Copy Markdown
Contributor

Fixes #2685.

The same code is on 2.x at Casts/AsAttributeData.php, so this wants a forward
port.

One byte from a Latin-1 product feed replaces the whole of attribute_data with
0, taking every attribute on the record with it.

The fix

json_encode() reports failure by returning false, and that false was bound
straight into the update. Encoding now uses JSON_THROW_ON_ERROR, so a value
that cannot be represented fails the write instead of overwriting what was
already there.

The cast runs on assignment, so the bad value never reaches the database — which
also means this is not a new failure mode for callers: on 1.x the save already
threw, just after writing.

Nothing else changes, and get() is untouched.

Tests

tests/core/Unit/Base/Casts/AsAttributeDataTest.php:

  • can not overwrite attribute data with an unencodable value — a product with
    a name, then a write carrying a single 0xE4 byte. Asserts the write throws
    and that the stored column and the readable name are exactly what they were.
    On 1.x the column is 0 afterwards and the product can no longer be loaded.
  • can store attribute data containing multibyte characters — accented text,
    Japanese and an emoji all still round-trip. Passes before and after; it is the
    guard that the fix rejects unencodable bytes rather than non-ASCII ones.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

A non-UTF-8 attribute value silently replaces the whole of attribute_data with 0

1 participant