Skip to content

security: Replace Math.random() with cryptographically secure invite code generation #392

@claude

Description

@claude

Summary

Invite codes are generated client-side using Math.random(), which is not cryptographically secure.

Finding

File: app/(app)/(tabs)/profile/index.tsx:169

const code = Math.random().toString(36).substring(2, 10).toUpperCase();

Risk

  • Math.random() produces approximately 41 bits of entropy
  • An attacker could enumerate or predict valid invite codes
  • Invite codes grant ability to link sponsor-sponsee relationships
  • No server-side uniqueness or rate-limiting controls

Recommended Fix

Option 1: Server-side generation (preferred)
Move invite code generation to a Supabase Edge Function or PostgreSQL function:

encode(gen_random_bytes(6), 'hex')

Option 2: Client-side with crypto API
Use expo-crypto or crypto.getRandomValues():

import * as Crypto from 'expo-crypto';
const randomBytes = await Crypto.getRandomBytesAsync(6);
const code = Array.from(randomBytes)
  .map(b => b.toString(16).padStart(2, '0'))
  .join('')
  .toUpperCase()
  .slice(0, 8);

Impact

HIGH - Weak invite codes could allow unauthorized sponsor-sponsee relationship creation.

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingsecuritySecurity related issues or improvements

    Type

    No type

    Projects

    Status

    Todo

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions