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
6 changes: 4 additions & 2 deletions docs/common/ArticleRecord.md
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,9 @@ The parser never rewrites Date. `NntpArticleParseResult` carries `CanonicalUtc`,
5. `X-Date`
6. `Delivery-Date`

For each name, headers are scanned in wire order. The first candidate that unfolds and `NewsDateParser.TryGetCanonicalUtc` succeeds wins. A malformed earlier header does not block a later good one.
The parser rejects a second `Date` header as `NntpArticleParseFailureCode.DuplicateDate` before resolution. `Date` and `date` are the same header. A folded continuation is part of that one header, not a second `Date`. No canonical instant or record is published for that rejection.

For each remaining candidate name, headers are scanned in wire order. The first value of that name that unfolds and `NewsDateParser.TryGetCanonicalUtc` succeeds wins. A missing or unparsable single `Date` does not block a later name such as `Injection-Date`.

### CanonicalUtc

Expand All @@ -271,7 +273,7 @@ These collisions are omitted rather than given one region's offset: `AST`/`ADT`,

### Failure

If no candidate parses: `NntpArticleParseFailureCode.MissingOrInvalidDate`. The factory returns `RejectedParse` and does not materialize.
If no candidate parses: `NntpArticleParseFailureCode.MissingOrInvalidDate`. A second `Date` is `DuplicateDate` instead. The factory returns `RejectedParse` and does not materialize.

