s3 0.1.36

A lean, modern, unofficial S3-compatible client for Rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
use url::{Host, Url};

use crate::{auth::AddressingStyle, error::Error};

#[derive(Debug)]
pub(crate) struct ResolvedUrl {
    pub(crate) url: Url,
    pub(crate) canonical_uri: String,
    pub(crate) canonical_query_string: String,
}

pub(crate) fn resolve_url(
    base_url: &Url,
    bucket: Option<&str>,
    key: Option<&str>,
    query_params: &[(String, String)],
    addressing: AddressingStyle,
) -> Result<ResolvedUrl, Error> {
    if !base_url.username().is_empty() || base_url.password().is_some() {
        return Err(Error::invalid_config("endpoint must not include user info"));
    }

    let mut url = base_url.clone();

    let canonical_query_string = crate::util::encode::canonical_query_string(query_params);

    if canonical_query_string.is_empty() {
        url.set_query(None);
    } else {
        url.set_query(Some(&canonical_query_string));
    }

    let Some(bucket) = bucket else {
        url.set_path("/");
        return Ok(ResolvedUrl {
            url,
            canonical_uri: "/".to_string(),
            canonical_query_string,
        });
    };
    validate_bucket_name(bucket)?;
    if let Some(key) = key {
        validate_object_key(key)?;
    }

    let host = base_url
        .host_str()
        .ok_or_else(|| Error::invalid_config("endpoint must include host"))?;

    let resolved_style = resolve_addressing_style(base_url, bucket, addressing);

    let (final_host, raw_path) = match resolved_style {
        AddressingStyle::Path => {
            let raw_path = match key {
                Some(key) => format!("/{bucket}/{key}"),
                None => format!("/{bucket}"),
            };
            (None, raw_path)
        }
        AddressingStyle::VirtualHosted => {
            if endpoint_has_ip_host(base_url) {
                return Err(Error::invalid_config(
                    "virtual-hosted-style requires a domain endpoint host",
                ));
            }
            if !is_dns_compatible_bucket(bucket) {
                return Err(Error::invalid_config(
                    "bucket is not DNS compatible for virtual-hosted-style",
                ));
            }
            let raw_path = match key {
                Some(key) => format!("/{key}"),
                _ => "/".to_string(),
            };
            (Some(format!("{bucket}.{host}")), raw_path)
        }
        AddressingStyle::Auto => {
            return Err(Error::invalid_config(
                "internal error: auto addressing style must be resolved",
            ));
        }
    };

    let canonical_uri = crate::util::encode::aws_percent_encode_path(&raw_path);

    url.set_path(&canonical_uri);
    if let Some(final_host) = final_host {
        url.set_host(Some(&final_host))
            .map_err(|_| Error::invalid_config("invalid endpoint host"))?;
    }

    Ok(ResolvedUrl {
        url,
        canonical_uri,
        canonical_query_string,
    })
}

fn resolve_addressing_style(
    base_url: &Url,
    bucket: &str,
    addressing: AddressingStyle,
) -> AddressingStyle {
    match addressing {
        AddressingStyle::Path | AddressingStyle::VirtualHosted => addressing,
        AddressingStyle::Auto => {
            if endpoint_requires_path_style(base_url) {
                return AddressingStyle::Path;
            }

            if base_url.scheme() == "https" && bucket.contains('.') {
                return AddressingStyle::Path;
            }

            if !is_dns_compatible_bucket(bucket) {
                return AddressingStyle::Path;
            }

            AddressingStyle::VirtualHosted
        }
    }
}

fn endpoint_requires_path_style(base_url: &Url) -> bool {
    match base_url.host() {
        Some(Host::Domain(host)) => host.eq_ignore_ascii_case("localhost"),
        Some(Host::Ipv4(_) | Host::Ipv6(_)) => true,
        None => false,
    }
}

fn endpoint_has_ip_host(base_url: &Url) -> bool {
    matches!(base_url.host(), Some(Host::Ipv4(_) | Host::Ipv6(_)))
}

fn validate_bucket_name(bucket: &str) -> Result<(), Error> {
    if bucket.is_empty() {
        return Err(Error::invalid_config("bucket must not be empty"));
    }
    if bucket.trim() != bucket {
        return Err(Error::invalid_config(
            "bucket must not include leading or trailing whitespace",
        ));
    }
    if bucket.contains('/') {
        return Err(Error::invalid_config("bucket must not contain '/'"));
    }
    if bucket
        .bytes()
        .any(|b| b.is_ascii_control() || b.is_ascii_whitespace())
    {
        return Err(Error::invalid_config(
            "bucket must not contain ASCII control or whitespace characters",
        ));
    }
    Ok(())
}

