Web Application Firewall (WAF) rules are a critical Layer 7 defense, providing granular control over HTTP/S traffic directed at your applications. Unlike network firewalls, WAFs inspect the actual content of web requests and responses, identifying and mitigating threats like SQL injection, cross-site scripting (XSS), and directory traversal. Cloud Architects leverage managed WAF rulesets, such as those based on the OWASP Core Rule Set, as a baseline, and augment them with custom rules to address application-specific vulnerabilities, enforce API contracts, and block known malicious patterns unique to their environment. Effective WAF deployment requires continuous tuning to minimize false positives while maintaining robust protection.
Key Takeaways
- WAF rules provide Layer 7 application-specific protection against web exploits.
- DDoS protection safeguards service availability against volumetric and application-layer attacks.
- Cloud Armor (GCP) and AWS Shield (AWS) are integrated, managed services for WAF and DDoS mitigation.
- Architects must use both managed and custom rules for comprehensive, tailored security policies.
Code Example
gcloud compute security-policies rules create 100 \
--security-policy=my-app-security-policy \
--expression="request.path.matches('/admin.*') && !source.ip.in('192.168.1.0/24')" \
--action=deny-403 \
--description="Block external access to admin paths" \
--priority=100How this code works
This code creates a critical Web Application Firewall (WAF) rule designed to protect sensitive administration paths on an application from unauthorized external access. Its job is to ensure that only requests originating from a specific internal network can reach any /admin section, significantly reducing the attack surface for common exploits and brute-force attempts on admin interfaces. By setting up this barrier, the application gains a fundamental layer of defense against malicious actors trying to access privileged areas.
The command begins with gcloud compute security-policies rules create to define a new rule within the my-app-security-policy, assigning it a priority of 100. The core logic resides in the --expression, which checks two conditions: request.path.matches('/admin.*') identifies any request path starting with /admin, and !source.ip.in('192.168.1.0/24') specifically excludes requests coming from the 192.168.1.0/24 internal IP range. The ! before source.ip.in is a subtle but vital detail, performing a logical "NOT" operation. If both conditions are true—meaning an external request tries to access an admin path—the --action=deny-403 rejects the request with a "Forbidden" error. The --priority value of 100 is crucial; rules are evaluated from lowest priority number first, so a rule with a lower priority (e.g., 50) that permitted a request would be processed first, potentially overriding this rule if not carefully considered.