Skip to main content

Security

Hyperscape implements multiple layers of security to protect player data and prevent exploits.
Security code lives in packages/client/src/auth/, packages/server/src/infrastructure/auth/, and packages/server/src/systems/ServerNetwork/services/.

Authentication

Privy Integration

Hyperscape uses Privy for secure authentication with support for:
  • Email/SMS — Passwordless login
  • Social OAuth — Google, Twitter, Discord, etc.
  • Wallet Connect — Ethereum and Solana wallets
  • Farcaster — Farcaster Frame integration

Configurable Auth Storage

Auth tokens can be stored in different locations based on security requirements:
Storage Options:
XSS Risk: Tokens stored in browser storage are accessible to JavaScript. For production, consider using sessionStorage or memory storage types.

Async Token Provider

The API client supports async token refresh for fresh tokens:
Fresh Tokens: The async token provider ensures API requests always use fresh tokens from Privy, not stale cached tokens.

URL Parameter Validation

Embedded Mode Security

Embedded mode (iframe integration) now validates all URL parameters to prevent injection attacks:
BREAKING CHANGE: authToken must now be passed via postMessage, not URL parameters. This prevents token exposure in browser history and server logs.

Secure Token Delivery

Auth tokens are now delivered via postMessage instead of URL parameters:
Parent Window Integration:

Content Security Policy

CSP Headers

Hyperscape implements Content Security Policy headers to mitigate XSS attacks:
Why unsafe-inline and unsafe-eval?
  1. Privy SDK: Injects inline styles for modal/popup UI and uses eval for authentication flows
  2. React: Some styled-components patterns use inline styles
  3. Three.js: Shader compilation may use inline scripts
  4. Webpack/Vite: Hot module replacement in development
Future Improvement: Consider migrating to CSP nonces when Privy SDK adds support.

CSP Violation Monitoring

CSP violations are monitored and reported:
Throttling: CSP violation reports are throttled to prevent flooding the server with duplicate reports.

Input Validation

Server-Side Validation

All client inputs are validated server-side to prevent exploits:

Combat Request Validation

Combat requests require timestamp validation to prevent replay attacks:
Timestamp Required: All combat requests must include a timestamp. Requests without timestamps or with invalid timestamps are rejected.

Type Guards

Event payloads use type guards for runtime validation:

Rate Limiting

Combat Rate Limiting

Combat actions are rate-limited to prevent spam:

Global Rate Limits


Anti-Cheat Measures

Server-Authoritative Validation

All game state changes are validated server-side:
Never Trust the Client: All calculations (distance, equipment, ammunition) are performed server-side. Client requests are treated as untrusted input.

Movement Anti-Cheat

Movement is validated to prevent teleportation and speed hacks:

Secure Storage

Client-Side Storage

The secureStorage utility provides safe browser storage access:
Error Handling: All storage operations are wrapped in try-catch to handle private browsing mode, quota exceeded, and other storage failures.

Error Reporting

Unhandled Error Tracking

Unhandled errors and promise rejections are tracked:

CSP Violation Reporting

CSP violations are reported to the server for security monitoring:

Database Security

Parameterized Queries

All database queries use Drizzle ORM with parameterized queries to prevent SQL injection:

Transaction Isolation

Critical operations use database transactions for atomicity:

Audit Logging

Activity Log

Player actions are logged for audit trails:
Logged Events:
  • Player login/logout
  • Item trades
  • Bank transactions
  • Combat kills
  • Admin actions

Best Practices

Client-Side

  1. Never trust client data — Validate everything server-side
  2. Use secure storage — Prefer sessionStorage over localStorage
  3. Validate URL parameters — Use schema-based validation
  4. Report errors — Send CSP violations and unhandled errors to server
  5. Use type guards — Validate event payloads at runtime

Server-Side

  1. Parameterized queries — Always use Drizzle ORM, never raw SQL
  2. Rate limiting — Apply to all player actions
  3. Timestamp validation — Prevent replay attacks
  4. Server-authoritative — Calculate all game state server-side
  5. Audit logging — Log critical actions for investigation