218 lines
9.5 KiB
C#
218 lines
9.5 KiB
C#
using System.Net;
|
|
using System.Text;
|
|
using System.Text.RegularExpressions;
|
|
using Flypaper.Core.Models;
|
|
using Flypaper.Core.Runtime;
|
|
using Flypaper.Core.Strategies;
|
|
using Flypaper.Tests.Fakes;
|
|
using Shouldly;
|
|
|
|
namespace Flypaper.Tests.Core;
|
|
|
|
public sealed partial class MazeContentTests
|
|
{
|
|
[GeneratedRegex("href=\"([^\"]*)\"")]
|
|
private static partial Regex Href();
|
|
|
|
private static List<string> Links(string html) => Href().Matches(html).Select(match => match.Groups[1].Value).ToList();
|
|
|
|
[Fact]
|
|
public void Head_start_already_carries_many_links()
|
|
{
|
|
using var fragments = MazeContent.Page(new SeededRandomSource(1), "/wiki/lorem", "example.com", 160).GetEnumerator();
|
|
|
|
var head = Encoding.UTF8.GetString(DripSchedule.TakeHead(fragments, 1024).Span);
|
|
|
|
head.Length.ShouldBeGreaterThanOrEqualTo(1024);
|
|
Links(head).Count.ShouldBeGreaterThanOrEqualTo(10);
|
|
}
|
|
|
|
[Fact]
|
|
public void Keeps_delivering_links_at_the_configured_spacing()
|
|
{
|
|
var body = string.Concat(MazeContent.Page(new SeededRandomSource(2), "/wiki/lorem", "example.com", 160).Skip(4).Take(40));
|
|
var tight = string.Concat(MazeContent.Page(new SeededRandomSource(2), "/wiki/lorem", "example.com", 40).Skip(4).Take(40));
|
|
|
|
var bytesPerLink = Encoding.UTF8.GetByteCount(body) / (double)Links(body).Count;
|
|
var tightBytesPerLink = Encoding.UTF8.GetByteCount(tight) / (double)Links(tight).Count;
|
|
|
|
bytesPerLink.ShouldBeInRange(100, 450);
|
|
tightBytesPerLink.ShouldBeLessThan(bytesPerLink);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("/")]
|
|
[InlineData("/index.php")]
|
|
[InlineData("//images/images/cache.php")]
|
|
public void Pages_outside_the_roots_only_link_into_the_disallowed_roots(string path)
|
|
{
|
|
var page = string.Concat(MazeContent.Page(new SeededRandomSource(3), path, "example.com", 80).Take(300));
|
|
var baseUri = new Uri("http://example.com" + path);
|
|
|
|
foreach (var href in Links(page))
|
|
{
|
|
var target = new Uri(baseUri, href);
|
|
target.Host.ShouldBe("example.com");
|
|
MazeContent.Roots.ShouldContain(target.AbsolutePath.Split('/', StringSplitOptions.RemoveEmptyEntries).First());
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void Listings_below_a_root_never_leave_the_root()
|
|
{
|
|
var listing = string.Concat(MazeContent.Listing(new SeededRandomSource(3), "/files/lorem/", "example.com").Take(200));
|
|
var baseUri = new Uri("http://example.com/files/lorem/");
|
|
|
|
foreach (var href in Links(listing).Where(href => href != "../"))
|
|
{
|
|
new Uri(baseUri, href).AbsolutePath.Split('/', StringSplitOptions.RemoveEmptyEntries).First().ShouldBeOneOf(MazeContent.Roots);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void Relative_links_below_a_root_never_leave_the_root()
|
|
{
|
|
var page = string.Concat(MazeContent.Page(new SeededRandomSource(4), "/blog/2025/03/lorem", "example.com", 80).Take(300));
|
|
var baseUri = new Uri("http://example.com/blog/2025/03/lorem");
|
|
|
|
foreach (var href in Links(page).Where(href => !href.StartsWith('/') && !href.StartsWith("http", StringComparison.Ordinal)))
|
|
{
|
|
new Uri(baseUri, href).AbsolutePath.ShouldStartWith("/blog/");
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void Mixes_relative_and_absolute_links_and_limits_depth()
|
|
{
|
|
var links = Links(string.Concat(MazeContent.Page(new SeededRandomSource(5), "/docs/lorem", "example.com", 80).Take(300)));
|
|
|
|
links.ShouldContain(href => href.StartsWith("http://example.com/docs/", StringComparison.Ordinal));
|
|
links.ShouldContain(href => href.StartsWith("/docs/", StringComparison.Ordinal));
|
|
links.ShouldContain(href => !href.StartsWith('/') && !href.StartsWith("http", StringComparison.Ordinal));
|
|
links.ShouldContain(href => href.StartsWith("/blog/20", StringComparison.Ordinal));
|
|
|
|
var deepPath = "/docs/" + string.Join('/', Enumerable.Repeat("x", 8)) + "/";
|
|
var deep = Links(string.Concat(MazeContent.Page(new SeededRandomSource(6), deepPath, null, 80).Take(300)));
|
|
deep.ShouldContain("../");
|
|
deep.ShouldNotContain(href => href.StartsWith("http", StringComparison.Ordinal));
|
|
}
|
|
|
|
[Fact]
|
|
public void Ignores_a_hostile_host_header()
|
|
{
|
|
var page = string.Concat(MazeContent.Page(new SeededRandomSource(7), "/wiki/a", "evil\"><script>", 80).Take(100));
|
|
|
|
page.ShouldNotContain("<script>");
|
|
Links(page).ShouldNotContain(href => href.StartsWith("http", StringComparison.Ordinal));
|
|
}
|
|
|
|
[Fact]
|
|
public void Listing_looks_like_a_directory_index_and_escapes_the_path()
|
|
{
|
|
var text = string.Concat(MazeContent.Listing(new SeededRandomSource(8), "/backup/<b>/", "example.com").Take(60));
|
|
|
|
text.ShouldContain("<title>Index of /backup/<b>/</title>");
|
|
text.ShouldContain("<a href=\"../\">Parent Directory</a>");
|
|
Links(text).Count.ShouldBeGreaterThan(40);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("/wiki/", "index-drip")]
|
|
[InlineData("/", "maze-drip")]
|
|
[InlineData("/wiki/lorem", "maze-drip")]
|
|
public void Selector_uses_listing_only_for_directories(string path, string expected)
|
|
{
|
|
var context = new TrapContext(new TrapSession(Guid.NewGuid(), TrapProtocol.Http, IPAddress.Parse("203.0.113.9"), DateTimeOffset.UtcNow), Path: path);
|
|
|
|
Selector(160).Select(context).ShouldNotBeNull().Name.ShouldBe(expected);
|
|
Selector(0).Select(context).ShouldNotBeNull().Name.ShouldBe("html-drip");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Strategy_sends_links_in_the_head_start_and_names_its_spacing()
|
|
{
|
|
var options = TestOptions.Monitor(new DripOptions { MinDelay = TimeSpan.FromSeconds(30), MaxDelay = TimeSpan.FromSeconds(30) });
|
|
var strategy = new MazeDripStrategy(MazeKind.Page, options, new SeededRandomSource(9), TimeProvider.System);
|
|
var channel = new FakeTrapChannel { Path = "/wiki/lorem", Host = "example.com" };
|
|
using var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(200));
|
|
|
|
await Should.ThrowAsync<OperationCanceledException>(() => strategy.RunAsync(channel, cts.Token));
|
|
|
|
Links(Encoding.UTF8.GetString(channel.Writes.First())).Count.ShouldBeGreaterThanOrEqualTo(10);
|
|
strategy.Describe().ShouldBe("maze-drip 30000-30000ms 1-64B head 1024B link 160B");
|
|
strategy.MediaType.ShouldStartWith("text/html");
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("/backup-2024/", true)]
|
|
[InlineData("/Backup-2024/old/dump.zip", true)]
|
|
[InlineData("/backup-2024", true)]
|
|
[InlineData("/internal/db-export/x", true)]
|
|
[InlineData("/internal/", false)]
|
|
[InlineData("/backup-20245/", false)]
|
|
[InlineData("/backup/", false)]
|
|
public void Bait_paths_and_everything_below_get_robots_bait(string path, bool bait)
|
|
{
|
|
var context = new TrapContext(new TrapSession(Guid.NewGuid(), TrapProtocol.Http, IPAddress.Parse("203.0.113.9"), DateTimeOffset.UtcNow), Path: path);
|
|
|
|
Selector(160).Select(context).ShouldNotBeNull().Name.ShouldBe(bait ? "robots-bait" : path.EndsWith('/') ? "index-drip" : "maze-drip");
|
|
Selector(0).Select(context).ShouldNotBeNull().Name.ShouldBe(bait ? "robots-bait" : "html-drip");
|
|
}
|
|
|
|
[Fact]
|
|
public void Bait_paths_are_never_linked_from_the_maze()
|
|
{
|
|
var text = string.Concat(MazeContent.Page(new SeededRandomSource(12), "/", "example.com", 40).Take(400))
|
|
+ string.Concat(MazeContent.Listing(new SeededRandomSource(12), "/files/", "example.com").Take(400));
|
|
|
|
foreach (var bait in BaitPaths.Parse(BaitPaths.Default))
|
|
{
|
|
text.ShouldNotContain(bait.TrimEnd('/'));
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void Bait_strategy_extends_the_hold_and_keeps_the_maze_content()
|
|
{
|
|
var options = TestOptions.Monitor(new DripOptions());
|
|
var bait = new MazeDripStrategy(MazeKind.Bait, options, new SeededRandomSource(1), TimeProvider.System);
|
|
|
|
bait.MinimumHold.ShouldBe(TimeSpan.FromHours(24));
|
|
bait.Describe().ShouldStartWith("robots-bait ");
|
|
new MazeDripStrategy(MazeKind.Page, options, new SeededRandomSource(1), TimeProvider.System).MinimumHold.ShouldBe(TimeSpan.Zero);
|
|
}
|
|
|
|
[Fact]
|
|
public void Empty_bait_paths_switch_the_bait_off()
|
|
{
|
|
var context = new TrapContext(new TrapSession(Guid.NewGuid(), TrapProtocol.Http, IPAddress.Parse("203.0.113.9"), DateTimeOffset.UtcNow), Path: "/backup-2024/");
|
|
var options = TestOptions.Monitor(new DripOptions { BaitPaths = "" });
|
|
var random = new SeededRandomSource(1);
|
|
var selector = new ConfiguredStrategySelector(
|
|
[
|
|
new MazeDripStrategy(MazeKind.Page, options, random, TimeProvider.System),
|
|
new MazeDripStrategy(MazeKind.Listing, options, random, TimeProvider.System),
|
|
new MazeDripStrategy(MazeKind.Bait, options, random, TimeProvider.System)
|
|
],
|
|
options,
|
|
random);
|
|
|
|
selector.Select(context).ShouldNotBeNull().Name.ShouldBe("index-drip");
|
|
}
|
|
|
|
private static ConfiguredStrategySelector Selector(int spacing)
|
|
{
|
|
var options = TestOptions.Monitor(new DripOptions { LinkSpacing = spacing });
|
|
var random = new SeededRandomSource(11);
|
|
return new ConfiguredStrategySelector(
|
|
[
|
|
new HtmlDripStrategy(options, random, TimeProvider.System),
|
|
new MazeDripStrategy(MazeKind.Page, options, random, TimeProvider.System),
|
|
new MazeDripStrategy(MazeKind.Listing, options, random, TimeProvider.System),
|
|
new MazeDripStrategy(MazeKind.Bait, options, random, TimeProvider.System)
|
|
],
|
|
options,
|
|
random);
|
|
}
|
|
}
|