Skip to content

Commit

Permalink
Fix endpoint matcher host label validation (#2910)
Browse files Browse the repository at this point in the history
  • Loading branch information
mullermp authored Sep 14, 2023
1 parent 714584d commit 3599d6a
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 9 deletions.
2 changes: 2 additions & 0 deletions gems/aws-sdk-core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
Unreleased Changes
------------------

* Issue - Fix host label validation in endpoint matchers.

3.181.0 (2023-08-22)
------------------

Expand Down
22 changes: 13 additions & 9 deletions gems/aws-sdk-core/lib/aws-sdk-core/endpoints/matchers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ def self.valid_host_label?(value, allow_sub_domains = false)
return false if value.empty?

if allow_sub_domains
labels = value.split('.')
labels = value.split('.', -1)
return labels.all? { |l| valid_host_label?(l) }
end

value =~ /\A(?!-)[a-zA-Z0-9-]{1,63}(?<!-)\z/
!!(value =~ /\A(?!-)[a-zA-Z0-9-]{1,63}(?<!-)\z/)
end

# AWS
Expand Down Expand Up @@ -114,13 +114,17 @@ def self.aws_parse_arn(value)

# aws.isVirtualHostableS3Bucket(value: string, allowSubDomains: bool) bool
def self.aws_virtual_hostable_s3_bucket?(value, allow_sub_domains = false)
!!(value.size < 64 &&
# regular naming rules
value =~ /^[a-z0-9][a-z0-9\-#{'.' if allow_sub_domains}]+[a-z0-9]$/ &&
# not IP address
value !~ /(\d+\.){3}\d+/ &&
# no dash and hyphen together
value !~ /[.-]{2}/)
return false if value.empty?

if allow_sub_domains
labels = value.split('.', -1)
return labels.all? { |l| aws_virtual_hostable_s3_bucket?(l) }
end

# must be between 3 and 63 characters long, no uppercase
value =~ /\A(?!-)[a-z0-9-]{3,63}(?<!-)\z/ &&
# not an IP address
value !~ /(\d+\.){3}\d+/
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@
}
}
},
{
"documentation": "bucket--with-multiple-dash: isVirtualHostable",
"params": {
"BucketName": "bucket--with-multiple-dash"
},
"expect": {
"endpoint": {
"url": "https://bucket--with-multiple-dash.s3.amazonaws.com"
}
}
},
{
"documentation": "BucketName: not isVirtualHostable (uppercase characters)",
"params": {
Expand Down Expand Up @@ -143,6 +154,15 @@
"expect": {
"error": "not isVirtualHostableS3Bucket"
}
},
{
"documentation": "bucket..name: not isVirtualHostable (consequetive dots)",
"params": {
"BucketName": "bucket..name"
},
"expect": {
"error": "not isVirtualHostableS3Bucket"
}
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,33 @@
"expect": {
"error": "Invalid hostlabel"
}
},
{
"documentation": "ending with a dot is not a valid hostlabel",
"params": {
"Region": "part1."
},
"expect": {
"error": "Invalid hostlabel"
}
},
{
"documentation": "multiple consecutive dots are not allowed",
"params": {
"Region": "part1..part2"
},
"expect": {
"error": "Invalid hostlabel"
}
},
{
"documentation": "labels cannot start with a dash",
"params": {
"Region": "part1.-part2"
},
"expect": {
"error": "Invalid hostlabel"
}
}
]
}

0 comments on commit 3599d6a

Please sign in to comment.