← Java / Spring · Contents

JAVA-088 / Intermediate

Missing Content-Disposition on download responses (MIME sniffing)

A03:2021 Injection · CWE-79 · CWE-116

Why is this a problem?

Uploaded files interpreted as HTML execute on the same origin.

How to fix it

java
return ResponseEntity.ok()
  .header(HttpHeaders.CONTENT_DISPOSITION, ContentDisposition.attachment().filename(name).build().toString())
  .header("X-Content-Type-Options", "nosniff")
  .contentType(MediaType.APPLICATION_OCTET_STREAM).body(resource);

Further reading