# [RFC] [Discuss] Random Functions Throwing Exceptions in PHP 7.0.0

- Thread: https://externals.io/message/87876
- Original archive: https://news-web.php.net/php.internals/87876
- Started by Thomas Bley on 2015-08-23T13:26:04+00:00
- Messages: 2
- RFC: Random Functions throwing Exceptions (Implemented (PHP 7.0)) — https://externals.io/rfc/random-function-exceptions.md
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #87876 — Thomas Bley — 2015-08-23T13:26:04+00:00

URL: https://externals.io/message/87876#87876

Scott Arciszewski wrote on 23.08.2015 02:50:

> 
> > […]
> 
> Hi Thomas,
> 
> Your proposal effectively blames the user if they get it wrong and
> burdens them with additional responsibilities. Increasing the
> cognitive load of PHP developers will not result in a net gain for the
> security of the applications they develop. I've made this argument
> elsewhere in the course of this discussion.
> 
> Cryptography implementations should do everything reasonable to not
> blame the user, and cryptographic primitives should do everything
> reasonable to not blame the implementor.
> 
> Read this: http://cr.yp.to/talks/2015.01.07/slides-djb-20150107-a4.pdf
> 
> > […]
> 
> Exceptions will only be thrown in exceptional circumstances:
> 
> 1. If they're using shared hosting, and a malicious script triggers a
> file descriptor exhaustion condition on another hosting account (we're
> assuming great sandboxing between customers), an exception will be
> thrown rather than returning FALSE.
> 2. If the system is utterly incapable of generating random numbers.
> 
> An exception must either be handled (try-catch), or it by default
> terminates script execution. This is the best of both worlds:
> 
> 1. It fails closed.
> 2. It's easy to handle gracefully should the developer wish to do so.
> 
> Designing a CSPRNG that will fail closed, without unavoidably killing
> their script (i.e. E_ERROR), will do far more to make PHP applications
> secure than telling people they should RTFM.
> 
> Hackers have been saying RTFM since the ARPANET days, and yet most
> people still don't. It's a losing battle. Let's patch what we can with
> good design decisions.
> 
> TL;DR - the path forward is Throwable. Whether it's an Error object or
> and Exception object (and when it throws which) is what's to be
> discussed and voted on.
> 
> Scott Arciszewski
> Chief Development Officer
> Paragon Initiative Enterprises <https://paragonie.com>
> 

why not have false + e_warning for strict_types=0 and fatal error for strict_types=1 ?
Doing function `random_int()`: int { ...

Regards
Thomas

---

## #87877 — Niklas Keller — 2015-08-23T14:30:01+00:00

URL: https://externals.io/message/87876#87877 · In reply to: #87876

>
> why not have false + e_warning for strict_types=0 and fatal error for
> strict_types=1 ?
> Doing function `random_int()`: int { ...


How's this connected to `strict_types`? It's not.

If people use this function without reading documentation, they will also
> use other things without documentation like database queries without
> binding/escaping, inject html without escaping, etc.
> Having core functions suddenly throw exceptions causes many problems in
> the code structure.


How are these things connected? How does this create any issues in any
existing code structure? This RFC affects only two new functions introduced
in PHP 7.

I think there are a lot of security problems if people ignore return
> values, e.g. password comparison, user lookup in database, lookups for
> permissions, etc.


You compare an edge case, where these two functions currently return false
instead of throwing an exception to fail closed, to functions with an
expected `true|false` return value.

This change is especially important, because these functions may be used in
a way like this, as already mentioned in the previous discussions:

for ($i = 0; $i < 10; $i++) {
    $result .= $values[random_int(0, 10)];
}

It's simply far too easy to make mistakes in security relevant code.

Regards, Niklas