pub(crate) fn validate_object_key(key: &str) -> Result<(), Error> {
    crate::util::validation::validate_object_key(key)
}

fn is_dns_compatible_bucket(bucket: &str) -> bool {
    let bytes = bucket.as_bytes();
    if bytes.len() < 3 || bytes.len() > 63 {
        return false;
    }

    if bytes.iter().any(|b| b.is_ascii_uppercase()) {
        return false;
    }

    let is_allowed = |b: u8| matches!(b, b'a'..=b'z' | b'0'..=b'9' | b'-' | b'.');
    if !bytes.iter().all(|&b| is_allowed(b)) {
        return false;
    }

    let starts_ok = matches!(bytes[0], b'a'..=b'z' | b'0'..=b'9');
    let ends_ok = matches!(bytes[bytes.len() - 1], b'a'..=b'z' | b'0'..=b'9');
    if !starts_ok || !ends_ok {
        return false;
    }

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

    if bucket
        .split('.')
        .any(|label| label.starts_with('-') || label.ends_with('-'))
    {
        return false;
    }

    if bucket.parse::<std::net::IpAddr>().is_ok() {
        return false;
    }

    true
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::auth::AddressingStyle;

    #[test]
    fn resolves_path_style_url_and_does_not_double_encode() {
        let base = Url::parse("https://example.com").unwrap();
        let resolved = resolve_url(
            &base,
            Some("my-bucket"),
            Some("a+b"),
            &[],
            AddressingStyle::Path,
        )
        .unwrap();

        assert_eq!(resolved.canonical_uri, "/my-bucket/a%2Bb");
        assert_eq!(resolved.url.as_str(), "https://example.com/my-bucket/a%2Bb");
    }

    #[test]
    fn resolves_virtual_hosted_style_url() {
        let base = Url::parse("https://s3.example.com").unwrap();
        let resolved = resolve_url(
            &base,
            Some("mybucket"),
            Some("a+b"),
            &[],
            AddressingStyle::VirtualHosted,
        )
        .unwrap();

        assert_eq!(resolved.url.host_str().unwrap(), "mybucket.s3.example.com");
        assert_eq!(resolved.canonical_uri, "/a%2Bb");
    }

    #[test]
    fn auto_falls_back_to_path_style_for_dot_bucket_on_https() {
        let base = Url::parse("https://s3.example.com").unwrap();
        let resolved = resolve_url(
            &base,
            Some("bucket.with.dots"),
            Some("key"),
            &[],
            AddressingStyle::Auto,
        )
        .unwrap();

        assert_eq!(resolved.url.host_str().unwrap(), "s3.example.com");
        assert_eq!(resolved.canonical_uri, "/bucket.with.dots/key");
    }

    #[test]
    fn auto_falls_back_to_path_style_for_ip_endpoints() {
        for endpoint in ["http://127.0.0.1:9000", "http://[::1]:9000"] {
            let base = Url::parse(endpoint).unwrap();
            let resolved = resolve_url(
                &base,
                Some("mybucket"),
                Some("key"),
                &[],
                AddressingStyle::Auto,
            )
            .unwrap();

            assert_eq!(resolved.canonical_uri, "/mybucket/key");
            assert_eq!(resolved.url.path(), "/mybucket/key");
            assert_eq!(resolved.url.host(), base.host());
        }
    }

    #[test]
    fn virtual_hosted_style_allows_localhost_domain_endpoints() {
        let base = Url::parse("http://localhost:9000").unwrap();
        let resolved = resolve_url(
            &base,
            Some("mybucket"),
            Some("key"),
            &[],
            AddressingStyle::VirtualHosted,
        )
        .unwrap();

        assert_eq!(resolved.url.host_str().unwrap(), "mybucket.localhost");
        assert_eq!(resolved.canonical_uri, "/key");
    }

    #[test]
    fn virtual_hosted_style_rejects_ip_endpoints() {
        for endpoint in ["http://127.0.0.1:9000", "http://[::1]:9000"] {
            let base = Url::parse(endpoint).unwrap();
            let err = resolve_url(
                &base,
                Some("mybucket"),
                Some("key"),
                &[],
                AddressingStyle::VirtualHosted,
            )
            .expect_err("virtual-hosted-style must require a domain endpoint");

            assert_invalid_config_contains(err, "domain endpoint host");
        }
    }

    #[test]
    fn virtual_hosted_style_rejects_invalid_dns_labels() {
        let base = Url::parse("https://s3.example.com").unwrap();
        for bucket in ["bad-.bucket", "bad.-bucket"] {
            let err = resolve_url(
                &base,
                Some(bucket),
                Some("key"),
                &[],
                AddressingStyle::VirtualHosted,
            )
            .expect_err("invalid DNS labels must be rejected");

            assert_invalid_config_contains(err, "DNS compatible");
        }
    }

    #[test]
    fn path_encoding_preserves_slash_in_key() {
        let base = Url::parse("https://example.com").unwrap();
        let resolved = resolve_url(
            &base,
            Some("my-bucket"),
            Some("a/b"),
            &[],
            AddressingStyle::Path,
        )
        .unwrap();

        assert_eq!(resolved.canonical_uri, "/my-bucket/a/b");
    }

    #[test]
    fn query_params_are_canonicalized_and_applied_to_url() {
        let base = Url::parse("https://example.com").unwrap();
        let resolved = resolve_url(
            &base,
            Some("my-bucket"),
            Some("key"),
            &[
                ("b".to_string(), "2".to_string()),
                ("a".to_string(), "".to_string()),
            ],
            AddressingStyle::Path,
        )
        .unwrap();

        assert_eq!(resolved.canonical_query_string, "a=&b=2");
        assert_eq!(resolved.url.query().unwrap_or(""), "a=&b=2");
    }

    #[test]
    fn empty_bucket_is_rejected() {
        let base = Url::parse("https://example.com").unwrap();
        let err = match resolve_url(&base, Some(""), Some("key"), &[], AddressingStyle::Path) {
            Ok(_) => panic!("empty bucket should be rejected"),
            Err(err) => err,
        };
        match err {
            Error::InvalidConfig { message } => {
                assert!(message.contains("bucket must not be empty"));
            }
            other => panic!("expected InvalidConfig, got {other:?}"),
        }
    }

    #[test]
    fn empty_object_key_is_rejected() {
        let base = Url::parse("https://example.com").unwrap();
        let err = match resolve_url(
            &base,
            Some("my-bucket"),
            Some(""),
            &[],
            AddressingStyle::Path,
        ) {
            Ok(_) => panic!("empty object key should be rejected"),
            Err(err) => err,
        };

        assert_invalid_config_contains(err, "object key");
    }

    #[test]
    fn object_key_dot_segments_are_rejected() {
        let base = Url::parse("https://example.com").unwrap();
        for key in [".", "..", "a/./b", "a/../b"] {
            let err = resolve_url(
                &base,
                Some("my-bucket"),
                Some(key),
                &[],
                AddressingStyle::Path,
            )
            .expect_err("period-only key segments must be rejected");

            assert_invalid_config_contains(err, "path segments");
        }
    }

    #[test]
    fn object_key_control_characters_are_rejected() {
        let base = Url::parse("https://example.com").unwrap();
        for key in ["line\nbreak", "bad\u{7f}"] {
            let err = resolve_url(
                &base,
                Some("my-bucket"),
                Some(key),
                &[],
                AddressingStyle::Path,
            )
            .expect_err("control-character key should be rejected");

            assert_invalid_config_contains(err, "control");
        }
    }

    #[test]
    fn malformed_bucket_names_are_rejected() {
        let base = Url::parse("https://example.com").unwrap();
        let cases = [
            (" bucket", "whitespace"),
            ("bucket ", "whitespace"),
            ("buck et", "whitespace"),
            ("buck\tet", "whitespace"),
            ("a/b", "'/'"),
        ];

        for (bucket, expected) in cases {
            let err =
                match resolve_url(&base, Some(bucket), Some("key"), &[], AddressingStyle::Path) {
                    Ok(_) => panic!("malformed bucket should be rejected"),
                    Err(err) => err,
                };

            assert_invalid_config_contains(err, expected);
        }
    }

    #[test]
    fn endpoint_with_user_info_is_rejected() {
        let base = Url::parse("https://user:pass@example.com").unwrap();
        let err = match resolve_url(
            &base,
            Some("my-bucket"),
            Some("key"),
            &[],
            AddressingStyle::Path,
        ) {
            Ok(_) => panic!("endpoint with user info should be rejected"),
            Err(err) => err,
        };
        match err {
            Error::InvalidConfig { message } => {
                assert!(message.contains("must not include user info"));
            }
            other => panic!("expected InvalidConfig, got {other:?}"),
        }
    }

    fn assert_invalid_config_contains(err: Error, expected: &str) {
        match err {
            Error::InvalidConfig { message } => assert!(
                message.contains(expected),
                "expected error message to contain {expected:?}, got {message:?}",
            ),
            other => panic!("expected InvalidConfig, got {other:?}"),
        }
    }
}