using System.Net; using System.Text; using Flypaper.Core.Models; using Flypaper.Core.Runtime; using Flypaper.Core.Strategies; using Flypaper.Tests.Fakes; using Shouldly; namespace Flypaper.Tests.Core; public sealed class FileContentTests { [Theory] [InlineData("/.env", "env-drip")] [InlineData("/app/.ENV.production", "env-drip")] [InlineData("/config.json", "json-drip")] [InlineData("/sitemap.xml", "xml-drip")] [InlineData("/docker-compose.yml", "yaml-drip")] [InlineData("/backup/db.sql", "sql-drip")] [InlineData("/.git/config", "git-drip")] [InlineData("/.git/HEAD", "git-drip")] [InlineData("/app/.git/logs/HEAD", "git-drip")] [InlineData("/.git", "git-drip")] [InlineData("/.gitconfig", "git-drip")] [InlineData("/.git-credentials", "git-drip")] [InlineData("/home/.GITCONFIG", "git-drip")] [InlineData("/.svn/format", "svn-drip")] [InlineData("/.svn/wc.db", "svn-drip")] [InlineData("/.svn/entries", "svn-drip")] [InlineData("/site/.svn", "svn-drip")] [InlineData("/.hg/requires", "hg-drip")] [InlineData("/.hg/store/requires", "hg-drip")] [InlineData("/.hg", "hg-drip")] [InlineData("/SDK/webLanguage", "hikvision-drip")] [InlineData("/ISAPI/System/deviceInfo", "hikvision-drip")] [InlineData("/wp-login.php", "wordpress-drip")] [InlineData("/blog/WP-Login.php", "wordpress-drip")] public void Matches_known_paths(string path, string expected) => FileContent.Match(path).ShouldNotBeNull().Name.ShouldBe(expected); [Theory] [InlineData("/")] [InlineData("/index.php")] [InlineData("/sdkfoo")] [InlineData("/.svnfoo")] [InlineData("/.hgrc-not")] [InlineData("/legit.git/x")] [InlineData("/environment")] [InlineData("")] [InlineData(null)] public void Ignores_other_paths(string? path) => FileContent.Match(path).ShouldBeNull(); [Theory] [InlineData("/.env", "APP_NAME=")] [InlineData("/config.json", "{")] [InlineData("/sitemap.xml", "")] public void Generates_an_endless_file_that_starts_like_the_real_thing(string path, string start) { var profile = FileContent.Match(path).ShouldNotBeNull(); var text = string.Concat(profile.Generate(new SeededRandomSource(5)).Take(200)); text.ShouldStartWith(start); text.Length.ShouldBeGreaterThan(1000); } [Fact] public void Selector_prefers_the_path_profile_and_can_be_switched_off() { var session = new TrapSession(Guid.NewGuid(), TrapProtocol.Http, IPAddress.Parse("203.0.113.9"), DateTimeOffset.UtcNow); 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("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] public async Task File_strategy_sends_the_head_start_and_names_itself() { var options = TestOptions.Monitor(new DripOptions { MinDelay = TimeSpan.FromSeconds(30), MaxDelay = TimeSpan.FromSeconds(30), HeadStartSize = 300 }); var strategy = new FileDripStrategy(FileContent.Match("/.env")!, options, new SeededRandomSource(1), TimeProvider.System); var channel = new FakeTrapChannel(); using var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(200)); await Should.ThrowAsync(() => strategy.RunAsync(channel, cts.Token)); Encoding.UTF8.GetString(channel.Writes.First()).ShouldStartWith("APP_NAME="); channel.Writes.First().Length.ShouldBeGreaterThanOrEqualTo(300); strategy.Describe().ShouldBe("env-drip 30000-30000ms 1-64B head 300B"); strategy.MediaType.ShouldStartWith("text/plain"); } private static ConfiguredStrategySelector Selector(bool matchPath) { var options = TestOptions.Monitor(new DripOptions { MatchPath = matchPath }); 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 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, random); } }