Make EntityMerger an allowlist; fix Ignore & falsy-merge bugs (#523)#553
Open
maltehuebner wants to merge 1 commit into
Open
Make EntityMerger an allowlist; fix Ignore & falsy-merge bugs (#523)#553maltehuebner wants to merge 1 commit into
maltehuebner wants to merge 1 commit into
Conversation
EntityMerger copied every property without a working guard, letting the update endpoints (StationApiController::postStationAction, CityApiController::postCityAction) overwrite identity/key fields via mass assignment. Three problems fixed: 1) Mass assignment (allowlist instead of denylist): merging is now opt-in via a dedicated #[App\Air\Util\EntityMerger\Attribute\Mergeable] attribute. Only annotated properties are merged. Identity keys (Station::$stationCode, Station::$provider, City::$slug) and system fields (id, createdAt, relations, derived coord) are excluded by default and can no longer be changed through the API. Annotated as mergeable: Station title/latitude/longitude/ubaStationId/fromDate/ untilDate/altitude and City name/description. 2) Wirkungsloser #[Ignore]-Check: the old isPropertyExposed() compared ReflectionAttribute instances with `instanceof Ignore`, which is always false. Replaced by the explicit Mergeable allowlist check. 3) Falsy values discarded: the old `if ($newValue)` guard dropped 0, 0.0, '' and false (e.g. altitude = 0 at sea level, coordinates at exactly 0). Changed to `null !== $newValue`; only null now means "not provided". Uninitialized typed properties on serializer-hydrated sources are skipped via ReflectionProperty::isInitialized() instead of catching an error. Removed the two now-obsolete phpstan-baseline entries (always-false instanceof, mismatched @var), added a unit test for EntityMerger, and updated the CLAUDE.md security note. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
EntityMerger(used byStationApiController::postStationActionandCityApiController::postCityAction) copied every reflected property with no effective guard, enabling mass assignment onto identity/key fields. Three defects fixed.Changes
1. Mass assignment -> explicit allowlist
Merging is now opt-in via a dedicated
#[App\Air\Util\EntityMerger\Attribute\Mergeable]attribute (separate from the serializer's#[Ignore], which is a serialization concern). Only annotated properties are merged.Stationtitle,latitude,longitude,ubaStationId,fromDate,untilDate,altitude;Cityname,description.Station::$stationCode,Station::$provider,City::$slug(identity keys), plusid,createdAt, relations and the derivedcoord. These can no longer be changed through the API.2. Wirkungsloser
#[Ignore]-Check (always-falseinstanceof)isPropertyExposed()did$reflectionAttribute instanceof Ignore, butgetAttributes()returnsReflectionAttributeobjects, neverIgnoreinstances — so it always returnedtrue. Replaced with the explicit#[Mergeable]allowlist check.3. Falsy values could not be set
if ($newValue)discarded0,0.0,'',false(e.g.altitude = 0at sea level, a coordinate at exactly0.0). Changed tonull !== $newValue; onlynullmeans "not provided". Uninitialized typed properties on serializer-hydrated sources are skipped viaReflectionProperty::isInitialized()(the oldcatch (\TypeError)didn't even catch theErrorthat uninitialized-property access throws).Housekeeping
phpstan-baseline.neonentries (always-false instanceof; mismatched@var).tests/Air/Util/EntityMerger/EntityMergerTest.php(6 cases: mergeable copied, identity protected, falsy0merged,nullskipped, uninitialized typed property skipped, returns destination).CLAUDE.mdEntityMerger security note.Verification
vendor/bin/phpstan analyse— No errors.vendor/bin/phpunit --testsuite="Project Test Suite"— 127 tests green (incl. the 6 new).bin/console lint:container --env=prod— OK.Note: end-to-end exercise of the two POST endpoints needs a populated PostGIS DB (not available here); the merge logic is covered by the unit test above.
Closes #523
🤖 Generated with Claude Code