Constrain project thumbnail responses

This commit is contained in:
Benjamin Oezcan
2026-11-29 23:49:29 +01:00
parent e1d88575fb
commit c4328c5449
2 changed files with 16 additions and 1 deletions

View File

@@ -0,0 +1,11 @@
# Projekt-Thumbnails als Bildinhalte begrenzen
**Status:** Umgesetzt
**Betroffene Stelle:** `server.js`, `findThumbnail` und `GET /api/projects/:name/thumb`
Die benutzerdefinierte Thumbnail-Auswahl akzeptierte jede Dateiendung. Die Route lieferte die Datei auf derselben Origin wie die Dashboard-Oberfläche aus. Auch ein unterstütztes SVG kann im Browser direkt als Dokument geöffnet werden.
Die Auswahl nutzt jetzt die vorhandene Liste unterstützter Bildendungen. Die Route setzt `Content-Security-Policy: sandbox; default-src 'none'` sowie `X-Content-Type-Options: nosniff`.
**Geprüft:** Ein harmloses Test-SVG wurde mit dem erwarteten MIME-Typ und beiden Headern geliefert. Bei einer gespeicherten Textdatei als Override verwendete die Route das automatisch erkannte Bild. Das explizit konfigurierte SVG blieb abrufbar. Die weitergehende Pfadprüfung steht in [Aufgabe 03](03-dateigrenzen.md).

View File

@@ -209,7 +209,8 @@ function findThumbnail(name, projectPath) {
const override = readStatusFile(projectPath).thumbnail const override = readStatusFile(projectPath).thumbnail
if (override) { if (override) {
const candidate = path.resolve(projectPath, override) const candidate = path.resolve(projectPath, override)
if (candidate.startsWith(projectPath + path.sep) && fs.existsSync(candidate) && fs.statSync(candidate).isFile()) { if (Object.hasOwn(IMAGE_EXT, path.extname(candidate).toLowerCase()) &&
candidate.startsWith(projectPath + path.sep) && fs.existsSync(candidate) && fs.statSync(candidate).isFile()) {
file = candidate file = candidate
} }
} }
@@ -776,6 +777,9 @@ app.get('/api/projects/:name/thumb', (req, res) => {
const file = findThumbnail(req.params.name, projectPath) const file = findThumbnail(req.params.name, projectPath)
if (!file) return res.status(404).end() if (!file) return res.status(404).end()
res.setHeader('Cache-Control', 'public, max-age=300') res.setHeader('Cache-Control', 'public, max-age=300')
// A project SVG can also be opened directly as a document.
res.setHeader('Content-Security-Policy', "sandbox; default-src 'none'")
res.setHeader('X-Content-Type-Options', 'nosniff')
res.sendFile(file, (err) => { if (err && !res.headersSent) res.status(404).end() }) res.sendFile(file, (err) => { if (err && !res.headersSent) res.status(404).end() })
}) })