itxshakil / laravel-form-shield
CAPTCHA-free spam scoring for Laravel forms: quarantine instead of reject, keep every verdict, and tune your rules against human-labelled data.
Fund package maintenance!
Requires
- php: ^8.2
- composer-runtime-api: ^2.0
- laravel/framework: ^12.0|^13.0
Requires (Dev)
- larastan/larastan: ^3.0
- laravel/pint: ^1.18
- orchestra/testbench: ^10.0|^11.0
- phpunit/phpunit: ^11.5|^12.0
Suggests
- livewire/livewire: Use the WithFormShield trait to protect Livewire forms (^3.0).
Provides
None
Conflicts
None
Replaces
None
README
Spam scoring for Laravel forms, with no CAPTCHA. Suspected spam is quarantined instead of rejected, and the rules are tuned against your own labelled data.
A honeypot catches the dumbest bots. A CAPTCHA catches more, but it costs you real visitors, a third-party script, and a privacy question. Rejecting suspected spam outright quietly throws away the occasional real lead, and nobody ever finds out.
Form Shield is the layer after the honeypot:
- It scores, it doesn't guess. Hard signals (honeypot, forged token, too fast) flag on their own. Soft signals (no JavaScript, duplicate message, disposable or dead email domain, link in the name field, gibberish...) add up to a threshold, and no single soft signal can quarantine anyone.
- It quarantines. A flagged submission gets the normal success page, but no email goes out and no side effects run. You keep the row. Bots learn nothing, and a false positive is one click to recover.
- It learns from your labels. Every verdict is stored, spam or not. When someone marks a submission as spam or ham,
php artisan form-shield:reportshows each signal's precision, so you tune weights from data instead of hunches.
Example report output:
+------------------+--------+-------+----------+-----------------+-----------+
| Signal | Weight | Fired | Reviewed | Reviewed as ham | Precision |
+------------------+--------+-------+----------+-----------------+-----------+
| no_js | 2 | 412 | 96 | 3 | 96.9% |
| duplicate | 3 | 188 | 41 | 0 | 100.0% |
| honeypot | hard | 1,204 | 87 | 0 | 100.0% |
| foreign_script | 2 | 57 | 12 | 4 | 66.7% |
+------------------+--------+-------+----------+-----------------+-----------+
It works with Blade forms, Livewire, Inertia/SPA forms, JSON APIs, and forms embedded in iframes on other sites.
Requirements
- PHP 8.2+
- Laravel 12 or 13
Install
composer require itxshakil/laravel-form-shield php artisan form-shield:install
The installer publishes the config, then asks where verdicts should be stored:
- on a table you already have (
inquiries,leads...): it writes a migration that adds the spam columns, or - in Form Shield's own
form_shield_submissionstable, for forms that don't store their posts anywhere (say, a contact form that only sends an email).
For scripts and CI: php artisan form-shield:install --table=inquiries --migrate or --log --migrate.
Then check the setup, locally and in your deploy script:
php artisan form-shield:doctor
It fails on problems that weaken the shield without any error: a missing APP_KEY, an array cache in production (rate caps and duplicate never fire), a missing exemption model, or store.auto without its table.
It warns about a honeypot name autofill recognises, a weight at or over the threshold, and the PCRE JIT being off.
php artisan about also lists the main settings.
Using Laravel Boost? The package ships Boost guidelines that teach your AI agent where the shield goes and how to inspect, store and test.
Pick itxshakil/laravel-form-shield when php artisan boost:install lists third-party guidelines.
Quick start
1. Add the shield to your form, and the script once in your layout.
<form method="POST" action="{{ route('contact.store') }}"> @csrf <input name="name"> <input name="email"> <textarea name="message"></textarea> {{-- last, so password managers don't take the honeypot for the username --}} <x-form-shield /> <button>Send</button> </form> {{-- once, e.g. at the end of your layout's <body> --}} @formShieldScripts
2. Store verdicts. If you picked an existing table, the installer already wrote the migration. You can also run it on its own:
php artisan form-shield:columns inquiries php artisan migrate
Add --no-index if you build the is_spam index yourself, for example concurrently.
Then add the trait to the model:
use Itxshakil\FormShield\Concerns\HasSpamVerdict; class Inquiry extends Model { use HasSpamVerdict; }
3. Inspect, store, and skip the side effects for spam.
Give each form a profile name, so it can get its own rules later. Add it to config/form-shield.php (an empty array uses the defaults):
'profiles' => [ 'default' => [], 'contact' => [], ],
use Itxshakil\FormShield\Facades\FormShield; public function store(Request $request) { $data = $request->validate([ 'name' => ['required', 'string', 'max:100'], 'email' => ['required', 'email'], 'message' => ['required', 'string', 'max:5000'], ]); $verdict = FormShield::inspect($request, 'contact'); // after validation if ($verdict->isExpired) { return back()->withInput()->withErrors(['message' => __('form-shield::messages.expired')]); } $inquiry = Inquiry::createWithVerdict($data, $verdict); if ($verdict->passes()) { // not spam, and not rate limited Mail::to('team@your-company.com')->queue(new NewInquiry($inquiry)); } return to_route('contact.thanks'); // the same response either way }
Don't write
Inquiry::create([...$data, ...$verdict->toAttributes()]). If your model lists its fields in$fillable, Laravel silently drops the spam columns and every row is saved as "not spam".createWithVerdict()fills$datanormally and force-fills only the verdict columns. The two-step equivalent isInquiry::make($data)->applyVerdict($verdict)->save().
No table of your own? Use the built-in log instead of the Inquiry model:
$verdict = FormShield::inspect($request, 'contact'); FormShield::record($request, $verdict); // or set FORM_SHIELD_STORE=true to log every inspection if ($verdict->passes()) { Mail::to('team@your-company.com')->queue(new ContactMessage($data)); }
Prefer match? Every verdict has a status enum:
use Itxshakil\FormShield\Enums\VerdictStatus; return match ($verdict->status) { VerdictStatus::Clean, VerdictStatus::Spam => to_route('contact.thanks'), VerdictStatus::Expired => back()->withInput()->withErrors(['message' => __('form-shield::messages.expired')]), VerdictStatus::RateLimited => abort(429), };
4. Label some submissions, then read the report.
$inquiry->markAsHam(auth()->user()); // a false positive $inquiry->markAsSpam(auth()->user()); // a miss
php artisan form-shield:report "App\Models\Inquiry" --days=60 php artisan form-shield:report # the built-in log
To make sure someone looks, schedule a daily email of what was quarantined (set FORM_SHIELD_DIGEST_TO first):
Schedule::command('form-shield:digest')->dailyAt('08:00');
Optional: reject obvious spam content
The scoring above never shows an error, because the point is that bots can't tell what was caught. A few patterns are different: a real person can fix them, so it's kinder to say so. NoSpamContent is a normal validation rule for those:
use Itxshakil\FormShield\Rules\NoSpamContent; 'message' => ['required', 'string', new NoSpamContent],
It fails with a visible error for more than 2 links, HTML or BBCode link markup, or a configured phrase like "quality backlinks". Leave it out if you'd rather everything went through quarantine. See signals.
Prefer middleware?
Route::post('/contact', ContactController::class)->middleware('form-shield:contact'); // in the controller $verdict = $request->spamVerdict();
The middleware applies the rate caps up front (a 429) and, if you turn it on, rejects expired forms (a 422). It never rejects spam, because quarantine is your controller's job. The inspection itself runs on the first spamVerdict() call, so call it after validation. A visitor who fixes a typo and resubmits shouldn't be scored as a duplicate of their own first attempt. The same applies when you call FormShield::inspect() yourself.
Livewire
use Itxshakil\FormShield\Livewire\WithFormShield; class ContactForm extends Component { use WithFormShield; public string $email = ''; public string $message = ''; public function save(): void { $verdict = $this->inspectFormShield('contact', ['email' => $this->email, 'message' => $this->message]); // ... } }
<form wire:submit="save"> ... <x-form-shield wire /> <button>Send</button> </form>
Inertia, SPAs, and cross-origin forms
Share FormShield::fields() as a prop, or enable the JSON endpoint (form-shield.route.enabled). Then arm the shield on the form's first interaction:
import { armShield, fieldNamesFrom } from '../../vendor/itxshakil/laravel-form-shield/resources/js/client.js'; const form = useForm({ email: '', message: '', ...props.formShield }); const names = fieldNamesFrom(props.formShield);
<form @focusin="armShield(form, names)" @submit.prevent="form.post('/contact')">
Testing your app
use Itxshakil\FormShield\Testing\InteractsWithFormShield; class ContactTest extends TestCase { use InteractsWithFormShield; public function test_a_visitor_can_send_a_message(): void { $this->post('/contact', [...$data, ...$this->formShieldFields()])->assertRedirect('/thanks'); } public function test_spam_is_quarantined(): void { FormShield::fake()->flagAll(); $this->post('/contact', $data); Mail::assertNothingQueued(); } }
Documentation
- How it works: hard vs soft signals, the exemption, and why quarantine
- Configuration
- Profiles: different rules per form
- Signals reference
- Custom signals
- Storing verdicts and reviewing
- Tuning with the report
- Livewire
- JSON, SPA, Inertia and iframe forms
- Rate limits
- Events
- Testing your app
- Accessibility and privacy
- FAQ
How it compares
| Honeypot packages | CAPTCHA (reCAPTCHA, hCaptcha, Turnstile) | Form Shield | |
|---|---|---|---|
| Friction for real people | none | a challenge, sometimes | none |
| Third-party script / privacy review | no | yes | no |
| Catches bots that skip the honeypot | rarely | usually | often, via scoring |
| What happens on a false positive | lead lost | lead lost | quarantined, recoverable |
| Tells you which rules are wrong | no | no | yes, precision per signal |
Form Shield isn't a CAPTCHA replacement for high-value targets like login or signup under attack. It's for contact, quote, booking, newsletter and lead forms, where the cost of a lost real message is high and the attacker is a generic bot.
Origin
Form Shield is extracted from the contact and booking forms of a production hosting platform, where it replaced a CAPTCHA proposal. The design decisions are written up in docs/how-it-works.md.
Upgrading
1.3
- A published config now picks up new keys from the package: a signal added in a later version gets its default weight instead of scoring 0.
If you deleted a line from
weightsto turn a signal off, set that weight to0instead; see A published config and later versions. The 1.1 and 1.2 steps of adding new weights by hand are no longer needed. - Each email address gets the known-sender exemption 3 times per profile per day (
exemption.max_per_day), then it is scored like anyone else's. Set it tonullfor the old behaviour. - If you use the built-in log, widen its payload column:
php artisan vendor:publish --tag=form-shield-payload-migration && php artisan migrate. - Run
php artisan form-shield:doctor.
1.2
Two changes: autofill no longer quarantines people silently, and attempts are counted per network.
Honeypot autofill. Browser and password-manager autofill could fill the honeypot and silently quarantine a real person.
- A filled honeypot is now hard only when the shield script didn't run.
When it did, the fill scores as the soft signal
honeypot_with_js(weight 3). A test that posts a filled honeypot to prove quarantine should send$this->formShieldFields(withJs: false), the shape a bot sends. - The honeypot carries password-manager opt-out attributes, and the default name is now
form_reference. A published config keepsfax_number; changefields.honeypottoform_reference(or any name autofill won't recognise) to get the fix. If you published the views (form-shield-views), re-publish them or add the attributes by hand. - Tests or hand-built SPA markup that hard-code
fax_numberstop hitting the honeypot once the name changes. Read the name fromFormShield::fieldNames()['honeypot']instead. - Forms rendered before the rename keep posting the old name until they expire (
max_age_seconds). Withallowed_fieldsset, each of those posts firesunexpected_fields(+2) in that window. - Move
<x-form-shield />to the end of each form, just before the submit button.
Busy networks. Every attempt also counts against the sender's network (/24 or /64), and the soft signal busy_network (weight 2) fires after busy_network_after (5) attempts a day from one network.
- Check your trusted proxies first: if every visitor shares the proxy's IP, every visitor counts against one network.
- A test that inspects more than five times from one network (127.0.0.1 in HTTP tests) will see the signal. Vary the IP, or leave
busy_networkout of that profile'ssignals.
1.1
content.signals adds three soft signals; see Content signals.
Changelog
See CHANGELOG.md.
Contributing
See CONTRIBUTING.md and the Code of Conduct. New signals are especially welcome; please include precision numbers from real traffic if you have them.
Security
Please report vulnerabilities privately. See SECURITY.md.
Credits
License
MIT. See LICENSE.md.