Skip to content

Release 1.12

Choose a tag to compare

@igorpocta igorpocta released this 27 Oct 21:36
· 56 commits to main since this release
b489242

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() and merge() 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 (default false) — map only public properties. Prevents leaking
    private / protected data (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.
  • #[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 (default false) — properties with #[MapProperty(path: 'address.city')]
    (or MapDateTimeProperty) are normalized back into the same nested structure ({"address": {"city": "..."}})
    instead of a flat key ({"city": "..."}). Output can be fed back to fromArray() and request/response share
    one schema.

  • MapperOptions::$jsonDepth (default 512) — maximum nesting depth for fromJson() and
    fromJsonCollection(). Previously hard-coded to 512. Deeper input throws JsonException; values < 1 throw
    InvalidArgumentException.

  • InvalidInputException (extends InvalidArgumentException) — thrown for input of the wrong shape, so it can
    be told apart from programming errors and mapped to HTTP 400.

  • PropertyPathResolver::set() and PropertyPathResolver::getRootKey() helpers.

Bug Fixes

  • Strict mode rejected the root key of a property path — with #[MapProperty(path: 'address.city')], strict
    mode threw Unknown 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 throws InvalidInputException for
    lists, scalars and null (previously a generic InvalidArgumentException for scalars).
  • fromArray() accepted a list — a non-empty list is now rejected with InvalidInputException; an empty array
    is still accepted as an empty object.
  • fromJsonCollection() / fromArrayCollection() crashed with TypeError on a non-object element
    ([{"name": "a"}, 2], HTTP 500). Now throws InvalidInputException with 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: publicPropertiesOnly and
    symmetricPropertyPaths default to true.
  • Code catching InvalidArgumentException keeps working — InvalidInputException and ValidationException
    both extend it.
  • Behaviour changes affecting only invalid input: list input to fromJson() / fromArray() is rejected;
    fromArrayCollection() throws InvalidInputException instead of TypeError for non-array elements.
  • PHPDoc of fromArray() / fromArrayCollection() widened to array<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::testFromArrayCollectionThrowsOnInvalidItem now expects InvalidInputException.

Documentation

  • docs/configuration.md — new options, Property Visibility, Ignore/OutputOnly/InputOnly, recommended API config.
  • docs/advanced.md — path normalization 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