Guard against a null asset in `MuxMirrorFieldtype::preload() - #99
Merged
Merged
Conversation
Owner
|
Nice, thanks! I don't think I'll be reading all that :) The fix looks plausible enough. I'd like to add a regression test for this, but let's get this fix out and I'll see about the test later. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #99 +/- ##
=========================================
Coverage 62.40% 62.40%
Complexity 829 829
=========================================
Files 68 68
Lines 2825 2825
=========================================
Hits 1763 1763
Misses 1062 1062 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Owner
|
Should be published as 3.4.1. Thanks again! |
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.
TLDR
This PR adds a null check in
MuxMirrorFieldtype::preload()because depending on your data Statamic's$this->asset()can returnnullin certain circumstances, resulting in aCall to a member function isVideo() on nullerror. The fix is tiny and perfectly safe as far as I can tell.Below is Claude's full explanation of the issue and how to reproduce it, if you really wanted to dig into it 🙂 .
Summary
MuxMirrorFieldtype::preload()calls$asset->isVideo()on a value that can benull, which throws a fatal error and returns a 500 for any control panel screen that builds the meta for an assets field whose container blueprint contains amux_mirrorfield:In practice this takes down entry edit screens (and anywhere else an assets field is preloaded), not just the asset editor.
The fix
preload()already treats$assetas nullable on the line directly above ('is_asset' => (bool) $asset), but the next line dereferences it unconditionally:The one-line change mirrors the null handling already used for
is_proxy:Root cause
MuxMirrorFieldtype::asset()returnsnullwhenever the field's parent is not anAssetinstance:That happens during normal control panel usage because of how Statamic builds an asset's blueprint meta:
preload()(Statamic\Fieldtypes\Assets\Assets) renders an inline preview of each selected asset viaStatamic\Http\Resources\CP\Assets\AssetsFieldtypeAsset::publishFormData(), which builds the asset blueprint's field meta — including themux_mirrorfield.AssetContainer::blueprint($asset = null)returns a single, Blink-cached blueprint object and mutates its parent in place:setParent($asset ?? $this).Assets::preload()also callsgetColumns(), which calls$this->container()->blueprint()with no asset, setting that shared blueprint's parent to the container.fields()) are cached and shared, a later asset preview ends up meta-ing themux_mirrorfield with theAssetContaineras its parent instead of anAsset.asset()returnsnull, and$asset->isVideo()throws.The underlying shared-mutable-blueprint behaviour is in Statamic core (present at least as far back as v5.74 / v6.0.0, so it is not a recent regression), but the addon is the thing that crashes on it. Guarding
preload()makes the fieldtype resilient to being meta'd with a non-asset parent, which is the contract the rest of the method already assumes.Reproduction
A minimal, isolated reproduction needs no Mux account, no API credentials and no mirrored video — just an image asset and the
mux_mirrorfield on the container blueprint.Fresh Statamic 6 site (reproduced on v6.23.0, PHP 8.5).
Add a
mux_mirrorfield to the asset container blueprint (resources/blueprints/assets/assets.yaml):Give a collection two assets fields pointing at that container (
image_one,image_two).Upload one image and select the same asset in both fields on one entry.
Open that entry in the control panel → 500.
Why two fields / the same asset
Within
Assets::preload(),getItemData()(the asset preview) runs beforegetColumns()(the call that poisons the shared blueprint), so a single field cannot crash its own preview. With two fields referencing the same asset:image_onepreviews the asset (caches its blueprint, parent = asset, fine), then itsgetColumns()poisons that cached blueprint (parent → container).image_twore-previews the same asset → cache hit on the now-poisoned blueprint → themux_mirrorfield is meta'd with the container as parent →isVideo()onnull→ 500.Impact
mux_mirrorfield can 500. It can present as "every entry is broken" once content references such assets.$assetis a real asset;is_videosimply becomesfalsewhen there is no asset, consistent withis_assetbeingfalsein the same payload.