Harden private media and backup recovery; add V2 deployment and migration guides
This commit is contained in:
parent
c549c34f60
commit
ffaa1f6afa
19 changed files with 623 additions and 14 deletions
|
|
@ -5,6 +5,7 @@ import { Hono } from 'hono';
|
|||
import { bodyLimit } from 'hono/body-limit';
|
||||
import { z } from 'zod';
|
||||
import { titleSchema } from '../../shared/content.ts';
|
||||
import { isAuthenticated } from '../auth.ts';
|
||||
import { badRequest, HttpError, idParam, readBody, type AppContext } from '../http.ts';
|
||||
import { getClass, mapClass, resolveFolder } from '../services/content.ts';
|
||||
import { deleteDocument, documentPath, enqueue, getDocument, ingestUpload, listChunks, mapDocument, sourceLabel } from '../services/documents.ts';
|
||||
|
|
@ -162,19 +163,35 @@ export function documentRoutes(app: AppContext): Hono {
|
|||
}
|
||||
|
||||
/**
|
||||
* Public image route. Images are addressed by an unguessable 128-bit name, which is what lets
|
||||
* a shared quiz show its pictures to visitors without exposing anything else.
|
||||
* Images are private unless an enabled share contains a reference to them.
|
||||
* Re-check on each request so revoking a share also revokes its embedded images.
|
||||
*/
|
||||
export function mediaRoute(app: AppContext): Hono {
|
||||
const r = new Hono();
|
||||
r.get('/:name', (c) => {
|
||||
const name = c.req.param('name');
|
||||
if (!/^[A-Za-z0-9_-]{16,40}\.(png|jpg|gif|webp)$/.test(name)) throw new HttpError(404, 'not_found', 'Not found');
|
||||
if (!isAuthenticated(app, c)) {
|
||||
const reference = `/media/${name}`;
|
||||
const shared = app.db.get(`
|
||||
SELECT 1 FROM shares s WHERE s.enabled = 1 AND (
|
||||
EXISTS (SELECT 1 FROM questions q JOIN quizzes z ON z.id = q.quiz_id
|
||||
WHERE ((s.kind = 'quiz' AND s.target_id = z.id) OR (s.kind = 'class' AND s.target_id = z.class_id) OR (s.kind = 'folder' AND s.target_id = z.folder_id))
|
||||
AND instr(q.stem || q.options || q.rationale, ?) > 0)
|
||||
OR EXISTS (SELECT 1 FROM cards c JOIN decks d ON d.id = c.deck_id
|
||||
WHERE ((s.kind = 'deck' AND s.target_id = d.id) OR (s.kind = 'class' AND s.target_id = d.class_id) OR (s.kind = 'folder' AND s.target_id = d.folder_id))
|
||||
AND instr(c.front || c.back || c.hint, ?) > 0)
|
||||
OR EXISTS (SELECT 1 FROM notes n
|
||||
WHERE ((s.kind = 'note' AND s.target_id = n.id) OR (s.kind = 'class' AND s.target_id = n.class_id) OR (s.kind = 'folder' AND s.target_id = n.folder_id))
|
||||
AND instr(n.content, ?) > 0)
|
||||
) LIMIT 1`, reference, reference, reference);
|
||||
if (!shared) throw new HttpError(404, 'not_found', 'Not found');
|
||||
}
|
||||
const row = app.db.get<{ mime: string }>('SELECT mime FROM media WHERE id = ?', name);
|
||||
const file = path.join(app.config.mediaDir, name);
|
||||
if (!row || !fs.existsSync(file)) throw new HttpError(404, 'not_found', 'Not found');
|
||||
return new Response(fs.readFileSync(file), {
|
||||
headers: { 'Content-Type': row.mime, 'Cache-Control': 'public, max-age=31536000, immutable', 'X-Content-Type-Options': 'nosniff', 'Content-Security-Policy': "default-src 'none'; sandbox" },
|
||||
headers: { 'Content-Type': row.mime, 'Cache-Control': 'private, no-store', 'X-Content-Type-Options': 'nosniff', 'Content-Security-Policy': "default-src 'none'; sandbox" },
|
||||
});
|
||||
});
|
||||
return r;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue