Skip to content

[BE-130] Validate uploaded document files by content, not declared type #989

Description

@mftee

Overview

The documents module accepts land ownership paperwork. A client-declared Content-Type and file extension are trivially spoofed, so neither can be trusted to decide what a file is.

This is distinct from the upload feature work already filed — this issue is specifically about validation and safe storage of whatever is uploaded.

Tasks

  • Add a reusable upload validation pipe: maximum size, MIME allowlist, and magic-byte sniffing to confirm the actual file type.
  • Reject archives, executables, and anything outside the allowlist regardless of extension.
  • Generate randomized storage keys — never use the client-supplied filename as a path component (path traversal).
  • Consider PDF-specific risks: pdf-lib and pdfkit are installed, and PDFs can embed active content. Sanitize or render rather than serving originals inline.
  • Serve downloads with Content-Disposition: attachment and a correct Content-Type, never text/html.
  • Strip metadata from uploaded images, which can carry GPS coordinates.
  • Add tests for each rejection path.

Acceptance Criteria

  • An executable renamed to .pdf is rejected by content inspection.
  • Path traversal via a crafted filename is impossible (covered by a test).
  • Downloads cannot be rendered inline as HTML.

Notes for Contributors

Comment below to be assigned.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions