Add link labyrinth, static robots.txt and robots bait paths
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
b7027e2a72
commit
a25072e6fb
29 files changed
+774
-34
No files matched your search
@@ -18,7 +18,6 @@ public sealed class FileContentTests
|
||||
[InlineData("/docker-compose.yml", "yaml-drip")]
|
||||
[InlineData("/backup/db.sql", "sql-drip")]
|
||||
[InlineData("/.git/config", "git-drip")]
|
||||
[InlineData("/robots.txt", "robots-drip")]
|
||||
[InlineData("/SDK/webLanguage", "hikvision-drip")]
|
||||
[InlineData("/ISAPI/System/deviceInfo", "hikvision-drip")]
|
||||
[InlineData("/wp-login.php", "wordpress-drip")]
|
||||
@@ -42,7 +41,6 @@ public sealed class FileContentTests
|
||||
[InlineData("/docker-compose.yml", "version:")]
|
||||
[InlineData("/dump.sql", "-- MySQL dump")]
|
||||
[InlineData("/.git/config", "[core]")]
|
||||
[InlineData("/robots.txt", "User-agent:")]
|
||||
[InlineData("/SDK/webLanguage", "<?xml")]
|
||||
[InlineData("/wp-login.php", "<!DOCTYPE html>")]
|
||||
public void Generates_an_endless_file_that_starts_like_the_real_thing(string path, string start)
|
||||
@@ -62,8 +60,24 @@ public sealed class FileContentTests
|
||||
var context = new TrapContext(session, "GET /.env", Path: "/.env");
|
||||
|
||||
Selector(matchPath: true).Select(context).ShouldNotBeNull().Name.ShouldBe("env-drip");
|
||||
Selector(matchPath: true).Select(context with { Path = "/" }).ShouldNotBeNull().Name.ShouldBe("html-drip");
|
||||
Selector(matchPath: false).Select(context).ShouldNotBeNull().Name.ShouldBe("html-drip");
|
||||
Selector(matchPath: true).Select(context with { Path = "/" }).ShouldNotBeNull().Name.ShouldBe("maze-drip");
|
||||
Selector(matchPath: false).Select(context).ShouldNotBeNull().Name.ShouldBe("maze-drip");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Robots_txt_is_served_completely_and_at_once_even_without_path_matching()
|
||||
{
|
||||
var session = new TrapSession(Guid.NewGuid(), TrapProtocol.Http, IPAddress.Parse("203.0.113.9"), DateTimeOffset.UtcNow);
|
||||
var context = new TrapContext(session, "GET /robots.txt", Path: "/Robots.txt");
|
||||
|
||||
var strategy = Selector(matchPath: false).Select(context).ShouldNotBeNull();
|
||||
var channel = new FakeTrapChannel();
|
||||
await strategy.RunAsync(channel, CancellationToken.None);
|
||||
|
||||
strategy.Name.ShouldBe("robots-static");
|
||||
channel.Writes.Count.ShouldBe(1);
|
||||
Encoding.UTF8.GetString(channel.Writes.Single())
|
||||
.ShouldBe("User-agent: *\nDisallow: /\nDisallow: /backup-2024/\nDisallow: /admin-old/\nDisallow: /internal/db-export/\nDisallow: /old-site/\n");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -89,7 +103,11 @@ public sealed class FileContentTests
|
||||
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 TextDripStrategy(options, random, TimeProvider.System),
|
||||
new RobotsStaticStrategy(options),
|
||||
new MazeDripStrategy(MazeKind.Bait, options, random, TimeProvider.System),
|
||||
.. FileContent.Profiles.Select(profile => new FileDripStrategy(profile, options, random, TimeProvider.System))
|
||||
],
|
||||
options,
|
||||
|
||||
@@ -6,7 +6,7 @@ namespace Flypaper.Tests.Core;
|
||||
|
||||
public sealed class HoldMethodParserTests
|
||||
{
|
||||
private static readonly HoldMethodInput Valid = new("Alternating", "50", "1000", "1", "64", "2048", true, "01:00:00");
|
||||
private static readonly HoldMethodInput Valid = new("Alternating", "50", "1000", "1", "64", "2048", true, "80", "/Backup-2024, /admin-old/x", "01:00:00");
|
||||
|
||||
[Fact]
|
||||
public void Parses_valid_input()
|
||||
@@ -20,6 +20,8 @@ public sealed class HoldMethodParserTests
|
||||
settings.MaxChunkSize.ShouldBe(64);
|
||||
settings.HeadStartSize.ShouldBe(2048);
|
||||
settings.MatchPath.ShouldBeTrue();
|
||||
settings.LinkSpacing.ShouldBe(80);
|
||||
settings.BaitPaths.ShouldBe("/backup-2024/,/admin-old/x/");
|
||||
settings.MaxHoldDuration.ShouldBe(TimeSpan.FromHours(1));
|
||||
settings.ToConfiguration().Keys.ShouldBe(SettingKeys.HoldMethod, ignoreOrder: true);
|
||||
}
|
||||
@@ -71,6 +73,34 @@ public sealed class HoldMethodParserTests
|
||||
public void Rejects_head_start_out_of_range(string value) =>
|
||||
RuntimeSettingsParser.ParseHoldMethod(Valid with { HeadStartSize = value }).Errors.Keys.ShouldBe([nameof(HoldMethodInput.HeadStartSize)]);
|
||||
|
||||
[Theory]
|
||||
[InlineData("0")]
|
||||
[InlineData("4096")]
|
||||
public void Accepts_link_spacing_bounds(string value) =>
|
||||
RuntimeSettingsParser.ParseHoldMethod(Valid with { LinkSpacing = value }).Settings.ShouldNotBeNull();
|
||||
|
||||
[Theory]
|
||||
[InlineData("-1")]
|
||||
[InlineData("4097")]
|
||||
[InlineData("")]
|
||||
public void Rejects_link_spacing_out_of_range(string value) =>
|
||||
RuntimeSettingsParser.ParseHoldMethod(Valid with { LinkSpacing = value }).Errors.Keys.ShouldBe([nameof(HoldMethodInput.LinkSpacing)]);
|
||||
|
||||
[Theory]
|
||||
[InlineData("")]
|
||||
[InlineData("/a/,/b/")]
|
||||
public void Accepts_bait_paths(string value) =>
|
||||
RuntimeSettingsParser.ParseHoldMethod(Valid with { BaitPaths = value }).Settings.ShouldNotBeNull();
|
||||
|
||||
[Theory]
|
||||
[InlineData("backup")]
|
||||
[InlineData("/robots.txt")]
|
||||
[InlineData("/wiki/old/")]
|
||||
[InlineData("/a b<>/")]
|
||||
[InlineData("/a/,/b/,/c/,/d/,/e/,/f/,/g/,/h/,/i/")]
|
||||
public void Rejects_invalid_bait_paths(string value) =>
|
||||
RuntimeSettingsParser.ParseHoldMethod(Valid with { BaitPaths = value }).Errors.Keys.ShouldBe([nameof(HoldMethodInput.BaitPaths)]);
|
||||
|
||||
[Theory]
|
||||
[InlineData("10")]
|
||||
[InlineData("00:00:00")]
|
||||
|
||||
@@ -0,0 +1,217 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user