Overview
Connect World implements multi-layer validation to ensure data integrity and prevent fraud. All user inputs are validated at the API boundary before reaching business logic or the database.Validation is applied after sanitization. First clean the input, then validate it against business rules.
Order Validation Pipeline
The order creation endpoint (src/app/api/orders/route.ts:19) implements comprehensive validation:
1. Rate Limiting
First defense against abuse:2. Honeypot Anti-Spam
Silently reject bot submissions using a hidden field:Why fake success? Returning errors would signal to sophisticated bots that they’ve been detected, potentially triggering retry logic. A fake success response wastes their resources instead.
How to implement honeypot in your frontend
How to implement honeypot in your frontend
Add a hidden field to your form:Human users never see or fill this field. Bots that auto-fill forms will populate it, revealing themselves.
3. Field Sanitization
All inputs are sanitized before validation:4. Required Field Validation
Ensure all required fields are present and valid:Business Logic Validation
Plan Validation
Verify the plan exists in the system:Duration Validation
Only allow predefined subscription durations:Limiting valid durations prevents abuse and simplifies pricing logic. Users can only select durations you explicitly support.
Price Tampering Prevention
Critical security check: Verify the submitted amount matches the actual price:Why use Math.abs and 0.01 tolerance?
Why use Math.abs and 0.01 tolerance?
Floating-point arithmetic can introduce tiny rounding errors. Using
Math.abs(amount - expectedPrice) > 0.01 allows for 1-cent rounding differences while still catching fraud attempts.Without this tolerance, legitimate payments might fail due to 9.99999999 not strictly equaling 10.00.Device Count Validation
Ensure device count matches the selected plan:Payment Endpoint Validation
Stripe Payment Intent
The Stripe endpoint (src/app/api/stripe/route.ts:11) validates payment requests:
PayPal Order Creation
The PayPal endpoint (src/app/api/paypal/create-order/route.ts:29) uses identical validation:
Consistency is key: Both payment endpoints use the same validation logic, ensuring uniform security across payment methods.
Validation Best Practices
Validate at the API boundary
Validate at the API boundary
Never trust client-side validation alone. Always validate on the server before processing requests or storing data.
Fail fast with clear errors
Fail fast with clear errors
Return validation errors immediately with HTTP 400 and descriptive messages. This helps developers debug integration issues while preventing invalid data from propagating.
Use allowlists, not blocklists
Use allowlists, not blocklists
Define valid values explicitly (like
VALID_MONTHS) rather than trying to block invalid ones. Allowlists are more secure and easier to maintain.Validate business logic, not just format
Validate business logic, not just format
Format validation (email regex, phone pattern) is not enough. Always validate against business rules: Does this plan exist? Does the price match? Is this combination allowed?
Log validation failures
Log validation failures
Consider logging validation failures for monitoring. Frequent failures from the same IP might indicate an attack or integration bug.
Common Attack Vectors Prevented
| Attack | Prevention |
|---|---|
| Price manipulation | Server-side price validation against source of truth |
| SQL injection | Input sanitization removes SQL metacharacters |
| XSS attacks | HTML tag stripping in all string inputs |
| Bot spam | Honeypot field + rate limiting |
| Invalid data types | Type coercion with strict validation |
| Buffer overflow | Maximum length enforcement on all strings |
| Enumeration attacks | Allowlist validation for enums |
Related Documentation
- Data Sanitization - Input cleaning functions
- Rate Limiting - Request throttling configuration
