Skip to main content

Insecure file upload

Description

The system allows the upload of files with two extensions or with unsafe ones, such as .html, .php or .exe. Furthermore, the system does not validate whether or not the file is free from malicious code.

Impact

Upload malicious files to the server.

Recommendation

  • Validate with regular expressions that the file has only one extension.
  • Validate with an antivirus software that the file does not contain any type of malicious code.
  • Validate that Content Type corresponds to the files extension before upload it to the server.

Threat

Authenticated attacker from the Internet.

Expected Remediation Time

⌚ 30 minutes.

Score

Default score using CVSS 3.1. It may change depending on the context of the vulnerability.

Base

  • Attack vector: N
  • Attack complexity: L
  • Privileges required: L
  • User interaction: N
  • Scope: U
  • Confidentiality: N
  • Integrity: L
  • Availability: N

Temporal

  • Exploit code madurity: P
  • Remediation level: U
  • Report confidence: C

Result

  • Vector string: CVSS:3.1/AV:N/AC:L/PR:L/UI:N/S:U/C:N/I:L/A:N/E:P/RL:U/RC:C
  • Score:
    • Base: 4.3
    • Temporal: 4.1
  • Severity:
    • Base: Medium
    • Temporal: Medium

Code Examples

Compliant code

Validate data types of files. If the file could be edited, validate the contents through some firewall

var uploadcontrol = document.getElementById('<%=FileUpload1.ClientID%>').value;
def validateFile(str):
regex = "^.*\.(jpg|JPG|gif|GIF|doc|DOC|pdf|PDF)$"
p = re.compile(regex)
if(re.search(p, str)):
return True
else:
return False
if validateFile(uploadControl):
uploadFile(file);

Non compliant code

The application uploads a file without validating data types

router.post("/", function (req, res, next) {
const file = req.body.userFile;
await uploadFile(file);
res.send("200");
});

Requirements