Unverified Commit f75e1bd7 authored by Nugine's avatar Nugine
Browse files

s3s: path: check_bucket_name

parent 5bf78d31
Loading
Loading
Loading
Loading
+25 −2
Original line number Diff line number Diff line
@@ -3,7 +3,7 @@
//! A path in the S3 storage.
//!
//! + [Request styles](https://docs.aws.amazon.com/AmazonS3/latest/dev/RESTAPI.html#virtual-hosted-path-style-requests)
//! + [Bucket nameing rules](https://docs.aws.amazon.com/AmazonS3/latest/dev/BucketRestrictions.html#bucketnamingrules)
//! + [Bucket nameing rules](https://docs.aws.amazon.com/AmazonS3/latest/userguide/bucketnamingrules.html)

use std::net::IpAddr;

@@ -42,7 +42,7 @@ pub enum ParseS3PathError {
    KeyTooLong,
}

/// See [bucket nameing rules](https://docs.aws.amazon.com/AmazonS3/latest/dev/BucketRestrictions.html#bucketnamingrules)
/// See [bucket nameing rules](https://docs.aws.amazon.com/AmazonS3/latest/userguide/bucketnamingrules.html)
#[must_use]
pub fn check_bucket_name(name: &str) -> bool {
    if !(3_usize..64).contains(&name.len()) {
@@ -65,6 +65,10 @@ pub fn check_bucket_name(name: &str) -> bool {
        return false;
    }

    if name.contains("..") {
        return false;
    }

    if name.parse::<IpAddr>().is_ok() {
        return false;
    }
@@ -155,6 +159,25 @@ pub fn parse_virtual_hosted_style<'a>(base_domain: &str, host: &'a str, uri_path
mod tests {
    use super::*;

    #[test]
    fn bucket_naming_rules() {
        let cases = [
            ("docexamplebucket1", true),
            ("log-delivery-march-2020", true),
            ("my-hosted-content", true),
            ("docexamplewebsite.com", true),
            ("www.docexamplewebsite.com", true),
            ("my.example.s3.bucket", true),
            ("doc_example_bucket", false),
            ("DocExampleBucket", false),
            ("doc-example-bucket-", false),
        ];

        for (input, expected) in cases {
            assert_eq!(check_bucket_name(input), expected);
        }
    }

    fn bucket(bucket: &str) -> S3Path {
        S3Path::Bucket { bucket: bucket.into() }
    }