Skip to content

[13.x] Guard Job::payload() against corrupt JSON#59740

Closed
sumaiazaman wants to merge 2 commits intolaravel:13.xfrom
sumaiazaman:fix/job-payload-null-safety
Closed

[13.x] Guard Job::payload() against corrupt JSON#59740
sumaiazaman wants to merge 2 commits intolaravel:13.xfrom
sumaiazaman:fix/job-payload-null-safety

Conversation

@sumaiazaman
Copy link
Copy Markdown
Contributor

Summary

Job::payload() calls json_decode($this->getRawBody(), true) which returns null when the raw body is malformed or empty JSON. Multiple callers — getName(), maxTries(), maxExceptions(), retryUntil(), resolveName() — then perform array-access on null, which either triggers a PHP warning or a null-coalescing fallback that returns null unexpectedly.

Context

Scenario Before After
Valid JSON body ✅ Returns decoded array ✅ Returns decoded array
Malformed JSON body ❌ Returns null → crashes callers ✅ Returns []
Empty body ❌ Returns null → crashes callers ✅ Returns []

Solution

Add a ?? [] null-coalescing fallback to the return value of payload():

// Before
public function payload()
{
    return json_decode($this->getRawBody(), true);
}

// After
public function payload()
{
    return json_decode($this->getRawBody(), true) ?? [];
}

This is the minimal, safe fix — all callers already use ?? for optional keys ($this->payload()['maxTries'] ?? null), so returning [] instead of null means they naturally return null without any risk of crashing.

Safety

  • No change in behavior for valid JSON payloads.
  • Corrupt-payload jobs already cannot be dispatched normally; this prevents a secondary crash when the worker encounters one.
  • No new exceptions are thrown — the behavior is strictly safer.

Changes

  • src/Illuminate/Queue/Jobs/Job.php — add ?? [] to payload() return
  • tests/Queue/QueueJobTest.php — new tests covering valid JSON, corrupt JSON, and empty body

Test Plan

  • php vendor/bin/phpunit tests/Queue/QueueJobTest.php — 4 tests, 4 assertions, all green

sumaiazaman and others added 2 commits April 16, 2026 10:50
…rray

Prevents null array access errors in getName(), maxTries(), and other
callers when the raw job body is malformed or empty JSON.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants