<!-- Source: @site/projects/fortify-hardening-for-laravel/docs/05-timing.md -->
<!-- Canonical: https://docs.pushery.com/fortify-hardening-for-laravel/timing/ -->

# Timing

> Why the same text is not enough, and how an answer for an address with an account comes to take as long as one for an address without.

An answer can read the same and still tell the caller which case it was, by how long it took. A
password is stored as a bcrypt hash, and at Laravel's default cost of 12 rounds one hash takes in
the order of a quarter of a second. Whichever answer pays for a hash that the other one does not is
that much slower, and a single request shows it.

## Registration

Creating an account hashes its password, and finding a taken address does not. Without the package
a taken address is answered in a fraction of the time a free one takes.

The package has both outcomes pay the same hash. On Fortify's route your `CreatesNewUsers` action
hashes the password of a new account, and for a taken address the package hashes the same password
with the same hasher. The [registrar](https://docs.pushery.com/fortify-hardening-for-laravel/registration.md#from-a-controller-of-your-own) hashes the
password once, before the address is looked up, and hands the hash to the code that creates the
account.

A new account costs more than its hash all the same: your `CreatesNewUsers` action checks the
address and inserts the account, and may create more beside it, such as a team, and the listeners of
`Registered` run for a new account alone. So both outcomes also run inside the
[minimum duration](#the-minimum-duration), on Fortify's route and in the registrar alike. Keep that
work inside the minimum: what runs past it shows again.

Both outcomes also send one mail: the verification mail of a new account, or the notice to the
holder of a taken address. Send them the same way. Laravel sends the verification mail at once, and
so does the package's notice; an application that queues the one queues the other, as shown in
[Registration](https://docs.pushery.com/fortify-hardening-for-laravel/registration.md#the-notice-to-the-holder). Over the
[mail budget](https://docs.pushery.com/fortify-hardening-for-laravel/registration.md#the-mail-budget) neither outcome sends its mail.

## The minimum duration

The password routes work the other way round. For an address with an account the broker creates a
token, which costs a hash, and sends the link; for an address without one it does neither. Laravel
holds these requests, and a failed sign-in, to a minimum duration called a timebox, so that the
short case waits until the long one would be done. Its default is 200 milliseconds, which is less
than the hash alone, so the case with an account still takes longer.

The package raises that minimum to one second by setting `auth.timebox_duration`, unless your
application sets that key itself. A failed sign-in, every reset-link request, a failed reset and
every registration then take one second, with an account or without. A successful sign-in and a
successful reset still return at once.

With Fortify's two-factor feature on, the sign-in checks the password in a step of Fortify's own
before the guard does. Releases 1.40 to 1.43 hold a failure there to a fixed 200 milliseconds,
earlier releases do not hold it at all, and from release 1.44.0 on Fortify holds it to
`auth.timebox_duration` itself. The package holds that step to the same minimum as the guard on
every release, whether the step is Fortify's or one your application
binds with `Fortify::redirectUserForTwoFactorAuthenticationUsing()`. A login pipeline of your own,
in `fortify.pipelines.login` or through `Fortify::authenticateThrough()`, is held where it names
the contract `Laravel\Fortify\Contracts\RedirectsIfTwoFactorAuthenticatable`, not where it names a
class.

With a callback set through `Fortify::authenticateUsing()`, Fortify signs in through that callback
and not through the guard. Below release 1.44.0 nothing holds such a sign-in to a minimum: a callback
that looks the user up and checks the password pays the hash for an address with an account alone.
From 1.44.0 on Fortify holds it to `auth.timebox_duration` itself. The package holds
Fortify's step that calls the callback, `Laravel\Fortify\Actions\AttemptToAuthenticate`, to the
same minimum, in Fortify's login pipeline and in one of your own that names the class. A sign-in that
goes through still returns at once.

| Setting | Default |
|---|---|
| `timebox.microseconds` | `1000000`, one second |
| `FORTIFY_HARDENING_TIMEBOX_MICROSECONDS` | the same, from the environment |

Set it to `0` to leave Laravel's own minimum in place. Only a whole number is read, so a value such
as `0.5`, `.5` or `-3` keeps the default rather than switching the minimum off. A whole number of
your own in `auth.timebox_duration` always wins over the package's, also where your
`config/auth.php` reads it from `env()`, which hands the number over as text.

The minimum only hides work that finishes inside it. Queue the reset-link notification, so that
sending the mail is not part of the request, and keep the minimum above the time your slowest
server needs for one hash and for one registration of a new account.

## The key of the sign-in limiter

Fortify counts failed sign-ins per address and IP, five a minute, and keeps the count in your cache
under a key made of the lower-cased address and the IP as they are. Every failed attempt writes that
key and a successful one clears it, so both stand in Redis or the cache table, and an error tracker
such as Sentry or Flare records the key with every cache call it traces.

The package binds its own `LoginRateLimiter` in place of Fortify's. It counts under the same pair,
hashed with your application key: the count, the limit and the wait are Fortify's, and the key no
longer names who tried to sign in from where. Counts that are running when you deploy the package, or
when you change the application key, start again.

An IPv6 client is counted by its /64, the network a single host is usually given. Counted by the full
address, a host that holds such a network could start a count of its own from every address in it,
five attempts a minute for each. An IPv4 address counts as it is, and an IPv4 address in its IPv6
form, such as `::ffff:203.0.113.7`, counts as the IPv4 address it carries. When you update the
package, the counts of IPv6 clients that are running start again.

The IP is the one Laravel reads through the proxies your application trusts. Below
`laravel/framework` 13.43.0, an application that configures no trusted proxy and whose web server
answers any host name takes a header the visitor sends as the mark of a Forge or Vapor proxy
(GHSA-559m-v249-7qw2), so a visitor can claim another IP with every attempt, and each one starts a
count of its own. Update the framework, configure the proxies you trust, or let the web server
answer only your own host names.

Switch it off with `FORTIFY_HARDENING_HASHED_LOGIN_KEYS=false` if you read those keys yourself. A
limiter of your own that you bind to Fortify's class after the package boots replaces this one.

### The `login` limiter of your service provider

Fortify's installer counts the same pair a second time, in your own code: the `login` rate limiter
in `app/Providers/FortifyServiceProvider.php`, which `fortify.limiters.login` puts on the sign-in
route. Laravel keeps the count of a named limiter under an MD5 of the limiter's name and its key, and
the MD5 of `login`, an address and an IP is a value anyone can compute from a guessed address and
IP. The package does not change that limiter, since it is part of your application. Key it with
`LoginThrottleKey::of()`, which builds the key the package's own limiter counts under: the address and
the client, an IPv6 client by its /64, hashed with your application key:

```php
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
use Pushery\FortifyHardening\Support\LoginThrottleKey;

RateLimiter::for('login', function (Request $request) {
    return Limit::perMinute(5)->by(LoginThrottleKey::of($request));
});
```

The count, the limit and the wait stay what the limiter says. Counts that are running when you deploy
the change start again. The helper works whether or not the package's own limiter is switched on. A
limiter that counts by something else hashes the key it builds with `LoginThrottleKey::hash()`.
