From daf2c2deae0f65c22ebd3035329128a52d08b834 Mon Sep 17 00:00:00 2001 From: "savage@savage.za.org" Date: Mon, 19 Oct 2026 22:15:38 +0200 Subject: [PATCH] Fix folded Path parsing and preserve verification markers Accept valid Path folding whitespace before delimiters and reject folds in invalid positions. Preserve interior !! verification markers without transferring them when duplicate tracker hops are removed. Add regression coverage for parsing and canonicalization. --- docs/common/ArticleRecord.md | 6 +- .../Parsing/ArticlePathCanonicalizer.cs | 338 +++++++++++++++++- .../Parsing/ArticlePathCanonicalizerTests.cs | 264 +++++++++++++- .../Parsing/NntpArticleParserTests.cs | 63 +++- 4 files changed, 648 insertions(+), 23 deletions(-) diff --git a/docs/common/ArticleRecord.md b/docs/common/ArticleRecord.md index adec2e1c..3c15b3f7 100644 --- a/docs/common/ArticleRecord.md +++ b/docs/common/ArticleRecord.md @@ -363,11 +363,13 @@ Tests: [`ArticlePathCanonicalizerTests.cs`](../../tests/VectorNNTPD.Common.Tests ### Token rules +- A CRLF followed by SP or HTAB is unfolded before validation only when that fold is in the whitespace immediately before a `!` delimiter. A fold after `!`, a fold inside a hop, a fold after the final hop, bare CR, bare LF, and CRLF without continuation whitespace are `InvalidPath` - Split on `!` - Trim SP/HTAB on each token -- Empty tokens (repeated `!`) are dropped +- One empty token between two hops is RFC 5536 `diag-match` (`!!`) and is kept when both hops are written. If tracker deduplication removes the right-hand hop, the marker is dropped with that hop. A second empty token in that position (`a!!!b`) is `InvalidPath` +- A leading `!` before the first hop is `InvalidPath`. A trailing `!` is omitted. A value that is only separators, such as `!!!`, stays `Empty` - Tokens must be printable ASCII, not space, not `!` (`IsValidPathComponent`) -- Entire Path must be printable 0x20–0x7E and ≤ 8192 +- The unfolded Path must be printable 0x20–0x7E. The raw value must be ≤ 8192 - Tracker and local FQDN matching is **case-insensitive complete-token** equality (`AsciiEqualsIgnoreCase`), not substring search Organizational tracker: `news.usenet.ninja` (`OrganizationalTrackerHost`). After any Vector application has seen the article, that token is present **exactly once**. diff --git a/src/VectorNNTPD.Common/Articles/Parsing/ArticlePathCanonicalizer.cs b/src/VectorNNTPD.Common/Articles/Parsing/ArticlePathCanonicalizer.cs index 27dc1e38..78fad3d4 100644 --- a/src/VectorNNTPD.Common/Articles/Parsing/ArticlePathCanonicalizer.cs +++ b/src/VectorNNTPD.Common/Articles/Parsing/ArticlePathCanonicalizer.cs @@ -145,17 +145,68 @@ internal static bool TryAnalyze( return false; } - for (var i = 0; i < rawPath.Length; i++) + if (!ContainsLineBreak(rawPath)) { - var b = rawPath[i]; - if (b is < 0x20 or > 0x7E) - { - failureCode = NntpArticleParseFailureCode.InvalidPath; - return false; - } + return TryAnalyzePrepared( + rawPath, + localIdentity, + organizationalTracker, + out kind, + out containsOrganizationalTracker, + out failureCode); + } + + if (!IsLegalPathFold(rawPath)) + { + failureCode = NntpArticleParseFailureCode.InvalidPath; + return false; + } + + Span unfoldBuffer = stackalloc byte[rawPath.Length]; + if (!TryUnfoldPath(rawPath, unfoldBuffer, out var prepared)) + { + failureCode = NntpArticleParseFailureCode.InvalidPath; + return false; } - var remaining = TrimAscii(rawPath); + return TryAnalyzePrepared( + prepared, + localIdentity, + organizationalTracker, + out kind, + out containsOrganizationalTracker, + out failureCode); + } + + /// + /// Classifies a Path value that has already been unfolded. + /// + /// Unfolded Path bytes. + /// Local application FQDN. + /// Tracker host compared with Path tokens. + /// Rewrite classification when validation succeeds. + /// Whether a token equals . + /// when the value is rejected. + /// when the Path is usable. + private static bool TryAnalyzePrepared( + ReadOnlySpan prepared, + ReadOnlySpan localIdentity, + ReadOnlySpan organizationalTracker, + out ArticlePathKind kind, + out bool containsOrganizationalTracker, + out NntpArticleParseFailureCode failureCode) + { + kind = ArticlePathKind.Missing; + containsOrganizationalTracker = false; + failureCode = NntpArticleParseFailureCode.None; + + if (!IsPrintablePathText(prepared)) + { + failureCode = NntpArticleParseFailureCode.InvalidPath; + return false; + } + + var remaining = TrimAscii(prepared); if (remaining.IsEmpty) { kind = ArticlePathKind.Empty; @@ -163,20 +214,36 @@ internal static bool TryAnalyze( } var sawComponent = false; + var sawLeadingEmpty = false; + var pendingEmpty = false; var leftmostMatchesLocal = false; while (TryConsumePathComponent(ref remaining, out var component)) { if (component.IsEmpty) { + if (!sawComponent) + { + sawLeadingEmpty = true; + continue; + } + + if (pendingEmpty) + { + failureCode = NntpArticleParseFailureCode.InvalidPath; + return false; + } + + pendingEmpty = true; continue; } - if (!IsValidPathComponent(component)) + if (sawLeadingEmpty || !IsValidPathComponent(component)) { failureCode = NntpArticleParseFailureCode.InvalidPath; return false; } + pendingEmpty = false; if (!sawComponent) { leftmostMatchesLocal = AsciiEqualsIgnoreCase(component, localIdentity); @@ -218,7 +285,10 @@ internal static bool TryAnalyze( /// A later traverse writes {application-fqdn}!existing even when that FQDN is already present. /// does not write an application FQDN. /// news.usenet.ninja is inserted when absent and reduced to the first occurrence. - /// Empty Path tokens from repeated ! separators are dropped. + /// One empty token between two hops is a diag-match and is written as !!. + /// If tracker deduplication removes that marker's right-hand hop, the marker is dropped. + /// Leading ! is invalid. A trailing ! is omitted. A run of only separators stays empty. + /// A CRLF fold is accepted only in the whitespace immediately before a ! delimiter. /// internal static bool TryWriteCanonicalPath( ReadOnlySpan rawPath, @@ -258,10 +328,6 @@ internal static bool TryWriteCanonicalPath( Span destination, out int bytesWritten) { - var remaining = kind is ArticlePathKind.Missing or ArticlePathKind.Empty - ? [] - : TrimAscii(rawPath); - var traverse = mode == ArticlePathMode.Traverse; var localIsTracker = traverse && !localIdentity.IsEmpty @@ -269,8 +335,45 @@ internal static bool TryWriteCanonicalPath( var prependTracker = !containsOrganizationalTracker && !localIsTracker; var applicationHop = traverse ? localIdentity : default; + if (kind is ArticlePathKind.Missing or ArticlePathKind.Empty) + { + return TryWriteJoinedComponents( + [], + prependTracker ? organizationalTracker : default, + applicationHop, + organizationalTracker, + trackerAlreadyEmitted: prependTracker || localIsTracker, + destination, + out bytesWritten); + } + + if (!ContainsLineBreak(rawPath)) + { + return TryWriteJoinedComponents( + TrimAscii(rawPath), + prependTracker ? organizationalTracker : default, + applicationHop, + organizationalTracker, + trackerAlreadyEmitted: prependTracker || localIsTracker, + destination, + out bytesWritten); + } + + if (!IsLegalPathFold(rawPath)) + { + bytesWritten = 0; + return false; + } + + Span unfoldBuffer = stackalloc byte[rawPath.Length]; + if (!TryUnfoldPath(rawPath, unfoldBuffer, out var prepared)) + { + bytesWritten = 0; + return false; + } + return TryWriteJoinedComponents( - remaining, + TrimAscii(prepared), prependTracker ? organizationalTracker : default, applicationHop, organizationalTracker, @@ -279,6 +382,181 @@ internal static bool TryWriteCanonicalPath( out bytesWritten); } + /// + /// Unfolds a Path value whose only line breaks are CRLF folds. + /// + /// Raw Path bytes, including continuation terminators. + /// Buffer at least as long as . + /// Unfolded bytes inside . + /// + /// for a bare CR, a bare LF, a CRLF that is not followed by SP or HTAB, + /// or a fold that is not in the whitespace immediately before a ! delimiter. + /// + private static bool TryUnfoldPath(ReadOnlySpan rawPath, Span destination, out ReadOnlySpan prepared) + { + prepared = default; + if (!IsLegalPathFold(rawPath) + || !NntpArticleHeaderValueUnfolder.TryUnfold(rawPath, destination, out var written)) + { + return false; + } + + prepared = destination[..written]; + return true; + } + + /// Returns whether contains CR or LF. + /// Path bytes. + /// when a line break is present. + private static bool ContainsLineBreak(ReadOnlySpan value) + { + for (var i = 0; i < value.Length; i++) + { + if (value[i] is (byte)'\r' or (byte)'\n') + { + return true; + } + } + + return false; + } + + /// + /// Returns whether every line break is a CRLF fold placed immediately before a ! delimiter. + /// + /// Raw Path bytes. + /// + /// for a bare CR, a bare LF, a fold after !, a fold inside a hop, + /// or a fold that is not followed by a delimiter. + /// + private static bool IsLegalPathFold(ReadOnlySpan value) + => HasOnlyCrlfFolds(value) && FoldsSitBeforeDelimiters(value); + + /// + /// Returns whether each fold is trailing whitespace of a hop that is followed by !. + /// + /// Raw Path bytes. + /// + /// when a line break occurs before a hop, inside a hop, after a delimiter, + /// or after the final hop. + /// + private static bool FoldsSitBeforeDelimiters(ReadOnlySpan value) + { + var segmentStart = 0; + for (var i = 0; i <= value.Length; i++) + { + var atEnd = i == value.Length; + if (!atEnd && value[i] != (byte)'!') + { + continue; + } + + if (!SegmentFoldIsBeforeDelimiter(value, segmentStart, i, followedByDelimiter: !atEnd)) + { + return false; + } + + segmentStart = i + 1; + } + + return true; + } + + /// + /// Returns whether line breaks in one !-separated segment are only trailing whitespace. + /// + /// Raw Path bytes containing and . + /// Inclusive segment start. + /// Exclusive segment end. The delimiter itself is not included. + /// Whether a ! follows this segment. + /// when a fold is not the trailing whitespace before that delimiter. + private static bool SegmentFoldIsBeforeDelimiter( + ReadOnlySpan value, + int start, + int end, + bool followedByDelimiter) + { + var sawContent = false; + var inTrailingBreak = false; + var contentAfterBreak = false; + for (var i = start; i < end; i++) + { + var b = value[i]; + if (b is (byte)'\r' or (byte)'\n') + { + if (!sawContent) + { + return false; + } + + inTrailingBreak = true; + continue; + } + + if (b is (byte)' ' or (byte)'\t') + { + continue; + } + + if (inTrailingBreak) + { + contentAfterBreak = true; + } + + sawContent = true; + inTrailingBreak = false; + } + + return !contentAfterBreak && (!inTrailingBreak || followedByDelimiter); + } + + /// + /// Returns whether every line break is a CRLF. The following SP or HTAB is checked by the unfolder. + /// + /// Raw Path bytes. + /// for a bare CR or a bare LF. + private static bool HasOnlyCrlfFolds(ReadOnlySpan value) + { + for (var i = 0; i < value.Length; i++) + { + var b = value[i]; + if (b == (byte)'\n') + { + return false; + } + + if (b != (byte)'\r') + { + continue; + } + + if ((uint)(i + 1) >= (uint)value.Length || value[i + 1] != (byte)'\n') + { + return false; + } + + i++; + } + + return true; + } + + /// Returns whether every byte is printable ASCII, including SP. + /// Unfolded Path bytes. + /// for a byte below 0x20 or above 0x7E. + private static bool IsPrintablePathText(ReadOnlySpan value) + { + for (var i = 0; i < value.Length; i++) + { + if (value[i] is < 0x20 or > 0x7E) + { + return false; + } + } + + return true; + } + /// /// Returns whether one Path component is syntactically acceptable. /// @@ -301,7 +579,7 @@ private static bool IsValidPathComponent(ReadOnlySpan component) /// /// Writes optional prepended hops, then !-separated components from . /// - /// Path value still to split. Empty components are skipped. + /// Path value still to split. A single empty component between hops is written as an extra ! unless the right-hand hop is a duplicate tracker that this method omits. /// First hop, or empty. Used for when it must be inserted. /// Second hop, or empty. Used for the application FQDN in traverse mode. /// Tracker component suppressed after its first occurrence. @@ -329,23 +607,51 @@ private static bool TryWriteJoinedComponents( return false; } + var sawComponent = false; + var pendingDiagnostic = false; while (TryConsumePathComponent(ref remaining, out var component)) { if (component.IsEmpty) { + if (sawComponent) + { + if (pendingDiagnostic) + { + bytesWritten = 0; + return false; + } + + pendingDiagnostic = true; + } + continue; } + sawComponent = true; if (AsciiEqualsIgnoreCase(component, organizationalTracker)) { if (trackerAlreadyEmitted) { + // A suppressed hop cannot stay the right-hand side of a pending marker. + pendingDiagnostic = false; continue; } trackerAlreadyEmitted = true; } + if (pendingDiagnostic) + { + if (first || bytesWritten >= destination.Length) + { + bytesWritten = 0; + return false; + } + + destination[bytesWritten++] = (byte)'!'; + pendingDiagnostic = false; + } + if (!TryWriteHop(component, ref first, destination, ref bytesWritten)) { return false; diff --git a/tests/VectorNNTPD.Common.Tests/Articles/Parsing/ArticlePathCanonicalizerTests.cs b/tests/VectorNNTPD.Common.Tests/Articles/Parsing/ArticlePathCanonicalizerTests.cs index 5029d0b2..a4a68c0f 100644 --- a/tests/VectorNNTPD.Common.Tests/Articles/Parsing/ArticlePathCanonicalizerTests.cs +++ b/tests/VectorNNTPD.Common.Tests/Articles/Parsing/ArticlePathCanonicalizerTests.cs @@ -363,6 +363,253 @@ public void TryAnalyze_WhenControlBytePresent_Rejects() Assert.Equal(NntpArticleParseFailureCode.InvalidPath, failure); } + /// + /// A CRLF fold in the whitespace immediately before ! is accepted and canonicalized without the fold. + /// + [Fact] + public void TryWriteCanonicalPath_WhenFoldedBetweenComponents_WritesUnfoldedPath() + { + var raw = "news.example\r\n !peer.example"u8.ToArray(); + var before = raw.ToArray(); + WriteAndAssert(raw, "bf01.usenet.ninja", "news.usenet.ninja!bf01.usenet.ninja!news.example!peer.example"); + Assert.Equal(before, raw); + } + + /// + /// A tab continuation immediately before ! is the same legal fold as a space continuation. + /// + [Fact] + public void TryWriteCanonicalPath_WhenFoldedWithTab_WritesUnfoldedPath() + { + WriteAndAssert( + "news.example\r\n\t!peer.example"u8.ToArray(), + "bf01.usenet.ninja", + "news.usenet.ninja!bf01.usenet.ninja!news.example!peer.example"); + } + + /// + /// Several CRLF folds immediately before ! are removed by canonicalization. + /// + [Fact] + public void TryWriteCanonicalPath_WhenMultipleFolds_WritesEachHop() + { + WriteAndAssert( + "a.example\r\n !b.example\r\n !c.example"u8.ToArray(), + "bf01.usenet.ninja", + "news.usenet.ninja!bf01.usenet.ninja!a.example!b.example!c.example"); + } + + /// + /// A bare CR is not a Path fold. + /// + [Fact] + public void TryAnalyze_WhenPathContainsBareCr_Rejects() + { + Assert.False(ArticlePathCanonicalizer.TryAnalyze( + "news.example\r !peer.example"u8, + LocalIdentity, + pathPresent: true, + out _, + out _, + out var failure)); + Assert.Equal(NntpArticleParseFailureCode.InvalidPath, failure); + } + + /// + /// A bare LF is not a Path fold. + /// + [Fact] + public void TryAnalyze_WhenPathContainsBareLf_Rejects() + { + Assert.False(ArticlePathCanonicalizer.TryAnalyze( + "news.example\n !peer.example"u8, + LocalIdentity, + pathPresent: true, + out _, + out _, + out var failure)); + Assert.Equal(NntpArticleParseFailureCode.InvalidPath, failure); + } + + /// + /// CRLF that is not followed by SP or HTAB is not a Path fold. + /// + [Fact] + public void TryAnalyze_WhenPathCrlfHasNoContinuationWhitespace_Rejects() + { + Assert.False(ArticlePathCanonicalizer.TryAnalyze( + "news.example\r\n!peer.example"u8, + LocalIdentity, + pathPresent: true, + out _, + out _, + out var failure)); + Assert.Equal(NntpArticleParseFailureCode.InvalidPath, failure); + } + + /// + /// A fold after ! is not RFC 5536 folding whitespace and is rejected. + /// + [Fact] + public void TryAnalyze_WhenFoldFollowsDelimiter_Rejects() + { + var raw = "news.example.org!\r\n feed2"u8.ToArray(); + var before = raw.ToArray(); + Assert.False(ArticlePathCanonicalizer.TryAnalyze( + raw, + LocalIdentity, + pathPresent: true, + out _, + out _, + out var failure)); + Assert.Equal(NntpArticleParseFailureCode.InvalidPath, failure); + + Span destination = stackalloc byte[128]; + Assert.False(ArticlePathCanonicalizer.TryWriteCanonicalPath( + raw, + LocalIdentity, + ArticlePathKind.NeedsLocalIdentityPrepend, + containsOrganizationalTracker: false, + ArticlePathMode.Traverse, + destination, + out _)); + Assert.Equal(before, raw); + } + + /// + /// A fold inside a hop is rejected, and an overlong Path is still rejected. + /// + [Fact] + public void TryAnalyze_WhenFoldSplitsHopOrPathIsTooLong_Rejects() + { + Assert.False(ArticlePathCanonicalizer.TryAnalyze( + "news.exam\r\n ple!peer.example"u8, + LocalIdentity, + pathPresent: true, + out _, + out _, + out var foldedFailure)); + Assert.Equal(NntpArticleParseFailureCode.InvalidPath, foldedFailure); + + var overlong = new byte[ArticlePathCanonicalizer.MaxPathLength + 1]; + overlong.AsSpan().Fill((byte)'a'); + Assert.False(ArticlePathCanonicalizer.TryAnalyze( + overlong, + LocalIdentity, + pathPresent: true, + out _, + out _, + out var lengthFailure)); + Assert.Equal(NntpArticleParseFailureCode.InvalidPath, lengthFailure); + } + + /// + /// a!!b keeps the verification marker in the canonical Path. + /// + [Fact] + public void TryWriteCanonicalPath_WhenDiagnosticMatch_PreservesMarker() + { + WriteAndAssert( + "a.example!!b.example", + "bf01.usenet.ninja", + "news.usenet.ninja!bf01.usenet.ninja!a.example!!b.example"); + WriteAndAssert( + "a.example!!b.example", + "bf01.usenet.ninja", + "news.usenet.ninja!a.example!!b.example", + ArticlePathMode.Normalize); + } + + /// + /// Traverse and a later normalize keep a verification marker beside the tracker. + /// + [Fact] + public void TryWriteCanonicalPath_WhenDiagnosticAdjoinsTracker_PreservesMarker() + { + var traversed = Rewrite("peer.example!!news.example.org", "bf01.usenet.ninja"); + Assert.Equal("news.usenet.ninja!bf01.usenet.ninja!peer.example!!news.example.org", traversed); + Assert.Equal(traversed, Rewrite(traversed, "bf01.usenet.ninja", ArticlePathMode.Normalize)); + WriteAndAssert( + "peer.example!!news.usenet.ninja!other.example", + "bf01.usenet.ninja", + "bf01.usenet.ninja!peer.example!!news.usenet.ninja!other.example"); + } + + /// + /// Collapsing a repeated tracker does not delete a verification marker whose hops both survive. + /// + [Fact] + public void TryWriteCanonicalPath_WhenTrackerIsRepeated_KeepsDiagnosticMarker() + { + WriteAndAssert( + "a.example!!b.example!news.usenet.ninja!NEWS.USENET.NINJA", + "bf01.usenet.ninja", + "a.example!!b.example!news.usenet.ninja", + ArticlePathMode.Normalize); + WriteAndAssert( + "a.example!!b.example!news.usenet.ninja!NEWS.USENET.NINJA", + "bf01.usenet.ninja", + "bf01.usenet.ninja!a.example!!b.example!news.usenet.ninja"); + } + + /// + /// Dropping a duplicate tracker that is the right-hand hop of !! also drops that marker. + /// + [Fact] + public void TryWriteCanonicalPath_WhenDuplicateTrackerIsRightHandDiagnosticHop_DropsMarker() + { + WriteAndAssert( + "news.usenet.ninja!!NEWS.USENET.NINJA!b.example", + "bf01.usenet.ninja", + "news.usenet.ninja!b.example", + ArticlePathMode.Normalize); + WriteAndAssert( + "news.usenet.ninja!!NEWS.USENET.NINJA!b.example", + "bf01.usenet.ninja", + "bf01.usenet.ninja!news.usenet.ninja!b.example"); + } + + /// + /// A leading ! is invalid. A trailing ! is omitted. Only separators stay empty. + /// + [Fact] + public void TryAnalyze_WhenLeadingOrTrailingSeparators_FollowsDiagnosticRules() + { + Assert.False(ArticlePathCanonicalizer.TryAnalyze( + "!peer.example"u8, + LocalIdentity, + pathPresent: true, + out _, + out _, + out var leadingFailure)); + Assert.Equal(NntpArticleParseFailureCode.InvalidPath, leadingFailure); + + Assert.False(ArticlePathCanonicalizer.TryAnalyze( + "a.example!!!b.example"u8, + LocalIdentity, + pathPresent: true, + out _, + out _, + out var repeatedFailure)); + Assert.Equal(NntpArticleParseFailureCode.InvalidPath, repeatedFailure); + + WriteAndAssert( + "peer.example!", + "bf01.usenet.ninja", + "news.usenet.ninja!bf01.usenet.ninja!peer.example"); + } + + /// + /// A second normalize does not add or remove a verification marker. + /// + [Fact] + public void TryWriteCanonicalPath_Normalize_DiagnosticMarkerIsIdempotent() + { + const string once = "news.usenet.ninja!a.example!!b.example"; + WriteAndAssert("a.example!!b.example", "cache01.usenet.ninja", once, ArticlePathMode.Normalize); + WriteAndAssert(once, "cache01.usenet.ninja", once, ArticlePathMode.Normalize); + } + /// /// Analyzes and returns the canonical Path text for . /// @@ -406,8 +653,23 @@ private static void WriteAndAssert( string localIdentity, string expected, ArticlePathMode mode = ArticlePathMode.Traverse) + => WriteAndAssert(Encoding.ASCII.GetBytes(rawPath), localIdentity, expected, mode); + + /// + /// Asserts that analyzing and writing produces + /// and that the result contains the organizational tracker exactly once. + /// + /// Input Path bytes. + /// Application hop used for analysis and the write. + /// Canonical Path text required from the write. + /// Path mode under test. Defaults to traverse. + private static void WriteAndAssert( + byte[] rawPath, + string localIdentity, + string expected, + ArticlePathMode mode = ArticlePathMode.Traverse) { - var raw = Encoding.ASCII.GetBytes(rawPath); + var raw = rawPath; var local = Encoding.ASCII.GetBytes(localIdentity); Assert.True(ArticlePathCanonicalizer.TryAnalyze( raw, diff --git a/tests/VectorNNTPD.Common.Tests/Articles/Parsing/NntpArticleParserTests.cs b/tests/VectorNNTPD.Common.Tests/Articles/Parsing/NntpArticleParserTests.cs index 838d7848..6bc26fb1 100644 --- a/tests/VectorNNTPD.Common.Tests/Articles/Parsing/NntpArticleParserTests.cs +++ b/tests/VectorNNTPD.Common.Tests/Articles/Parsing/NntpArticleParserTests.cs @@ -424,6 +424,61 @@ public void Parse_WhenPathPresentAndMissingLocalHost_PrependsLocalHost() Assert.False(result.ContainsOrganizationalTracker); } + /// + /// A Path fold immediately before ! is accepted, left unchanged in the source buffer, and canonicalized without the fold. + /// + [Fact] + public void Parse_WhenPathIsFoldedBeforeDelimiter_AcceptsAndCanonicalizes() + { + NntpArticleParser parser = new(LocalFqdn); + byte[] article = BuildArticle( + headers: + [ + "Date: Fri, 23 Aug 2024 07:30:10 +0000", + "Message-ID: ", + "Newsgroups: alt.test", + "From: user@example.test", + "Path: news.example.org\r\n !feed2", + ], + body: "body\r\n"); + byte[] before = [.. article]; + + NntpArticleParseResult result = parser.Parse(article); + ArticleRecordCreateResult created = ArticleRecordFactory.TryCreate(parser, article); + + Assert.True(result.IsAccepted); + Assert.Equal(before, article); + Assert.Equal("news.usenet.ninja!bf01.usenet.ninja!news.example.org!feed2", FormatCanonicalPath(result)); + Assert.True(created.IsAccepted); + Assert.Equal("news.usenet.ninja!bf01.usenet.ninja!news.example.org!feed2", Encoding.ASCII.GetString(created.Record.Path)); + } + + /// + /// A Path fold after ! is rejected. The source buffer is left unchanged. + /// + [Fact] + public void Parse_WhenPathFoldFollowsDelimiter_RejectsInvalidPath() + { + NntpArticleParser parser = new(LocalFqdn); + byte[] article = BuildArticle( + headers: + [ + "Date: Fri, 23 Aug 2024 07:30:10 +0000", + "Message-ID: ", + "Newsgroups: alt.test", + "From: user@example.test", + "Path: news.example.org!\r\n feed2", + ], + body: "body\r\n"); + byte[] before = [.. article]; + + NntpArticleParseResult result = parser.Parse(article); + + Assert.False(result.IsAccepted); + Assert.Equal(NntpArticleParseFailureCode.InvalidPath, result.FailureCode); + Assert.Equal(before, article); + } + /// /// Verifies a traverse prepends the local host again when it is already leftmost. /// @@ -1095,10 +1150,10 @@ NntpArticleParserOptions.Default with } /// - /// Verifies malformed Path separators are normalized by dropping empty path components while preserving deterministic local prepend behavior. + /// A leading ! and a run of three ! separators are not a single verification marker. /// [Fact] - public void Parse_WhenPathContainsRepeatedSeparators_NormalizesAndPrependsLocalHostOnce() + public void Parse_WhenPathContainsRepeatedSeparators_RejectsInvalidPath() { NntpArticleParser parser = new(LocalFqdn); byte[] article = BuildArticle( @@ -1114,8 +1169,8 @@ public void Parse_WhenPathContainsRepeatedSeparators_NormalizesAndPrependsLocalH NntpArticleParseResult result = parser.Parse(article); - Assert.True(result.IsAccepted); - Assert.Equal("news.usenet.ninja!bf01.usenet.ninja!feed1!feed2", FormatCanonicalPath(result)); + Assert.False(result.IsAccepted); + Assert.Equal(NntpArticleParseFailureCode.InvalidPath, result.FailureCode); } ///