Netz-Modul src/net (Offline-Modus, Host-Liste, kein fetch ausserhalb), Renderer-Umschalter WebGL2 entfernt, Offline-Schalter in den Einstellungen; i18n-Waechter gegen fest eingebaute Texte
This commit is contained in:
1 parent
cf99fc3d11
commit
3d864047cf
19 files changed
+329
-95
No files matched your search
@@ -0,0 +1,66 @@
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import { readdirSync, readFileSync, statSync } from "node:fs";
|
||||
import { join, relative, sep } from "node:path";
|
||||
import { HostNotAllowedError, OfflineError, allowHost, isLocalUrl, netFetch, resetOfflineForTests, setOffline } from "./net";
|
||||
|
||||
afterEach(() => {
|
||||
resetOfflineForTests();
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
|
||||
describe("netFetch", () => {
|
||||
it("lokale Quellen laufen immer durch, auch offline", async () => {
|
||||
const f = vi.fn(async () => new Response("ok"));
|
||||
vi.stubGlobal("fetch", f);
|
||||
setOffline(true);
|
||||
await netFetch("data:text/plain,hi");
|
||||
await netFetch("blob:abc");
|
||||
await netFetch("/assets/x.png");
|
||||
expect(f).toHaveBeenCalledTimes(3);
|
||||
expect(isLocalUrl("https://api.ambientcg.com/x")).toBe(false);
|
||||
});
|
||||
|
||||
it("offline blockiert entfernte URLs", async () => {
|
||||
const f = vi.fn();
|
||||
vi.stubGlobal("fetch", f);
|
||||
setOffline(true);
|
||||
await expect(netFetch("https://api3.geo.admin.ch/rest")).rejects.toBeInstanceOf(OfflineError);
|
||||
expect(f).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("online: bekannte Hosts ja, fremde Hosts nein, konfigurierter Proxy nach allowHost ja", async () => {
|
||||
const f = vi.fn(async () => new Response("ok"));
|
||||
vi.stubGlobal("fetch", f);
|
||||
setOffline(false);
|
||||
await netFetch("https://api.ambientcg.com/v2/full_json");
|
||||
await netFetch("https://data.geo.admin.ch/x");
|
||||
await expect(netFetch("https://evil.example.com/steal")).rejects.toBeInstanceOf(HostNotAllowedError);
|
||||
await expect(netFetch("https://notgeo.admin.ch.evil.com/")).rejects.toBeInstanceOf(HostNotAllowedError);
|
||||
allowHost("https://proxy.meinbuero.ch/geo");
|
||||
await netFetch("https://proxy.meinbuero.ch/geo?u=x");
|
||||
expect(f).toHaveBeenCalledTimes(3);
|
||||
});
|
||||
});
|
||||
|
||||
describe("kein fetch ausserhalb von src/net", () => {
|
||||
it("nur net/net.ts ruft fetch( direkt auf", () => {
|
||||
const SRC = join(process.cwd(), "src");
|
||||
const hits: string[] = [];
|
||||
const walk = (d: string) => {
|
||||
for (const n of readdirSync(d)) {
|
||||
const p = join(d, n);
|
||||
if (statSync(p).isDirectory()) {
|
||||
if (/^pkg/.test(n)) continue; // erzeugte WASM-Glue
|
||||
walk(p);
|
||||
} else if (/\.tsx?$/.test(n) && !/\.test\.tsx?$/.test(n)) {
|
||||
const rel = relative(SRC, p).split(sep).join("/");
|
||||
if (rel === "net/net.ts") continue;
|
||||
const src = readFileSync(p, "utf8").replace(/\/\/.*$|\/\*[\s\S]*?\*\//gm, "");
|
||||
if (/(^|[^\w.])fetch\s*\(/.test(src)) hits.push(rel);
|
||||
}
|
||||
}
|
||||
};
|
||||
walk(SRC);
|
||||
expect(hits).toEqual([]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,94 @@
|
||||
// Einziges Tor ins Netz (lessons-bimcraft [47]). Jeder entfernte Abruf läuft über `netFetch`:
|
||||
// • Offline-Modus (Einstellungen → Allgemein): entfernte URLs werden abgelehnt.
|
||||
// • Nur bekannte Hosts (ambientCG, swisstopo, Overpass, lokaler Proxy) — alles andere wird
|
||||
// abgelehnt, damit nirgends unbemerkt neue Verbindungen entstehen.
|
||||
// • Lokale Quellen (data:, blob:, relative Pfade) laufen unverändert durch.
|
||||
// Ein Test (`net.test.ts`) verbietet `fetch(` ausserhalb dieses Moduls. Das Update-Plugin von
|
||||
// Tauri (`update/checkForUpdate.ts`) ist die zweite, bewusst getrennte Netz-Stelle.
|
||||
|
||||
const OFFLINE_KEY = "dossier.offline";
|
||||
|
||||
/** Hosts, zu denen Dossier Verbindung aufnehmen darf. */
|
||||
export const ALLOWED_HOSTS: readonly string[] = [
|
||||
"ambientcg.com",
|
||||
"api.ambientcg.com",
|
||||
"geo.admin.ch",
|
||||
"api3.geo.admin.ch",
|
||||
"wms.geo.admin.ch",
|
||||
"data.geo.admin.ch",
|
||||
"overpass-api.de",
|
||||
"nominatim.openstreetmap.org",
|
||||
"localhost",
|
||||
"127.0.0.1",
|
||||
];
|
||||
|
||||
export class OfflineError extends Error {
|
||||
constructor(url: string) {
|
||||
super(`Offline-Modus: Zugriff auf ${url} blockiert.`);
|
||||
this.name = "OfflineError";
|
||||
}
|
||||
}
|
||||
export class HostNotAllowedError extends Error {
|
||||
constructor(url: string) {
|
||||
super(`Host nicht erlaubt: ${url}`);
|
||||
this.name = "HostNotAllowedError";
|
||||
}
|
||||
}
|
||||
|
||||
const extraHosts = new Set<string>();
|
||||
/** Erlaubt zusätzlich einen vom Nutzer konfigurierten Host (z. B. den Geo-Proxy). */
|
||||
export function allowHost(urlOrHost: string): void {
|
||||
try {
|
||||
extraHosts.add(urlOrHost.includes("/") ? new URL(urlOrHost).hostname.toLowerCase() : urlOrHost.toLowerCase());
|
||||
} catch {
|
||||
/* ungültige URL ignorieren */
|
||||
}
|
||||
}
|
||||
|
||||
let offlineOverride: boolean | null = null;
|
||||
|
||||
export function isOffline(): boolean {
|
||||
if (offlineOverride !== null) return offlineOverride;
|
||||
try {
|
||||
return typeof localStorage !== "undefined" && localStorage.getItem(OFFLINE_KEY) === "1";
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export function setOffline(value: boolean): void {
|
||||
offlineOverride = value;
|
||||
try {
|
||||
if (typeof localStorage !== "undefined") localStorage.setItem(OFFLINE_KEY, value ? "1" : "0");
|
||||
} catch {
|
||||
/* Speicher gesperrt: nur der Laufzeitwert gilt */
|
||||
}
|
||||
}
|
||||
|
||||
/** Nur für Tests: Override zurücksetzen. */
|
||||
export function resetOfflineForTests(): void {
|
||||
offlineOverride = null;
|
||||
}
|
||||
|
||||
/** Ist `url` eine lokale Quelle (kein Netzzugriff)? */
|
||||
export function isLocalUrl(url: string): boolean {
|
||||
return /^(data:|blob:)/i.test(url) || !/^[a-z][a-z0-9+.-]*:/i.test(url) || /^(tauri|asset|ipc):/i.test(url) || /^https?:\/\/(localhost|127\.0\.0\.1)(:|\/|$)/i.test(url);
|
||||
}
|
||||
|
||||
function hostAllowed(url: string): boolean {
|
||||
try {
|
||||
const h = new URL(url).hostname.toLowerCase();
|
||||
return extraHosts.has(h) || ALLOWED_HOSTS.some((a) => h === a || h.endsWith("." + a));
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/** `fetch` mit Offline-Schalter und Host-Liste. */
|
||||
export async function netFetch(url: string, init?: RequestInit): Promise<Response> {
|
||||
if (!isLocalUrl(url)) {
|
||||
if (isOffline()) throw new OfflineError(url);
|
||||
if (!hostAllowed(url)) throw new HostNotAllowedError(url);
|
||||
}
|
||||
return fetch(url, init);
|
||||
}
|
||||
Reference in new issue
Block a user