Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/common/ArticleRecord.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ Semantics (tests: `Parse_BodyLineCount_MatchesParserLineWalk`):

Terminators: CR, LF, or CRLF (`FindLineTerminator` / `AdvancePastTerminator`). The last unterminated fragment counts as one line.

Materialization copies body lines and inserts one leading `.` on each line whose first content byte is `.`. That does not add a line, so the destuffed body line count stays valid for canonical ArtData. Overview `:lines` can use `ArtLines` without another body scan. `:bytes` is the stuffed `ArtSize`.
Materialization copies body lines and inserts one leading `.` on each line whose first content byte is `.`. `ArticleBodyWireStuffer` uses the same CR, LF, and CRLF line ends as that walk, so a bare CR before a dot-leading line is stuffed. That does not add a line, so the destuffed body line count stays valid for canonical ArtData. Overview `:lines` can use `ArtLines` without another body scan. `:bytes` is the stuffed `ArtSize`. Delivery framing still requires exclusive CRLF; a retained article that used bare CR or bare LF is not frameable.

---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ namespace VectorNNTPD.Common.Articles.Processing
/// </para>
/// <para>
/// Headers, including a header line that begins with <c>.</c>, and the header/body separator are copied unchanged.
/// A body line whose first content byte is <c>.</c> gains one leading <c>.</c>. Content excludes a CR that
/// immediately precedes LF. The NNTP multiline terminator <c>.CRLF</c> is not appended.
/// A body line whose first content byte is <c>.</c> gains one leading <c>.</c>. Line ends are CR, LF, or CRLF,
/// matching the parser's body walk. A CR that immediately precedes LF is one terminator, not two.
/// The NNTP multiline terminator <c>.CRLF</c> is not appended.
/// When no body line needs a dot, the original array is returned.
/// Delivery framing still requires exclusive CRLF and may reject a retained article that used bare CR or bare LF.
/// </para>
/// </remarks>
internal static class ArticleBodyWireStuffer
Expand Down Expand Up @@ -102,7 +104,7 @@ internal static int CountExtraDots(ReadOnlySpan<byte> body)
}

extra++;
SkipThroughLineFeed(body, ref index);
SkipThroughLineTerminator(body, ref index);
}

