Umbau durch Gemini Flash, Durchsicht und Nacharbeit durch Claude. Umfang: - Desktop: LocalizationManager (JSON-Pakete, Rueckfall auf Englisch, Umschalten zur Laufzeit), Localize-Markup-Erweiterung, Sprachwahl in den Einstellungen (AppSettings.Language, Vorgabe "en"); 317 Schluessel je Sprache - WebUI: alle Ansichten auf t()/data-i18n, 656 Schluessel je Sprache - Agenten-Werkzeuge: Beschreibungen, Schemata und Meldungen auf Englisch - Tests an die englischen Meldungen angepasst, keine Pruefung abgeschwaecht Nacharbeit bei der Durchsicht: - ~25 Kommentarbloecke mit Begruendungen waren ersatzlos entfallen, darunter sicherheitsrelevante (NTFS-Datenstrom beim shared:-Praefix, Bremse gegen Agentenschleifen, doppelter POST beim Anlegen einer Instanz). Wieder eingesetzt: auf Englisch, wo die Datei umgestellt ist, sonst im Wortlaut. - Drei WebUI-Schluessel fehlten (logs.paused, instances.instanceOfflineError, instances.noInstanceActiveError). Weil t() bei fehlendem Schluessel den Schluessel selbst liefert, griff der "|| err.message"-Rueckfall nie — die Oberflaeche haette den Schluesselnamen angezeigt. Geprueft: Schluesselgleichheit en/de, Platzhalter je Schluessel, jeder im Code benutzte Schluessel vorhanden, node --check fuer alle Module, 1.220 Tests gruen. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
149 lines
4.6 KiB
C#
149 lines
4.6 KiB
C#
using System.Text.RegularExpressions;
|
|
|
|
namespace ClawdDotNet.Tools.SocialMediaManager;
|
|
|
|
/// <summary>
|
|
/// Prüft und normalisiert YouTube-Adressen, bevor sie an yt-dlp übergeben werden.
|
|
///
|
|
/// Hintergrund (S2): Die Eingabe wurde ungeprüft in eine Argument-Zeichenkette
|
|
/// interpoliert. <c>UseShellExecute = false</c> verhindert zwar Shell-Metazeichen,
|
|
/// nicht aber Options-Injection: yt-dlp kennt <c>--exec</c>, das beliebige Befehle nach
|
|
/// dem Download ausführt. Ein Wert wie
|
|
/// <c>--exec "cmd /c …" https://youtube.com/…</c> führte damit zu Codeausführung.
|
|
///
|
|
/// Kritisch ist das, weil der Agent untrusted Inhalte verarbeitet (Videotitel, Posts,
|
|
/// Mails) — eine Prompt-Injection darin kann ihn dazu bringen, genau so einen Wert zu
|
|
/// setzen.
|
|
/// </summary>
|
|
public static class YouTubeUrl
|
|
{
|
|
private static readonly string[] AllowedHosts =
|
|
[
|
|
"youtube.com", "www.youtube.com", "m.youtube.com",
|
|
"music.youtube.com", "youtu.be", "www.youtu.be"
|
|
];
|
|
|
|
/// <summary>Handles wie "@ChannelName" — bewusst eng gefasst.</summary>
|
|
private static readonly Regex HandlePattern = new(
|
|
@"^@?[A-Za-z0-9._-]{1,100}$", RegexOptions.Compiled);
|
|
|
|
/// <summary>
|
|
/// Löst eine Channel-Angabe zu einer vollständigen URL auf.
|
|
/// Akzeptiert ein Handle ("@Name" oder "Name") oder eine YouTube-URL.
|
|
/// </summary>
|
|
public static bool TryResolveChannelUrl(string? input, out string url, out string? error)
|
|
{
|
|
url = "";
|
|
error = null;
|
|
|
|
var value = input?.Trim() ?? "";
|
|
|
|
if (value.Length == 0)
|
|
{
|
|
error = "Channel specification missing.";
|
|
return false;
|
|
}
|
|
|
|
// Reject anything that looks like an option immediately
|
|
if (value.StartsWith('-'))
|
|
{
|
|
error = "Channel specification must not start with '-'.";
|
|
return false;
|
|
}
|
|
|
|
if (value.StartsWith("http://", StringComparison.OrdinalIgnoreCase) ||
|
|
value.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
if (!TryValidateUrl(value, out error))
|
|
return false;
|
|
|
|
url = value;
|
|
return true;
|
|
}
|
|
|
|
if (!HandlePattern.IsMatch(value))
|
|
{
|
|
error = "Invalid channel handle. Allowed are letters, digits, dots, hyphens, and underscores.";
|
|
return false;
|
|
}
|
|
|
|
var handle = value.StartsWith('@') ? value : "@" + value;
|
|
url = $"https://www.youtube.com/{handle}/videos";
|
|
return true;
|
|
}
|
|
|
|
/// <summary>Validates a video or channel URL against allowed hosts.</summary>
|
|
public static bool TryValidateUrl(string? input, out string? error)
|
|
{
|
|
error = null;
|
|
var value = input?.Trim() ?? "";
|
|
|
|
if (value.StartsWith('-'))
|
|
{
|
|
error = "URL must not start with '-'.";
|
|
return false;
|
|
}
|
|
|
|
if (!Uri.TryCreate(value, UriKind.Absolute, out var uri))
|
|
{
|
|
error = "Not a valid URL.";
|
|
return false;
|
|
}
|
|
|
|
if (uri.Scheme != Uri.UriSchemeHttp && uri.Scheme != Uri.UriSchemeHttps)
|
|
{
|
|
error = $"Unsupported scheme '{uri.Scheme}'. Allowed are http and https.";
|
|
return false;
|
|
}
|
|
|
|
if (!AllowedHosts.Contains(uri.Host, StringComparer.OrdinalIgnoreCase))
|
|
{
|
|
error = $"Host '{uri.Host}' is not allowed. Only YouTube URLs are permitted.";
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Baut die Argumentliste, um die neueste Video-Id eines Kanals abzufragen.
|
|
/// Die Liste wird an <c>ProcessStartInfo.ArgumentList</c> übergeben, wo jedes
|
|
/// Element als genau ein Argument ankommt.
|
|
/// </summary>
|
|
public static List<string> BuildLatestVideoIdArgs(string channelUrl) =>
|
|
[
|
|
"--print", "%(id)s",
|
|
"--playlist-end", "1",
|
|
"--", // beendet die Optionsliste
|
|
channelUrl
|
|
];
|
|
|
|
/// <summary>Baut die Argumentliste für den Audio-Download.</summary>
|
|
public static List<string> BuildAudioDownloadArgs(
|
|
string outputPath, string videoUrl, string? ffmpegDirectory, bool convertToMp3)
|
|
{
|
|
var args = new List<string>();
|
|
|
|
if (!string.IsNullOrEmpty(ffmpegDirectory))
|
|
{
|
|
args.Add("--ffmpeg-location");
|
|
args.Add(ffmpegDirectory);
|
|
}
|
|
|
|
args.Add("-x");
|
|
|
|
if (convertToMp3)
|
|
{
|
|
args.Add("--audio-format");
|
|
args.Add("mp3");
|
|
}
|
|
|
|
args.Add("-o");
|
|
args.Add(outputPath);
|
|
args.Add("--");
|
|
args.Add(videoUrl);
|
|
|
|
return args;
|
|
}
|
|
}
|