demo knacksicherer: edition im code + launcher prüft dateien

- edition.json entfällt; die Edition steckt als Marker im Code
  (const BUILD_EDITION). build.ps1 -Demo flippt ihn nur in der gestagten
  Kopie auf 'demo'; die Quelle bleibt 'full'. Keine lösch-/editierbare
  Extra-Datei mehr.
- Launcher: beim Auspacken wird ein Fingerabdruck (SHA-256 über server.js,
  package.json, lib\, public\, tools\) in .ok gespeichert und bei JEDEM
  Start neu geprüft. Wurde im entpackten App-Ordner etwas geändert oder
  gelöscht (z. B. der Demo-Marker zurückgesetzt), wird automatisch das
  Original neu ausgepackt. node_modules/node bleiben aussen vor (gross,
  nicht Teil der Demo-Logik → schneller Start).

Getestet: Marker im entpackten server.js auf 'full' gepatcht → nach
Neustart wieder 'full'->'demo', Server meldet wieder Demo.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-11-25 09:47:17 +01:00
parent d9c20fa076
commit ca9bde64f7
3 changed files with 47 additions and 13 deletions

View File

@@ -343,8 +343,33 @@ sealed class MainForm : Form
catch { }
}
// Unpacks the embedded payload.zip once per version to <DataDir>\app\<hash>
// and removes older versions. Returns the app folder.
// App files whose change would alter the app logic or unlock the demo. node_modules and node.exe
// are third-party and large, so they're left out: the edition marker and the demo limits live only
// in these. ".ok" is the stored fingerprint and is never part of it.
static readonly string[] CheckFiles = { "server.js", "package.json" };
static readonly string[] CheckDirs = { "lib", "public", "tools" };
// Hash of the integrity-relevant files (path + contents), stable regardless of file order.
static string Fingerprint(string dir)
{
var files = new List<string>();
foreach (var f in CheckFiles) { var p = Path.Combine(dir, f); if (File.Exists(p)) files.Add(p); }
foreach (var d in CheckDirs) { var p = Path.Combine(dir, d); if (Directory.Exists(p)) files.AddRange(Directory.GetFiles(p, "*", SearchOption.AllDirectories)); }
var items = files
.Select(f => (key: Path.GetRelativePath(dir, f).Replace('\\', '/').ToLowerInvariant(), path: f))
.OrderBy(x => x.key, StringComparer.Ordinal);
using var ih = IncrementalHash.CreateHash(HashAlgorithmName.SHA256);
foreach (var (key, path) in items)
{
ih.AppendData(System.Text.Encoding.UTF8.GetBytes(key + "\n"));
ih.AppendData(File.ReadAllBytes(path));
}
return Convert.ToHexString(ih.GetHashAndReset()).ToLowerInvariant();
}
// Unpacks the embedded payload.zip once per version to <DataDir>\app\<hash> and removes older
// versions. On every start the unpacked files are re-checked against their fingerprint (stored in
// ".ok"); if anything was changed or removed, the originals are unpacked again. Returns the app folder.
string UnpackPayload()
{
var asm = Assembly.GetExecutingAssembly();
@@ -354,14 +379,16 @@ sealed class MainForm : Form
using (var sha = SHA256.Create()) hash = Convert.ToHexString(sha.ComputeHash(res))[..12].ToLowerInvariant();
var appsRoot = Path.Combine(DataDir, "app");
var target = Path.Combine(appsRoot, hash);
if (File.Exists(Path.Combine(target, ".ok"))) return target;
var okFile = Path.Combine(target, ".ok");
// valid only if the files still match what was unpacked (tamper-evident)
try { if (File.Exists(okFile) && File.ReadAllText(okFile) == Fingerprint(target)) return target; } catch { }
Directory.CreateDirectory(appsRoot);
var tmp = target + ".tmp";
if (Directory.Exists(tmp)) Directory.Delete(tmp, true);
res.Position = 0;
using (var zip = new ZipArchive(res, ZipArchiveMode.Read)) zip.ExtractToDirectory(tmp);
File.WriteAllText(Path.Combine(tmp, ".ok"), DateTime.Now.ToString("s"));
File.WriteAllText(Path.Combine(tmp, ".ok"), Fingerprint(tmp));
if (Directory.Exists(target)) Directory.Delete(target, true);
Directory.Move(tmp, target);
foreach (var old in Directory.GetDirectories(appsRoot))