mirror of
https://github.com/Jeuners/agenttwo-tools.git
synced 2026-09-12 16:32:31 +02:00
feat: Bestätigungspflicht für Werkzeuge mit Außenwirkung, DNS-Pinning
Ergebnis eines Security-Reviews. Die schwerwiegendste Kette war: Fremdinhalt aus read_webpage weist das Modell an, remember aufzurufen — der Anker landet gepinnt und sessionübergreifend im Gedächtnis und geht danach als "verlässliches Wissen" in jeden System-Prompt. Beide Werkzeuge liefen ungefragt, requiresConfirmation war nur ein Feld ohne Wirkung. Bestätigung: - runTool wertet requiresConfirmation über ToolContext.confirm aus; ohne Rückkanal (Skript, Test) gilt abgelehnt statt ungefragt ausführen - Handshake über den WebSocket: tool-confirm mit ungekürzten Argumenten, Antwort allow/always/deny. Ablehnung auch bei Timeout (2 min), Verbindungsabbruch und Stop; "always" gilt pro Verbindung - read_webpage und remember sind bestätigungspflichtig; remember pinnt nicht mehr automatisch, damit Modell-Anker normal verfallen - ToolConfirm-Komponente zeigt Werkzeug und vollständige Argumente SSRF-Guard (DNS-Rebinding): - fetch gegen node:http/https mit eigenem lookup-Hook getauscht: geprüft wird genau die Adresse, die dann auch verbunden wird. Vorher löste fetch ein zweites Mal auf — öffentlich beim Prüfen, 127.0.0.1 beim Verbinden - IPv6-Literale werden entklammert, gzip/deflate/br werden ausgepackt Nebenbei: - ctx.signal kombiniert Abbruch und Zeitlimit und wirkt bis in den Abruf - activeAborts als Set: Stop erwischt alle laufenden Antworten - Chat-Rate-Limit 30/min je Verbindung, Rate-Limiter räumt Buckets ab - web/dist untracked Nicht umgesetzt: Auth/Origin-Härtung — der Server läuft bewusst lokal, Anfragen ohne Origin-Header bleiben erlaubt (im README dokumentiert). Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015AUP4R3rgq4XwVs4bVf7uh
This commit is contained in:
parent
878acd9933
commit
dd0ee30165
18 changed files with 570 additions and 181 deletions
|
|
@ -2,6 +2,7 @@ import Fastify from "fastify";
|
|||
import cors from "@fastify/cors";
|
||||
import { WebSocketServer, WebSocket } from "ws";
|
||||
import type { IncomingMessage } from "node:http";
|
||||
import { randomUUID } from "node:crypto";
|
||||
import { readFileSync } from "node:fs";
|
||||
import path from "node:path";
|
||||
import * as dbmod from "./db.js";
|
||||
|
|
@ -35,6 +36,15 @@ const PORT = Number(process.env.PORT ?? 8788);
|
|||
const OLLAMA_URL = process.env.OLLAMA_URL ?? "http://localhost:11434";
|
||||
const MODEL = process.env.MODEL ?? "qwen3.5:latest";
|
||||
|
||||
/**
|
||||
* Wie lange auf die Freigabe eines bestätigungspflichtigen Werkzeugs gewartet
|
||||
* wird. Danach gilt "abgelehnt" — eine Antwort soll nicht ewig hängen, nur
|
||||
* weil niemand am Rechner sitzt.
|
||||
*/
|
||||
const CONFIRM_TIMEOUT_MS = 120_000;
|
||||
/** Chats pro Minute und Verbindung. Bremst Schleifen und OpenRouter-Kosten. */
|
||||
const CHAT_LIMIT_PER_MIN = 30;
|
||||
|
||||
const DREAM_IDLE_MS = 180_000;
|
||||
const DREAM_BATCH = 10;
|
||||
const dreamTimers = new Map<string, ReturnType<typeof setTimeout>>();
|
||||
|
|
@ -325,7 +335,54 @@ function broadcast(data: unknown) {
|
|||
}
|
||||
|
||||
wss.on("connection", (socket: WebSocket, _req: IncomingMessage) => {
|
||||
let activeAbort: AbortController | null = null;
|
||||
// Mehrere Antworten können parallel laufen (zweite Nachricht bei laufendem
|
||||
// Stream). Ein einzelnes Feld würde beim Abbruch nur die letzte erwischen.
|
||||
const activeAborts = new Set<AbortController>();
|
||||
const pendingConfirms = new Map<string, { name: string; decide(ok: boolean): void }>();
|
||||
/** Werkzeuge, die der Nutzer für diese Verbindung generell freigegeben hat. */
|
||||
const alwaysAllowed = new Set<string>();
|
||||
const chatLimiter = createRateLimiter(CHAT_LIMIT_PER_MIN, 60_000);
|
||||
|
||||
function denyAllConfirms() {
|
||||
for (const entry of [...pendingConfirms.values()]) entry.decide(false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fragt den Nutzer, bevor ein Werkzeug mit Außenwirkung läuft. Bricht die
|
||||
* Verbindung weg oder bleibt die Antwort aus, gilt das als Ablehnung.
|
||||
*/
|
||||
function confirmTool(
|
||||
messageId: string,
|
||||
name: string,
|
||||
args: Record<string, unknown>,
|
||||
): Promise<boolean> {
|
||||
if (alwaysAllowed.has(name)) return Promise.resolve(true);
|
||||
if (socket.readyState !== WebSocket.OPEN) return Promise.resolve(false);
|
||||
|
||||
const id = randomUUID();
|
||||
return new Promise<boolean>((resolve) => {
|
||||
const timer = setTimeout(() => decide(false), CONFIRM_TIMEOUT_MS);
|
||||
function decide(approved: boolean) {
|
||||
clearTimeout(timer);
|
||||
pendingConfirms.delete(id);
|
||||
resolve(approved);
|
||||
}
|
||||
pendingConfirms.set(id, { name, decide });
|
||||
socket.send(
|
||||
JSON.stringify({
|
||||
type: "tool-confirm",
|
||||
id,
|
||||
messageId,
|
||||
name,
|
||||
// Ungekürzt: der Nutzer muss genau sehen, was rausgeht — bei
|
||||
// read_webpage ist die vollständige URL der eigentliche Punkt.
|
||||
args: JSON.stringify(args),
|
||||
}),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
socket.on("close", denyAllConfirms);
|
||||
|
||||
socket.on("message", async (raw: Buffer) => {
|
||||
let msg: Record<string, unknown>;
|
||||
|
|
@ -336,13 +393,31 @@ wss.on("connection", (socket: WebSocket, _req: IncomingMessage) => {
|
|||
return;
|
||||
}
|
||||
|
||||
if (msg.type === "tool-confirm-reply") {
|
||||
const entry = pendingConfirms.get(String(msg.id ?? ""));
|
||||
if (!entry) return;
|
||||
// Der Werkzeugname kommt aus dem Server-Zustand, nicht aus der Antwort:
|
||||
// sonst könnte eine Freigabe für ein Werkzeug ein anderes freischalten.
|
||||
if (msg.decision === "always") alwaysAllowed.add(entry.name);
|
||||
entry.decide(msg.decision === "allow" || msg.decision === "always");
|
||||
return;
|
||||
}
|
||||
|
||||
if (msg.type === "abort") {
|
||||
activeAbort?.abort();
|
||||
for (const controller of activeAborts) controller.abort();
|
||||
denyAllConfirms();
|
||||
return;
|
||||
}
|
||||
|
||||
if (msg.type !== "chat") return;
|
||||
|
||||
if (!chatLimiter("chat")) {
|
||||
socket.send(
|
||||
JSON.stringify({ type: "error", error: "Zu viele Anfragen — kurz warten." }),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
const sessionId = String(msg.sessionId ?? "");
|
||||
const content = String(msg.content ?? "").trim();
|
||||
|
||||
|
|
@ -423,7 +498,8 @@ wss.on("connection", (socket: WebSocket, _req: IncomingMessage) => {
|
|||
const assistantRow = dbmod.insertMessage(session.id, "assistant", "");
|
||||
socket.send(JSON.stringify({ type: "assistant-start", message: assistantRow }));
|
||||
|
||||
activeAbort = new AbortController();
|
||||
const abort = new AbortController();
|
||||
activeAborts.add(abort);
|
||||
let full = "";
|
||||
let thinking = "";
|
||||
const callbacks = {
|
||||
|
|
@ -463,6 +539,9 @@ wss.on("connection", (socket: WebSocket, _req: IncomingMessage) => {
|
|||
JSON.stringify({ type: "tool-result", messageId: assistantRow.id, name, ok, durationMs }),
|
||||
);
|
||||
},
|
||||
onToolConfirm(name: string, args: Record<string, unknown>) {
|
||||
return confirmTool(assistantRow.id, name, args);
|
||||
},
|
||||
};
|
||||
|
||||
try {
|
||||
|
|
@ -479,10 +558,10 @@ wss.on("connection", (socket: WebSocket, _req: IncomingMessage) => {
|
|||
},
|
||||
apiKey,
|
||||
callbacks,
|
||||
activeAbort.signal,
|
||||
abort.signal,
|
||||
);
|
||||
} else {
|
||||
await streamChat(history, system, opts, callbacks, activeAbort.signal);
|
||||
await streamChat(history, system, opts, callbacks, abort.signal);
|
||||
}
|
||||
dbmod.updateAssistantMessage(assistantRow.id, full.trim(), thinking.trim() || null);
|
||||
mem.appendEvent(session.id, "message", {
|
||||
|
|
@ -493,7 +572,7 @@ wss.on("connection", (socket: WebSocket, _req: IncomingMessage) => {
|
|||
JSON.stringify({
|
||||
type: "done",
|
||||
messageId: assistantRow.id,
|
||||
aborted: activeAbort.signal.aborted,
|
||||
aborted: abort.signal.aborted,
|
||||
}),
|
||||
);
|
||||
broadcast({ type: "sessions-changed" });
|
||||
|
|
@ -507,7 +586,7 @@ wss.on("connection", (socket: WebSocket, _req: IncomingMessage) => {
|
|||
}
|
||||
} catch (err) {
|
||||
const aborted =
|
||||
activeAbort.signal.aborted ||
|
||||
abort.signal.aborted ||
|
||||
(err instanceof Error && err.name === "AbortError");
|
||||
dbmod.updateAssistantMessage(assistantRow.id, full.trim(), thinking.trim() || null);
|
||||
if (aborted) {
|
||||
|
|
@ -522,7 +601,7 @@ wss.on("connection", (socket: WebSocket, _req: IncomingMessage) => {
|
|||
);
|
||||
}
|
||||
} finally {
|
||||
activeAbort = null;
|
||||
activeAborts.delete(abort);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -24,6 +24,11 @@ export interface StreamCallbacks {
|
|||
onToolCall?(name: string, args: Record<string, unknown>): void;
|
||||
/** Werkzeug ist fertig. */
|
||||
onToolResult?(name: string, ok: boolean, durationMs: number): void;
|
||||
/**
|
||||
* Holt die Freigabe des Nutzers für ein bestätigungspflichtiges Werkzeug.
|
||||
* Fehlt der Rückkanal, lehnt `runTool` solche Werkzeuge ab.
|
||||
*/
|
||||
onToolConfirm?(name: string, args: Record<string, unknown>): Promise<boolean>;
|
||||
}
|
||||
|
||||
/** Muss zum OLLAMA_URL in index.ts passen — vorher war der Host hier hartkodiert. */
|
||||
|
|
@ -200,7 +205,13 @@ export async function streamChat(
|
|||
|
||||
for (const call of toolCalls) {
|
||||
cb.onToolCall?.(call.name, call.arguments);
|
||||
const result = await runTool(call, { signal, sessionId: opts.sessionId });
|
||||
const result = await runTool(call, {
|
||||
signal,
|
||||
sessionId: opts.sessionId,
|
||||
confirm: cb.onToolConfirm
|
||||
? (c) => cb.onToolConfirm!(c.name, c.arguments)
|
||||
: undefined,
|
||||
});
|
||||
cb.onToolResult?.(result.name, result.ok, result.durationMs);
|
||||
messages.push({ role: "tool", tool_name: result.name, content: result.content });
|
||||
}
|
||||
|
|
|
|||
|
|
@ -39,6 +39,9 @@ interface Bucket {
|
|||
resetAt: number;
|
||||
}
|
||||
|
||||
/** Ab dieser Größe wird aufgeräumt — reicht für jede realistische Nutzung. */
|
||||
const MAX_BUCKETS = 1000;
|
||||
|
||||
/**
|
||||
* Einfacher In-Memory-Zähler pro Zeitfenster. Bremst teure Endpunkte
|
||||
* (Whisper läuft bis zu 180 s) gegen versehentliche oder böswillige Fluten.
|
||||
|
|
@ -46,8 +49,16 @@ interface Bucket {
|
|||
export function createRateLimiter(limit: number, windowMs: number) {
|
||||
const buckets = new Map<string, Bucket>();
|
||||
|
||||
/** Abgelaufene Zähler wegräumen, damit die Map nicht unbegrenzt wächst. */
|
||||
function sweep(now: number) {
|
||||
for (const [key, bucket] of buckets) {
|
||||
if (now >= bucket.resetAt) buckets.delete(key);
|
||||
}
|
||||
}
|
||||
|
||||
return function allow(key: string): boolean {
|
||||
const now = Date.now();
|
||||
if (buckets.size > MAX_BUCKETS) sweep(now);
|
||||
const bucket = buckets.get(key);
|
||||
|
||||
if (!bucket || now >= bucket.resetAt) {
|
||||
|
|
|
|||
|
|
@ -43,6 +43,11 @@ export function toolNames(): string[] {
|
|||
return REGISTRY.map((t) => t.name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Zeitlimit für das Ergebnis. Der `signal` unten bricht das Werkzeug zusätzlich
|
||||
* ab — beides zusammen, weil ein Werkzeug den Signal auch ignorieren kann und
|
||||
* die Antwort dann trotzdem nicht ewig hängen darf.
|
||||
*/
|
||||
function withTimeout<T>(p: Promise<T>, ms: number, name: string): Promise<T> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const timer = setTimeout(
|
||||
|
|
@ -74,9 +79,26 @@ export async function runTool(call: ToolCall, ctx: ToolContext): Promise<ToolRes
|
|||
};
|
||||
}
|
||||
|
||||
if (tool.requiresConfirmation && !(await isApproved(call, ctx))) {
|
||||
return {
|
||||
name: tool.name,
|
||||
content: JSON.stringify({
|
||||
error:
|
||||
"Vom Nutzer abgelehnt. Nicht erneut aufrufen — ohne dieses Werkzeug " +
|
||||
"weitermachen und sagen, was dadurch fehlt.",
|
||||
}),
|
||||
ok: false,
|
||||
durationMs: Date.now() - started,
|
||||
};
|
||||
}
|
||||
|
||||
// Zeitlimit und Nutzer-Abbruch als ein Signal, das an das Werkzeug geht:
|
||||
// damit endet auch ein laufender Netzwerkabruf, statt weiterzulaufen.
|
||||
const signal = AbortSignal.any([ctx.signal, AbortSignal.timeout(TOOL_TIMEOUT_MS)]);
|
||||
|
||||
try {
|
||||
const value = await withTimeout(
|
||||
tool.run(call.arguments, ctx),
|
||||
tool.run(call.arguments, { ...ctx, signal }),
|
||||
TOOL_TIMEOUT_MS,
|
||||
tool.name,
|
||||
);
|
||||
|
|
@ -98,3 +120,17 @@ export async function runTool(call: ToolCall, ctx: ToolContext): Promise<ToolRes
|
|||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Freigabe für ein bestätigungspflichtiges Werkzeug. Ohne Rückkanal (Skripte,
|
||||
* Tests) gilt "abgelehnt" — die Bestätigung soll sich nicht dadurch umgehen
|
||||
* lassen, dass niemand zum Fragen da ist.
|
||||
*/
|
||||
async function isApproved(call: ToolCall, ctx: ToolContext): Promise<boolean> {
|
||||
if (!ctx.confirm) return false;
|
||||
try {
|
||||
return await ctx.confirm(call);
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ import type { Tool } from "./types.js";
|
|||
export const rememberTool: Tool = {
|
||||
name: "remember",
|
||||
description:
|
||||
"Speichert einen dauerhaft wichtigen Punkt als Ankerpunkt im Gedächtnis des aktuellen Chats. Nur für Fakten, Entscheidungen, Präferenzen oder offene Punkte — nicht für flüchtige Inhalte.",
|
||||
"Speichert einen dauerhaft wichtigen Punkt als Ankerpunkt im Gedächtnis des aktuellen Chats. Nur für Fakten, Entscheidungen, Präferenzen oder offene Punkte — nicht für flüchtige Inhalte. Der Nutzer muss jeden Aufruf freigeben.",
|
||||
requiresConfirmation: true,
|
||||
parameters: {
|
||||
type: "object",
|
||||
|
|
@ -27,6 +27,10 @@ export const rememberTool: Tool = {
|
|||
const text = String(args.text ?? "").trim();
|
||||
if (!ctx.sessionId) throw new ToolError("Keine Sitzung für das Gedächtnis bekannt");
|
||||
if (text.length < 6) throw new ToolError("Text ist zu kurz, um ihn zu merken");
|
||||
// Bewusst ungepinnt: ein vom Modell gesetzter Anker soll dem normalen
|
||||
// Verfall unterliegen. Gepinnt wird nur, was der Nutzer im
|
||||
// Gedächtnis-Panel selbst mit ★ markiert — sonst überlebt ein einmal
|
||||
// untergeschobener "Fakt" jede Traumphase und jede Rekonstruktion.
|
||||
const result = upsertAnchor(ctx.sessionId, {
|
||||
text,
|
||||
kind: ANCHOR_KINDS.includes(args.kind as AnchorKind)
|
||||
|
|
@ -34,7 +38,6 @@ export const rememberTool: Tool = {
|
|||
: "fact",
|
||||
importance: 0.9,
|
||||
origin: "model",
|
||||
pinned: true,
|
||||
});
|
||||
return { stored: result, text };
|
||||
},
|
||||
|
|
|
|||
|
|
@ -10,6 +10,12 @@ export interface ToolContext {
|
|||
signal: AbortSignal;
|
||||
/** Sitzung des aktuellen Chats — für Werkzeuge mit Gedächtniszugriff. */
|
||||
sessionId?: string;
|
||||
/**
|
||||
* Holt die Freigabe des Nutzers für ein Werkzeug mit Außenwirkung.
|
||||
* Fehlt der Rückkanal, werden bestätigungspflichtige Werkzeuge abgelehnt —
|
||||
* lieber nicht ausführen als ungefragt.
|
||||
*/
|
||||
confirm?(call: ToolCall): Promise<boolean>;
|
||||
}
|
||||
|
||||
export interface Tool {
|
||||
|
|
@ -17,9 +23,9 @@ export interface Tool {
|
|||
description: string;
|
||||
parameters: ToolSchema;
|
||||
/**
|
||||
* Kennzeichnet Werkzeuge mit Außenwirkung (schreibend, Netzwerk, Server).
|
||||
* Bisher gibt es nur lesende Werkzeuge; das Feld existiert, damit die
|
||||
* Bestätigungspflicht später nicht nachträglich eingezogen werden muss.
|
||||
* Kennzeichnet Werkzeuge mit Außenwirkung (Netzwerk, dauerhafter Speicher).
|
||||
* `runTool` fragt vor der Ausführung über `ToolContext.confirm` beim Nutzer
|
||||
* nach und lehnt ab, wenn keine Freigabe kommt.
|
||||
*/
|
||||
requiresConfirmation?: boolean;
|
||||
/** Gibt zurück, was dem Modell als Ergebnis gezeigt wird. */
|
||||
|
|
|
|||
|
|
@ -1,5 +1,8 @@
|
|||
import { lookup } from "node:dns/promises";
|
||||
import { isIP } from "node:net";
|
||||
import dns from "node:dns";
|
||||
import { request as httpRequest, type IncomingMessage } from "node:http";
|
||||
import { request as httpsRequest } from "node:https";
|
||||
import { isIP, type LookupFunction } from "node:net";
|
||||
import { createBrotliDecompress, createGunzip, createInflate } from "node:zlib";
|
||||
import { parseHTML } from "linkedom";
|
||||
import { Defuddle } from "defuddle/node";
|
||||
import { ToolError } from "./types.js";
|
||||
|
|
@ -9,6 +12,8 @@ const TIMEOUT_MS = 15_000;
|
|||
const MAX_HTML_BYTES = 2 * 1024 * 1024;
|
||||
const MAX_CONTENT_CHARS = 25_000;
|
||||
const MAX_REDIRECTS = 5;
|
||||
const ALLOWED_TYPES =
|
||||
/text\/html|text\/plain|application\/xhtml|application\/json|application\/xml|text\/markdown/;
|
||||
const USER_AGENT =
|
||||
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0 Safari/537.36 agenttwo-readweb/1.0";
|
||||
|
||||
|
|
@ -39,23 +44,53 @@ function isPrivateIP(ip: string): boolean {
|
|||
first.startsWith("fe9") || first.startsWith("fea") || first.startsWith("feb");
|
||||
}
|
||||
|
||||
/** `new URL().hostname` liefert IPv6-Literale in Klammern: [::1] -> ::1. */
|
||||
function bareHost(hostname: string): string {
|
||||
return hostname.startsWith("[") && hostname.endsWith("]")
|
||||
? hostname.slice(1, -1)
|
||||
: hostname;
|
||||
}
|
||||
|
||||
/**
|
||||
* SSRF-Schutz: Der Server löst den Host selbst auf und weist private Bereiche
|
||||
* ab. Ohne das könnte das Modell http://localhost:8788/api/sessions lesen —
|
||||
* der Origin-Check schützt nicht vor server-eigenem fetch.
|
||||
* DNS-Auflösung, die private Adressen ablehnt — eingehängt als `lookup` der
|
||||
* HTTP-Verbindung.
|
||||
*
|
||||
* Entscheidend ist, dass genau diese Auflösung auch verbunden wird. Ein
|
||||
* getrennter Vorab-Check (wie ihn `fetch` erzwingt, das selbst noch einmal
|
||||
* auflöst) ließe DNS-Rebinding zu: öffentlich bei der Prüfung, 127.0.0.1 beim
|
||||
* Verbinden.
|
||||
*/
|
||||
const guardedLookup: LookupFunction = (hostname, options, callback) => {
|
||||
dns.lookup(hostname, options, (err, address, family) => {
|
||||
if (err) return callback(err, "", 0);
|
||||
const addresses = Array.isArray(address) ? address : [{ address, family }];
|
||||
for (const a of addresses) {
|
||||
if (isPrivateIP(a.address)) {
|
||||
return callback(new ToolError("Zugriff auf private Adressen ist gesperrt"), "", 0);
|
||||
}
|
||||
}
|
||||
callback(null, address as string, family);
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Vorab-Prüfung, rein für die Fehlermeldung: so bekommt das Modell "private
|
||||
* Adresse gesperrt" statt eines generischen Verbindungsfehlers. Die
|
||||
* verbindliche Grenze ist `guardedLookup`.
|
||||
*/
|
||||
async function assertPublicHost(hostname: string): Promise<void> {
|
||||
if (isIP(hostname)) {
|
||||
if (isPrivateIP(hostname)) throw new ToolError("Zugriff auf private Adressen ist gesperrt");
|
||||
const host = bareHost(hostname);
|
||||
if (isIP(host)) {
|
||||
if (isPrivateIP(host)) throw new ToolError("Zugriff auf private Adressen ist gesperrt");
|
||||
return;
|
||||
}
|
||||
let addrs: { address: string }[];
|
||||
try {
|
||||
addrs = await lookup(hostname, { all: true, verbatim: true });
|
||||
addrs = await dns.promises.lookup(host, { all: true, verbatim: true });
|
||||
} catch {
|
||||
throw new ToolError(`Host nicht auflösbar: ${hostname}`);
|
||||
throw new ToolError(`Host nicht auflösbar: ${host}`);
|
||||
}
|
||||
if (addrs.length === 0) throw new ToolError(`Host nicht auflösbar: ${hostname}`);
|
||||
if (addrs.length === 0) throw new ToolError(`Host nicht auflösbar: ${host}`);
|
||||
for (const a of addrs) {
|
||||
if (isPrivateIP(a.address)) {
|
||||
throw new ToolError("Zugriff auf private Adressen ist gesperrt");
|
||||
|
|
@ -76,49 +111,93 @@ function assertHttpUrl(raw: string): URL {
|
|||
return url;
|
||||
}
|
||||
|
||||
async function fetchWithGuards(rawUrl: string): Promise<{ url: string; body: string }> {
|
||||
let url = assertHttpUrl(rawUrl).toString();
|
||||
/** Ein GET mit gepinnter Auflösung. Weiterleitungen bleiben Sache des Aufrufers. */
|
||||
function send(url: URL, signal: AbortSignal): Promise<IncomingMessage> {
|
||||
const request = url.protocol === "https:" ? httpsRequest : httpRequest;
|
||||
return new Promise((resolve, reject) => {
|
||||
const req = request(
|
||||
url,
|
||||
{
|
||||
method: "GET",
|
||||
lookup: guardedLookup,
|
||||
signal,
|
||||
timeout: TIMEOUT_MS,
|
||||
headers: {
|
||||
"User-Agent": USER_AGENT,
|
||||
Accept: "text/html, text/plain, application/xhtml+xml",
|
||||
"Accept-Encoding": "gzip, deflate, br",
|
||||
},
|
||||
},
|
||||
resolve,
|
||||
);
|
||||
req.on("timeout", () => req.destroy(new ToolError("Zeitlimit beim Abruf überschritten")));
|
||||
req.on("error", (err) => {
|
||||
if (err instanceof ToolError) return reject(err);
|
||||
if (signal.aborted) return reject(new ToolError("Abruf abgebrochen"));
|
||||
reject(new ToolError(`Abruf fehlgeschlagen: ${url.host}`));
|
||||
});
|
||||
req.end();
|
||||
});
|
||||
}
|
||||
|
||||
/** Antwortkörper bis MAX_HTML_BYTES lesen, komprimierte Antworten auspacken. */
|
||||
async function readCapped(res: IncomingMessage): Promise<string> {
|
||||
const encoding = String(res.headers["content-encoding"] ?? "").toLowerCase();
|
||||
const stream =
|
||||
encoding === "gzip" ? res.pipe(createGunzip())
|
||||
: encoding === "deflate" ? res.pipe(createInflate())
|
||||
: encoding === "br" ? res.pipe(createBrotliDecompress())
|
||||
: res;
|
||||
|
||||
const decoder = new TextDecoder();
|
||||
let html = "";
|
||||
let bytes = 0;
|
||||
try {
|
||||
for await (const chunk of stream as AsyncIterable<Buffer>) {
|
||||
bytes += chunk.byteLength;
|
||||
html += decoder.decode(chunk, { stream: true });
|
||||
if (bytes > MAX_HTML_BYTES) break;
|
||||
}
|
||||
} catch {
|
||||
// Abbruch mitten im Strom: was schon da ist, reicht dem Extraktor meist.
|
||||
if (!html) throw new ToolError("Antwort konnte nicht gelesen werden");
|
||||
} finally {
|
||||
res.destroy();
|
||||
}
|
||||
return html;
|
||||
}
|
||||
|
||||
async function fetchWithGuards(
|
||||
rawUrl: string,
|
||||
signal: AbortSignal,
|
||||
): Promise<{ url: string; body: string }> {
|
||||
let url = assertHttpUrl(rawUrl);
|
||||
|
||||
for (let hop = 0; hop <= MAX_REDIRECTS; hop++) {
|
||||
await assertPublicHost(new URL(url).hostname);
|
||||
await assertPublicHost(url.hostname);
|
||||
|
||||
const res = await fetch(url, {
|
||||
redirect: "manual",
|
||||
headers: { "User-Agent": USER_AGENT, Accept: "text/html, text/plain, application/xhtml+xml" },
|
||||
signal: AbortSignal.timeout(TIMEOUT_MS),
|
||||
});
|
||||
const res = await send(url, signal);
|
||||
const status = res.statusCode ?? 0;
|
||||
|
||||
if (res.status >= 300 && res.status < 400) {
|
||||
const location = res.headers.get("location");
|
||||
if (!location) break;
|
||||
url = new URL(location, url).toString();
|
||||
assertHttpUrl(url);
|
||||
if (status >= 300 && status < 400) {
|
||||
const location = res.headers.location;
|
||||
res.destroy();
|
||||
if (!location) throw new ToolError(`Weiterleitung ohne Ziel (HTTP ${status})`);
|
||||
url = assertHttpUrl(new URL(location, url).toString());
|
||||
continue;
|
||||
}
|
||||
if (!res.ok) throw new ToolError(`HTTP ${res.status} für ${url}`);
|
||||
if (status < 200 || status >= 300) {
|
||||
res.destroy();
|
||||
throw new ToolError(`HTTP ${status} für ${url}`);
|
||||
}
|
||||
|
||||
const type = (res.headers.get("content-type") ?? "").toLowerCase();
|
||||
if (!/text\/html|text\/plain|application\/xhtml|application\/json|application\/xml|text\/markdown/.test(type)) {
|
||||
const type = String(res.headers["content-type"] ?? "").toLowerCase();
|
||||
if (!ALLOWED_TYPES.test(type)) {
|
||||
res.destroy();
|
||||
throw new ToolError(`Nicht unterstützter Inhaltstyp: ${type || "unbekannt"}`);
|
||||
}
|
||||
|
||||
const reader = res.body?.getReader();
|
||||
if (!reader) throw new ToolError("Leere Antwort");
|
||||
const decoder = new TextDecoder();
|
||||
let html = "";
|
||||
let bytes = 0;
|
||||
for (;;) {
|
||||
const { done, value } = await reader.read();
|
||||
if (done) break;
|
||||
bytes += value.byteLength;
|
||||
if (bytes > MAX_HTML_BYTES) {
|
||||
void reader.cancel();
|
||||
html += decoder.decode(value, { stream: true });
|
||||
break;
|
||||
}
|
||||
html += decoder.decode(value, { stream: true });
|
||||
}
|
||||
return { url, body: html };
|
||||
return { url: url.toString(), body: await readCapped(res) };
|
||||
}
|
||||
throw new ToolError(`Zu viele Weiterleitungen (> ${MAX_REDIRECTS})`);
|
||||
}
|
||||
|
|
@ -126,7 +205,10 @@ async function fetchWithGuards(rawUrl: string): Promise<{ url: string; body: str
|
|||
export const readWebpageTool: Tool = {
|
||||
name: "read_webpage",
|
||||
description:
|
||||
"Liest eine öffentliche Website und gibt den Hauptinhalt als Markdown zurück (Titel, Autor, Text). Nur für öffentliche URLs — lokale/private Adressen werden abgewiesen.",
|
||||
"Liest eine öffentliche Website und gibt den Hauptinhalt als Markdown zurück (Titel, Autor, Text). Nur für öffentliche URLs — lokale/private Adressen werden abgewiesen. Der Nutzer muss jeden Aufruf freigeben.",
|
||||
// Der Abruf verlässt den Rechner: die URL selbst ist ein Kanal nach außen.
|
||||
// Deshalb sieht der Nutzer sie vor dem Aufruf und gibt sie frei.
|
||||
requiresConfirmation: true,
|
||||
parameters: {
|
||||
type: "object",
|
||||
properties: {
|
||||
|
|
@ -134,11 +216,11 @@ export const readWebpageTool: Tool = {
|
|||
},
|
||||
required: ["url"],
|
||||
},
|
||||
async run(args) {
|
||||
async run(args, ctx) {
|
||||
const raw = String(args.url ?? "").trim();
|
||||
if (!raw) throw new ToolError("url fehlt");
|
||||
|
||||
const { url, body } = await fetchWithGuards(raw);
|
||||
const { url, body } = await fetchWithGuards(raw, ctx.signal);
|
||||
const { document } = parseHTML(body);
|
||||
const result = await Defuddle(document, url, { markdown: true });
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue