Headers and the first 8 KB of the body are stored unmasked for 30 days, read alongside the response so the trap answers as before. MCP get_request and the expanded catch row show them. Changelog prepared for 0.10.0. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
173 lines
5.2 KiB
C#
173 lines
5.2 KiB
C#
using System.Globalization;
|
|
using System.Text;
|
|
using Flypaper.Core.Abstractions;
|
|
using Flypaper.Core.Models;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Http.Features;
|
|
using Microsoft.AspNetCore.Server.Kestrel.Core.Features;
|
|
|
|
namespace Flypaper.Http;
|
|
|
|
internal sealed class RequestCapture(HttpContext context) : IRequestCapture
|
|
{
|
|
public const int MaxBodyBytes = 8 * 1024;
|
|
private const int MaxHeaders = 64;
|
|
private const int MaxHeaderValueLength = 1024;
|
|
private const int MaxHeadersLength = 8 * 1024;
|
|
private static readonly TimeSpan ReadTimeout = TimeSpan.FromSeconds(5);
|
|
private static readonly TimeSpan CompletionGrace = TimeSpan.FromSeconds(1);
|
|
private static readonly UTF8Encoding StrictUtf8 = new(false, true);
|
|
|
|
private readonly byte[] _buffer = new byte[MaxBodyBytes + 1];
|
|
private readonly CancellationTokenSource _stop = new();
|
|
private int _length;
|
|
private string? _headers;
|
|
private long? _contentLength;
|
|
private Task? _reading;
|
|
|
|
public void Start()
|
|
{
|
|
var request = context.Request;
|
|
_headers = FormatHeaders(request.Headers);
|
|
_contentLength = request.ContentLength;
|
|
if (request.ContentLength is not > 0 && request.Headers.TransferEncoding.Count == 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (context.Features.Get<IHttpMinRequestBodyDataRateFeature>() is { } rate)
|
|
{
|
|
rate.MinDataRate = null;
|
|
}
|
|
|
|
if (context.Features.Get<IHttpMaxRequestBodySizeFeature>() is { IsReadOnly: false } size)
|
|
{
|
|
size.MaxRequestBodySize = null;
|
|
}
|
|
|
|
_reading = ReadAsync(request.Body, context.RequestAborted);
|
|
}
|
|
|
|
public async ValueTask<RequestDetails?> CompleteAsync()
|
|
{
|
|
if (_headers is null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
if (_reading is { IsCompleted: false } pending)
|
|
{
|
|
await Task.WhenAny(pending, Task.Delay(CompletionGrace));
|
|
await _stop.CancelAsync();
|
|
}
|
|
|
|
if (_reading is not null)
|
|
{
|
|
await _reading;
|
|
}
|
|
|
|
var length = Volatile.Read(ref _length);
|
|
var kept = Math.Min(length, MaxBodyBytes);
|
|
return new RequestDetails(
|
|
_headers,
|
|
kept == 0 ? null : Printable(_buffer.AsSpan(0, kept)),
|
|
_contentLength,
|
|
length > MaxBodyBytes || _contentLength > kept);
|
|
}
|
|
|
|
private async Task ReadAsync(Stream body, CancellationToken aborted)
|
|
{
|
|
using var timeout = new CancellationTokenSource(ReadTimeout);
|
|
using var linked = CancellationTokenSource.CreateLinkedTokenSource(aborted, timeout.Token, _stop.Token);
|
|
try
|
|
{
|
|
while (_length < _buffer.Length)
|
|
{
|
|
var read = await body.ReadAsync(_buffer.AsMemory(_length), linked.Token);
|
|
if (read == 0)
|
|
{
|
|
break;
|
|
}
|
|
|
|
Volatile.Write(ref _length, _length + read);
|
|
}
|
|
}
|
|
catch (Exception ex) when (ex is OperationCanceledException or IOException or InvalidOperationException)
|
|
{
|
|
}
|
|
}
|
|
|
|
private static string FormatHeaders(IHeaderDictionary headers)
|
|
{
|
|
var text = new StringBuilder();
|
|
var count = 0;
|
|
foreach (var (name, values) in headers)
|
|
{
|
|
if (IsAddedByProxy(name))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
foreach (var value in values)
|
|
{
|
|
if (++count > MaxHeaders || text.Length >= MaxHeadersLength)
|
|
{
|
|
return Limit(text.ToString(), MaxHeadersLength);
|
|
}
|
|
|
|
text.Append(Escape(name)).Append(": ").Append(Escape(Limit(value ?? "", MaxHeaderValueLength))).Append('\n');
|
|
}
|
|
}
|
|
|
|
return Limit(text.ToString(), MaxHeadersLength);
|
|
}
|
|
|
|
private static bool IsAddedByProxy(string name) =>
|
|
name.Equals("X-Real-IP", StringComparison.OrdinalIgnoreCase)
|
|
|| name.StartsWith("X-Forwarded-", StringComparison.OrdinalIgnoreCase)
|
|
|| name.StartsWith("X-Original-", StringComparison.OrdinalIgnoreCase);
|
|
|
|
private static string Limit(string value, int length) => value.Length > length ? value[..length] : value;
|
|
|
|
private static string Printable(ReadOnlySpan<byte> data)
|
|
{
|
|
try
|
|
{
|
|
return Escape(StrictUtf8.GetString(data));
|
|
}
|
|
catch (DecoderFallbackException)
|
|
{
|
|
var text = new StringBuilder(data.Length);
|
|
foreach (var value in data)
|
|
{
|
|
Append(text, (char)value, value > 0x7E);
|
|
}
|
|
|
|
return text.ToString();
|
|
}
|
|
}
|
|
|
|
private static string Escape(string value)
|
|
{
|
|
var text = new StringBuilder(value.Length);
|
|
foreach (var character in value)
|
|
{
|
|
Append(text, character, false);
|
|
}
|
|
|
|
return text.ToString();
|
|
}
|
|
|
|
private static void Append(StringBuilder text, char character, bool escape)
|
|
{
|
|
if (escape || (char.IsControl(character) && character is not ('\n' or '\r' or '\t')))
|
|
{
|
|
text.Append(CultureInfo.InvariantCulture, $"\\x{(int)character:X2}");
|
|
}
|
|
else
|
|
{
|
|
text.Append(character);
|
|
}
|
|
}
|
|
}
|