Hardening Laravel Vapor with Managed Firewall Protection
Overview
Managing serverless environments requires more than just scaling code; it demands robust perimeter security.
Prerequisites
To follow this guide, you should be familiar with the
Key Libraries & Tools
- Laravel Vapor: A serverless deployment platform for Laravel.
- Vapor CLI: The command-line interface used to deploy and manage environments.
- Guzzle: A PHP HTTP client often used by bots or scripts to make requests.
Code Walkthrough
To enable the firewall, modify your vapor.yml file. This configuration acts as the blueprint for your environment's security rules.
Setting Rate Limits
Add a firewall section to your environment configuration to limit how many requests a single IP can make within a five-minute window.
id: 1
name: my-app
environments:
production:
firewall:
rate_limit: 100
When a source exceeds 100 requests in 5 minutes, Vapor automatically blocks subsequent attempts, protecting your database and compute resources from exhaustion.
Implementing Bot Control
You can further refine traffic by blocking specific categories of automated agents. This is particularly useful for internal APIs that shouldn't be indexed by search engines.
firewall:
bot_control:
- http_libraries
- search_engines
Syntax Notes
The firewall key must sit under the specific environment block (e.g., production or staging). The bot_control option accepts a list of predefined categories. Always ensure your
Practical Examples
A common use case involves blocking http_libraries. If you run a script using curl against an endpoint protected with this rule, the firewall will reject the traffic immediately. This effectively stops simple scraping scripts from impacting your app.
Tips & Gotchas
- Deployment Required: Changes to
vapor.ymldo not take effect until you runvapor deploy. - Monitoring: Check your environment metrics after enabling these rules. Vapor provides visual feedback on how many requests the firewall has successfully blocked.
- Cooldown: Rate-limited IPs are generally blocked for the remainder of the five-minute sliding window.
