business-logic-flaws-ai-generated-code
Understand business logic vulnerabilities in AI code including race conditions, integer overflow, and calculation errors that pass functional tests but create security holes. Use this skill when you need to learn about race conditions in AI code, understand integer overflow vulnerabilities, recognize business logic security flaws, or identify calculation errors. Triggers include "race conditions", "business logic vulnerabilities", "integer overflow", "race condition AI", "flash sale security", "concurrent access", "negative totals", "calculation errors".
What this skill does
# Business Logic Vulnerabilities in AI-Generated Code
## The Subtlety of Logic Flaws
According to Contrast Security:
> "Business logic flaws in AI-generated code are particularly insidious because they **often pass all functional tests** while creating significant security vulnerabilities."
These vulnerabilities arise from the AI's **lack of understanding of business context** and security implications.
## 1.5.1 Race Conditions
### The Problem
Race conditions occur when multiple requests access shared resources simultaneously without proper synchronization. The AI generates "correct" code for single-user scenarios but fails to consider concurrent access.
### AI-Generated Vulnerable Code
```javascript
// Prompt: "Implement flash sale with limited quantity"
let availableStock = 100;
app.post('/api/purchase', async (req, res) => {
const { userId, quantity } = req.body;
// ❌ VULNERABLE: Race condition - multiple requests can pass this check
if (availableStock >= quantity) {
// Time window where multiple requests see stock as available
availableStock -= quantity;
// ❌ VULNERABLE: Database update happens after check
await db.orders.create({
userId,
quantity,
timestamp: Date.now()
});
await db.products.update(
{ id: 'flash-sale-item' },
{ stock: availableStock }
);
res.json({ success: true, remaining: availableStock });
} else {
res.status(400).json({ error: 'Insufficient stock' });
}
});
// Attack: Send 100 concurrent requests for 1 item each
// Result: Could sell 200+ items when only 100 available
```
### Why This Is Vulnerable
**The Race Condition Timeline:**
```
Time Request A Request B Stock
T0 Read stock: 100 Read stock: 100 100
T1 Check: 100 >= 1 ✓ Check: 100 >= 1 ✓ 100
T2 stock = 99 stock = 99 100
T3 Update DB: 99 - 99
T4 - Update DB: 99 99
Result: Both purchases succeed, but stock should be 98!
```
**With 100 concurrent requests:**
- All read stock = 100 at T0
- All pass check at T1
- All decrement to 99 at T2
- Final stock = 99 (should be 0)
- **Oversold by 99 items!**
### Real-World Impact
**Flash Sale Scenarios:**
- Limited edition product: 100 units
- 1000 customers try to purchase
- Race condition allows 200+ purchases
- Company must fulfill or refund
- Loss: product cost × oversold quantity
- Reputation damage
**Financial Services:**
- Account balance: $1000
- Concurrent withdrawals of $800 each
- Both succeed (race condition)
- Account balance: -$600
- Bank loses money
**Documented Incident:**
- Major retailer flash sale: 100 units
- 10,000 customers rushed to buy
- Race condition allowed **1,500 purchases**
- Loss: **$500,000** (product cost + shipping + reputation)
### Secure Implementation
#### Option 1: Database Transactions with Locking
```javascript
// ✅ SECURE: Using database transactions and locks
const { Sequelize, Transaction } = require('sequelize');
app.post('/api/purchase', async (req, res) => {
const { userId, quantity } = req.body;
// ✅ SECURE: Use database transaction with row locking
const transaction = await sequelize.transaction({
isolationLevel: Transaction.ISOLATION_LEVELS.SERIALIZABLE
});
try {
// ✅ SECURE: Lock the product row during read
const product = await Product.findOne({
where: { id: 'flash-sale-item' },
lock: transaction.LOCK.UPDATE,
transaction
});
if (product.stock >= quantity) {
// ✅ SECURE: Atomic update
await product.decrement('stock', {
by: quantity,
transaction
});
// Create order
const order = await Order.create({
userId,
productId: product.id,
quantity,
price: product.price * quantity,
timestamp: Date.now()
}, { transaction });
// ✅ SECURE: Commit only if all operations succeed
await transaction.commit();
res.json({
success: true,
orderId: order.id,
remaining: product.stock - quantity
});
} else {
await transaction.rollback();
res.status(400).json({
error: 'Insufficient stock',
available: product.stock
});
}
} catch (error) {
await transaction.rollback();
// ✅ SECURE: Log error securely
logger.error('Purchase failed', {
userId,
error: error.code,
timestamp: new Date().toISOString()
});
res.status(500).json({ error: 'Purchase failed' });
}
});
```
#### Option 2: Redis Distributed Locking
```javascript
// ✅ SECURE: Alternative using Redis for distributed locking
const Redis = require('ioredis');
const Redlock = require('redlock');
const redis = new Redis();
const redlock = new Redlock([redis], {
driftFactor: 0.01,
retryCount: 10,
retryDelay: 200,
retryJitter: 200
});
app.post('/api/purchase-redis', async (req, res) => {
const { userId, quantity } = req.body;
const lockKey = 'lock:flash-sale-item';
try {
// ✅ SECURE: Acquire distributed lock
const lock = await redlock.acquire([lockKey], 5000);
try {
const stock = await redis.get('stock:flash-sale-item');
if (parseInt(stock) >= quantity) {
// ✅ SECURE: Atomic decrement
const newStock = await redis.decrby('stock:flash-sale-item', quantity);
// Record order
await saveOrder(userId, quantity);
res.json({ success: true, remaining: newStock });
} else {
res.status(400).json({ error: 'Insufficient stock' });
}
} finally {
// ✅ SECURE: Always release lock
await lock.release();
}
} catch (error) {
if (error.name === 'LockError') {
res.status(503).json({ error: 'System busy, please retry' });
} else {
res.status(500).json({ error: 'Purchase failed' });
}
}
});
```
### Why AI Generates Race Conditions
**1. Single-Request Testing:**
- AI tests with one request at a time
- Functional test passes: "Can I purchase?" ✓
- Never tests concurrent requests
- Race condition invisible in single-threaded test
**2. Synchronous Thinking:**
- AI generates code as if sequential
- Doesn't reason about concurrent execution
- Treats database as instant (no time gap)
**3. Simplicity Over Correctness:**
- Simple check-then-update pattern
- No transactions (complex to generate)
- No locking (requires understanding concurrency)
---
## 1.5.2 Integer Overflow and Business Logic Flaws
### The Problem
AI generates calculations that work for normal inputs but fail (often catastrophically) with edge cases or malicious inputs.
### AI-Generated Vulnerable Code
```python
# Prompt: "Calculate shopping cart total with discounts"
def calculate_cart_total(items, discount_percent=0):
total = 0
for item in items:
# ❌ VULNERABLE: No validation of negative quantities
subtotal = item['price'] * item['quantity']
total += subtotal
# ❌ VULNERABLE: No validation of discount range
discount_amount = total * (discount_percent / 100)
final_total = total - discount_amount
return final_total
# Attack vectors:
# 1. items = [{'price': 100, 'quantity': -10}] # Negative total
# 2. discount_percent = 150 # Final total becomes negative
# 3. items = [{'price': 999999999, 'quantity': 999999999}] # Integer overflow
```
### Attack Scenarios
**Attack 1: Negative Quantities**
```python
items = [
{'price': 100Related in Security
mac-ops
IncludedComprehensive macOS workstation operations — diagnose kernel panics, identify failing drives, audit launchd startup items, decode wake reasons, triage TCC permission denials, manage APFS snapshots, recover from no-boot. Use for: Mac is slow, slow bootup, won't boot, kernel panic, kernel_task hot, mds_stores CPU, photoanalysisd, cloudd, login loop, gray screen, sleep wake failure, drive failing, IO errors, APFS snapshots eating space, Time Machine local snapshots, Spotlight indexing, launchd, LaunchAgent, LaunchDaemon, login items, TCC permissions, Full Disk Access, Screen Recording denied, Gatekeeper, quarantine, com.apple.quarantine, app is damaged, helper tool, /Library/PrivilegedHelperTools, pmset, wake reasons, dark wake, sysdiagnose, panic.ips, DiagnosticReports, configuration profile, MDM profile, remote diagnostics over SSH.
a11y-audit
IncludedRun accessibility audits on web projects combining automated scanning (axe-core, Lighthouse) with WCAG 2.1 AA compliance mapping, manual check guidance, and structured reporting. Output is configurable: markdown report only, markdown plus machine-readable JSON, or markdown plus issue tracker integration. Use this skill whenever the user mentions "accessibility audit", "a11y audit", "WCAG audit", "accessibility check", "compliance scan", or asks to check a web project for accessibility issues. Also trigger when the user wants to verify WCAG conformance or map findings to a specific standard (CAN-ASC-6.2, EN 301 549, ADA/AODA).
erpclaw
IncludedAI-native ERP system with self-extending OS. Full accounting, invoicing, inventory, purchasing, tax, billing, HR, payroll, advanced accounting (ASC 606/842, intercompany, consolidation), and financial reporting. 413 actions across 14 domains, 43 expansion modules. Constitutional guardrails, adversarial audit, schema migration. Double-entry GL, immutable audit trail, US GAAP.
assess
IncludedAssesses and rates quality 0-10 across multiple dimensions (correctness, maintainability, security, performance, testability, simplicity) with pros/cons analysis. Compares against project conventions and prior decisions from memory. Produces structured evaluation reports with actionable improvement suggestions. Use when evaluating code, designs, architectures, or comparing alternative approaches.
spring-boot-security-jwt
IncludedProvides JWT authentication and authorization patterns for Spring Boot 3.5.x covering token generation with JJWT, Bearer/cookie authentication, database/OAuth2 integration, and RBAC/permission-based access control using Spring Security 6.x. Use when implementing authentication or authorization in Spring Boot applications.
code-hardcode-audit
IncludedDetect hardcoded values, magic numbers, and leaked secrets. TRIGGERS - hardcode audit, magic numbers, PLR2004, secret scanning.