Scan borné du système de fichiers (racines configurables, défaut home ; profondeur/nombre/timeout bornés ; symlinks non suivis ; exclusions node_modules/dotdirs) qui auto-enregistre les nouveaux dépôts. Insertion atomique ON CONFLICT DO NOTHING (idempotence + anti-résurrection d'un dépôt masqué + anti-course). Scan au démarrage + bouton manuel + re-scan périodique (RepoDiscoveryService, démarré dans runDaemon). Colonne repos.hidden : masquer = conservé en DB mais exclu du dashboard et jamais ré-ajouté ; supprimer = re-découvrable. UI : bouton œil par dépôt + bascule afficher-les-masqués sur le dashboard, section Découverte dans les Réglages (racines + intervalle, allow-list stricte). Robustesse : listAllWorktrees tolère l'échec git par dépôt ; flag --no-discover (escape hatch + hermétisme des acceptations). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
164 lines
7.2 KiB
TypeScript
164 lines
7.2 KiB
TypeScript
// Routes Réglages : GET expose la config non sensible (jamais les secrets), PATCH n'écrit que
|
||
// l'allow-list et valide l'URL Gitea (http/https only, anti-XSS).
|
||
import { mkdtempSync, rmSync } from 'node:fs';
|
||
import { tmpdir } from 'node:os';
|
||
import { join } from 'node:path';
|
||
import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest';
|
||
import { buildApp, type AppBundle } from '../src/app.js';
|
||
import { getSetting, openDb, type Db } from '../src/db/index.js';
|
||
import type { Config } from '../src/config.js';
|
||
import type { SettingsResponse } from '@arboretum/shared';
|
||
|
||
vi.mock('node:child_process', () => ({ execFileSync: () => '/usr/bin/claude\n' }));
|
||
vi.mock('@homebridge/node-pty-prebuilt-multiarch', () => {
|
||
class FakePty {
|
||
pid = 424242;
|
||
write = vi.fn();
|
||
resize = vi.fn();
|
||
pause = vi.fn();
|
||
resume = vi.fn();
|
||
kill = vi.fn();
|
||
onData(): { dispose: () => void } {
|
||
return { dispose: () => {} };
|
||
}
|
||
onExit(): { dispose: () => void } {
|
||
return { dispose: () => {} };
|
||
}
|
||
}
|
||
return { default: { spawn: (): FakePty => new FakePty() } };
|
||
});
|
||
|
||
process.env.ARBORETUM_LOG = 'silent';
|
||
|
||
let dir: string;
|
||
let bundle: AppBundle;
|
||
let db: Db;
|
||
let token: string;
|
||
|
||
const auth = (): { authorization: string } => ({ authorization: `Bearer ${token}` });
|
||
|
||
beforeAll(() => {
|
||
dir = mkdtempSync(join(tmpdir(), 'arboretum-settings-'));
|
||
const dbPath = join(dir, 'settings.db');
|
||
db = openDb(dbPath);
|
||
const config: Config = {
|
||
port: 9999,
|
||
bind: '127.0.0.1',
|
||
dbPath,
|
||
dataDir: dir,
|
||
allowedOrigins: ['https://host.tailnet.ts.net'],
|
||
printToken: false,
|
||
claudeProjectsDir: join(dir, 'claude', 'projects'),
|
||
claudeSessionsDir: join(dir, 'claude', 'sessions'),
|
||
vapidContact: 'mailto:test@localhost',
|
||
};
|
||
bundle = buildApp(config, db, '1.2.3-test');
|
||
const t = bundle.auth.ensureBootstrapToken();
|
||
if (!t) throw new Error('bootstrap token attendu');
|
||
token = t;
|
||
});
|
||
|
||
afterAll(async () => {
|
||
await bundle.app.close();
|
||
db.close();
|
||
rmSync(dir, { recursive: true, force: true });
|
||
});
|
||
|
||
describe('GET /api/v1/settings', () => {
|
||
it('renvoie la config serveur non sensible et giteaUrl null par défaut', async () => {
|
||
const res = await bundle.app.inject({ method: 'GET', url: '/api/v1/settings', headers: auth() });
|
||
expect(res.statusCode).toBe(200);
|
||
const body = res.json() as SettingsResponse;
|
||
expect(body.server.version).toBe('1.2.3-test');
|
||
expect(body.server.port).toBe(9999);
|
||
expect(body.server.bind).toBe('127.0.0.1');
|
||
expect(body.server.allowedOrigins).toEqual(['https://host.tailnet.ts.net']);
|
||
expect(body.server.vapidPublicKey).toBeTruthy(); // clé publique = sûre à exposer
|
||
expect(body.settings.giteaUrl).toBeNull();
|
||
// défauts de découverte : home (1 racine) + intervalle 5 min
|
||
expect(Array.isArray(body.settings.scanRoots)).toBe(true);
|
||
expect(body.settings.scanRoots).toHaveLength(1);
|
||
expect(body.settings.scanIntervalMin).toBe(5);
|
||
});
|
||
|
||
it('n’expose AUCUN secret (server_secret, clé privée VAPID)', async () => {
|
||
const res = await bundle.app.inject({ method: 'GET', url: '/api/v1/settings', headers: auth() });
|
||
const raw = res.body;
|
||
const secret = getSetting(db, 'server_secret');
|
||
const vapidPrivate = getSetting(db, 'vapid_private');
|
||
expect(secret).toBeTruthy();
|
||
expect(raw).not.toContain(secret as string);
|
||
expect(raw).not.toContain(vapidPrivate as string);
|
||
expect(raw).not.toMatch(/server_secret|vapid_private|privateKey/);
|
||
});
|
||
});
|
||
|
||
describe('PATCH /api/v1/settings', () => {
|
||
it('enregistre une URL Gitea valide et la renvoie', async () => {
|
||
const res = await bundle.app.inject({ method: 'PATCH', url: '/api/v1/settings', headers: auth(), payload: { giteaUrl: 'https://git.lidge.fr' } });
|
||
expect(res.statusCode).toBe(200);
|
||
expect((res.json() as SettingsResponse).settings.giteaUrl).toBe('https://git.lidge.fr/');
|
||
// persisté
|
||
const get = await bundle.app.inject({ method: 'GET', url: '/api/v1/settings', headers: auth() });
|
||
expect((get.json() as SettingsResponse).settings.giteaUrl).toBe('https://git.lidge.fr/');
|
||
});
|
||
|
||
it('efface l’URL avec null ou chaîne vide', async () => {
|
||
await bundle.app.inject({ method: 'PATCH', url: '/api/v1/settings', headers: auth(), payload: { giteaUrl: 'https://git.lidge.fr' } });
|
||
const res = await bundle.app.inject({ method: 'PATCH', url: '/api/v1/settings', headers: auth(), payload: { giteaUrl: null } });
|
||
expect((res.json() as SettingsResponse).settings.giteaUrl).toBeNull();
|
||
});
|
||
|
||
it('rejette une URL non http(s) — anti-XSS (400)', async () => {
|
||
const res = await bundle.app.inject({ method: 'PATCH', url: '/api/v1/settings', headers: auth(), payload: { giteaUrl: 'javascript:alert(1)' } });
|
||
expect(res.statusCode).toBe(400);
|
||
const notUrl = await bundle.app.inject({ method: 'PATCH', url: '/api/v1/settings', headers: auth(), payload: { giteaUrl: 'pas une url' } });
|
||
expect(notUrl.statusCode).toBe(400);
|
||
});
|
||
|
||
it('ignore toute clé hors allow-list (ne touche pas aux secrets)', async () => {
|
||
const before = getSetting(db, 'server_secret');
|
||
await bundle.app.inject({ method: 'PATCH', url: '/api/v1/settings', headers: auth(), payload: { server_secret: 'pwned', vapid_private: 'pwned' } });
|
||
expect(getSetting(db, 'server_secret')).toBe(before); // inchangé
|
||
expect(getSetting(db, 'vapid_private')).not.toBe('pwned');
|
||
});
|
||
|
||
it('sans authentification → 401', async () => {
|
||
const res = await bundle.app.inject({ method: 'PATCH', url: '/api/v1/settings', payload: { giteaUrl: 'https://x.example' } });
|
||
expect(res.statusCode).toBe(401);
|
||
});
|
||
});
|
||
|
||
describe('PATCH /api/v1/settings — découverte des dépôts', () => {
|
||
it('enregistre des scanRoots et un intervalle valides et les renvoie', async () => {
|
||
const res = await bundle.app.inject({
|
||
method: 'PATCH',
|
||
url: '/api/v1/settings',
|
||
headers: auth(),
|
||
payload: { scanRoots: ['/home/u/work', '/srv/code'], scanIntervalMin: 10 },
|
||
});
|
||
expect(res.statusCode).toBe(200);
|
||
const body = res.json() as SettingsResponse;
|
||
expect(body.settings.scanRoots).toEqual(['/home/u/work', '/srv/code']);
|
||
expect(body.settings.scanIntervalMin).toBe(10);
|
||
// persisté
|
||
const get = await bundle.app.inject({ method: 'GET', url: '/api/v1/settings', headers: auth() });
|
||
expect((get.json() as SettingsResponse).settings.scanRoots).toEqual(['/home/u/work', '/srv/code']);
|
||
});
|
||
|
||
it('rejette des racines non absolues, "/", avec ".." ou en surnombre (400)', async () => {
|
||
const bads: unknown[] = [['relative/path'], ['/'], ['/a/../b'], Array.from({ length: 17 }, (_, i) => `/r${i}`)];
|
||
for (const scanRoots of bads) {
|
||
const res = await bundle.app.inject({ method: 'PATCH', url: '/api/v1/settings', headers: auth(), payload: { scanRoots } });
|
||
expect(res.statusCode).toBe(400);
|
||
}
|
||
});
|
||
|
||
it('rejette un intervalle hors borne (400)', async () => {
|
||
const res = await bundle.app.inject({ method: 'PATCH', url: '/api/v1/settings', headers: auth(), payload: { scanIntervalMin: 5000 } });
|
||
expect(res.statusCode).toBe(400);
|
||
const neg = await bundle.app.inject({ method: 'PATCH', url: '/api/v1/settings', headers: auth(), payload: { scanIntervalMin: -1 } });
|
||
expect(neg.statusCode).toBe(400);
|
||
});
|
||
});
|