Last Updated: November 15, 2025 Version: 1.0.0 Target Audience: Development team, security reviewers
This directory contains comprehensive security guidance for the Sonar Quiz System offline browser application.
Purpose: Comprehensive reference guide covering all security areas
Contents:
- Executive summary
- 6 detailed security areas with implementation patterns:
- Password configuration & environment variables
- XSS prevention (Safe DOM manipulation)
- Browser encryption (Web Crypto API)
- Rate limiting (Client-side authentication)
- Timing attack prevention (Constant-time comparison)
- Cross-tab message validation (BroadcastChannel security)
- Security considerations for each area
- Browser compatibility matrices
- Implementation priority (Phase 1-3)
- Testing & validation checklist
- References & resources
Best For: Understanding the "why" behind each security measure
Time to Read: 30-45 minutes
Purpose: Practical implementation patterns you can use directly
Contents:
- Quick start checklist
- Implementation patterns for each security area
- Simplified, focused code examples
- Setup instructions for each feature
- Integration checklist
- Deployment verification steps
- Common patterns (Safe Input β Store β Display, Rate Limited Actions, etc.)
- Troubleshooting guide
Best For: Developers implementing security features
Time to Read: 20-30 minutes
Purpose: Quick reference for daily development work
Contents:
- Developer security checklist (before each commit)
- Code snippets for common patterns (β DO vs β DON'T)
- Security checklist by phase
- Common security patterns (4 key patterns)
- Dangerous code patterns (red flags)
- Security decision tree
- Browser compatibility matrix
- Pre-commit hook template
- Quick troubleshooting
Best For: Developers during code review and implementation
Time to Read: 5-10 minutes (reference card)
Purpose: Complete test suite you can copy into your project
Contents:
- Testing setup & configuration
- Test 1: XSS Prevention (Lit safety, input sanitization)
- Test 2: Rate Limiting (blocking, exponential backoff)
- Test 3: Timing-Safe Comparison (constant-time verification)
- Test 4: Web Crypto API (encryption/decryption)
- Test 5: BroadcastChannel Signing (message validation)
- Running security tests
- Coverage targets
- CI/CD integration
- Real-world scenarios in each test
Best For: Writing and validating security tests
Time to Read: 25-35 minutes
- Read: SECURITY_QUICK_REFERENCE.md (5 min)
- Skim: SECURITY_BEST_PRACTICES.md sections 1-3 (15 min)
- Reference: SECURITY_IMPLEMENTATION_GUIDE.md as needed (40 min)
- Read: Relevant section in SECURITY_BEST_PRACTICES.md
- Copy: Code from SECURITY_IMPLEMENTATION_GUIDE.md
- Test: Using SECURITY_TEST_EXAMPLES.md
- Check: SECURITY_QUICK_REFERENCE.md before committing
- Open: SECURITY_QUICK_REFERENCE.md
- Check: "Dangerous Code Patterns" section
- Verify: All items on developer checklist
| Area | Best Practices | Implementation | Quick Ref | Tests |
|---|---|---|---|---|
| 1. Environment Variables | β | β | β | Setup only |
| 2. XSS Prevention | β | β | β | β |
| 3. Encryption | β | β | β | β |
| 4. Rate Limiting | β | β | β | β |
| 5. Timing Attacks | β | β | β | β |
| 6. Cross-Tab Messages | β | β | β | β |
Before implementing any security feature:
- Read relevant section in SECURITY_BEST_PRACTICES.md
- Review browser compatibility (all target browsers listed)
- Copy code from SECURITY_IMPLEMENTATION_GUIDE.md
- Adapt tests from SECURITY_TEST_EXAMPLES.md
- Run
npm run test(all tests pass) - Run
npm run lint(no errors) - Run
npm run format:check(code formatted) - Review code with peer using SECURITY_QUICK_REFERENCE.md
- Check against "Dangerous Code Patterns"
- Verify
.envfiles not committed
For immediate deployment
- β Environment variables (.env, Vite define)
- β XSS prevention (Lit templates, sanitizeInput)
- β Input validation (length limits, type checks)
Checklist:
npm run build && npm run audit:secrets # Verify no secrets exposed
npm run test -- xss # XSS tests passFor production hardening
- Rate limiting (max 5 attempts/minute)
- Timing-safe comparison (constantTimeCompare)
- Message validation setup
Checklist:
npm run test -- rate-limiter # Rate limiting tests
npm run test -- timing-safe # Timing tests
npm run test # All tests passFor maximum security
- sessionStorage encryption (optional)
- IndexedDB encryption (optional)
- BroadcastChannel signing (for multi-window)
Checklist:
npm run test -- crypto # Crypto tests
npm run test -- broadcast # Broadcast tests
npm run build && npm run size-check # Verify bundle size# Run all security tests
npm run test -- tests/security
# Run specific test area
npm run test -- xss # XSS tests only
npm run test -- rate-limiter # Rate limiter tests
npm run test -- timing-safe # Timing tests
# Watch mode for development
npm run test:watch -- tests/security
# Check code quality
npm run lint
npm run format:check
npm run build# Run everything
npm run test
npm run lint
npm run format:check
npm run build
npm run audit:secrets
# Verify .env not in git
git check-ignore .env # Should exit 0npm run test # All tests pass
npm run lint # Zero errors
npm run build # Successful build
npm run audit:secrets # No secrets in bundle
npm run size-check # Under 25KB gzippedUse this when reviewing security-related PRs:
- No hardcoded credentials (check for: password, secret, apikey, token)
- No
console.log()of sensitive data - No
innerHTMLwith user input - No
eval()orFunction()with untrusted data - All user input sanitized
- Rate limiting on sensitive actions
- Timing-safe comparison for secrets
-
.envfile NOT committed - Environment variables injected at build time only
- All security tests passing
- New tests for new security features
- Edge cases covered
- Error handling tested
- Real-world scenarios tested
- Build succeeds:
npm run build - No errors:
npm run lint - Secrets audit passes:
npm run audit:secrets - Bundle size OK:
npm run size-check - All tests pass:
npm run test
Why: Attackers can inject malicious scripts through user input How: Lit auto-escapes, sanitizeInput removes dangerous patterns, textContent for plain text Test: See SECURITY_TEST_EXAMPLES.md - XSS tests
Why: Prevents brute-force attacks on authentication How: Track attempts, block after threshold, exponential backoff Test: See SECURITY_TEST_EXAMPLES.md - Rate Limiter tests
Why: Different timing for different answers reveals secret structure How: constantTimeCompare always checks all bytes Test: See SECURITY_TEST_EXAMPLES.md - Timing-Safe Comparison tests
Why: Data at-rest protection if device is stolen How: Web Crypto API with AES-GCM and PBKDF2 derivation Test: See SECURITY_TEST_EXAMPLES.md - Crypto tests
Why: Prevent tampering/replay attacks between browser tabs How: HMAC signatures + nonce + timestamp validation Test: See SECURITY_TEST_EXAMPLES.md - Broadcast Channel tests
All security features are supported in target browsers without polyfills:
| Browser | Version | Support | Notes |
|---|---|---|---|
| Chrome | β₯96 | β Full | Includes Web Components |
| Firefox | β₯102 | β Full | Full Web Crypto support |
| Edge | β₯96 | β Full | Chromium-based |
| Safari | β₯14 | β Full | Web Crypto available |
Security measures add minimal overhead:
| Feature | Impact | Notes |
|---|---|---|
| Input sanitization | <1ms | Per input event |
| XSS prevention | <1ms | Lit built-in |
| Rate limiting | <1ms | In-memory check |
| Constant-time compare | <10ms | Full string check |
| Encryption (optional) | 50-200ms | PBKDF2 intentionally slow |
| HMAC signing | <5ms | Per message |
Result: No user-visible performance degradation
// NEVER commit this
password = 'hardcoded123';
const SECRET = process.env.MY_SECRET;
element.innerHTML = userInput;
if (userCode === CORRECT_CODE) { /* timing leak */ }// ALWAYS use this pattern
// .env file (DO NOT COMMIT)
// vite.config.ts
define: {
__UNLOCK_HASH__: JSON.stringify(env.UNLOCK_HASH)
}
// Usage
const safe = sanitizeInput(userInput);
html`<div>${safe}</div>`; // Lit auto-escapes
if (constantTimeCompare(userHash, storedHash)) { /* safe */ }- Best Practices: Read SECURITY_BEST_PRACTICES.md
- Implementation: Check SECURITY_IMPLEMENTATION_GUIDE.md
- Code Review: Use SECURITY_QUICK_REFERENCE.md
- Testing: See SECURITY_TEST_EXAMPLES.md
- Do NOT create public GitHub issue
- Use GitHub Security Advisory
- Email security team privately
- Follow responsible disclosure (90 days)
- Copy code from SECURITY_IMPLEMENTATION_GUIDE.md
- Adapt tests from SECURITY_TEST_EXAMPLES.md
- Review patterns in SECURITY_QUICK_REFERENCE.md
- Ask team in code review
- CLAUDE.md: Project constitution and constraints
- System_Requirements.md: Functional requirements
- Technical_Design.md: Architecture overview
- Contracts.md: Frozen type definitions
- Delivery_Plan.md: 8-phase plan
- demo/README.md: Manual testing guide
This documentation should be reviewed:
- Quarterly (every 3 months)
- When new browser features arrive
- When security vulnerabilities discovered
- Before each major release
Last Review: November 15, 2025 Next Review: February 15, 2026
| Document | Size | Topics | Code Examples |
|---|---|---|---|
| SECURITY_BEST_PRACTICES.md | 55 KB | 6 areas | 15+ |
| SECURITY_IMPLEMENTATION_GUIDE.md | 20 KB | Setup + patterns | 20+ |
| SECURITY_QUICK_REFERENCE.md | 11 KB | Checklist + reference | 8 |
| SECURITY_TEST_EXAMPLES.md | 30 KB | 5 test suites | 50+ |
| Total | 116 KB | Complete coverage | 90+ examples |
You now have:
β Comprehensive security guide (SECURITY_BEST_PRACTICES.md) β Implementation-ready code (SECURITY_IMPLEMENTATION_GUIDE.md) β Quick reference card (SECURITY_QUICK_REFERENCE.md) β Complete test suite (SECURITY_TEST_EXAMPLES.md) β This index (SECURITY_README.md)
Total: 116 KB of practical, actionable security guidance for your offline browser application.
- First time? β Read SECURITY_QUICK_REFERENCE.md (5 min)
- Need to understand why? β Read SECURITY_BEST_PRACTICES.md (30 min)
- Ready to code? β Use SECURITY_IMPLEMENTATION_GUIDE.md + SECURITY_TEST_EXAMPLES.md (2-3 hours)
- Reviewing code? β Use SECURITY_QUICK_REFERENCE.md checklist (5 min per PR)
Version: 1.0.0 Status: Complete & Production-Ready Last Updated: November 15, 2025 Maintained By: Security Team
Questions? Check the relevant section above or use the "Getting Help" section.