Skip to content

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

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

[12.x] Add InteractsWithData::clamp()#58608
taylorotwell merged 3 commits intolaravel:12.xfrom
cosmastech:patch-7

Conversation

@cosmastech
Copy link
Contributor

@cosmastech cosmastech commented Feb 3, 2026

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);
}
Copy link
Contributor

@shaedrich shaedrich Feb 3, 2026

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
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
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
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

Comments