feat: harden gateway reply remap and leaf loop transparency

This commit is contained in:
Joseph Doherty
2026-02-23 14:40:07 -05:00
parent d83b37fec1
commit 958c4aa8ed
5 changed files with 70 additions and 10 deletions

View File

@@ -4,6 +4,9 @@ public static class LeafLoopDetector
{
private const string LeafLoopPrefix = "$LDS.";
public static bool HasLoopMarker(string subject)
=> subject.StartsWith(LeafLoopPrefix, StringComparison.Ordinal);
public static string Mark(string subject, string serverId)
=> $"{LeafLoopPrefix}{serverId}.{subject}";
@@ -13,14 +16,20 @@ public static class LeafLoopDetector
public static bool TryUnmark(string subject, out string unmarked)
{
unmarked = subject;
if (!subject.StartsWith(LeafLoopPrefix, StringComparison.Ordinal))
if (!HasLoopMarker(subject))
return false;
var serverSeparator = subject.IndexOf('.', LeafLoopPrefix.Length);
if (serverSeparator < 0 || serverSeparator == subject.Length - 1)
return false;
var current = subject;
while (HasLoopMarker(current))
{
var serverSeparator = current.IndexOf('.', LeafLoopPrefix.Length);
if (serverSeparator < 0 || serverSeparator == current.Length - 1)
return false;
unmarked = subject[(serverSeparator + 1)..];
current = current[(serverSeparator + 1)..];
}
unmarked = current;
return true;
}
}