Skip to content

Commit 6e17095

Browse files
Copilotcotti
andcommitted
Add URI well-formed validation to mapped_pages URLs
- Enhanced validation to include Uri.IsWellFormedUriString check - Added test cases for malformed URIs, invalid characters, and non-absolute URIs - URLs must now be both properly prefixed AND well-formed absolute URIs Co-authored-by: cotti <[email protected]>
1 parent b4e1e01 commit 6e17095

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

src/Elastic.Markdown/IO/MarkdownFile.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ private YamlFrontMatter ProcessYamlFrontMatter(MarkdownDocument document)
364364
{
365365
foreach (var url in fm.MappedPages)
366366
{
367-
if (!string.IsNullOrEmpty(url) && !url.StartsWith("https://www.elastic.co/guide", StringComparison.OrdinalIgnoreCase))
367+
if (!string.IsNullOrEmpty(url) && (!url.StartsWith("https://www.elastic.co/guide", StringComparison.OrdinalIgnoreCase) || !Uri.IsWellFormedUriString(url, UriKind.Absolute)))
368368
{
369369
Collector.EmitError(FilePath, $"Invalid mapped_pages URL: \"{url}\". All mapped_pages URLs must start with \"https://www.elastic.co/guide\". Please update the URL to reference content under the Elastic documentation guide.");
370370
}

tests/Elastic.Markdown.Tests/FrontMatter/YamlFrontMatterTests.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,3 +279,60 @@ public void HasErrorsForExternalUrl()
279279
Collector.Diagnostics.Should().Contain(d => d.Message.Contains("Invalid mapped_pages URL: \"https://github.com/elastic/docs-builder\". All mapped_pages URLs must start with \"https://www.elastic.co/guide\""));
280280
}
281281
}
282+
283+
public class MappedPagesMalformedUri(ITestOutputHelper output) : DirectiveTest(output,
284+
"""
285+
---
286+
mapped_pages:
287+
- "https://www.elastic.co/guide/[invalid-characters]"
288+
---
289+
290+
# Test Page
291+
"""
292+
)
293+
{
294+
[Fact]
295+
public void HasErrorsForMalformedUri()
296+
{
297+
Collector.Diagnostics.Should().HaveCount(1);
298+
Collector.Diagnostics.Should().Contain(d => d.Message.Contains("Invalid mapped_pages URL: \"https://www.elastic.co/guide/[invalid-characters]\". All mapped_pages URLs must start with \"https://www.elastic.co/guide\""));
299+
}
300+
}
301+
302+
public class MappedPagesInvalidScheme(ITestOutputHelper output) : DirectiveTest(output,
303+
"""
304+
---
305+
mapped_pages:
306+
- "https://www.elastic.co/guide/invalid uri with spaces"
307+
---
308+
309+
# Test Page
310+
"""
311+
)
312+
{
313+
[Fact]
314+
public void HasErrorsForInvalidScheme()
315+
{
316+
Collector.Diagnostics.Should().HaveCount(1);
317+
Collector.Diagnostics.Should().Contain(d => d.Message.Contains("Invalid mapped_pages URL: \"https://www.elastic.co/guide/invalid uri with spaces\". All mapped_pages URLs must start with \"https://www.elastic.co/guide\""));
318+
}
319+
}
320+
321+
public class MappedPagesNotAbsoluteUri(ITestOutputHelper output) : DirectiveTest(output,
322+
"""
323+
---
324+
mapped_pages:
325+
- "not-a-uri-at-all"
326+
---
327+
328+
# Test Page
329+
"""
330+
)
331+
{
332+
[Fact]
333+
public void HasErrorsForNotAbsoluteUri()
334+
{
335+
Collector.Diagnostics.Should().HaveCount(1);
336+
Collector.Diagnostics.Should().Contain(d => d.Message.Contains("Invalid mapped_pages URL: \"not-a-uri-at-all\". All mapped_pages URLs must start with \"https://www.elastic.co/guide\""));
337+
}
338+
}

0 commit comments

Comments
 (0)