Skip to content

Commit

Permalink
Disallow a single dot in IDN hostnames (#247)
Browse files Browse the repository at this point in the history
Based on
json-schema-org/JSON-Schema-Test-Suite#759 it is
not allowed to have a single dot in the hostname (and IDN hostname). The
hostname validator already had the correct behavior but the IDN hostname
validator did not
  • Loading branch information
OptimumCode authored Feb 3, 2025
1 parent 48fa0b4 commit 6b652e8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ internal object IdnHostnameFormatValidator : AbstractStringFormatValidator() {
return FormatValidator.Invalid()
}
if (value.length == 1 && isLabelSeparator(value[0])) {
return FormatValidator.Valid()
return FormatValidator.Invalid()
}

// https://datatracker.ietf.org/doc/html/rfc5893#section-1.4
Expand Down Expand Up @@ -506,6 +506,11 @@ internal object IdnHostnameFormatValidator : AbstractStringFormatValidator() {
private fun isACE(label: String): Boolean =
label.length > Punycode.PREFIX_SIZE && label.startsWith(Punycode.PREFIX_STRING)

/**
* Returns `true` if the [c] is a dot
* according to [RFC3490 Section 3.1](https://datatracker.ietf.org/doc/html/rfc3490#section-3.1).
* Otherwise, returns `false`
*/
private fun isLabelSeparator(c: Char): Boolean = c == '.' || c == '\u3002' || c == '\uFF0E' || c == '\uFF61'

private fun findDot(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ class JsonSchemaIdnHostnameFormatValidationTest : FunSpec() {
format = "idn-hostname",
validTestCases =
listOf(
".",
"a",
"hostname",
// 63
Expand All @@ -30,6 +29,10 @@ class JsonSchemaIdnHostnameFormatValidationTest : FunSpec() {
invalidTestCases =
listOf(
TestCase("", "empty value"),
TestCase(".", "single separator"),
TestCase("\u3002", "single separator U+3002"),
TestCase("\uFF0E", "single separator U+FF0E"),
TestCase("\uFF61", "single separator U+FF61"),
TestCase("xn--80aakdqneodaeaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaai7g2bxc6qoj1f", "too long punycode"),
TestCase("оооооооооооооооооооооооооооооооооооченьдлиннаястрока", "too long unicode"),
// Not normalized \u4E3D. Example from https://unicode.org/Public/UNIDATA/NormalizationTest.txt
Expand Down

1 comment on commit 6b652e8

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'KMP JSON schema validator'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.50.

Benchmark suite Current: 6b652e8 Previous: 48fa0b4 Ratio
macosArm64.CommonAvgTimeBench.validateDetailed ( {"objectPath":"openapi-invalid.json","schemaPath":"openapi_schema.json"} ) 5531.368728571429 us/op 3395.13523 us/op 1.63
macosArm64.CommonAvgTimeBench.validateVerbose ( {"objectPath":"openapi.json","schemaPath":"openapi_schema.json"} ) 8076.157166101696 us/op 5328.244831016043 us/op 1.52

This comment was automatically generated by workflow using github-action-benchmark.

CC: @OptimumCode

Please sign in to comment.