return extra;
Expand Down Expand Up @@ -156,7 +158,7 @@ internal static int WriteStuffed(ReadOnlySpan<byte> body, Span<byte> destination

destination[written++] = (byte)'.';
var start = index;
SkipThroughLineFeed(body, ref index);
SkipThroughLineTerminator(body, ref index);
var line = index - start;
body.Slice(start, line).CopyTo(destination[written..]);
written += line;
Expand All @@ -183,23 +185,40 @@ private static void SkipPlainLines(ReadOnlySpan<byte> body, ref int index)
{
while (index < body.Length && body[index] != (byte)'.')
{
SkipThroughLineFeed(body, ref index);
SkipThroughLineTerminator(body, ref index);
}
}

/// <summary>Advances <paramref name="index"/> to the first byte after this line's LF, or to the buffer length.</summary>
/// <summary>
/// Advances <paramref name="index"/> to the first byte after this line's CR, LF, or CRLF, or to the buffer length.
/// </summary>
/// <param name="body">Logical body.</param>
/// <param name="index">A byte inside the current line on entry.</param>
private static void SkipThroughLineFeed(ReadOnlySpan<byte> body, ref int index)
/// <remarks>
/// Matches the parser's <c>AdvancePastTerminator</c> rules so a bare CR ends a line for stuffing.
/// A CR immediately followed by LF is consumed as one terminator.
/// </remarks>
private static void SkipThroughLineTerminator(ReadOnlySpan<byte> body, ref int index)
{
while (index < body.Length && body[index] != (byte)'\n')
while (index < body.Length)
{
index++;
}
var b = body[index++];
if (b == (byte)'\n')
{
return;
}

if (index < body.Length)
{
index++;
if (b != (byte)'\r')
{
continue;
}

if (index < body.Length && body[index] == (byte)'\n')
{
index++;
}

return;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -788,33 +788,54 @@ public async Task Responsive_replacement_still_sends_quit()
/// <summary>
/// A replenish delay that never fires does not hold replacement after the shutdown token is cancelled.
/// </summary>
/// <remarks>
/// Responsive servers keep this case on the replenish-timer invariant alone.
/// Stalled QUIT and shared-budget coverage stay in
/// <see cref="Many_stalled_sessions_share_one_shutdown_budget"/> and the other replacement drain tests.
/// Completing under <see cref="SharedShutdownCeiling"/> is well below the fifteen-second replenish interval.
/// </remarks>
[Fact]
public async Task Replacement_does_not_wait_for_a_replenish_delay_that_ignores_its_timer()
{
const int sessions = 2;
var factory = new ScriptedNntpTransportFactory();
var servers = EnqueueStalledQuitServers(factory, sessions);
var servers = EnqueueReadyServers(factory, sessions);
var clock = new NeverFiringTimeProvider();
var pool = NntpSessionPoolTests.CreatePool(
factory,
max: sessions,
time: clock,
shutdownGrace: SharedBudget);
var dispose = Task.CompletedTask;
try
{
await pool.EnsureDesiredSessionsAsync(CancellationToken.None);
await WaitUntilAsync(() => clock.TimersCreated >= 1);

var elapsed = await MeasureAsync(() => pool.DrainAndDisposeAsync(CancellationToken.None), servers);
Report("replacement-replenish", SharedBudget, sessions, providers: 1, QuitStarts(servers), elapsed);
var started = Stopwatch.GetTimestamp();
dispose = pool.DrainAndDisposeAsync(CancellationToken.None);
await dispose.WaitAsync(SharedShutdownCeiling);
var elapsed = Stopwatch.GetElapsedTime(started);
Report("replacement-replenish", SharedBudget, sessions, providers: 1, quitStarted: 0, elapsed);

AssertWithinCeiling(elapsed);
Assert.True(
elapsed < TimeSpan.FromSeconds(15),
$"elapsed {elapsed.TotalMilliseconds:0} ms must stay below the replenish interval");
Assert.All(servers, static server => Assert.Equal(1, server.ClientStreamDisposeCount));
Assert.Equal(0, pool.LiveSessionCount);
}
finally
{
Release(servers);
try
{
await dispose.WaitAsync(TimeSpan.FromSeconds(5));
}
catch (TimeoutException)
{
// Observed so an abandoned drain cannot outlive the test. The original failure stands.
}

await pool.DisposeAsync();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using VectorNNTPD.Common.Articles;
using VectorNNTPD.Common.Articles.Parsing;
using VectorNNTPD.Common.Articles.Processing;
using System.Text;

Expand Down Expand Up @@ -31,7 +33,7 @@ public void Header_dot_is_not_stuffed_and_an_unstuffed_body_is_the_same_array()
Assert.Same(logical, wire);
}

/// <summary>Parameterized body fragments keep the one-dot-per-line-start rule across CRLF, LF, and mixed endings.</summary>
/// <summary>Parameterized body fragments keep the one-dot-per-line-start rule across CRLF, LF, CR, and mixed endings.</summary>
/// <param name="body">Logical body bytes after the header separator.</param>
/// <param name="stuffed">Expected stuffed body including any added dots.</param>
[Theory]
Expand All @@ -46,6 +48,11 @@ public void Header_dot_is_not_stuffed_and_an_unstuffed_body_is_the_same_array()
[InlineData("...\r\n", "....\r\n")]
[InlineData("a.b\r\n", "a.b\r\n")]
[InlineData("plain\r\n.\r\n..x\n...y\r\n.last", "plain\r\n..\r\n...x\n....y\r\n..last")]
[InlineData("hello\r\n.secret\r\n", "hello\r\n..secret\r\n")]
[InlineData("hello\r.secret\n", "hello\r..secret\n")]
[InlineData("hello\r\r.secret\r\n", "hello\r\r..secret\r\n")]
[InlineData("hello\n.secret\r\n", "hello\n..secret\r\n")]
[InlineData(".\r\n.\n.\r", "..\r\n..\n..\r")]
public void Logical_body_lines_keep_one_stuffed_dot_at_the_line_start(string body, string stuffed)
{
var logical = Encoding.ASCII.GetBytes("From: a@b.test\r\n\r\n" + body);
Expand All @@ -54,6 +61,49 @@ public void Logical_body_lines_keep_one_stuffed_dot_at_the_line_start(string bod
Assert.Equal(Encoding.ASCII.GetBytes("From: a@b.test\r\n\r\n" + stuffed), wire);
}

/// <summary>Issue #168: a bare CR ends the preceding line so the following dot-leading line is stuffed.</summary>
[Fact]
public void Issue168_BareCrBeforeDotLeadingLine_IsStuffed()
{
AssertStuffedBody("hello\r.secret\n", "hello\r..secret\n");
}

/// <summary>A CRLF before a dot-leading line still stuffs exactly once.</summary>
[Fact]
public void DotFollowingCrlf_IsStuffed()
{
AssertStuffedBody("hello\r\n.secret\r\n", "hello\r\n..secret\r\n");
}

/// <summary>A bare LF before a dot-leading line still stuffs exactly once.</summary>
[Fact]
public void DotFollowingBareLf_IsStuffed()
{
AssertStuffedBody("hello\n.secret\r\n", "hello\n..secret\r\n");
}

/// <summary>Consecutive bare CR bytes each end a line, so a later dot-leading line is stuffed.</summary>
[Fact]
public void ConsecutiveBareCr_BeforeDotLeadingLine_IsStuffed()
{
AssertStuffedBody("hello\r\r.secret\r\n", "hello\r\r..secret\r\n");
}

/// <summary>A body that begins with a dot is stuffed, including consecutive dot-leading lines.</summary>
[Fact]
public void BodyBeginningWithDot_AndConsecutiveDotLines_AreStuffed()
{
AssertStuffedBody(".\r\n.second\r\n", "..\r\n..second\r\n");
}

/// <summary>An empty body and a CRLF-only body need no stuffing and keep their exact bytes.</summary>
[Fact]
public void EmptyAndCrlfOnlyBodies_RemainUnchanged()
{
AssertStuffedBody(string.Empty, string.Empty);
AssertStuffedBody("\r\n", "\r\n");
}

/// <summary>LF header separators and mixed line endings affect only the body region after the first blank line.</summary>
[Fact]
public void Lf_separator_and_mixed_endings_stuff_only_the_body()
Expand All @@ -63,5 +113,64 @@ public void Lf_separator_and_mixed_endings_stuff_only_the_body()

Assert.Equal(Encoding.ASCII.GetBytes("From: a@b.test\n\n..\n...x\r\n"), wire);
}

/// <summary>
/// Materializing the Issue #168 bare-CR body stores the stuffed image and remains not frameable for NNTP delivery.
/// </summary>
[Fact]
public void Materialize_WhenBodyUsesBareCrBeforeDot_StoresStuffedBytesAndIsNotFrameable()
{
var parser = new NntpArticleParser("bf01.usenet.ninja");
var article = Encoding.ASCII.GetBytes(
"Date: Fri, 23 Aug 2024 07:30:10 +0000\r\n"
+ "Message-ID: <bare-cr-dot@example.test>\r\n"
+ "Newsgroups: alt.test\r\n"
+ "From: user@example.test\r\n"
+ "\r\n"
+ "hello\r.secret\n");

var created = ArticleRecordFactory.TryCreate(parser, article);

Assert.True(created.IsAccepted, created.ParseFailure.ToString());
Assert.Equal(
Encoding.ASCII.GetBytes("hello\r..secret\n"),
created.Record.ArtData.Span[created.Record.Framing.BodyStart..].ToArray());
Assert.False(created.Record.Framing.IsFrameable);
Assert.False(CanonicalArticleFraming.Analyze(created.Record.ArtData.Span).IsFrameable);
}

/// <summary>
/// A CRLF body with a dot-leading line is stuffed and remains frameable.
/// </summary>
[Fact]
public void Materialize_WhenBodyUsesCrlfBeforeDot_StoresStuffedBytesAndIsFrameable()
{
var parser = new NntpArticleParser("bf01.usenet.ninja");
var article = Encoding.ASCII.GetBytes(
"Date: Fri, 23 Aug 2024 07:30:10 +0000\r\n"
+ "Message-ID: <crlf-dot@example.test>\r\n"
+ "Newsgroups: alt.test\r\n"
+ "From: user@example.test\r\n"
+ "\r\n"
+ "hello\r\n.secret\r\n");

var created = ArticleRecordFactory.TryCreate(parser, article);

Assert.True(created.IsAccepted, created.ParseFailure.ToString());
Assert.Equal(
Encoding.ASCII.GetBytes("hello\r\n..secret\r\n"),
created.Record.ArtData.Span[created.Record.Framing.BodyStart..].ToArray());
Assert.True(created.Record.Framing.IsFrameable);
}

/// <summary>Asserts stuffing of a body fragment after a fixed CRLF header separator.</summary>
/// <param name="body">Logical body.</param>
/// <param name="stuffed">Expected stuffed body.</param>
private static void AssertStuffedBody(string body, string stuffed)
{
var logical = Encoding.ASCII.GetBytes("From: a@b.test\r\n\r\n" + body);
var wire = ArticleBodyWireStuffer.StuffBody(logical);
Assert.Equal(Encoding.ASCII.GetBytes("From: a@b.test\r\n\r\n" + stuffed), wire);
}
}
}
Loading