Summary#
An upload carries the type its sender CLAIMED — a browser reads it off the name, an agent writes whatever it likes. A function that keeps a file, or serves it, should type it by what the bytes ARE:
var type = File.ContentTypeOf(File.ReadAllBytes(upload.Path), upload.FileName);Signature#
string File.ContentTypeOf(byte[] bytes) // by the bytes alone
string File.ContentTypeOf(byte[] bytes, string name) // a name narrows text, never what the bytes already areDescription#
Needs use Osysharp.Storage;. It reads only its arguments, so it answers the same for the same bytes and name. It
runs on the server, where an upload's bytes are.
How it decides, in order:
| the bytes | answer | the name |
|---|---|---|
| a known signature — PNG, JPEG, GIF, WebP, AVIF, HEIC, PDF, MP4, WebM, Ogg, MP3, FLAC, WAV | that type (image/png, application/pdf, …) | ignored: a PNG named site.css is image/png |
| a ZIP container | application/zip | a ZIP-family name gives its kind: .docx, .xlsx, .pptx, .odt, .ods, .odp, .epub |
text that begins with < | text/html, image/svg+xml or application/xml | ignored: markup never passes as text, a stylesheet or a script |
| other text | text/plain | .css → text/css, .js/.mjs → text/javascript, .json/.map → application/json, .md → text/markdown, .csv, .tsv, .yaml/.yml, .html, .svg, .xml |
| binary with no known signature | application/octet-stream | .woff, .woff2, .ttf, .otf, .ico give their type; a name claiming a kind that HAS a signature (.png) does not |
So a caller cannot make a web page pass as a stylesheet or a picture by naming it so, and a picture sent as
application/octet-stream is still image/png. Compare it with the type the caller claimed when the two must agree:
app Boards { use Osysharp.Storage; }
using Osysharp.Storage;
/// The type an upload really has; refused when the caller claimed a different one.
[AllowAnonymous]
public string TypeOf(UploadedFile upload) {
if (!File.IsOwnUpload(upload.Path)) { throw new ValidationException("That file was uploaded somewhere else."); }
var type = File.ContentTypeOf(File.ReadAllBytes(upload.Path), upload.FileName);
if (upload.ContentType != "" && upload.ContentType != "application/octet-stream" && upload.ContentType != type) {
throw new ValidationException($"{upload.FileName} is {type}, not {upload.ContentType}.");
}
return type;
}[Test]
void A_file_is_what_its_bytes_are() {
byte[] png = [137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13];
Assert.Equal("image/png", File.ContentTypeOf(png, "site.css"));
Assert.Equal("text/javascript", File.ContentTypeOf(Encoding.UTF8.GetBytes("console.log(1);"), "app.js"));
Assert.Equal("text/html", File.ContentTypeOf(Encoding.UTF8.GetBytes("<!doctype html><p>hi</p>"), "notes.txt"));
Assert.Equal("text/plain", File.ContentTypeOf(Encoding.UTF8.GetBytes("just words")));
}See also#
- File.IsOwnUpload and File.SaveUpload — uploads, one or several, and whose they are
- A file's history — supersede, and what each version keeps — keeping a file, and reading each version
- Files (addressing something the app stores) — the two ways the platform stores a file