feat: harden gateway reply remap and leaf loop transparency
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user