| name | pitfalls-security |
| description | Security patterns for session keys, caching, logging, and environment variables. Use when implementing authentication, caching sensitive data, or setting up logging. Triggers on: session key, private key, cache, logging, secrets, environment variable. |
Security Pitfalls
Common pitfalls and correct patterns for security.
When to Use
- Implementing session key management
- Caching data (especially sensitive)
- Setting up structured logging
- Handling environment variables
- Reviewing security-sensitive code
Workflow
Step 1: Check Key Storage
Verify no private keys stored in plaintext.
Step 2: Verify Cache Safety
Ensure sensitive data not cached inappropriately.
Step 3: Check Logging
Confirm no secrets in logs.
Session Key Security
localStorage.setItem('privateKey', key);
interface SessionKey {
address: Address;
permissions: Permission[];
expiresAt: Date;
maxPerTrade: bigint;
}
import { createCipheriv, randomBytes } from 'crypto';
const iv = randomBytes(16);
const cipher = createCipheriv('aes-256-gcm', key, iv);
await auditLog.create({
action: 'SESSION_KEY_CREATED',
userId,
metadata: { permissions, expiresAt },
});
Environment Variables
const apiUrl = import.meta.env.VITE_API_URL;
const dbUrl = process.env.DATABASE_URL;
console.log('Config:', config);
console.log('Config loaded for:', config.environment);
Caching Strategies
const priceCache = new Map<string, { value: number; expires: number }>();
function getCachedPrice(token: string): number | null {
const cached = priceCache.get(token);
if (cached && cached.expires > Date.now()) {
return cached.value;
}
return null;
}
const CACHE_TTL = {
tokenPrice: 10_000,
poolReserves: 5_000,
gasPrice: 15_000,
userBalance: 30_000,
tokenMetadata: 3600_000,
};
cache.set(`user:${userId}:privateKey`, key);
Structured Logging
const logger = {
info: (message: string, context?: object) => {
console.log(JSON.stringify({
level: 'info',
message,
timestamp: new Date().toISOString(),
...context,
}));
},
error: (message: string, error: Error, context?: object) => {
console.error(JSON.stringify({
level: 'error',
message,
error: error.message,
stack: error.stack,
timestamp: new Date().toISOString(),
...context,
}));
},
};
logger.info('Trade executed', {
userId: 'user123',
txHash: '0x...',
chain: 'ethereum',
profit: ,
});
logger.(, { : process.. });
Audit Logging
await auditLog.create({
action: 'TRADE_EXECUTED',
userId,
before: previousState,
after: newState,
timestamp: new Date(),
metadata: { txHash, chain },
});
Quick Checklist