From 270a296817e703bccca3c100dbfbf8498a1b67dd Mon Sep 17 00:00:00 2001
From: whoswhip
Date: Sat, 14 Nov 2026 16:11:51 -0500
Subject: [PATCH] normalize mime types; application/octet-stream returns an
attachment
---
config.go | 18 ++++++++++--------
handlers.go | 35 ++++++++++++++++++++++++++++++++++-
main.go | 5 +++--
readme.md | 2 +-
templates/faq.html | 19 +++++++++++++------
5 files changed, 61 insertions(+), 18 deletions(-)
diff --git a/config.go b/config.go
index 8cb7b56..7c6d2ea 100644
--- a/config.go
+++ b/config.go
@@ -11,10 +11,11 @@ import (
)
type Config struct {
- UploadDir string
- MaxPasteBytes int64
- MaxFileBytes int64
- Expiries []Expiry
+ UploadDir string
+ MaxPasteBytes int64
+ MaxFileBytes int64
+ Expiries []Expiry
+ NormalizeMIMETypes bool
DatabasePath string
// public host like "https://bin.zip", NOT the address minbin listens on
@@ -56,10 +57,11 @@ func loadConfig() (Config, error) {
}
return Config{
- UploadDir: envOrDefault("MINIBIN_UPLOAD_DIR", "uploads"),
- MaxPasteBytes: maxPasteKiB * 1024,
- MaxFileBytes: maxFileKiB * 1024,
- Expiries: expiries,
+ UploadDir: envOrDefault("MINIBIN_UPLOAD_DIR", "uploads"),
+ MaxPasteBytes: maxPasteKiB * 1024,
+ MaxFileBytes: maxFileKiB * 1024,
+ Expiries: expiries,
+ NormalizeMIMETypes: envOrDefault("MINIBIN_NORMALIZE_MIME_TYPE", "true") == "true",
DatabasePath: envOrDefault("MINIBIN_DATABASE_PATH", "minibin.sqlite"),
Host: envOrDefault("MINIBIN_HOST", "http://localhost:8080"),
diff --git a/handlers.go b/handlers.go
index 7dda853..540fd9b 100644
--- a/handlers.go
+++ b/handlers.go
@@ -332,11 +332,22 @@ func contentHandler(db *gorm.DB, cfg Config, tracker *AccessTracker) http.Handle
if upload.ExpiresAt != nil {
ttl = int(*upload.ExpiresAt - now)
}
+ mimetype := upload.MIMEType
+ if cfg.NormalizeMIMETypes {
+ mimetype = normalizeMIMEType(mimetype)
+ }
+
+ if mimetype == "application/octet-stream" {
+ w.Header().Set(
+ "Content-Disposition",
+ fmt.Sprintf("attachment; filename=\"%s%s\"", upload.ID, filepath.Ext(upload.Filename)),
+ )
+ }
w.Header().Set("Cache-Control", fmt.Sprintf("public, max-age=%d, must-revalidate", ttl))
w.Header().Set("ETag", "\""+hex.EncodeToString(upload.SHA256)+"\"")
w.Header().Set("Content-Security-Policy", "sandbox")
- w.Header().Set("Content-Type", upload.MIMEType)
+ w.Header().Set("Content-Type", mimetype)
w.Header().Set("X-Content-Type-Options", "nosniff")
http.ServeContent(w, r, upload.Filename, time.Unix(upload.CreatedAt, 0), file)
}
@@ -427,3 +438,25 @@ func detectMimeType(file io.ReadSeeker) (string, error) {
return mtype.String(), nil
}
+
+func normalizeMIMEType(contentType string) string {
+ const plaintext = "text/plain; charset=utf-8"
+ mediaType, _, err := mime.ParseMediaType(contentType)
+ if err != nil {
+ return plaintext
+ }
+ if strings.HasSuffix(mediaType, "+xml") {
+ return plaintext
+ }
+ switch mediaType {
+ case "text/html", "text/javascript", "application/javascript",
+ "application/ecmascript", "text/ecmascript", "application/xml",
+ "text/xml", "text/xsl",
+ "text/css", "application/wasm":
+ return plaintext
+ case "application/pdf":
+ return "application/octet-stream"
+ default:
+ return contentType
+ }
+}
diff --git a/main.go b/main.go
index d70a6bb..61d689b 100644
--- a/main.go
+++ b/main.go
@@ -114,8 +114,9 @@ func main() {
mux.HandleFunc("GET /privacy", staticPage(pages, "privacy.html"))
mux.HandleFunc("GET /faq", func(w http.ResponseWriter, r *http.Request) {
render(pages, w, "faq.html", map[string]any{
- "Expiries": config.Expiries,
- "Host": config.Host,
+ "Expiries": config.Expiries,
+ "Host": config.Host,
+ "NormalizeMIMETypes": config.NormalizeMIMETypes,
})
})
mux.HandleFunc("GET /{id}", contentHandler(db, config, tracker))
diff --git a/readme.md b/readme.md
index e63c2c2..d7edd93 100644
--- a/readme.md
+++ b/readme.md
@@ -26,7 +26,7 @@ goals:
- [x] delete content endpoints
- [x] return a page for nojs clients, such as on delete or upload
- [ ] max total storage usage
-- [ ] normalize mime types (like html)
+- [x] normalize mime types (like html)
- converts specific mime types to text/plain to prevent rendering
- [ ] filter mime types config
- prevents uploads with specific mime types
diff --git a/templates/faq.html b/templates/faq.html
index d88743a..4313c2f 100644
--- a/templates/faq.html
+++ b/templates/faq.html
@@ -26,19 +26,26 @@
how do i upload via the api?
- send a POST request to /upload. for files,
- use multipart form data with a field named file.
- for pastes, send the text with Content-Type: text/plain.
+ send a POST request to /upload. for files, use multipart
+ form data with a field named file. for pastes, send the
+ text with Content-Type: text/plain.
- to add an expiry, append ?expiry={value}.
supported values are:
- {{range $i, $expiry := .Expiries}}{{if $i}},{{end}}{{$expiry.Label}}{{end}}.
- omitting an expiry will use the first expiry listed.
+ to add an expiry, append ?expiry={value}.
supported
+ values are:
+ {{range $i, $expiry := .Expiries}}{{if
+ $i}},{{end}}{{$expiry.Label}}{{end}}. omitting an expiry will use the first expiry listed.
uploading the output of a command
cat super-cool-text.txt | curl -T - {{.Host}}/upload
upload a file via curl
curl -F "file=@very-cool-image.png" {{.Host}}/upload
+ {{if .NormalizeMIMETypes}}
+ why are my html/svg/pdf uploads not being rendered?
+ they are being normalized for security.
+ {{end}}