Skip to content

History Encryption

Security

History Encryption

Imagine a scenario where your user is authenticated, browses privileged information on your site, then logs out. If they press the back button, they can still see the privileged information that is stored in the window's history state. This is a security risk. To prevent this, Inertia.js provides a history encryption feature.

How It Works

When you instruct Inertia to encrypt your app's history, it uses the browser's built-in Crypto API to encrypt the current page's data before pushing it to the history state. We store the corresponding key in the browser's session storage. When the user navigates back to a page, we decrypt the data using the key stored in the session storage.

Once you instruct Inertia to clear your history state, it clears the existing key from session storage and generates a new one. If it attempts to decrypt the history state with the new key, it will fail and Inertia will make a fresh request back to your server for the page data.

History encryption relies on window.crypto.subtle which is only available in secure environments (sites with SSL enabled).

Opting in

History encryption is an opt-in feature. There are several methods for enabling it:

Global Encryption

If you'd like to enable history encryption globally, call config.encrypt_history(true) in HandleInertiaRequests::configureset EncryptHistory: true on inertia.Middleware in bootstrap/app.goset the inertia.history.encrypt configuration value to true.

1use laravel::inertia;
2 
3impl inertia::Middleware for HandleInertiaRequests {
4 fn configure(&self, config: inertia::Config) -> inertia::Config {
5 config.encrypt_history(true)
6 }
7}
1use laravel::inertia;
2 
3impl inertia::Middleware for HandleInertiaRequests {
4 fn configure(&self, config: inertia::Config) -> inertia::Config {
5 config.encrypt_history(true)
6 }
7}
bootstrap/app.go
1m.Web.Append(inertia.Middleware{
2 Share: share,
3 ErrorPage: "Error",
4 EncryptHistory: true,
5})
1m.Web.Append(inertia.Middleware{
2 Share: share,
3 ErrorPage: "Error",
4 EncryptHistory: true,
5})
config/inertia.php
1'history' => [
2 'encrypt' => true,
3],
1'history' => [
2 'encrypt' => true,
3],

You are able to opt out of encryption on specific pages by calling inertia::encrypt_history(false)? or chaining .encrypt_history(false) onto inertia::render().

1inertia::render("Dashboard", ()).encrypt_history(false)
1inertia::render("Dashboard", ()).encrypt_history(false)

Laravel Go does not provide a per-page opt-out when EncryptHistory: true is enabled globally. If only specific pages need encryption, leave EncryptHistory disabled in inertia.Middleware and call inertia.EncryptHistory(r) in those handlers instead.

You are able to opt out of encryption on specific pages by passing false to Inertia::encryptHistory().

1Inertia::encryptHistory(false);
1Inertia::encryptHistory(false);

Per-request Encryption

To encrypt the history of an individual request, call inertia::encrypt_history(true)? or chain .encrypt_history(true) onto inertia::render()call inertia.EncryptHistory(r) before rendering the responsecall Inertia::encryptHistory() before returning the response.

1inertia::render("Billing/Invoices", props! { invoices }).encrypt_history(true)
1inertia::render("Billing/Invoices", props! { invoices }).encrypt_history(true)
1inertia.EncryptHistory(r)
2 
3return inertia.Render("Billing/Invoices", inertia.Props{"invoices": invoices})
1inertia.EncryptHistory(r)
2 
3return inertia.Render("Billing/Invoices", inertia.Props{"invoices": invoices})
1Inertia::encryptHistory();
1Inertia::encryptHistory();

Encrypt Middleware

In Laravel Rust, you can enable history encryption globally in HandleInertiaRequests::configure and opt out on specific pages with .encrypt_history(false), or enable it per-request with .encrypt_history(true).

In Laravel Go, you can enable history encryption globally with EncryptHistory: true on inertia.Middleware, or enable it per-request by calling inertia.EncryptHistory(r) in your handler.

To encrypt a group of routes, you may use Inertia's included EncryptHistory middleware.

1Route::middleware([Inertia\Middleware\EncryptHistory::class])->get('/', function() {
2 //
3});
4 
5Route::middleware(['inertia::encrypt'])->get('/', function() {
6 //
7});
1Route::middleware([Inertia\Middleware\EncryptHistory::class])->get('/', function() {
2 //
3});
4 
5Route::middleware(['inertia::encrypt'])->get('/', function() {
6 //
7});

Clearing History

To clear the history state, call inertia::clear_history()?inertia.ClearHistory(r)Inertia::clearHistory() before returning the response.

1use laravel::inertia;
2use laravel::prelude::*;
3 
4pub async fn logout() -> laravel::Result<Redirect> {
5 inertia::clear_history()?;
6 
7 Ok(Redirect::to("/"))
8}
1use laravel::inertia;
2use laravel::prelude::*;
3 
4pub async fn logout() -> laravel::Result<Redirect> {
5 inertia::clear_history()?;
6 
7 Ok(Redirect::to("/"))
8}
1package auth
2 
3import (
4 "go.laravel.com/inertia"
5 "go.laravel.com/web"
6)
7 
8func Logout(r *web.Request) *web.Response {
9 inertia.ClearHistory(r)
10 
11 return web.Redirect("/")
12}
1package auth
2 
3import (
4 "go.laravel.com/inertia"
5 "go.laravel.com/web"
6)
7 
8func Logout(r *web.Request) *web.Response {
9 inertia.ClearHistory(r)
10 
11 return web.Redirect("/")
12}
1Inertia::clearHistory();
1Inertia::clearHistory();

Once the response has rendered on the client, the encryption key will be rotated, rendering the previous history state unreadable.

You can also clear history on the client side by calling router.clearHistory().