diff --git a/internal/api/attachments.go b/internal/api/attachments.go index 862f544..63fca22 100644 --- a/internal/api/attachments.go +++ b/internal/api/attachments.go @@ -91,6 +91,17 @@ func (l *limitReader) Read(p []byte) (int, error) { return n, err } +// isImageMime reports whether the sniffed mime is a raster image the viewer +// can render inline (#221). SVG is excluded: it is forced to text/plain on +// serving by the active-content rule and must never render as an image. +func isImageMime(mime string) bool { + switch mime { + case "image/png", "image/jpeg", "image/gif", "image/webp": + return true + } + return false +} + // handleCreatePasteMultipart implements POST /api/pastes with // multipart/form-data (#38). Fields mirror the JSON create path; a 'file' // part makes the paste a file paste (1 file = 1 paste: if text content is diff --git a/internal/api/server.go b/internal/api/server.go index dfe1ac4..5a9128f 100644 --- a/internal/api/server.go +++ b/internal/api/server.go @@ -451,6 +451,22 @@ func (a *apiServer) handleRaw(w http.ResponseWriter, r *http.Request) { http.Error(w, "not found", 404) return } + // #221: raw view of an image paste serves the image bytes themselves as + // an image, not the (empty) text content. + if att, err := a.store.GetAttachmentForPaste(row.ID); err == nil && att != nil && isImageMime(att.Mime) { + blobs := a.store.Blobs() + if blobs != nil { + if blob, err := blobs.Get(row.ID + "/" + att.SHA256); err == nil { + defer blob.Close() + a.store.IncrementViews(row.ID, "", 0) // raw views always count (#49/#95) + w.Header().Set("Content-Type", att.Mime) + w.Header().Set("X-Content-Type-Options", "nosniff") + w.Header().Set("Content-Length", fmt.Sprintf("%d", att.Size)) + http.ServeContent(w, r, "", time.Unix(att.CreatedAt, 0), blob) + return + } + } + } // #34: content_type is attacker-controlled via the create API. Serving it // verbatim let a paste be stored with text/html (or image/svg+xml) and // render as active content on this origin when fetched from /raw — diff --git a/internal/web/static/app.css b/internal/web/static/app.css index c96034b..00b4c26 100644 --- a/internal/web/static/app.css +++ b/internal/web/static/app.css @@ -810,15 +810,35 @@ button[type="submit"]:focus-visible, } .file-chip .file-chip-remove:hover { color: var(--danger, #c0392b); } .attachment-bar { display: flex; flex-direction: column; gap: 10px; } -.attachment-chip { +/* #221: the link pill under an image preview stays a small inline chip, + left-aligned under the image, not stretched above it. */ +.attachment-bar .attachment-chip { display: inline-flex; align-items: center; gap: 12px; align-self: flex-start; border: 1px solid var(--border); border-radius: var(--radius); - padding: 8px 16px; text-decoration: none; color: var(--fg); - background: var(--surface-2); font-size: 21.6px; + padding: 6px 14px; text-decoration: none; color: var(--fg); + background: var(--surface-2); font-size: 19px; +} +.attachment-bar .attachment-chip:hover { border-color: var(--accent); } +.attachment-bar .attachment-chip .attachment-name { + max-width: 320px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; +} +/* #221: images scale to fit the viewer box, aspect ratio preserved. */ +.attachment-preview { + max-width: 100%; + align-self: flex-start; + border: 1px solid var(--border); + border-radius: var(--radius); + background: var(--surface-2); + overflow: hidden; +} +.attachment-preview img { + display: block; + max-width: 100%; + max-height: 70vh; + width: auto; + height: auto; + object-fit: contain; } -.attachment-chip:hover { border-color: var(--accent); } -.attachment-chip .attachment-size { color: var(--muted-fg); font-size: 19px; } -.attachment-preview img { max-width: 480px; max-height: 360px; border-radius: var(--radius); border: 1px solid var(--border); } /* #139: CSP-safe replacements for inline style attributes (style-src 'self') */ .hidden { display: none; } diff --git a/internal/web/templates/paste.html b/internal/web/templates/paste.html index b907ded..b549f3b 100644 --- a/internal/web/templates/paste.html +++ b/internal/web/templates/paste.html @@ -39,19 +39,27 @@ {{if .Attachment}}