Runner: Analyse-Skript und Datenschutz-Zusatzseite einsetzen
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
add7fa4add
commit
7de9f1aaa3
6 files changed
+309
No files matched your search
@@ -0,0 +1,66 @@
|
||||
// Website-Analyse: Skript-Tag in veröffentlichte HTML-Dateien einsetzen und Datenschutz-Zusatzseite schreiben.
|
||||
import * as nodeFs from 'node:fs/promises';
|
||||
import { join } from 'node:path';
|
||||
|
||||
export const RE_SITE_KEY = /^[a-z0-9]{16,40}$/;
|
||||
export const RE_COLOR = /^#[0-9a-f]{6}$/;
|
||||
const MAX_HTML_BYTES = 5 * 1024 * 1024;
|
||||
const PAGE = 'datenschutz-analyse.html';
|
||||
const TEMPLATE_URL = new URL('./templates/datenschutz-analyse.html', import.meta.url);
|
||||
|
||||
const esc = (s) => String(s).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/'/g, ''');
|
||||
|
||||
export const isValidBase = (b) => typeof b === 'string' && /^https:\/\/[^\s"'<>]+\/$/.test(b);
|
||||
|
||||
export function scriptTag(analytics, baseUrl) {
|
||||
// Attributwerte nur aus geprüften Eingaben (Regex), daher ohne Maskierung sicher.
|
||||
return `<script data-wk-analytics src="${baseUrl}w.js" data-site="${analytics.siteKey}" data-color="${analytics.color}" defer></script>`;
|
||||
}
|
||||
|
||||
// Arbeitet auf latin1-Zeichenketten (1 Byte = 1 Zeichen), damit beliebige Kodierungen byte-genau erhalten bleiben.
|
||||
export function injectIntoHtml(buf, tag) {
|
||||
let s = buf.toString('latin1');
|
||||
const old = /<script\b[^>]*\bdata-wk-analytics\b[^>]*>\s*<\/script\s*>/gi;
|
||||
if (old.test(s)) {
|
||||
let first = true;
|
||||
s = s.replace(old, () => (first ? ((first = false), tag) : ''));
|
||||
} else {
|
||||
const head = s.search(/<\/head\s*>/i);
|
||||
if (head >= 0) s = s.slice(0, head) + tag + s.slice(head);
|
||||
else {
|
||||
const m = /<body\b[^>]*>/i.exec(s);
|
||||
if (m) { const at = m.index + m[0].length; s = s.slice(0, at) + tag + s.slice(at); } else s = tag + s;
|
||||
}
|
||||
}
|
||||
return Buffer.from(s, 'latin1');
|
||||
}
|
||||
|
||||
async function* walk(fs, dir) {
|
||||
for (const e of await fs.readdir(dir, { withFileTypes: true })) {
|
||||
if (e.name.startsWith('.') || e.name === 'node_modules') continue;
|
||||
const p = join(dir, e.name);
|
||||
if (e.isDirectory()) yield* walk(fs, p);
|
||||
else if (e.isFile() && /\.html?$/i.test(e.name)) yield p;
|
||||
}
|
||||
}
|
||||
|
||||
export async function injectAnalytics(dir, analytics, { baseUrl, onLine, fs = nodeFs } = {}) {
|
||||
if (!analytics) return;
|
||||
if (!RE_SITE_KEY.test(analytics.siteKey || '')) throw new Error('Analyse: ungültiger siteKey');
|
||||
if (!RE_COLOR.test(analytics.color || '')) throw new Error('Analyse: ungültige Farbe');
|
||||
if (!isValidBase(baseUrl)) throw new Error('Analyse: ungültige öffentliche Basis-URL');
|
||||
const tag = scriptTag(analytics, baseUrl);
|
||||
const p = analytics.privacy || {};
|
||||
const template = await fs.readFile(TEMPLATE_URL, 'utf8');
|
||||
const page = template.replace(/\{\{(company|address|email)\}\}/g, (_, k) => esc(p[k] ?? ''));
|
||||
await fs.writeFile(join(dir, PAGE), page, 'utf8');
|
||||
await fs.chmod(join(dir, PAGE), 0o644);
|
||||
let n = 0;
|
||||
for await (const file of walk(fs, dir)) {
|
||||
const rel = file.slice(dir.length + 1);
|
||||
if ((await fs.stat(file)).size > MAX_HTML_BYTES) { onLine?.(`Analyse: ${rel} übersprungen (größer als 5 MB)`); continue; }
|
||||
await fs.writeFile(file, injectIntoHtml(await fs.readFile(file), tag));
|
||||
n++;
|
||||
}
|
||||
onLine?.(`Analyse-Skript in ${n} HTML-Datei(en) eingesetzt`);
|
||||
}
|
||||
@@ -1,6 +1,9 @@
|
||||
// Konfiguration aus Umgebungsvariablen. Pfade sind HOST-Pfade (R6): der Runner mountet sie identisch.
|
||||
export function loadConfig(env = process.env) {
|
||||
const analyticsPublicBase = env.ANALYTICS_PUBLIC_BASE || 'https://project.webklar.com/a/';
|
||||
if (!/^https:\/\/[^\s"'<>]+\/$/.test(analyticsPublicBase)) throw new Error('ANALYTICS_PUBLIC_BASE muss mit https:// beginnen und mit / enden');
|
||||
return {
|
||||
analyticsPublicBase,
|
||||
token: env.PREVIEW_RUNNER_TOKEN || '',
|
||||
giteaUrl: env.GITEA_URL || '',
|
||||
giteaToken: env.GITEA_TOKEN || '',
|
||||
|
||||
@@ -7,6 +7,7 @@ import { join, dirname, sep } from 'node:path';
|
||||
import { buildRunArgs } from './docker.js';
|
||||
import { detectFromDir } from './detect.js';
|
||||
import { readMemAvailable } from './server.js';
|
||||
import { injectAnalytics } from './analytics-inject.js';
|
||||
|
||||
const LOG_LINES = 300;
|
||||
const MAX_LINE = 2000;
|
||||
@@ -254,6 +255,8 @@ export function createJobs({ config, docker, fs: fsOver = {}, git, mem = readMem
|
||||
await fs.mkdir(config.previewsRoot, { recursive: true });
|
||||
await fs.copyTree(fs, srcDir, fresh, { skipGit, skipNodeModules });
|
||||
await ensureIndexHtml(fs, fresh, job.displayName || job.sub, onLine);
|
||||
// Analyse (optional): auf dem .new-Ordner vor dem Tausch; Fehler brechen die Veröffentlichung ab, die alte Preview bleibt (V4).
|
||||
if (job.analytics) await injectAnalytics(fresh, job.analytics, { baseUrl: config.analyticsPublicBase || 'https://project.webklar.com/a/', onLine, fs });
|
||||
try { await fs.rename(target, old); } catch (e) { if (e.code !== 'ENOENT') throw e; hadOld = false; }
|
||||
try { await fs.rename(fresh, target); } catch (e) {
|
||||
if (hadOld) await fs.rename(old, target).catch((e2) => onLine?.(`Rollback fehlgeschlagen: ${e2.message}`));
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
<!doctype html>
|
||||
<html lang="de">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<meta name="robots" content="noindex">
|
||||
<title>Datenschutz – Reichweitenmessung</title>
|
||||
<style>
|
||||
body{font-family:system-ui,-apple-system,Segoe UI,Roboto,sans-serif;line-height:1.6;color:#111;max-width:46rem;margin:0 auto;padding:1.5rem 1rem 4rem}
|
||||
h1{font-size:1.6rem}h2{font-size:1.15rem;margin-top:2rem}
|
||||
.entwurf{background:#fff7d6;border:1px solid #e0c200;padding:.75rem 1rem;border-radius:6px}
|
||||
a{color:#0b57d0}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Datenschutz bei der Reichweitenmessung</h1>
|
||||
<p class="entwurf"><strong>Vorläufiger Entwurf:</strong> Dieser Text wird vor der Veröffentlichung noch redaktionell und rechtlich überarbeitet.</p>
|
||||
|
||||
<h2>Verantwortlicher</h2>
|
||||
<p>{{company}}<br>{{address}}<br>E-Mail: <a href="mailto:{{email}}">{{email}}</a></p>
|
||||
|
||||
<h2>Zweck</h2>
|
||||
<p>Wir möchten verstehen, wie diese Website genutzt wird, um sie zu verbessern (zum Beispiel welche Seiten gelesen werden und wo Besucher hängen bleiben). Es gibt keine Werbung und kein Profiling.</p>
|
||||
|
||||
<h2>Was erfasst wird</h2>
|
||||
<p><strong>Ohne Deine Einwilligung</strong> wird nur gezählt, dass eine Seite aufgerufen wurde (Seitenpfad, Verweis-Website, Gerätegröße, grobes Land). Es werden keine Cookies gesetzt, keine IP-Adresse und kein Browser-Kennzeichen gespeichert. Eine tagesweise wechselnde, nicht zurückrechenbare Kennung dient nur dazu, Besuche zu zählen.</p>
|
||||
<p><strong>Mit Deiner Einwilligung</strong> (Klick auf „Annehmen“) erfassen wir zusätzlich Interaktionen wie Verweildauer, Scrolltiefe und Klickpositionen auf der Seite (ohne Eingaben in Formulare).</p>
|
||||
|
||||
<h2>Rechtsgrundlagen</h2>
|
||||
<p>Zählung ohne Einwilligung: berechtigtes Interesse, Art. 6 Abs. 1 lit. f DSGVO. Erweiterte Messung: Einwilligung, Art. 6 Abs. 1 lit. a DSGVO und § 25 Abs. 1 TDDDG, soweit auf Informationen im Endgerät zugegriffen wird (Speicherung Deiner Wahl im lokalen Speicher des Browsers).</p>
|
||||
|
||||
<h2>Empfänger</h2>
|
||||
<p>Die Messung wird von der WEBklar als Auftragsverarbeiter für uns betrieben. Die Daten liegen auf Servern der Hetzner Online GmbH in Deutschland. Eine Übermittlung in Drittländer findet nicht statt.</p>
|
||||
|
||||
<h2>Speicherdauer</h2>
|
||||
<p>Einzelne Ereignisse und Klicks werden nach 30 Tagen gelöscht, danach bleiben nur zusammengefasste Zahlen ohne Personenbezug. Deine Auswahl im Browser gilt sechs Monate.</p>
|
||||
|
||||
<h2>Widerruf</h2>
|
||||
<p>Du kannst Deine Entscheidung jederzeit ändern: Klicke dazu auf „Cookie-Einstellungen“ am Seitenende.</p>
|
||||
|
||||
<h2>Deine Rechte</h2>
|
||||
<p>Du hast das Recht auf Auskunft, Berichtigung, Löschung, Einschränkung der Verarbeitung, Widerspruch und Datenübertragbarkeit sowie das Recht, Dich bei einer Datenschutz-Aufsichtsbehörde zu beschweren. Wende Dich dazu an die oben genannte Adresse.</p>
|
||||
</body>
|
||||
</html>
|
||||
@@ -4,6 +4,8 @@ const RE_REPO = /^[\w.-]+\/[\w.-]+$/;
|
||||
const RE_SHA = /^[0-9a-f]{7,40}$/;
|
||||
const RE_BRANCH = /^[\w./-]{1,100}$/;
|
||||
const RE_ENV = /^[A-Z_][A-Z0-9_]*$/;
|
||||
const RE_SITE_KEY = /^[a-z0-9]{16,40}$/;
|
||||
const RE_COLOR = /^#[0-9a-f]{6}$/;
|
||||
const MODES = ['auto', 'build', 'dev'];
|
||||
|
||||
export const isValidRunId = (s) => typeof s === 'string' && /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(s);
|
||||
@@ -45,9 +47,27 @@ export function validateJob(body) {
|
||||
env[n] = v;
|
||||
}
|
||||
}
|
||||
let analytics;
|
||||
if (body.analytics !== undefined) {
|
||||
const a = body.analytics;
|
||||
if (!a || typeof a !== 'object' || Array.isArray(a)) return { ok: false, error: 'Ungültige Analyse-Angaben' };
|
||||
if (typeof a.siteKey !== 'string' || !RE_SITE_KEY.test(a.siteKey)) return { ok: false, error: 'Ungültiger Analyse-Schlüssel' };
|
||||
if (typeof a.color !== 'string' || !RE_COLOR.test(a.color)) return { ok: false, error: 'Ungültige Analyse-Farbe' };
|
||||
const pr = a.privacy;
|
||||
if (!pr || typeof pr !== 'object' || Array.isArray(pr)) return { ok: false, error: 'Ungültige Analyse-Datenschutzangaben' };
|
||||
const privacy = {};
|
||||
for (const [k, max] of [['company', 200], ['address', 300], ['email', 200]]) {
|
||||
const v = pr[k];
|
||||
// eslint-disable-next-line no-control-regex
|
||||
if (typeof v !== 'string' || !v.trim() || v.length > max || /[\x00-\x1f\x7f]/.test(v)) return { ok: false, error: `Ungültige Analyse-Datenschutzangabe: ${k}` };
|
||||
privacy[k] = v;
|
||||
}
|
||||
analytics = { siteKey: a.siteKey, color: a.color, privacy };
|
||||
}
|
||||
// Whitelist: keine ungeprüften Client-Felder im Job
|
||||
const job = { sub, repo, sha, branch, mode, env };
|
||||
if (index !== undefined) job.index = index;
|
||||
if (displayName !== undefined) job.displayName = displayName;
|
||||
if (analytics !== undefined) job.analytics = analytics;
|
||||
return { ok: true, job };
|
||||
}
|
||||
Reference in new issue
Block a user