darvis/livewire-injection-stopper
Installation:
composer require darvis/livewire-injection-stopper
No additional configuration is required for spam bot blocking—it activates immediately.
First Use Case:
php artisan livewire:audit
This outputs a list of public/protected properties in your Livewire components that could be manipulated by attackers (e.g., $isAdmin, $userRole).Where to Look First:
livewire:audit for immediate action items.app/Http/Middleware/BlockSpamBots.php (extendable for custom rules).app/Exceptions/HandleLivewireInjectionErrors.php to customize how bot-driven errors are silenced.Spam Bot Blocking:
BlockSpamBots middleware to whitelist/blacklist specific user agents or IPs:
// app/Http/Middleware/BlockSpamBots.php
protected function isSpamBot(Request $request): bool {
return $request->userAgent() === 'Python-urllib/3.11' ||
parent::isSpamBot($request);
}
Livewire Security Auditing:
app/Console/Kernel.php to run audits daily:
protected function schedule(Schedule $schedule) {
$schedule->command('livewire:audit')->daily();
}
# .github/workflows/security.yml
jobs:
audit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: composer install
- run: php artisan livewire:audit
Error Suppression:
CannotUpdateLockedPropertyException are caught and logged silently. To customize:
// app/Exceptions/HandleLivewireInjectionErrors.php
public function report(Throwable $exception) {
if ($exception instanceof \Livewire\Exceptions\CannotUpdateLockedPropertyException) {
\Log::warning('Bot attempted to manipulate Livewire property', [
'exception' => $exception,
'user_agent' => request()->userAgent()
]);
return; // Prevent Sentry from reporting
}
parent::report($exception);
}
public properties sparingly. Prefer:
// Secure: Protected property (not directly manipulable via URL)
protected $isAdmin = false;
// Unsafe: Public property (exposed to injection)
public $isAdmin = false;
$rules validation:
use Livewire\Attributes\Rule;
public $email;
#[Rule('required|email')]
public function rules() { return []; }
False Positives in Audits:
$searchQuery for a search component).use Livewire\Attributes\IgnoreInjectionCheck;
#[IgnoreInjectionCheck]
public $searchQuery;
Over-Silencing Errors:
CannotUpdateLockedPropertyException errors might hide legitimate issues.single channel in config/logging.php):
'channels' => [
'injection_attempts' => [
'driver' => 'single',
'path' => storage_path('logs/injection_attempts.log'),
'level' => 'warning',
],
],
Performance Impact:
artisan livewire:audit --profile # If profiling becomes available
Livewire 3.x Compatibility:
public properties). If upgrading to Livewire 3.x:BlockSpamBots middleware to match Livewire 3’s property system.Audit False Negatives:
composer dump-autoload
config/livewire.php under component_path.Middleware Debugging:
// app/Http/Kernel.php
protected $middleware = [
// ... other middleware
// \Darvis\LivewireInjectionStopper\Http\Middleware\BlockSpamBots::class, // Comment out
];
Custom Spam Detection:
isSpamBot() in BlockSpamBots to use IP reputation services (e.g., AbuseIPDB):
use AbuseFilter\AbuseFilter;
protected function isSpamBot(Request $request): bool {
$ip = $request->ip();
$filter = new AbuseFilter('your_api_key');
return $filter->check($ip) || parent::isSpamBot($request);
}
Audit Customization:
// app/Providers/AppServiceProvider.php
use Darvis\LivewireInjectionStopper\Scanners\LivewireScanner;
public function boot() {
LivewireScanner::macro('checkForSqlKeywords', function ($properties) {
foreach ($properties as $property) {
if (preg_match('/(select|insert|drop)/i', $property)) {
return [$property, 'Potential SQL keyword in property name'];
}
}
return [];
});
}
Error Handling Granularity:
// app/Exceptions/HandleLivewireInjectionErrors.php
public function report(Throwable $exception) {
if ($exception instanceof \TypeError && $this->isBotRequest()) {
$exception->setContext(['source' => 'bot_injection']);
}
}
How can I help you explore Laravel packages today?