Upload security
On This Lesson 4 topics
This lesson is planned. The outline below lists the topics it will cover. The full lesson has not been published yet.
In this lesson
name the main upload risks.
Planned topics
- Path traversal: a name like
../../etc/passwdescapes the folder. Prevent it by never using the client name in a path; if you must resolve a path,normalize()it and check it stays inside the base directory. - Oversized files: exhaust disk or memory. Enforce limits.
- Content sniffing: a browser may guess a type and run a file as script. Send the right
Content-Type, andX-Content-Type-Options: nosniff. - Trusting the declared type: verify signatures for important types.
- Serve uploads only through your controller, with access checks.
Draft outline details
Demonstration
Path target = base.resolve(storedName).normalize();
if (!target.startsWith(base)) throw new IllegalArgumentException("Bad path");Check your understanding
Which input should never be part of a path?
Show answer
The client's filename.
Practice
Apply this lesson with the module's lab, exercises and quiz.
Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.