Skip to content

Add comprehensive input validation and sanitization for HTTP endpoints #9

@HRISHIKESH-hackoff

Description

@HRISHIKESH-hackoff

Problem

The current HTTP endpoints in /cmd/server/handlers.go lack comprehensive input validation and proper URL encoding, which can lead to several issues:

  1. Missing Key Validation: The GET, PUT, and DELETE handlers don't validate key parameters for:

    • Empty strings
    • Excessively long keys (no maximum length enforcement)
    • Special characters that could cause encoding issues
  2. URL Encoding Issue in forwardToLeader(): Keys and values aren't URL-encoded before forwarding to leader:

    • Special characters in keys/values can break request forwarding
    • Example: Keys with &, =, ? characters will cause malformed URLs
  3. Value Size Limits: No validation on value sizes, which could lead to:

    • Memory exhaustion attacks
    • Denial of service
  4. Missing Request Timeout Context: HTTP requests could hang indefinitely

Solution

Implement a reusable validation middleware that:

  • Validates key is not empty and has reasonable length (e.g., max 256 bytes)
  • Validates value size (e.g., max 10MB)
  • Properly URL-encodes parameters in forwarded requests
  • Implements request context with timeout

Files Affected

  • cmd/server/handlers.go
  • cmd/server/proxy.go
  • New file: cmd/server/validation.go

Acceptance Criteria

  • Validation middleware rejects keys > 256 bytes with 400 Bad Request
  • Validation middleware rejects empty keys with 400 Bad Request
  • Validation middleware rejects values > 10MB with 413 Payload Too Large
  • All special characters in keys/values are properly URL-encoded in forwarded requests
  • Request context has timeout to prevent hanging requests

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions