← All Exercises

Reject Uploads That Violate Policy

basicjavascript~20min

Background

Upload endpoints are a common path for stored XSS and disk exhaustion. Checking extension, MIME and size together closes the obvious bypasses.

Problem

Check declared name, size and MIME type of an upload against a policy and return every violated rule as a sorted code.

Input

JSON object: { file: { name, sizeBytes, mime }, policy: { maxBytes, allowedMime, allowedExt } }

Output

JSON array of violation codes, sorted

Sample tests

Sample 1

Input: {"file":{"name":"avatar.exe","sizeBytes":2048,"mime":"text/html"},"policy":{"maxBytes":1024,"allowedMime":["image/png"],"allowedExt":["png"]}}
Expected: ["extension-not-allowed","mime-not-allowed","too-large"]

Sample 2

Input: {"file":{"name":"avatar.png","sizeBytes":512,"mime":"image/png"},"policy":{"maxBytes":1024,"allowedMime":["image/png"],"allowedExt":["png"]}}
Expected: []

Your solution

Learning objective

Enforce upload policy at the boundary before bytes are stored.