fix: sechs Befunde aus dem ersten Start des Servers
Das Grundgeruest war typgeprueft, aber nie gelaufen. Beim ersten Start
haben sich sechs Dinge gezeigt, alle nachgemessen statt vermutet:
1. Die Token-Pruefung hing an '*' und galt damit auch fuer /api/dash/*,
das daneben liegt. Das Dashboard bekam "Token fehlt oder ist
unbekannt" auf die Anmeldung, obwohl es nie einen Token haben kann.
Sie haengt jetzt an den drei eigenen Pfaden.
2. DELETE /api/media/... und GET /api/exists/... sahen am Ziel vorbei.
c.req.path traegt den Einhaengepunkt mit, das replace(/^\/media\//)
schnitt ihn nicht weg -- aus vehicles/adder.png wurde
api/media/vehicles/adder.png. Loeschen fand nie etwas, exists meldete
immer false. Jetzt :pfad{.+}; Hono liefert den Parameter fertig
dekodiert (an einer Probe gemessen), ein zweites decodeURIComponent
waere eine Dekodierung zu viel.
3. Verzeichnisdurchstieg in der SPA-Rueckfallroute: GET /..%5Cpackage.json
hat unter Windows die Datei ausgeliefert. Hono reicht %5C durch,
path.join behandelt den Backslash dort als Trenner, und eine
Eindaemmung gab es nicht. Unter Linux traegt genau dieser Angriff
nicht -- Glueck, keine Abwehr. Jetzt dieselbe resolve-Pruefung wie in
storage.ts.
4. Die Auskunft "Oberflaeche ist nicht gebaut" war unerreichbar.
createReadStream meldet eine fehlende Datei asynchron, das try/catch
darum fing nichts. Ergebnis war ein leerer 200 samt ENOENT im Log.
5. CSS und JS kamen als application/octet-stream -- die MIME-Tabelle
kennt nur Medientypen, das Dashboard haette weder Stylesheet noch
Modul geladen. Die Oberflaeche bekommt eine eigene Tabelle: in der
geteilten fehlt html mit Absicht, sonst koennte jeder mit einem
Upload-Token eine Seite unter fivecdn.d4rkst3r.de veroeffentlichen.
6. Kaputtes JSON endete als nackter "Internal Server Error".
Ausgerechnet das -- ein 500 ohne ein Wort dazu ist der Fehler, wegen
dem wir hier neu bauen. Jetzt 400 mit Text.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
+29
-11
@@ -6,6 +6,7 @@
|
||||
// Rueckmeldung wie ein kaputter Knopf aussieht.
|
||||
|
||||
import { Hono } from 'hono'
|
||||
import type { Context } from 'hono'
|
||||
import { deleteCookie, getCookie, setCookie } from 'hono/cookie'
|
||||
import { config } from '../config.js'
|
||||
import { db, now, pruneSessions, type Media, type Token, type User } from '../db.js'
|
||||
@@ -25,13 +26,27 @@ type Vars = { user: User }
|
||||
|
||||
export const dashRoutes = new Hono<{ Variables: Vars }>()
|
||||
|
||||
/** Den JSON-Rumpf lesen — oder null, wenn keiner ankam.
|
||||
*
|
||||
* c.req.json() wirft bei kaputtem JSON, und Hono macht daraus einen nackten
|
||||
* "Internal Server Error" ohne ein Wort dazu. Genau diese Sorte Antwort ist
|
||||
* der Grund, warum wir hier neu bauen. */
|
||||
async function jsonBody<T>(c: Context<any>): Promise<T | null> {
|
||||
try {
|
||||
return await c.req.json<T>()
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
const KEIN_JSON = { error: 'Rumpf ist kein gueltiges JSON' } as const
|
||||
|
||||
// ---------------------------------------------------------------- Anmeldung
|
||||
|
||||
dashRoutes.post('/auth/login', async (c) => {
|
||||
const { username, password } = await c.req.json<{
|
||||
username?: string
|
||||
password?: string
|
||||
}>()
|
||||
const body = await jsonBody<{ username?: string; password?: string }>(c)
|
||||
if (!body) return c.json(KEIN_JSON, 400)
|
||||
const { username, password } = body
|
||||
|
||||
if (!username || !password) {
|
||||
return c.json({ error: 'Benutzername und Passwort noetig' }, 400)
|
||||
@@ -81,10 +96,9 @@ dashRoutes.use('*', async (c, next) => {
|
||||
})
|
||||
|
||||
dashRoutes.post('/auth/password', async (c) => {
|
||||
const { current, next: fresh } = await c.req.json<{
|
||||
current?: string
|
||||
next?: string
|
||||
}>()
|
||||
const body = await jsonBody<{ current?: string; next?: string }>(c)
|
||||
if (!body) return c.json(KEIN_JSON, 400)
|
||||
const { current, next: fresh } = body
|
||||
const user = c.get('user')
|
||||
|
||||
if (!current || !fresh) return c.json({ error: 'beide Passwoerter noetig' }, 400)
|
||||
@@ -145,7 +159,9 @@ dashRoutes.delete('/media/:id', async (c) => {
|
||||
|
||||
/** Mehrere auf einmal — bei 900 Fahrzeugbildern will niemand 900 Mal klicken. */
|
||||
dashRoutes.post('/media/delete', async (c) => {
|
||||
const { ids } = await c.req.json<{ ids?: number[] }>()
|
||||
const body = await jsonBody<{ ids?: number[] }>(c)
|
||||
if (!body) return c.json(KEIN_JSON, 400)
|
||||
const { ids } = body
|
||||
if (!Array.isArray(ids) || ids.length === 0) {
|
||||
return c.json({ error: 'keine Auswahl' }, 400)
|
||||
}
|
||||
@@ -197,11 +213,13 @@ dashRoutes.get('/tokens', (c) => {
|
||||
})
|
||||
|
||||
dashRoutes.post('/tokens', async (c) => {
|
||||
const { name, prefix, canDelete } = await c.req.json<{
|
||||
const body = await jsonBody<{
|
||||
name?: string
|
||||
prefix?: string
|
||||
canDelete?: boolean
|
||||
}>()
|
||||
}>(c)
|
||||
if (!body) return c.json(KEIN_JSON, 400)
|
||||
const { name, prefix, canDelete } = body
|
||||
|
||||
if (!name?.trim()) return c.json({ error: 'Name fehlt' }, 400)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user