Pick tarpit content by request path
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e48b6bb832
commit
13468bbdf9
18 files changed
+307
-9
No files matched your search
@@ -0,0 +1,92 @@
|
||||
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("/robots.txt", "robots-drip")]
|
||||
public void Matches_known_paths(string path, string expected) =>
|
||||
FileContent.Match(path).ShouldNotBeNull().Name.ShouldBe(expected);
|
||||
|
||||
[Theory]
|
||||
[InlineData("/")]
|
||||
[InlineData("/index.php")]
|
||||
[InlineData("/wp-login.php")]
|
||||
[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", "<?xml")]
|
||||
[InlineData("/docker-compose.yml", "version:")]
|
||||
[InlineData("/dump.sql", "-- MySQL dump")]
|
||||
[InlineData("/.git/config", "[core]")]
|
||||
[InlineData("/robots.txt", "User-agent:")]
|
||||
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("html-drip");
|
||||
Selector(matchPath: false).Select(context).ShouldNotBeNull().Name.ShouldBe("html-drip");
|
||||
}
|
||||
|
||||
[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<OperationCanceledException>(() => 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 TextDripStrategy(options, random, TimeProvider.System),
|
||||
.. FileContent.Profiles.Select(profile => new FileDripStrategy(profile, options, random, TimeProvider.System))
|
||||
],
|
||||
options,
|
||||
random);
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,7 @@ namespace Flypaper.Tests.Core;
|
||||
|
||||
public sealed class HoldMethodParserTests
|
||||
{
|
||||
private static readonly HoldMethodInput Valid = new("Alternating", "50", "1000", "1", "64", "2048", "01:00:00");
|
||||
private static readonly HoldMethodInput Valid = new("Alternating", "50", "1000", "1", "64", "2048", true, "01:00:00");
|
||||
|
||||
[Fact]
|
||||
public void Parses_valid_input()
|
||||
@@ -19,6 +19,7 @@ public sealed class HoldMethodParserTests
|
||||
settings.MinChunkSize.ShouldBe(1);
|
||||
settings.MaxChunkSize.ShouldBe(64);
|
||||
settings.HeadStartSize.ShouldBe(2048);
|
||||
settings.MatchPath.ShouldBeTrue();
|
||||
settings.MaxHoldDuration.ShouldBe(TimeSpan.FromHours(1));
|
||||
settings.ToConfiguration().Keys.ShouldBe(SettingKeys.HoldMethod, ignoreOrder: true);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user