Skip to content

Commit a64023f

Browse files
committed
Use async method in honeypot service
1 parent d5385e1 commit a64023f

File tree

4 files changed

+38
-34
lines changed

4 files changed

+38
-34
lines changed

src/AspNetCore.Honeypot/Extensions.cs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,8 @@ namespace AspNetCore.Honeypot;
99
public static class Extensions
1010
{
1111
/// <summary>
12-
/// AddHoneypot
12+
/// Adds honeypot services.
1313
/// </summary>
14-
/// <param name="services"></param>
15-
/// <param name="options"></param>
16-
/// <returns></returns>
1714
public static IServiceCollection AddHoneypot(this IServiceCollection services, Action<HoneypotOptions>? options = null)
1815
{
1916
if (options != null)
@@ -27,14 +24,12 @@ public static IServiceCollection AddHoneypot(this IServiceCollection services, A
2724
}
2825

2926
/// <summary>
30-
/// IsHoneypotTrapped
27+
/// Is honepot triggered?
3128
/// </summary>
32-
/// <param name="httpContext"></param>
33-
/// <returns></returns>
34-
public static bool IsHoneypotTrapped(this HttpContext httpContext)
29+
public static async Task<bool> IsHoneypotTriggeredAsync(this HttpContext httpContext)
3530
{
3631
HoneypotService service = httpContext.RequestServices.GetRequiredService<HoneypotService>();
3732

38-
return service.IsTrapped(httpContext);
33+
return await service.IsTriggeredAsync(httpContext);
3934
}
4035
}

src/AspNetCore.Honeypot/HoneypotAttribute.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ namespace AspNetCore.Honeypot;
99
/// </summary>
1010
public class HoneypotAttribute : ActionFilterAttribute
1111
{
12-
public override void OnActionExecuting(ActionExecutingContext context)
12+
public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
1313
{
14-
base.OnActionExecuting(context);
14+
bool triggered = await context.HttpContext.IsHoneypotTriggeredAsync();
1515

16-
bool isTrapped = context.HttpContext.IsHoneypotTrapped();
17-
18-
if (isTrapped == true)
16+
if (triggered == true)
1917
{
2018
context.Result = new ContentResult() { Content = "bot detection", ContentType = "text/plain", StatusCode = StatusCodes.Status200OK };
2119
}
20+
21+
await base.OnActionExecutionAsync(context, next);
2222
}
2323
}

src/AspNetCore.Honeypot/HoneypotOptions.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,29 @@
66
public class HoneypotOptions
77
{
88
/// <summary>
9-
/// EnableFieldCheck
9+
/// Is field check enabled?
1010
/// </summary>
1111
public bool IsFieldCheckEnabled { get; set; } = true;
1212

1313
/// <summary>
14-
/// EnableTimeCheck
14+
/// Is time check enabled?
1515
/// </summary>
1616
public bool IsTimeCheckEnabled { get; set; } = true;
1717

1818
/// <summary>
19-
/// PrefixFieldName
19+
/// Prefix for fields.
2020
/// </summary>
2121
public string PrefixFieldName { get; set; } = "hp_";
2222

2323
/// <summary>
24-
/// TimeFieldName
24+
/// Prefix for time fields.
2525
/// </summary>
2626
public string TimeFieldName { get; set; } = "_time";
2727

2828
/// <summary>
29-
/// MinTimeDuration
29+
/// Minimal time for user response.
3030
/// </summary>
31-
public TimeSpan MinTimeDuration { get; set; } = TimeSpan.FromSeconds(1);
31+
public TimeSpan MinResponseTime { get; set; } = TimeSpan.FromSeconds(1);
3232

3333
internal bool IsFieldName(string name)
3434
{

src/AspNetCore.Honeypot/HoneypotService.cs

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace AspNetCore.Honeypot;
99
/// </summary>
1010
class HoneypotService
1111
{
12-
public const string HttpContextItemName = "AspNetCore.Honeypot.IsHoneypotTrapped";
12+
public const string HttpContextItemName = "AspNetCore.Honeypot.IsHoneypotTriggered";
1313

1414
public HoneypotService(IOptions<HoneypotOptions> options)
1515
{
@@ -22,42 +22,51 @@ public HoneypotService(IOptions<HoneypotOptions> options)
2222
private HoneypotOptions Options { get; }
2323

2424
/// <summary>
25-
/// IsTrapped
25+
/// Is honeypot triggered?
2626
/// </summary>
27-
public bool IsTrapped(HttpContext httpContext)
27+
public async Task<bool> IsTriggeredAsync(HttpContext httpContext)
2828
{
2929
if (httpContext.Items.TryGetValue(HttpContextItemName, out object? value) == false)
3030
{
31-
bool trapped = false;
31+
bool triggered = false;
3232

3333
if (httpContext.Request.HasFormContentType == false)
3434
{
35-
trapped = true;
35+
triggered = true;
3636
}
3737

38-
if (trapped == false && Options.IsFieldCheckEnabled)
38+
IFormCollection form = await httpContext.Request.ReadFormAsync();
39+
40+
if (triggered == false && Options.IsFieldCheckEnabled)
3941
{
4042
//check fields
41-
trapped = httpContext.Request.Form.Any(x => Options.IsFieldName(x.Key) && x.Value.Any(v => string.IsNullOrEmpty(v) == false));
43+
triggered = form.Any(x => Options.IsFieldName(x.Key) && x.Value.Any(v => string.IsNullOrEmpty(v) == false));
4244
}
4345

44-
if (trapped == false && Options.IsTimeCheckEnabled)
46+
if (triggered == false && Options.IsTimeCheckEnabled)
4547
{
4648
//check time
47-
if (httpContext.Request.Form.TryGetValue(Options.TimeFieldName, out StringValues timeValues))
49+
if (form.TryGetValue(Options.TimeFieldName, out StringValues timeValues))
4850
{
49-
if (timeValues.Any())
51+
if (timeValues.Count > 0 && timeValues[0] is string timeValue)
5052
{
51-
TimeSpan diff = DateTime.UtcNow - new DateTime(long.Parse(timeValues.First()), DateTimeKind.Utc);
53+
if (long.TryParse(timeValue, out long time))
54+
{
55+
TimeSpan diff = DateTime.UtcNow - new DateTime(time, DateTimeKind.Utc);
5256

53-
trapped = diff < Options.MinTimeDuration;
57+
triggered = diff < Options.MinResponseTime;
58+
}
59+
else
60+
{
61+
triggered = true; //time field doesn't contain long value.
62+
}
5463
}
5564
}
5665
}
5766

58-
httpContext.Items.Add(HttpContextItemName, trapped);
67+
httpContext.Items.Add(HttpContextItemName, triggered);
5968

60-
return trapped;
69+
return triggered;
6170
}
6271
else
6372
{

0 commit comments

Comments
 (0)