Skip to content

Commit 43b49ef

Browse files
committed
fix(validation): reject server keys containing newlines
Use the strict \z end-of-string anchor for server key validation so keys with a trailing newline cannot pass through the $ anchor. Add regression cases for trailing LF, CRLF, and embedded newlines in AsyncAPI 2.6.0 and 3.0.0. Escape carriage returns and newlines in double-quoted YAML keys so the tests preserve these characters for validation.
1 parent 9ebf225 commit 43b49ef

2 files changed

Lines changed: 4 additions & 3 deletions

File tree

‎src/ByteBard.AsyncAPI/Validation/Rules/AsyncApiDocumentRules.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public static class AsyncApiDocumentRules
1616
/// </summary>
1717
private static Regex keyRegex = new Regex(@"^[a-zA-Z0-9\.\-_]+$", RegexOptions.None, RegexTimeout);
1818

19-
private static Regex serverKeyRegex = new Regex(@"^[a-zA-Z0-9_\-]+$", RegexOptions.None, RegexTimeout);
19+
private static Regex serverKeyRegex = new Regex(@"^[a-zA-Z0-9_\-]+\z", RegexOptions.None, RegexTimeout);
2020

2121
public static ValidationRule<AsyncApiDocument> DocumentRequiredFields =>
2222
new ValidationRule<AsyncApiDocument>(

‎test/ByteBard.AsyncAPI.Tests/Validation/KeyRegexTests.cs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public static IEnumerable<TestCaseData> ServerKeys()
3636
yield return new TestCaseData(version, key, true);
3737
}
3838

39-
foreach (var key in new[] { "server.one", "sérver", "server/name", "server name", "" })
39+
foreach (var key in new[] { "server.one", "sérver", "server/name", "server name", "", "server\n", "server\r\n", "server\nother" })
4040
{
4141
yield return new TestCaseData(version, key, false);
4242
}
@@ -63,6 +63,7 @@ public static IEnumerable<TestCaseData> ComponentKeys()
6363
public void ServerKey_UsesSpecificationPattern(string version, string key, bool valid)
6464
{
6565
var addressField = version == "2.6.0" ? "url" : "host";
66+
var yamlKey = key.Replace("\r", "\\r").Replace("\n", "\\n");
6667
var input = $$"""
6768
asyncapi: {{version}}
6869
info:
@@ -71,7 +72,7 @@ public void ServerKey_UsesSpecificationPattern(string version, string key, bool
7172
channels:
7273
chat: {}
7374
servers:
74-
'{{key}}':
75+
"{{yamlKey}}":
7576
{{addressField}}: localhost
7677
protocol: kafka
7778
""";

0 commit comments

Comments
 (0)