108 lines
5.6 KiB
TypeScript
108 lines
5.6 KiB
TypeScript
import fs from 'node:fs';
|
|
import os from 'node:os';
|
|
import path from 'node:path';
|
|
import { unzipSync } from 'fflate';
|
|
import { afterEach, describe, expect, it } from 'vitest';
|
|
import { loadConfig } from '../server/config.ts';
|
|
import { createApp } from '../server/app.ts';
|
|
import { encryptSecret, decryptSecret } from '../server/crypto.ts';
|
|
import { openDatabase } from '../server/db.ts';
|
|
import { TINY_PNG } from '../server/documents/testFixtures.ts';
|
|
import { createTestApp, type TestApp } from './helpers.ts';
|
|
|
|
let app: TestApp | undefined;
|
|
const dirs: string[] = [];
|
|
afterEach(() => {
|
|
app?.close();
|
|
app = undefined;
|
|
for (const dir of dirs.splice(0)) fs.rmSync(dir, { recursive: true, force: true });
|
|
});
|
|
const temporary = () => {
|
|
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'studydesk-hardening-test-'));
|
|
dirs.push(dir);
|
|
return dir;
|
|
};
|
|
|
|
describe('private image authorization', () => {
|
|
it('only exposes an embedded image while its content has an enabled share', async () => {
|
|
app = await createTestApp();
|
|
const form = new FormData();
|
|
form.set('file', new File([Buffer.from(TINY_PNG)], 'private.png', { type: 'image/png' }));
|
|
const uploaded = await app.raw('POST', '/api/media', { body: form });
|
|
expect(uploaded.status).toBe(201);
|
|
const { url } = await uploaded.json() as { url: string };
|
|
expect((await app.raw('GET', url)).status).toBe(200);
|
|
expect((await app.raw('GET', url, { cookie: null })).status).toBe(404);
|
|
const cls = (await app.api('POST', '/api/classes', { name: 'Images' })).body.class;
|
|
const deck = (await app.api('POST', '/api/decks', { classId: cls.id, title: 'Picture deck', cards: [{ front: ``, back: 'Answer' }] })).body.deck;
|
|
const share = (await app.api('POST', '/api/shares', { kind: 'deck', targetId: deck.id })).body.share;
|
|
const publicImage = await app.raw('GET', url, { cookie: null });
|
|
expect(publicImage.status).toBe(200);
|
|
expect(publicImage.headers.get('cache-control')).toContain('no-store');
|
|
await app.api('DELETE', `/api/shares/${share.id}`);
|
|
expect((await app.raw('GET', url, { cookie: null })).status).toBe(404);
|
|
const collection = (await app.api('POST', '/api/shares', { kind: 'class', targetId: cls.id })).body.share;
|
|
expect((await app.raw('GET', url, { cookie: null })).status).toBe(200);
|
|
await app.api('PATCH', `/api/shares/${collection.id}`, { enabled: false });
|
|
expect((await app.raw('GET', url, { cookie: null })).status).toBe(404);
|
|
});
|
|
});
|
|
|
|
describe('key durability and restore', () => {
|
|
it('does not serve a sibling directory through an encoded static path', async () => {
|
|
app = await createTestApp();
|
|
const dir = temporary();
|
|
const web = path.join(dir, 'web');
|
|
fs.mkdirSync(web);
|
|
fs.mkdirSync(path.join(dir, 'web-private'));
|
|
fs.writeFileSync(path.join(web, 'index.html'), '<html>Study Desk</html>');
|
|
fs.writeFileSync(path.join(dir, 'web-private', 'secret.txt'), 'private fixture');
|
|
const hono = createApp({ ...app.ctx, config: { ...app.ctx.config, webDir: web } });
|
|
const result = await hono.request('/..%2fweb-private%2fsecret.txt');
|
|
expect(result.status).toBe(404);
|
|
expect(await result.text()).not.toContain('private fixture');
|
|
expect((await hono.request('/%ZZ')).status).toBe(400);
|
|
expect((await hono.request('/classes/example')).status).toBe(200);
|
|
});
|
|
|
|
it('refuses a damaged encryption key without replacing it', () => {
|
|
const dir = temporary();
|
|
const config = loadConfig({ dataDir: dir }, {});
|
|
expect(loadConfig({ dataDir: dir }, {}).secretKey).toEqual(config.secretKey);
|
|
fs.writeFileSync(path.join(dir, 'secret.key'), 'damaged');
|
|
expect(() => loadConfig({ dataDir: dir }, {})).toThrow(/damaged/);
|
|
expect(fs.readFileSync(path.join(dir, 'secret.key'), 'utf8')).toBe('damaged');
|
|
expect(() => loadConfig({ dataDir: dir }, { APP_SECRET: 'short' })).toThrow(/16 characters/);
|
|
fs.unlinkSync(path.join(dir, 'secret.key'));
|
|
fs.writeFileSync(path.join(dir, 'study.db'), 'fixture');
|
|
expect(() => loadConfig({ dataDir: dir }, {})).toThrow(/no secret.key/);
|
|
});
|
|
|
|
it('restores a full backup into a new folder with data and the original encryption key', async () => {
|
|
app = await createTestApp();
|
|
await app.api('POST', '/api/classes', { name: 'Restored class' });
|
|
const encrypted = encryptSecret('test-only-provider-secret', app.ctx.config.secretKey);
|
|
fs.writeFileSync(path.join(app.ctx.config.uploadsDir, 'fixture.txt'), 'Preserved document');
|
|
const res = await app.raw('GET', '/api/backup/download');
|
|
const files = unzipSync(new Uint8Array(await res.arrayBuffer()));
|
|
expect(Object.keys(files)).toEqual(expect.arrayContaining(['study.db', 'secret.key', 'uploads/fixture.txt', 'manifest.json']));
|
|
const dir = temporary();
|
|
for (const [name, bytes] of Object.entries(files)) {
|
|
const file = path.join(dir, name);
|
|
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
fs.writeFileSync(file, bytes);
|
|
}
|
|
const config = loadConfig({ dataDir: dir }, {});
|
|
expect(decryptSecret(encrypted, config.secretKey)).toBe('test-only-provider-secret');
|
|
const { db, report } = openDatabase(config.dbPath, config.backupsDir);
|
|
try {
|
|
expect(report.applied).toEqual([]);
|
|
expect(db.get<{ name: string }>('SELECT name FROM classes')?.name).toBe('Restored class');
|
|
expect(Object.values(db.get('PRAGMA integrity_check') ?? {})).toEqual(['ok']);
|
|
expect(db.all('PRAGMA foreign_key_check')).toEqual([]);
|
|
expect(fs.readFileSync(path.join(config.uploadsDir, 'fixture.txt'), 'utf8')).toBe('Preserved document');
|
|
} finally {
|
|
db.close();
|
|
}
|
|
});
|
|
});
|