Skip to content

[12.x] Add InteractsWithData::clamp() - #58608

Merged
taylorotwell merged 3 commits into
laravel:12.xfrom
cosmastech:patch-7
Feb 4, 2026
Merged

taylorotwell merged 3 commits into
laravel:12.xfrom
cosmastech:patch-7

Conversation

@cosmastech

@cosmastech cosmastech commented Feb 3, 2026 •

Copy link
Copy Markdown
Contributor

I see it happen pretty routinely that an endpoint has a per_page parameter that someone will retrieve like this:

/** @var int<PHP_INT_MIN, PHP_INT_MAX> $perPage */
$perPage = request()->integer('per_page', 50);

If the request doesn't have validation in front of it (which happens pretty frequently), an attack surface is opened up to perform painfully large DB queries. If we can gently nudge developers to instead use:

/** @var int<1, 100> $perPage */
$perPage = request()->clamp('per_page', 1, 100, 50);

then databases everywhere will be overjoyed! OWASP experts will have to find new work! Gitlab and GitHub will receive fewer comments by yours truly!

Add clamp method to retrieve data clamped between min and max values.
@cosmastech
cosmastech marked this pull request as draft February 3, 2026 18:35
Comment on lines +293 to +300
* @param int|float $max
* @param int|float $default
* @return float|int
*/
public function clamp($key, $min, $max, $default = 0)
{
return Number::clamp($this->data($key, $default), $min, $max);
}

@shaedrich shaedrich Feb 3, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure but people might appreciate a shortcut 🤔

Suggested change
* @param int|float $max
* @param int|float $default
* @return float|int
*/
public function clamp($key, $min, $max, $default = 0)
{
return Number::clamp($this->data($key, $default), $min, $max);
}
* @param int|float|null $max
* @param int|float $default
* @return float|int
*/
public function clamp($key, $min, $max = null, $default = 0)
{
if (func_num_args() === 2) {
$max = $min;
$min = 0;
}
return Number::clamp($this->data($key, $default), $min, $max);
}

or

Suggested change
* @param int|float $max
* @param int|float $default
* @return float|int
*/
public function clamp($key, $min, $max, $default = 0)
{
return Number::clamp($this->data($key, $default), $min, $max);
}
* @param int|float|null $max
* @param int|float $default
* @return float|int
*/
public function clamp($key, $min, $max = null, $default = 0)
{
if (func_num_args() === 2) {
[$min, $max] = [0, $min];
}
return Number::clamp($this->data($key, $default), $min, $max);
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's clever!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'll leave it as is for now. I have a feeling this one is maybe a bit on the "we already have Number::clamp()" side of the house, but we'll see.

@cosmastech
cosmastech marked this pull request as ready for review February 3, 2026 20:53
@taylorotwell
taylorotwell merged commit 8ff1a38 into laravel:12.x Feb 4, 2026
70 checks passed
@cosmastech
cosmastech deleted the patch-7 branch February 7, 2026 12:35
binaryfire added a commit to hypervel/components-backup that referenced this pull request Oct 1, 2026
Complete the request clamp port from Laravel #58608 and its follow-up
#61355. Empty strings, null and non-numeric input now use the supplied
default before applying the requested bounds instead of raising TypeError.

Keep numeric-string conversion in InteractsWithData so the strictly typed
Number::clamp call accepts normal query-string numbers without losing
fractional values. Remove the obsolete rejection comment and analysis
suppression now that the shared input boundary guarantees a numeric value.

Port all current upstream request cases, preserve numeric-string coverage,
and replace the older rejection expectation with a check that the implicit
default is itself clamped. Update the existing request documentation.

Validated with both complete changed test files, related ParaTest coverage,
ValidatedInput tests, full source and type-fixture PHPStan, formatting and
diff checks. Regression checks distinguish the old failure, an unbounded
default and missing numeric-string conversion.

Upstream:
laravel/framework#58608
laravel/framework#61355
Porting source: 01d008c9b5f32cb7c5e50a9a22273113d810b2a2 (13.x).
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.

3 participants