File metadata and validation
What does a file claim?
An upload supplies bytes plus metadata, such as a filename and a declared media type (MIME type). Both labels can come from the client. A fictional school accepts plain-text assignment notes: a name ending in .txt and a text/plain header are hints, not proof. The server must decide whether the actual data meets its own contract before releasing it.
Build a narrow acceptance policy
Start with the business need: accept only the formats required. Limit the received bytes while streaming, not just by trusting a declared length. Check normalized extension, declared type, format evidence and actual parser results. A format signature indicates a possible format but does not establish safety. For plain text, there is no universal magic signature; use an explicit encoding and content contract.
Example: two tiny notes
The toy service accepts 1–64 bytes of UTF-8 text with a .txt suffix, text/plain declaration and printable characters or line feeds. The bytes for Hello occupy five bytes and pass. The bytes for سلام occupy eight bytes in UTF-8 and also pass. An empty note, a mismatching type or a failed decoding check is rejected. This is a classroom policy, not a general-purpose text sanitizer.
Avoid false confidence
Successful parsing, antivirus scanning and content transformation answer different questions; none alone proves a file harmless. Keep uncertain files unavailable and record a reason code instead of exposing internal parser errors. Scanning services themselves require isolation and privacy review; do not send private uploads to an external scanner without authorization.
Practice: classify the evidence
For the notes in the offline lab, predict accept or reject. Identify which facts are client claims, which are measured byte counts and which are decoded content. Explain why a successful result is permission to enter the next processing stage, not permission to publish the file.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
File metadata and validation — knowledge check
Apply file security principles to fictional scenarios.
What does text/plain supplied by a client establish?
Why measure bytes while receiving?
Under the toy contract, how many bytes does Hello occupy?
A correctly parsed upload is safe to publish automatically. What is missing?
Why avoid unnecessary external scanning of private files?