Skip to content

[security] OAuth state parameter uses Math.random() — cryptographically weak, vulnerable to CSRF #123

@MehtabSandhu11

Description

@MehtabSandhu11

The generateState() function in both auth.ts and connect.ts uses Math.random():

function generateState(): string {
  return Math.random().toString(36).substring(2, 15);
}

Math.random() is not cryptographically secure. The OAuth state parameter exists specifically to prevent CSRF attacks. Using a predictable value defeats this protection entirely.

Expected behaviour

The state parameter should be generated using crypto.randomBytes() to ensure it is cryptographically unpredictable.

Proposed fix

import crypto from 'crypto';

function generateState(): string {
  return crypto.randomBytes(32).toString('hex');
}

Files to touch

  • apps/backend/src/routes/auth.ts
  • apps/backend/src/routes/connect.ts

GSSoC 2026 — Assignment Request

I would like to work on this issue as part of GirlScript Summer of Code 2026. I have reviewed the codebase and understand the root cause and the fix required.

Could you please assign this issue to me?

GitHub: @MehtabSandhu11

Thank you!

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions