normalize mime types; application/octet-stream returns an attachment
This commit is contained in:
@@ -11,10 +11,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
UploadDir string
|
UploadDir string
|
||||||
MaxPasteBytes int64
|
MaxPasteBytes int64
|
||||||
MaxFileBytes int64
|
MaxFileBytes int64
|
||||||
Expiries []Expiry
|
Expiries []Expiry
|
||||||
|
NormalizeMIMETypes bool
|
||||||
|
|
||||||
DatabasePath string
|
DatabasePath string
|
||||||
// public host like "https://bin.zip", NOT the address minbin listens on
|
// public host like "https://bin.zip", NOT the address minbin listens on
|
||||||
@@ -56,10 +57,11 @@ func loadConfig() (Config, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return Config{
|
return Config{
|
||||||
UploadDir: envOrDefault("MINIBIN_UPLOAD_DIR", "uploads"),
|
UploadDir: envOrDefault("MINIBIN_UPLOAD_DIR", "uploads"),
|
||||||
MaxPasteBytes: maxPasteKiB * 1024,
|
MaxPasteBytes: maxPasteKiB * 1024,
|
||||||
MaxFileBytes: maxFileKiB * 1024,
|
MaxFileBytes: maxFileKiB * 1024,
|
||||||
Expiries: expiries,
|
Expiries: expiries,
|
||||||
|
NormalizeMIMETypes: envOrDefault("MINIBIN_NORMALIZE_MIME_TYPE", "true") == "true",
|
||||||
|
|
||||||
DatabasePath: envOrDefault("MINIBIN_DATABASE_PATH", "minibin.sqlite"),
|
DatabasePath: envOrDefault("MINIBIN_DATABASE_PATH", "minibin.sqlite"),
|
||||||
Host: envOrDefault("MINIBIN_HOST", "http://localhost:8080"),
|
Host: envOrDefault("MINIBIN_HOST", "http://localhost:8080"),
|
||||||
|
|||||||
+34
-1
@@ -332,11 +332,22 @@ func contentHandler(db *gorm.DB, cfg Config, tracker *AccessTracker) http.Handle
|
|||||||
if upload.ExpiresAt != nil {
|
if upload.ExpiresAt != nil {
|
||||||
ttl = int(*upload.ExpiresAt - now)
|
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("Cache-Control", fmt.Sprintf("public, max-age=%d, must-revalidate", ttl))
|
||||||
w.Header().Set("ETag", "\""+hex.EncodeToString(upload.SHA256)+"\"")
|
w.Header().Set("ETag", "\""+hex.EncodeToString(upload.SHA256)+"\"")
|
||||||
w.Header().Set("Content-Security-Policy", "sandbox")
|
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")
|
w.Header().Set("X-Content-Type-Options", "nosniff")
|
||||||
http.ServeContent(w, r, upload.Filename, time.Unix(upload.CreatedAt, 0), file)
|
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
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -114,8 +114,9 @@ func main() {
|
|||||||
mux.HandleFunc("GET /privacy", staticPage(pages, "privacy.html"))
|
mux.HandleFunc("GET /privacy", staticPage(pages, "privacy.html"))
|
||||||
mux.HandleFunc("GET /faq", func(w http.ResponseWriter, r *http.Request) {
|
mux.HandleFunc("GET /faq", func(w http.ResponseWriter, r *http.Request) {
|
||||||
render(pages, w, "faq.html", map[string]any{
|
render(pages, w, "faq.html", map[string]any{
|
||||||
"Expiries": config.Expiries,
|
"Expiries": config.Expiries,
|
||||||
"Host": config.Host,
|
"Host": config.Host,
|
||||||
|
"NormalizeMIMETypes": config.NormalizeMIMETypes,
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
mux.HandleFunc("GET /{id}", contentHandler(db, config, tracker))
|
mux.HandleFunc("GET /{id}", contentHandler(db, config, tracker))
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ goals:
|
|||||||
- [x] delete content endpoints
|
- [x] delete content endpoints
|
||||||
- [x] return a page for nojs clients, such as on delete or upload
|
- [x] return a page for nojs clients, such as on delete or upload
|
||||||
- [ ] max total storage usage
|
- [ ] 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
|
- converts specific mime types to text/plain to prevent rendering
|
||||||
- [ ] filter mime types config
|
- [ ] filter mime types config
|
||||||
- prevents uploads with specific mime types
|
- prevents uploads with specific mime types
|
||||||
|
|||||||
+13
-6
@@ -26,19 +26,26 @@
|
|||||||
</p>
|
</p>
|
||||||
<h3>how do i upload via the api?</h3>
|
<h3>how do i upload via the api?</h3>
|
||||||
<p>
|
<p>
|
||||||
send a POST request to <code>/upload</code>. for files,
|
send a POST request to <code>/upload</code>. for files, use multipart
|
||||||
use multipart form data with a field named <code>file</code>.
|
form data with a field named <code>file</code>. for pastes, send the
|
||||||
for pastes, send the text with <code>Content-Type: text/plain</code>.
|
text with <code>Content-Type: text/plain</code>.
|
||||||
</p>
|
</p>
|
||||||
<p>
|
<p>
|
||||||
to add an expiry, append <code>?expiry={value}</code>. <br>supported values are:
|
to add an expiry, append <code>?expiry={value}</code>. <br />supported
|
||||||
<code>{{range $i, $expiry := .Expiries}}{{if $i}},{{end}}{{$expiry.Label}}{{end}}</code>.
|
values are:
|
||||||
omitting an expiry will use the first expiry listed.
|
<code
|
||||||
|
>{{range $i, $expiry := .Expiries}}{{if
|
||||||
|
$i}},{{end}}{{$expiry.Label}}{{end}}</code
|
||||||
|
>. omitting an expiry will use the first expiry listed.
|
||||||
</p>
|
</p>
|
||||||
<h4>uploading the output of a command</h4>
|
<h4>uploading the output of a command</h4>
|
||||||
<code>cat super-cool-text.txt | curl -T - {{.Host}}/upload</code>
|
<code>cat super-cool-text.txt | curl -T - {{.Host}}/upload</code>
|
||||||
<h4>upload a file via curl</h4>
|
<h4>upload a file via curl</h4>
|
||||||
<code>curl -F "file=@very-cool-image.png" {{.Host}}/upload</code>
|
<code>curl -F "file=@very-cool-image.png" {{.Host}}/upload</code>
|
||||||
|
{{if .NormalizeMIMETypes}}
|
||||||
|
<h3>why are my html/svg/pdf uploads not being rendered?</h3>
|
||||||
|
<p>they are being normalized for security.</p>
|
||||||
|
{{end}}
|
||||||
</main>
|
</main>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
Reference in New Issue
Block a user