Skip to content

Bug: JSON.parse without error handling in request body parsingΒ #4711

@RoyRoki

Description

@RoyRoki

πŸ› Bug Report

Severity: High
Category: Error Handling / DoS
Location: src/request.ts

Found by: WhiteRose - AI-powered bug hunter
Bug ID: WR-014


πŸ“‹ Description

JSON.parse() is called without try-catch blocks in request body parsing. Malformed JSON causes uncaught exceptions and crashes the application.


πŸ” Vulnerable Code

// src/request.ts
const body = await this.raw.text()
const parsed = JSON.parse(body)  // ❌ No error handling!

πŸ’₯ Impact

  • Application crashes: Malformed JSON = unhandled exception
  • DoS attacks: Attacker sends invalid JSON to crash server
  • Poor error messages: Users don't get helpful validation errors

πŸ§ͺ Attack Example

curl -X POST http://api.example.com/data \
  -H "Content-Type: application/json" \
  -d "{invalid json here}"

Result: Server crashes with SyntaxError: Unexpected token


βœ… Suggested Fix

Wrap JSON.parse() in try-catch:

async json<T = unknown>(): Promise<T> {
  const body = await this.raw.text()
  
  try {
    return JSON.parse(body)
  } catch (e) {
    throw new HTTPException(400, {
      message: 'Invalid JSON in request body',
      cause: e,
    })
  }
}

πŸ”— References


Found automatically by WhiteRose AI bug hunter - helping make open source more secure πŸ›‘οΈ

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions