From 0167c1af32c080375f4ae7be531e125ef0a9a782 Mon Sep 17 00:00:00 2001 From: "savage@savage.za.org" Date: Mon, 19 Oct 2026 21:13:29 +0200 Subject: [PATCH] Reject articles with duplicate Date headers Enforce the RFC 5536 single-Date requirement before date resolution, preventing CanonicalUtc from disagreeing with the Date field retained for overview and transfer. Add regression coverage for duplicate and malformed Date headers. --- docs/common/ArticleRecord.md | 6 +- .../DateParser/ArticleDateHeaderResolver.cs | 8 +- .../Articles/Parsing/NntpArticleParser.cs | 40 +++++ .../Parsing/NntpArticleParserContracts.cs | 3 + .../Parsing/NntpArticleParserTests.cs | 169 ++++++++++++++++++ 5 files changed, 221 insertions(+), 5 deletions(-) diff --git a/docs/common/ArticleRecord.md b/docs/common/ArticleRecord.md index f003bee4..6ff16093 100644 --- a/docs/common/ArticleRecord.md +++ b/docs/common/ArticleRecord.md @@ -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 @@ -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 diff --git a/src/VectorNNTPD.Common/Articles/DateParser/ArticleDateHeaderResolver.cs b/src/VectorNNTPD.Common/Articles/DateParser/ArticleDateHeaderResolver.cs index e16ac8e4..2a51d67f 100644 --- a/src/VectorNNTPD.Common/Articles/DateParser/ArticleDateHeaderResolver.cs +++ b/src/VectorNNTPD.Common/Articles/DateParser/ArticleDateHeaderResolver.cs @@ -6,14 +6,16 @@ namespace VectorNNTPD.Common.Articles.DateParser /// Resolves the first usable article date header in the parser's deterministic candidate order. /// /// - /// The resolver keeps scanning later candidate headers when an earlier candidate is present but - /// malformed, allowing fallback values such as Injection-Date to recover otherwise acceptable articles. + /// The parser rejects a second Date header before calling this method, so a later Date + /// is not a fallback. A missing or unparsable single Date still falls through to later names + /// such as Injection-Date. /// internal static class ArticleDateHeaderResolver { /// /// 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 Date before resolution, so this scan is not the duplicate-Date policy. /// private static readonly NntpArticleHeaderName[] CandidateHeaderNames = [ diff --git a/src/VectorNNTPD.Common/Articles/Parsing/NntpArticleParser.cs b/src/VectorNNTPD.Common/Articles/Parsing/NntpArticleParser.cs index b8139bcb..7bed660b 100644 --- a/src/VectorNNTPD.Common/Articles/Parsing/NntpArticleParser.cs +++ b/src/VectorNNTPD.Common/Articles/Parsing/NntpArticleParser.cs @@ -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, @@ -634,6 +648,32 @@ private static NntpArticleHeaderName ClassifyKnownHeaderName(ReadOnlySpan ? NntpArticleHeaderName.References : NntpArticleHeaderName.Unknown; + /// + /// Reports a second Date header. Folded continuation lines belong to the current header and are not a second header. + /// + /// Parsed headers in wire order. is already case-folded. + /// when two or more entries have . + private static bool HasDuplicateDateHeader(ReadOnlySpan 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; + } + /// /// Requires exactly one Message-ID whose unfolded bytes pass . /// Unfolding is header parsing. Validity is only the Common Message-ID contract. diff --git a/src/VectorNNTPD.Common/Articles/Parsing/NntpArticleParserContracts.cs b/src/VectorNNTPD.Common/Articles/Parsing/NntpArticleParserContracts.cs index b2754abe..04cf67ce 100644 --- a/src/VectorNNTPD.Common/Articles/Parsing/NntpArticleParserContracts.cs +++ b/src/VectorNNTPD.Common/Articles/Parsing/NntpArticleParserContracts.cs @@ -108,6 +108,9 @@ public enum NntpArticleParseFailureCode /// Body line exceeded configured maximum line length. BodyLineTooLong = 24, + + /// A second Date header was found. Header-name matching is ASCII case-insensitive. + DuplicateDate = 25, } /// diff --git a/tests/VectorNNTPD.Common.Tests/Articles/Parsing/NntpArticleParserTests.cs b/tests/VectorNNTPD.Common.Tests/Articles/Parsing/NntpArticleParserTests.cs index 05e2ec80..964cd424 100644 --- a/tests/VectorNNTPD.Common.Tests/Articles/Parsing/NntpArticleParserTests.cs +++ b/tests/VectorNNTPD.Common.Tests/Articles/Parsing/NntpArticleParserTests.cs @@ -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 { @@ -258,6 +260,145 @@ public void Parse_WhenMessageIdDuplicated_Rejects() Assert.Equal(NntpArticleParseFailureCode.DuplicateMessageId, result.FailureCode); } + /// + /// Two parsable Date headers are rejected before a canonical instant is published. + /// + [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"); + } + + /// + /// An unparsable first Date does not fall through to a later Date. + /// + [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: ", + "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); + } + + /// + /// A valid first Date followed by an unparsable second Date is still a duplicate. + /// + [Fact] + public void Parse_WhenFirstDateIsValidAndSecondIsInvalid_RejectsDuplicate() + { + AssertDuplicateDateRejected( + "Date: Fri, 23 Aug 2024 07:30:10 +0000", + "Date: not-a-date"); + } + + /// + /// Equivalent instants with different date text are still two Date headers. + /// + [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"); + } + + /// + /// date and Date are the same header name. + /// + [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"); + } + + /// + /// A folded continuation stays one Date. A later Date line is the duplicate. + /// + [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: \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); + } + + /// + /// One Date still canonicalizes, and overview and transfer re-read that same stored value. + /// + [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: ", + "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)); + } + /// /// Verifies Path augmentation prepends local host exactly once. /// @@ -2134,6 +2275,34 @@ private static NntpArticleHeaderEntry FindHeaderEntry(IReadOnlyList + /// Parses two Date lines and asserts the article is rejected with no canonical instant. + /// + /// First Date header line, without CRLF. + /// Second Date header line, without CRLF. + private static void AssertDuplicateDateRejected(string firstDate, string secondDate) + { + NntpArticleParser parser = new(LocalFqdn); + byte[] article = BuildArticle( + headers: + [ + firstDate, + secondDate, + "Message-ID: ", + "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); + } + /// /// Builds an article with explicit header lines and optional byte body. ///