Repository navigation
Release 1.12
What's Changed
Security and input-hardening release based on the sabservis/api security audit. All new behaviour that would
change existing output or input handling is opt-in via MapperOptions; defaults stay backward compatible
(except the bug fixes below).
Security Fixes
- Static properties are never mapped —
toArray()/toJson()serialized static properties, and
fromArray(),fromJson()andmerge()overwrote them from input data. A client could change class-level
state ({"counter": "HACKED"}→Dto::$counter === 'HACKED'), which persists across requests in long-running
workers (RoadRunner, Swoole). Static properties are now skipped in both directions and reported as unknown keys
in strict mode. Applies regardless of options.
New Features
-
MapperOptions::$publicPropertiesOnly(defaultfalse) — map only public properties. Prevents leaking
private/protecteddata (password hashes, tokens) in output and mass assignment of fields outside the public
contract (role,isAdmin,ownerId) from input.- Non-public properties can be opted in explicitly with
#[MapProperty]or#[MapDateTimeProperty]. - Non-promoted constructor parameters are always hydrated.
- A non-public promoted parameter without opt-in gets its default value (or
null); a required one throws
LogicException(configuration error, not invalid input). - Applies to
merge()and strict-mode unknown-key detection as well.
- Non-public properties can be opted in explicitly with
-
#[Ignore],#[OutputOnly],#[InputOnly]attributes — per-property control of mapping direction:Attribute Output Input #[Ignore]skipped skipped #[OutputOnly]written skipped (server-controlled fields: id,role)#[InputOnly]skipped hydrated (write-only fields: password)They always win over
#[MapProperty]opt-in. A key skipped on input is reported as unknown in strict mode. -
MapperOptions::$symmetricPropertyPaths(defaultfalse) — properties with#[MapProperty(path: 'address.city')]
(orMapDateTimeProperty) are normalized back into the same nested structure ({"address": {"city": "..."}})
instead of a flat key ({"city": "..."}). Output can be fed back tofromArray()and request/response share
one schema. -
MapperOptions::$jsonDepth(default512) — maximum nesting depth forfromJson()and
fromJsonCollection(). Previously hard-coded to 512. Deeper input throwsJsonException; values< 1throw
InvalidArgumentException. -
InvalidInputException(extendsInvalidArgumentException) — thrown for input of the wrong shape, so it can
be told apart from programming errors and mapped to HTTP 400. -
PropertyPathResolver::set()andPropertyPathResolver::getRootKey()helpers.
Bug Fixes
- Strict mode rejected the root key of a property path — with
#[MapProperty(path: 'address.city')], strict
mode threwUnknown key 'address'for valid input. The root key of each path is now accepted as known. fromJson()accepted non-object JSON — a JSON list ([1, 2],[]) was silently mapped to an object with
default values.fromJson()now requires a JSON object at the top level and throwsInvalidInputExceptionfor
lists, scalars andnull(previously a genericInvalidArgumentExceptionfor scalars).fromArray()accepted a list — a non-empty list is now rejected withInvalidInputException; an empty array
is still accepted as an empty object.fromJsonCollection()/fromArrayCollection()crashed withTypeErroron a non-object element
([{"name": "a"}, 2], HTTP 500). Now throwsInvalidInputExceptionwith the element index
(Collection element [1] ...).fromJsonCollection()also rejects a JSON object at the top level.
Backward Compatibility
- Defaults of all new options keep 1.11 behaviour. Planned for 2.0:
publicPropertiesOnlyand
symmetricPropertyPathsdefault totrue. - Code catching
InvalidArgumentExceptionkeeps working —InvalidInputExceptionandValidationException
both extend it. - Behaviour changes affecting only invalid input: list input to
fromJson()/fromArray()is rejected;
fromArrayCollection()throwsInvalidInputExceptioninstead ofTypeErrorfor non-array elements. - PHPDoc of
fromArray()/fromArrayCollection()widened toarray<mixed>(input may be untrusted).
Recommended Configuration for HTTP APIs
$mapper = new Mapper(new MapperOptions(
autoValidate: true,
strictMode: true,
throwOnMissingData: true,
publicPropertiesOnly: true,
symmetricPropertyPaths: true,
jsonDepth: 64,
));Do not disable throwOnMissingData for API input — a missing typed property without default would stay
uninitialized and fail on first access (HTTP 500 instead of 422).
Tests
StaticPropertyTest(6),PublicPropertiesOnlyTest(16),IgnoreAttributeTest(14),StrictModePathTest(4),
SymmetricPropertyPathTest(8),InvalidJsonInputTest(19 incl. data sets).BatchProcessingTest::testFromArrayCollectionThrowsOnInvalidItemnow expectsInvalidInputException.
Documentation
docs/configuration.md— new options, Property Visibility, Ignore/OutputOnly/InputOnly, recommended API config.docs/advanced.md—pathnormalization and strict-mode behaviour.docs/architecture.md— error handling: which exception means 400 vs. 422.
Affected files
- New:
src/PropertyVisibilityResolver.php,src/Attributes/Ignore.php,src/Attributes/OutputOnly.php,
src/Attributes/InputOnly.php,src/Exceptions/InvalidInputException.php - Changed:
src/Mapper.php,src/MapperOptions.php,src/Normalizer/Normalizer.php,
src/Denormalizer/Denormalizer.php,src/PropertyPathResolver.php