### Why source offsets cannot be reused

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ namespace VectorNNTPD.Common.Articles.DateParser
/// Resolves the first usable article date header in the parser's deterministic candidate order.
/// </summary>
/// <remarks>
/// The resolver keeps scanning later candidate headers when an earlier candidate is present but
/// malformed, allowing fallback values such as <c>Injection-Date</c> to recover otherwise acceptable articles.
/// The parser rejects a second <c>Date</c> header before calling this method, so a later <c>Date</c>
/// is not a fallback. A missing or unparsable single <c>Date</c> still falls through to later names
/// such as <c>Injection-Date</c>.
/// </remarks>
internal static class ArticleDateHeaderResolver
{
/// <summary>
/// Date-family headers in the order they are tried: Date, Injection-Date, NNTP-Posting-Date, Posted, X-Date, Delivery-Date.
/// Within one name, the first entry in wire order that unfolds and parses wins. A present but unparsable candidate does not stop the scan.
/// Within one name, the first entry in wire order that unfolds and parses wins. A present but unparsable candidate does not stop the scan of a later name.
/// The parser rejects a second <c>Date</c> before resolution, so this scan is not the duplicate-<c>Date</c> policy.
/// </summary>
private static readonly NntpArticleHeaderName[] CandidateHeaderNames =
[
Expand Down
40 changes: 40 additions & 0 deletions src/VectorNNTPD.Common/Articles/Parsing/NntpArticleParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,20 @@ internal NntpArticleParseResult Parse(
localIdentity);
}

if (HasDuplicateDateHeader(parsedHeaders))
{
return NntpArticleParseResult.Rejected(
NntpArticleParseFailureCode.DuplicateDate,
NntpArticleType.Malformed,
articleBytes,
headerOutcome.HeaderBytes,
headerOutcome.BodyBytes,
parsedHeaders,
parsedHeaders.Length,
localIdentity,
originalMessageIdValue: originalMessageIdValue);
}

if (!ArticleDateHeaderResolver.TryResolve(
articleBytes,
parsedHeaders,
Expand Down Expand Up @@ -634,6 +648,32 @@ private static NntpArticleHeaderName ClassifyKnownHeaderName(ReadOnlySpan<byte>
? NntpArticleHeaderName.References
: NntpArticleHeaderName.Unknown;

/// <summary>
/// Reports a second <c>Date</c> header. Folded continuation lines belong to the current header and are not a second header.
/// </summary>
/// <param name="headers">Parsed headers in wire order. <see cref="NntpArticleHeaderName.Date"/> is already case-folded.</param>
/// <returns><see langword="true"/> when two or more entries have <see cref="NntpArticleHeaderName.Date"/>.</returns>
private static bool HasDuplicateDateHeader(ReadOnlySpan<NntpArticleHeaderEntry> headers)
{
var found = false;
for (var i = 0; i < headers.Length; i++)
{
if (headers[i].KnownName != NntpArticleHeaderName.Date)
{
continue;
}

if (found)
{
return true;
}

found = true;
}

return false;
}

/// <summary>
/// Requires exactly one Message-ID whose unfolded bytes pass <see cref="NntpMessageIdValidation.IsValidMessageId(ReadOnlySpan{byte})"/>.
/// Unfolding is header parsing. Validity is only the Common Message-ID contract.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ public enum NntpArticleParseFailureCode

/// <summary>Body line exceeded configured maximum line length.</summary>
BodyLineTooLong = 24,

/// <summary>A second <c>Date</c> header was found. Header-name matching is ASCII case-insensitive.</summary>
DuplicateDate = 25,
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System.Text;
using VectorNNTPD.Common.Articles;
using VectorNNTPD.Common.Articles.DateParser;
using VectorNNTPD.Common.Articles.OverviewDb;
using VectorNNTPD.Common.Articles.Parsing;
using VectorNNTPD.Common.Articles.YEnc;
using VectorNNTPD.Common.Transport.ArticleTransfer;

namespace VectorNNTPD.Common.Tests.Articles.Parsing
{
Expand Down Expand Up @@ -258,6 +260,145 @@ public void Parse_WhenMessageIdDuplicated_Rejects()
Assert.Equal(NntpArticleParseFailureCode.DuplicateMessageId, result.FailureCode);
}

/// <summary>
/// Two parsable <c>Date</c> headers are rejected before a canonical instant is published.
/// </summary>
[Fact]
public void Parse_WhenTwoValidDateHeadersDiffer_RejectsDuplicateBeforeCanonicalUtc()
{
AssertDuplicateDateRejected(
"Date: Fri, 23 Aug 2024 07:30:10 +0000",
"Date: Sat, 24 Aug 2024 08:00:00 +0000");
}

/// <summary>
/// An unparsable first <c>Date</c> does not fall through to a later <c>Date</c>.
/// </summary>
[Fact]
public void Parse_WhenFirstDateIsInvalidAndSecondIsValid_RejectsDuplicate()
{
NntpArticleParser parser = new(LocalFqdn);
byte[] article = BuildArticle(
headers:
[
"Date: not-a-date",
"Date: Fri, 23 Aug 2024 07:30:10 +0000",
"Message-ID: <dup-date-invalid-first@example.test>",
"Newsgroups: alt.test",
"From: user@example.test",
],
body: "body\r\n");

NntpArticleParseResult result = parser.Parse(article);
ArticleRecordCreateResult created = ArticleRecordFactory.TryCreate(parser, article);

Assert.False(result.IsAccepted);
Assert.Equal(NntpArticleParseFailureCode.DuplicateDate, result.FailureCode);
Assert.Equal(default, result.CanonicalUtc);
Assert.Equal(NntpArticleHeaderName.Unknown, result.SelectedDateHeaderName);
Assert.False(created.IsAccepted);
Assert.Equal(NntpArticleParseFailureCode.DuplicateDate, created.ParseFailure);
Assert.Equal(default, created.Record.CanonicalUtc);
Assert.Equal(0, created.Record.ArtSize);
}

/// <summary>
/// A valid first <c>Date</c> followed by an unparsable second <c>Date</c> is still a duplicate.
/// </summary>
[Fact]
public void Parse_WhenFirstDateIsValidAndSecondIsInvalid_RejectsDuplicate()
{
AssertDuplicateDateRejected(
"Date: Fri, 23 Aug 2024 07:30:10 +0000",
"Date: not-a-date");
}

/// <summary>
/// Equivalent instants with different date text are still two <c>Date</c> headers.
/// </summary>
[Fact]
public void Parse_WhenDuplicateDatesAreEquivalentInstants_RejectsDuplicate()
{
AssertDuplicateDateRejected(
"Date: Fri, 23 Aug 2024 07:30:10 +0000",
"Date: Fri, 23 Aug 2024 07:30:10 GMT");
}

/// <summary>
/// <c>date</c> and <c>Date</c> are the same header name.
/// </summary>
[Fact]
public void Parse_WhenDateHeaderCaseDiffers_RejectsDuplicate()
{
AssertDuplicateDateRejected(
"date: Fri, 23 Aug 2024 07:30:10 +0000",
"DATE: Sat, 24 Aug 2024 08:00:00 +0000");
}

/// <summary>
/// A folded continuation stays one <c>Date</c>. A later <c>Date</c> line is the duplicate.
/// </summary>
[Fact]
public void Parse_WhenFoldedDateIsFollowedByAnotherDate_RejectsDuplicate()
{
NntpArticleParser parser = new(LocalFqdn);
byte[] article = Encoding.ASCII.GetBytes(
"Date: Fri, 23 Aug 2024\r\n" +
"\t07:30:10 +0000\r\n" +
"Date: Sat, 24 Aug 2024 08:00:00 +0000\r\n" +
"Message-ID: <dup-date-folded@example.test>\r\n" +
"Newsgroups: alt.test\r\n" +
"From: user@example.test\r\n" +
"\r\n" +
"body\r\n");

NntpArticleParseResult result = parser.Parse(article);

Assert.False(result.IsAccepted);
Assert.Equal(NntpArticleParseFailureCode.DuplicateDate, result.FailureCode);
Assert.Equal(default, result.CanonicalUtc);
}

/// <summary>
/// One <c>Date</c> still canonicalizes, and overview and transfer re-read that same stored value.
/// </summary>
[Fact]
public void Parse_WhenSingleDateHeader_KeepsCanonicalUtcOverviewAndTransferOnTheSameValue()
{
NntpArticleParser parser = new(LocalFqdn);
byte[] article = BuildArticle(
headers:
[
"Date: Fri, 23 Aug 2024 07:30:10 +0200",
"Message-ID: <single-date@example.test>",
"Newsgroups: alt.test",
"From: user@example.test",
"Subject: one date",
],
body: "body\r\n");

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

Assert.True(created.IsAccepted);
Assert.Equal(NntpArticleHeaderName.Date, created.SelectedDateHeaderName);
ArticleRecord record = created.Record;
Assert.True(NewsDateParser.TryGetCanonicalUtc(record.Date, out DateTime storedUtc, out DateParseFailureReason storedFailure));
Assert.Equal(DateParseFailureReason.None, storedFailure);
Assert.Equal(record.CanonicalUtc, storedUtc);
Assert.Equal("Fri, 23 Aug 2024 05:30:10 +0000", Encoding.ASCII.GetString(record.Date));

OverviewArticleV1 overview = OverviewArticleV1Codec.Decode(OverviewArticleV1Codec.Encode(record));
Assert.Equal(Encoding.ASCII.GetString(record.Date), overview.Date);

byte[] artData = record.ArtData.Span[..record.ArtSize].ToArray();
ArticleCanonicalTransferMeta meta = ArticleCanonicalTransferMeta.FromRecord(record, created.SelectedDateHeaderName);
ArticleRecordCreateResult transferred = ArticleRecordFactory.TryCreateFromCanonicalTransfer(artData, in meta, record.ArtId);

Assert.True(transferred.IsAccepted);
Assert.Equal(record.CanonicalUtc, transferred.Record.CanonicalUtc);
Assert.True(record.Date.SequenceEqual(transferred.Record.Date));
}

/// <summary>
/// Verifies Path augmentation prepends local host exactly once.
/// </summary>
Expand Down Expand Up @@ -2134,6 +2275,34 @@ private static NntpArticleHeaderEntry FindHeaderEntry(IReadOnlyList<NntpArticleH
throw new InvalidOperationException($"Header '{knownName}' was not found in parse output.");
}

/// <summary>
/// Parses two <c>Date</c> lines and asserts the article is rejected with no canonical instant.
/// </summary>
/// <param name="firstDate">First Date header line, without CRLF.</param>
/// <param name="secondDate">Second Date header line, without CRLF.</param>
private static void AssertDuplicateDateRejected(string firstDate, string secondDate)
{
NntpArticleParser parser = new(LocalFqdn);
byte[] article = BuildArticle(
headers:
[
firstDate,
secondDate,
"Message-ID: <dup-date@example.test>",
"Newsgroups: alt.test",
"From: user@example.test",
],
body: "body\r\n");

NntpArticleParseResult result = parser.Parse(article);

Assert.False(result.IsAccepted);
Assert.Equal(NntpArticleParseFailureCode.DuplicateDate, result.FailureCode);
Assert.Equal(default, result.CanonicalUtc);
Assert.Equal(NntpArticleHeaderName.Unknown, result.SelectedDateHeaderName);
Assert.True(result.OriginalDateValue.IsEmpty);
}

/// <summary>
/// Builds an article with explicit header lines and optional byte body.
/// </summary>
Expand Down
Loading