Tutorial
Bots posting your Laravel contact form keep it awake
A POST is never cached, so every bot that posts your contact form wakes the container. How edge.require_xhr refuses those posts at the edge with a 403.
In this post
- Why does a bot posting a form keep the container awake?
- Why do CSRF, honeypots and captchas not help?
- What tells a bot's post apart from a real one?
- How do you refuse those posts at the edge?
- How do you check that it works?
- Which forms should you leave out?
- What else keeps a form page from sleeping?
Every public Laravel site with a contact form gets posts from bots. Your honeypot catches them, your captcha refuses them, or the CSRF check answers 419, and nothing reaches your inbox. On a server that runs all day, that is the end of it.
On a serverless container it is not, because every one of those posts had to wake the container to be refused. This post explains why form posts get past an edge cache, why the checks in your application cannot help with that, and how a pull zone that Brewless manages refuses most of them at the bunny.net edge with edge.require_xhr.
Why does a bot posting a form keep the container awake?
Because a CDN never keeps an answer to a POST, so every post goes on to the container. On Scaleway a serverless container scales to zero only after 15 minutes without a request, and every request that reaches it starts those 15 minutes again. A bot that posts your contact form every few minutes keeps it awake all day.
The rest of the site can be at the edge already. Pages that are the same for every visitor are kept by bunny.net and answered without the container; Why your Laravel container never scales to zero explains how. Scanner addresses such as /wp-login.php are refused at the edge on every pull zone Brewless manages, as Why /wp-login.php wakes your Laravel container describes. A POST to /contact is neither: it is a real path of your application, and the edge has no copy to give back.
How much this matters, the edge cache documentation says from one site: bots posting a plain HTML form to /contact all day, about 2,700 posts a day, one every 30 seconds, kept its container awake for good.
Why do CSRF, honeypots and captchas not help?
They help against spam. They do not help against the wake-up, because they all run in PHP:
| Defence | Where it runs | Container woken? |
|---|---|---|
CSRF check (419 Page Expired) |
Laravel middleware | Yes |
| Honeypot field | Your controller or a package | Yes |
| Captcha verification | Your controller, often with a call to the captcha service | Yes |
RateLimiter or throttle middleware |
Laravel, with its cache store | Yes |
| A rule at the edge | bunny.net, before the origin | No |
By the time Laravel can decide that a post is spam, Scaleway has started an instance, FrankenPHP has booted your application and the 15 minutes have started again. Only something in front of the container can refuse a request without waking it.
What tells a bot's post apart from a real one?
For many forms, one header. Laravel's own JavaScript setup gives axios a default header in resources/js/bootstrap.js:
import axios from 'axios';
window.axios = axios;
window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
jQuery's $.ajax sends the same header by default. A form whose page submits it with either of them always carries X-Requested-With: XMLHttpRequest. With fetch you set it yourself:
await fetch('/contact', {
method: 'POST',
headers: {
'X-Requested-With': 'XMLHttpRequest',
'Content-Type': 'application/json',
Accept: 'application/json',
},
body: JSON.stringify(Object.fromEntries(new FormData(form))),
});
Most form bots do not run your JavaScript. They read the HTML, find the <form>, fill in its fields and post it as application/x-www-form-urlencoded, without that header. That difference is something the edge can see without asking your application.
Note
Laravel's
$request->ajax()reads the same header. If your controller already answers JSON for an Ajax post and redirects otherwise, the header is part of how your form works today.
How do you refuse those posts at the edge?
Name the form paths under edge.require_xhr in brewless.yml:
brewless.ymlenvironments:
production:
edge:
require_xhr: ['/contact', '/newsletter']
On the next deploy, Brewless gives the environment's pull zone edge rules named brewless: form posts without X-Requested-With 1 and on, five paths per rule. Each rule needs all three to hold: the path matches, the method is POST, and the request header X-Requested-With does not match *XMLHttpRequest*, which includes a request that does not send it at all. Such a post gets a 403 from bunny.net, and the container never hears of it.
A post that carries the header goes through as before, and so does every other method: the GET of the contact page is still kept at the edge like any other page.
The paths follow the same rules as edge.refuse, which Old URLs that keep waking your Laravel container covers:
- a path starts with
/and a first segment written out, such as/contact; after that,*stands for anything (/forms/*); - no query string, and never anything under
/_opsor/.well-known; - at most 20 paths, each at most 200 characters.
brewless plan shows the change before you deploy. A deploy puts the rules back when they were changed or switched off at bunny.net, removes them when the file no longer names any paths, and writes each change to the audit trail. A deploy without a brewless.yml keeps what the last one set. The full reference is in brewless.yml.
How do you check that it works?
With curl, once without the header and once with it:
curl -i -X POST https://example.com/contact
curl -i -X POST https://example.com/contact -H 'X-Requested-With: XMLHttpRequest'
The first must answer 403 from the edge. The second must reach your application. Whatever it answers then, often a 419 because curl sent no CSRF token, comes from Laravel, and that is the proof that the post got through.
To see whether the posts were waking the container in the first place, open What reaches the container on the environment's page in the console, or run brewless wakes production. It lists the addresses of the last 24 hours that the edge did not answer itself, with the cache status bunny.net logged. A POST is never kept, so it shows up without a cache status; a /contact near the top of that list, many times a day, is the pattern this rule is for. Once the rule is live, the edge's 403s on those paths are counted as answered by the edge and drop out of the list. The scaling documentation explains every column.
Which forms should you leave out?
Only name a path whose own page always submits with X-Requested-With: XMLHttpRequest. Everything else that posts there gets the 403 too:
- A plain HTML form. A
<form method="post">without JavaScript sends no such header. Visitors with JavaScript switched off, or a page whose script failed to load, are refused as well. - Webhooks. A payment provider or mail service that posts its callbacks to a path under the one you named does not send the header. Keep webhooks on their own paths.
- API clients. A mobile app or an integration that posts to the same route as your form.
- Frameworks with their own transport. Check in your browser's developer tools which headers your form actually sends before you name its path. If
X-Requested-Withis not among them, set it, or leave the path out.
Warning
This is a first line of defence against bots that post your form as HTML, not authentication. A bot can send the header too. Keep your CSRF check, honeypot or captcha and rate limiting in Laravel for every post that gets through.
What else keeps a form page from sleeping?
The form page itself can wake the container too, not only its posts. A page with @csrf in it starts a session, the session sets a cookie, and a page that sets a cookie is never kept at the edge. If the form is in a footer or a modal on every page, no page of the site is kept.
From brewless/laravel 0.18 on Laravel 13, a guest without a cookie gets no session on a page that puts nothing in it and shows no CSRF token. A deploy also tells you when the edge cannot keep your homepage, and why: a cookie is the most common reason, and the usual fix is to send the form with fetch() and leave the token out of the page. Pages without a session explains the details and how to take the session off a group of routes yourself.
Together that gives a contact form that costs nothing while nobody uses it: the page is kept at the edge, the bots' HTML posts are refused there, and only a visitor who actually presses the button wakes the container.
Frequently asked questions
Why does a bot posting my contact form keep my serverless container awake?
A CDN only keeps answers to GET and HEAD requests, so a POST always goes on to the origin. On Scaleway a serverless container scales to zero only after 15 minutes without a request, so a bot that posts every few minutes keeps it running all day, even when Laravel rejects every post.
Does Laravel's CSRF protection stop these posts from waking the container?
No. The CSRF check is middleware in your application, so the container has to start and run Laravel before it can answer 419. The same goes for honeypots, captchas and rate limiting in PHP. They stop the spam, not the wake-up.
Which forms can I name in edge.require_xhr?
Only form endpoints whose own page always submits with JavaScript and sends X-Requested-With with XMLHttpRequest, such as a form posted with Laravel's axios setup or jQuery. A plain HTML form, a webhook or an API client that posts to such a path without the header gets the 403 too.
Can a bot get past edge.require_xhr?
Yes. A bot can send the header as well. The rule keeps out the many bots that post a plain HTML form, and your own checks in Laravel still run for every post that gets through.
How do I check that the edge refuses the posts?
Post to the path with curl, once without and once with the header. Without it the edge answers 403. With X-Requested-With set to XMLHttpRequest, the request reaches your application.