sécurité: audit commercialisation — hardening + 366 tests (24 E2E)
Sandbox Electron, HTTPS ntfy, validation import structurelle, suppression executeJavaScript, nettoyage memory leaks, try/catch sur tous les appels electronAPI. 27 nouveaux tests de sécurité et validation. README mis à jour avec changelog et couverture. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -10,7 +10,6 @@ import {
|
||||
} from 'electron';
|
||||
import path from 'path';
|
||||
import https from 'https';
|
||||
import http from 'http';
|
||||
import fs from 'fs';
|
||||
import { autoUpdater } from 'electron-updater';
|
||||
|
||||
@@ -66,6 +65,7 @@ function createWindow(): void {
|
||||
webPreferences: {
|
||||
nodeIntegration: false,
|
||||
contextIsolation: true,
|
||||
sandbox: true,
|
||||
backgroundThrottling: false,
|
||||
preload: path.join(__dirname, 'preload.js'),
|
||||
},
|
||||
@@ -117,18 +117,9 @@ function createWindow(): void {
|
||||
if (updateInfo) {
|
||||
mainWindow!.webContents.send('update-available', updateInfo);
|
||||
}
|
||||
// Badge DEV visible dans l'interface
|
||||
// Badge DEV visible dans l'interface (via IPC, pas executeJavaScript)
|
||||
if (!app.isPackaged) {
|
||||
mainWindow!.webContents.executeJavaScript(`
|
||||
const p = document.querySelector('header p');
|
||||
if (p && !document.getElementById('dev-badge')) {
|
||||
const b = document.createElement('span');
|
||||
b.id = 'dev-badge';
|
||||
b.textContent = 'DEV';
|
||||
b.style.cssText = 'background:#ff9820;color:#000;padding:2px 10px;border-radius:8px;font-size:0.72rem;font-weight:800;margin-left:8px;vertical-align:middle';
|
||||
p.appendChild(b);
|
||||
}
|
||||
`).catch(() => {});
|
||||
mainWindow!.webContents.send('dev-mode');
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -222,7 +213,10 @@ ipcMain.on('send-ntfy', (_event, { url, title, message }: { url: string; title:
|
||||
if (!url) return;
|
||||
try {
|
||||
const parsed = new URL(url.trim());
|
||||
const mod = parsed.protocol === 'https:' ? https : http;
|
||||
if (parsed.protocol !== 'https:') {
|
||||
console.warn('ntfy: HTTPS requis, requête ignorée');
|
||||
return;
|
||||
}
|
||||
// API JSON ntfy : supporte nativement l'UTF-8 (accents, emojis)
|
||||
const topic = parsed.pathname.replace(/^\//, '');
|
||||
const jsonBody = JSON.stringify({
|
||||
@@ -242,7 +236,7 @@ ipcMain.on('send-ntfy', (_event, { url, title, message }: { url: string; title:
|
||||
'Content-Length': Buffer.byteLength(jsonBody, 'utf-8'),
|
||||
},
|
||||
};
|
||||
const req = mod.request(options, (res) => {
|
||||
const req = https.request(options, (res) => {
|
||||
res.on('data', () => {}); // drain
|
||||
res.on('end', () => {});
|
||||
});
|
||||
|
||||
@@ -31,4 +31,7 @@ contextBridge.exposeInMainWorld('electronAPI', {
|
||||
onUpdateProgress: (cb: (info: any) => void) => ipcRenderer.on('update-progress', (_e, info) => cb(info)),
|
||||
onUpdateReady: (cb: () => void) => ipcRenderer.on('update-ready', () => cb()),
|
||||
onUpdateError: (cb: (info: any) => void) => ipcRenderer.on('update-error', (_e, info) => cb(info)),
|
||||
|
||||
// Dev mode badge
|
||||
onDevMode: (cb: () => void) => ipcRenderer.on('dev-mode', () => cb()),
|
||||
});
|
||||
|
||||
@@ -45,8 +45,8 @@ export class LocalStorageRepository implements StateRepository {
|
||||
if (typeof localStorage !== 'undefined') {
|
||||
localStorage.setItem(this.storageKey, json);
|
||||
}
|
||||
} catch {
|
||||
// Silently fail
|
||||
} catch (e) {
|
||||
console.error('Erreur sauvegarde:', (e as Error).message);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -30,6 +30,7 @@ export class App {
|
||||
private unsubscribe: (() => void) | null = null;
|
||||
private rafId: number | null = null;
|
||||
private completionIntervalId: number | null = null;
|
||||
private ctrlZHandler: ((e: KeyboardEvent) => void) | null = null;
|
||||
private lastView: string | number | null = null;
|
||||
|
||||
// Tab drag-and-drop state
|
||||
@@ -80,11 +81,25 @@ export class App {
|
||||
Toast.mount(appShell);
|
||||
|
||||
// Ctrl+Z → undo
|
||||
document.addEventListener('keydown', (e) => {
|
||||
this.ctrlZHandler = (e: KeyboardEvent) => {
|
||||
if ((e.ctrlKey || e.metaKey) && e.key === 'z' && UndoManager.canUndo()) {
|
||||
e.preventDefault();
|
||||
UndoManager.undo();
|
||||
}
|
||||
};
|
||||
document.addEventListener('keydown', this.ctrlZHandler);
|
||||
|
||||
// DEV badge via IPC (pas d'executeJavaScript)
|
||||
const api = (window as any).electronAPI;
|
||||
api?.onDevMode?.(() => {
|
||||
const p = this.root.querySelector('header p');
|
||||
if (p && !document.getElementById('dev-badge')) {
|
||||
const b = document.createElement('span');
|
||||
b.id = 'dev-badge';
|
||||
b.textContent = 'DEV';
|
||||
b.style.cssText = 'background:#ff9820;color:#000;padding:2px 10px;border-radius:8px;font-size:0.72rem;font-weight:800;margin-left:8px;vertical-align:middle';
|
||||
p.appendChild(b);
|
||||
}
|
||||
});
|
||||
|
||||
// Hamburger toggle
|
||||
@@ -350,6 +365,10 @@ export class App {
|
||||
clearInterval(this.completionIntervalId);
|
||||
this.completionIntervalId = null;
|
||||
}
|
||||
if (this.ctrlZHandler) {
|
||||
document.removeEventListener('keydown', this.ctrlZHandler);
|
||||
this.ctrlZHandler = null;
|
||||
}
|
||||
if (this.unsubscribe) {
|
||||
this.unsubscribe();
|
||||
this.unsubscribe = null;
|
||||
|
||||
@@ -310,24 +310,28 @@ export class ParametresView {
|
||||
const api = (window as any).electronAPI;
|
||||
if (!api?.loadData || !api?.exportFile) return;
|
||||
|
||||
const raw = await api.loadData();
|
||||
if (!raw) {
|
||||
Toast.show('error', 'Aucune donnée à exporter.');
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const raw = await api.loadData();
|
||||
if (!raw) {
|
||||
Toast.show('error', 'Aucune donnée à exporter.');
|
||||
return;
|
||||
}
|
||||
|
||||
const version = await api.getVersion?.() ?? 'unknown';
|
||||
const backup = {
|
||||
app: 'minuteur-dragodinde',
|
||||
version,
|
||||
exportedAt: new Date().toISOString(),
|
||||
data: JSON.parse(raw),
|
||||
};
|
||||
const version = await api.getVersion?.() ?? 'unknown';
|
||||
const backup = {
|
||||
app: 'minuteur-dragodinde',
|
||||
version,
|
||||
exportedAt: new Date().toISOString(),
|
||||
data: JSON.parse(raw),
|
||||
};
|
||||
|
||||
const date = new Date().toISOString().slice(0, 10);
|
||||
const ok = await api.exportFile(JSON.stringify(backup, null, 2), `dd-timer-backup-${date}.json`);
|
||||
if (ok) {
|
||||
Toast.show('success', 'Données exportées avec succès.');
|
||||
const date = new Date().toISOString().slice(0, 10);
|
||||
const ok = await api.exportFile(JSON.stringify(backup, null, 2), `dd-timer-backup-${date}.json`);
|
||||
if (ok) {
|
||||
Toast.show('success', 'Données exportées avec succès.');
|
||||
}
|
||||
} catch {
|
||||
Toast.show('error', 'Erreur lors de l\'export des données.');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -335,41 +339,65 @@ export class ParametresView {
|
||||
const api = (window as any).electronAPI;
|
||||
if (!api?.importFile || !api?.saveData) return;
|
||||
|
||||
const raw = await api.importFile();
|
||||
if (!raw) return; // dialogue annulé
|
||||
|
||||
let parsed: any;
|
||||
try {
|
||||
parsed = JSON.parse(raw);
|
||||
const raw = await api.importFile();
|
||||
if (!raw) return; // dialogue annulé
|
||||
|
||||
let parsed: any;
|
||||
try {
|
||||
parsed = JSON.parse(raw);
|
||||
} catch {
|
||||
Toast.show('error', 'Le fichier sélectionné n\'est pas un JSON valide.');
|
||||
return;
|
||||
}
|
||||
|
||||
// Validation structurelle des données importées
|
||||
const validateEnclosData = (data: any): boolean => {
|
||||
if (!data || typeof data !== 'object' || data === null) return false;
|
||||
if (!Array.isArray(data.enclos)) return false;
|
||||
return data.enclos.every((enc: any) =>
|
||||
enc && typeof enc === 'object' &&
|
||||
typeof enc.id === 'number' &&
|
||||
typeof enc.name === 'string' &&
|
||||
Array.isArray(enc.dragodindes) &&
|
||||
enc.gaugeLevels && typeof enc.gaugeLevels === 'object' &&
|
||||
enc.timer && typeof enc.timer === 'object',
|
||||
);
|
||||
};
|
||||
|
||||
// Validation du format backup
|
||||
if (parsed.app === 'minuteur-dragodinde' && parsed.data && typeof parsed.data === 'object' && parsed.data !== null) {
|
||||
if (!validateEnclosData(parsed.data)) {
|
||||
Toast.show('error', 'Le backup contient des données corrompues ou incomplètes.');
|
||||
return;
|
||||
}
|
||||
const date = parsed.exportedAt ? new Date(parsed.exportedAt).toLocaleDateString('fr-FR') : 'inconnue';
|
||||
const ok = await ConfirmModal.show(
|
||||
'Importer les données',
|
||||
`Ce backup date du ${date} (v${parsed.version ?? '?'}). Toutes vos données actuelles seront remplacées. Continuer ?`,
|
||||
);
|
||||
if (!ok) return;
|
||||
api.saveData(JSON.stringify(parsed.data));
|
||||
} else if (parsed.enclos && Array.isArray(parsed.enclos)) {
|
||||
if (!validateEnclosData(parsed)) {
|
||||
Toast.show('error', 'Le fichier contient des données corrompues ou incomplètes.');
|
||||
return;
|
||||
}
|
||||
const ok = await ConfirmModal.show(
|
||||
'Importer les données',
|
||||
'Toutes vos données actuelles seront remplacées. Continuer ?',
|
||||
);
|
||||
if (!ok) return;
|
||||
api.saveData(JSON.stringify(parsed));
|
||||
} else {
|
||||
Toast.show('error', 'Format de fichier non reconnu.');
|
||||
return;
|
||||
}
|
||||
|
||||
Toast.show('success', 'Données importées. Rechargement...');
|
||||
setTimeout(() => window.location.reload(), 1000);
|
||||
} catch {
|
||||
Toast.show('error', 'Le fichier sélectionné n\'est pas un JSON valide.');
|
||||
return;
|
||||
Toast.show('error', 'Erreur lors de l\'import des données.');
|
||||
}
|
||||
|
||||
// Validation du format backup
|
||||
if (parsed.app === 'minuteur-dragodinde' && parsed.data && typeof parsed.data === 'object') {
|
||||
// Format backup avec métadonnées
|
||||
const date = parsed.exportedAt ? new Date(parsed.exportedAt).toLocaleDateString('fr-FR') : 'inconnue';
|
||||
const ok = await ConfirmModal.show(
|
||||
'Importer les données',
|
||||
`Ce backup date du ${date} (v${parsed.version ?? '?'}). Toutes vos données actuelles seront remplacées. Continuer ?`,
|
||||
);
|
||||
if (!ok) return;
|
||||
api.saveData(JSON.stringify(parsed.data));
|
||||
} else if (parsed.enclos && Array.isArray(parsed.enclos)) {
|
||||
// Format brut (ancien export ou fichier state direct)
|
||||
const ok = await ConfirmModal.show(
|
||||
'Importer les données',
|
||||
'Toutes vos données actuelles seront remplacées. Continuer ?',
|
||||
);
|
||||
if (!ok) return;
|
||||
api.saveData(JSON.stringify(parsed));
|
||||
} else {
|
||||
Toast.show('error', 'Format de fichier non reconnu.');
|
||||
return;
|
||||
}
|
||||
|
||||
Toast.show('success', 'Données importées. Rechargement...');
|
||||
setTimeout(() => window.location.reload(), 1000);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -116,7 +116,7 @@ export class Sidebar {
|
||||
api.getVersion().then((v: string) => {
|
||||
const verEl = this.el?.querySelector('#sb-ver');
|
||||
if (verEl) verEl.textContent = `v${v}`;
|
||||
});
|
||||
}).catch(() => {});
|
||||
}
|
||||
|
||||
destroy(): void {
|
||||
|
||||
@@ -30,13 +30,14 @@ const items: ToastItem[] = [];
|
||||
|
||||
export const Toast = {
|
||||
mount(parent: HTMLElement): void {
|
||||
if (container?.isConnected) return; // déjà monté
|
||||
container = document.createElement('div');
|
||||
container.id = 'toast-container';
|
||||
parent.appendChild(container);
|
||||
},
|
||||
|
||||
show(type: ToastType, message: string, action?: ToastAction): void {
|
||||
if (!container) return;
|
||||
if (!container?.isConnected) return;
|
||||
|
||||
const id = nextId++;
|
||||
items.push({ id, type, message });
|
||||
|
||||
@@ -598,21 +598,25 @@ export class WorkflowsView {
|
||||
const suffix = toExport.length === 1 ? toExport[0].target.toLowerCase().replace(/\s+/g, '-') : 'tous';
|
||||
const defaultName = `plans-dragodinde-${suffix}-${new Date().toISOString().slice(0, 10)}.json`;
|
||||
|
||||
const api = (window as any).electronAPI;
|
||||
if (api?.exportFile) {
|
||||
const ok = await api.exportFile(data, defaultName);
|
||||
if (ok) { Toast.show('success', 'Plans exportés avec succès.'); this.exportMode = false; this.selectedIds.clear(); this.dirty = true; this.update(); }
|
||||
} else {
|
||||
// Fallback navigateur : téléchargement via Blob
|
||||
const blob = new Blob([data], { type: 'application/json' });
|
||||
const url = URL.createObjectURL(blob);
|
||||
const a = document.createElement('a');
|
||||
a.href = url;
|
||||
a.download = defaultName;
|
||||
a.click();
|
||||
URL.revokeObjectURL(url);
|
||||
Toast.show('success', 'Plans exportés avec succès.');
|
||||
this.exportMode = false; this.selectedIds.clear(); this.dirty = true; this.update();
|
||||
try {
|
||||
const api = (window as any).electronAPI;
|
||||
if (api?.exportFile) {
|
||||
const ok = await api.exportFile(data, defaultName);
|
||||
if (ok) { Toast.show('success', 'Plans exportés avec succès.'); this.exportMode = false; this.selectedIds.clear(); this.dirty = true; this.update(); }
|
||||
} else {
|
||||
// Fallback navigateur : téléchargement via Blob
|
||||
const blob = new Blob([data], { type: 'application/json' });
|
||||
const url = URL.createObjectURL(blob);
|
||||
const a = document.createElement('a');
|
||||
a.href = url;
|
||||
a.download = defaultName;
|
||||
a.click();
|
||||
URL.revokeObjectURL(url);
|
||||
Toast.show('success', 'Plans exportés avec succès.');
|
||||
this.exportMode = false; this.selectedIds.clear(); this.dirty = true; this.update();
|
||||
}
|
||||
} catch {
|
||||
Toast.show('error', 'Erreur lors de l\'export des plans.');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -620,29 +624,29 @@ export class WorkflowsView {
|
||||
const api = (window as any).electronAPI;
|
||||
let raw: string | null = null;
|
||||
|
||||
if (api?.importFile) {
|
||||
raw = await api.importFile();
|
||||
} else {
|
||||
// Fallback navigateur : input file
|
||||
raw = await new Promise<string | null>(resolve => {
|
||||
const input = document.createElement('input');
|
||||
input.type = 'file';
|
||||
input.accept = '.json';
|
||||
input.addEventListener('change', () => {
|
||||
const file = input.files?.[0];
|
||||
if (!file) { resolve(null); return; }
|
||||
const reader = new FileReader();
|
||||
reader.onload = () => resolve(reader.result as string);
|
||||
reader.onerror = () => resolve(null);
|
||||
reader.readAsText(file);
|
||||
});
|
||||
input.click();
|
||||
});
|
||||
}
|
||||
|
||||
if (!raw) return;
|
||||
|
||||
try {
|
||||
if (api?.importFile) {
|
||||
raw = await api.importFile();
|
||||
} else {
|
||||
// Fallback navigateur : input file
|
||||
raw = await new Promise<string | null>(resolve => {
|
||||
const input = document.createElement('input');
|
||||
input.type = 'file';
|
||||
input.accept = '.json';
|
||||
input.addEventListener('change', () => {
|
||||
const file = input.files?.[0];
|
||||
if (!file) { resolve(null); return; }
|
||||
const reader = new FileReader();
|
||||
reader.onload = () => resolve(reader.result as string);
|
||||
reader.onerror = () => resolve(null);
|
||||
reader.readAsText(file);
|
||||
});
|
||||
input.click();
|
||||
});
|
||||
}
|
||||
|
||||
if (!raw) return;
|
||||
|
||||
const parsed = JSON.parse(raw);
|
||||
const arr = Array.isArray(parsed) ? parsed : [parsed];
|
||||
|
||||
@@ -669,7 +673,7 @@ export class WorkflowsView {
|
||||
this.dirty = true;
|
||||
this.update();
|
||||
} catch {
|
||||
Toast.show('error', 'Le fichier sélectionné n\'est pas un JSON valide.');
|
||||
Toast.show('error', 'Erreur lors de l\'import. Vérifiez que le fichier est un JSON valide.');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user