-
-
Notifications
You must be signed in to change notification settings - Fork 642
[6.x] Guard ImageGenerator against a null asset #15447
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 6.x
Are you sure you want to change the base?
Changes from all commits
c94f024
199dbca
7536259
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ | |
| namespace Statamic\Imaging; | ||
|
|
||
| use Facades\Statamic\Imaging\ImageValidator; | ||
| use Illuminate\Support\Facades\Log; | ||
| use Illuminate\Support\Facades\Storage; | ||
| use League\Flysystem\Filesystem; | ||
| use League\Flysystem\UnableToReadFile; | ||
|
|
@@ -150,11 +151,17 @@ public function generateVideoThumbnail($asset, array $params) | |
| /** | ||
| * Generate a manipulated image by an asset. | ||
| * | ||
| * @param \Statamic\Contracts\Assets\Asset $asset | ||
| * @param \Statamic\Contracts\Assets\Asset|null $asset | ||
| * @return mixed | ||
| */ | ||
| public function generateByAsset($asset, array $params) | ||
| { | ||
| if (! $asset) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The new
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fair — moved the guard to |
||
| Log::error('Cannot generate an image for a missing asset.'); | ||
|
|
||
| return ''; | ||
| } | ||
|
|
||
| if ($asset->isVideo() && ThumbnailExtractor::available()) { | ||
| return $this->generateVideoThumbnail($asset, $params); | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Confirmed the crash is real:
Glide::generateImage()(src/Tags/Glide.php:180) does callgenerateByAsset(Asset::find($item), $params)unguarded, andAsset::find()can return null (e.g. when an item resolves as an asset ID/instance rather than a raw path, so it skips theStr::isUrl()branch and falls through to this line). So a null asset really can reachisVideo()here pre-fix — good catch, and the regression test faithfully reproduces the exactErrorfrom the issue (fails pre-fix, passes post-fix).\n\nOne gap though: this silently returns''with noLog::error()(or equivalent). The issue's own "Expected" behavior is "an asset that cannot be resolved is logged and skipped, like every other error inside the tag" — every other failure mode inGlide::generate()'s closure hits thecatch (\Exception $e) { Log::error(...) }and gets logged. This new path is the one exception: it degrades gracefully but leaves zero trace, so the underlying "asset repository returned null for a resolvable file" condition (the actual bug, per statamic/eloquent-driver#609) becomes invisible/undebuggable in production — which is exactly the visibility gap that made the original 500 take a day to notice at scale.\n\nWorth considering: either add aLog::error()/Log::warning()call here before returning'', or (closer to the issue's first suggested alternative) guard inGlide::generateImage()by throwing an\ExceptionwhenAsset::find($item)is null, letting the tag's existingcatch (\Exception)log it — that would also mean the 3 other callers ofgenerateByAsset()(PresetGenerator, StaticUrlBuilder, ThumbnailController), which always pass an already-resolved asset, don't carry a defensive null-check they don't need. Not a blocker — the fix does stop the 500 — but as-is a real production trigger of this bug will still go unnoticed.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair catch, added —
Log::error()now fires before the early return (also rebased onto 6.x, which had picked up an unrelated conflicting change to this same method in the meantime).