cargo-rail 0.27.0

Cargo-native Rust workspace engine for affected CI, Rust source-surface analysis, compiler reuse, dependency coherence, exact-SHA releases, and split/sync
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
//! Strict parsing and normalization for remote compiler-cache URLs.

use std::net::IpAddr;

use rscrypto::Sha256;

use super::{RemoteStoreError, RemoteStoreResult};
use crate::compiler::native_cache::RemoteAuthorityId;
use crate::source::ContentDigest;

pub(super) const MAX_URL_BYTES: usize = 4 * 1024;
const MAX_REGION_BYTES: usize = 64;
const MAX_BUCKET_BYTES: usize = 255;
const MAX_PREFIX_BYTES: usize = 2 * 1024;
const AUTHORITY_DOMAIN: &[u8] = b"cargo-rail-remote-authority-v2\0";
const PROTOCOL: &str = "native-v6";

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Provider {
    AwsS3,
    AzureBlob,
    CloudflareR2,
    S3Compatible,
}

struct RemoteCacheUrlParts {
    provider: Provider,
    normalized_url: String,
    endpoint: String,
    bucket: String,
    prefix: String,
    region: String,
    expected_owner: Option<String>,
    addressing: &'static str,
}

impl Provider {
    const fn as_str(self) -> &'static str {
        match self {
            Self::AwsS3 => "aws-s3",
            Self::AzureBlob => "azure-blob",
            Self::CloudflareR2 => "cloudflare-r2",
            Self::S3Compatible => "s3-compatible",
        }
    }
}

#[derive(Clone, PartialEq, Eq)]
pub(super) struct RemoteCacheAuthority {
    provider: Provider,
    normalized_url: String,
    endpoint: String,
    bucket: String,
    prefix: String,
    region: String,
    expected_owner: Option<String>,
    addressing: &'static str,
    identity: RemoteAuthorityId,
}

impl RemoteCacheAuthority {
    pub(super) fn parse(value: &str) -> RemoteStoreResult<Self> {
        validate_url_bytes(value)?;
        let (scheme, remainder) = value
            .split_once("://")
            .ok_or_else(|| RemoteStoreError::configuration("remote cache URL has no supported scheme"))?;
        if scheme != scheme.to_ascii_lowercase() {
            return Err(RemoteStoreError::configuration(
                "remote cache URL scheme must be lowercase",
            ));
        }
        let (location, query) = split_query(remainder)?;
        let (authority, path) = location
            .split_once('/')
            .map_or((location, ""), |(authority, path)| (authority, path));
        if authority.is_empty() || authority.contains('@') {
            return Err(RemoteStoreError::configuration(
                "remote cache URL authority is empty or contains user information",
            ));
        }
        match scheme {
            "s3" => Self::parse_aws(authority, path, query),
            "azure" => Self::parse_azure(authority, path, query),
            "r2" => Self::parse_r2(authority, path, query),
            "s3+http" => Self::parse_loopback_fixture(authority, path, query),
            _ => Err(RemoteStoreError::configuration(
                "remote cache URL scheme must be s3, azure, r2, or loopback s3+http",
            )),
        }
    }

    fn parse_azure(authority: &str, path: &str, query: Option<&str>) -> RemoteStoreResult<Self> {
        if query.is_some() || authority.contains(':') {
            return Err(RemoteStoreError::configuration(
                "Azure Blob URLs do not accept query parameters or ports",
            ));
        }
        let account = authority.to_ascii_lowercase();
        if !(3..=24).contains(&account.len())
            || !account
                .bytes()
                .all(|byte| byte.is_ascii_lowercase() || byte.is_ascii_digit())
        {
            return Err(RemoteStoreError::configuration(
                "Azure Blob URL account must contain 3 to 24 lowercase letters or digits",
            ));
        }
        let (container, prefix) = split_bucket_path(path)?;
        let container = normalize_azure_container(container)?;
        let prefix = normalize_path(prefix)?;
        let endpoint = format!("https://{account}.blob.core.windows.net");
        let normalized_url = format!("azure://{account}/{container}{}", normalized_prefix_path(&prefix));
        Self::new(RemoteCacheUrlParts {
            provider: Provider::AzureBlob,
            normalized_url,
            endpoint,
            bucket: container,
            prefix,
            region: "global".to_string(),
            expected_owner: None,
            addressing: "path",
        })
    }

    fn parse_aws(authority: &str, path: &str, query: Option<&str>) -> RemoteStoreResult<Self> {
        if authority.contains(':') {
            return Err(RemoteStoreError::configuration(
                "official S3 URLs do not accept an explicit endpoint or port",
            ));
        }
        let bucket = normalize_aws_bucket(authority)?;
        let prefix = normalize_path(path)?;
        let query = parse_query(query, &["owner", "region"])?;
        let region = required_query(&query, "region")?;
        validate_region(region)?;
        let owner = required_query(&query, "owner")?;
        validate_expected_owner(owner)?;
        let endpoint = format!("https://{bucket}.s3.{region}.amazonaws.com");
        let normalized_url = format!(
            "s3://{bucket}{}?owner={owner}&region={region}",
            normalized_prefix_path(&prefix)
        );
        Self::new(RemoteCacheUrlParts {
            provider: Provider::AwsS3,
            normalized_url,
            endpoint,
            bucket,
            prefix,
            region: region.to_string(),
            expected_owner: Some(owner.to_string()),
            addressing: "virtual-hosted",
        })
    }

    fn parse_r2(authority: &str, path: &str, query: Option<&str>) -> RemoteStoreResult<Self> {
        if query.is_some() || authority.contains(':') {
            return Err(RemoteStoreError::configuration(
                "R2 URLs do not accept query parameters or ports",
            ));
        }
        let account = authority.to_ascii_lowercase();
        if account.len() != 32 || !account.bytes().all(|byte| byte.is_ascii_hexdigit()) {
            return Err(RemoteStoreError::configuration(
                "R2 URL account ID must contain exactly 32 hexadecimal digits",
            ));
        }
        let (bucket, prefix) = split_bucket_path(path)?;
        let bucket = normalize_compatible_bucket(bucket)?;
        let prefix = normalize_path(prefix)?;
        let endpoint = format!("https://{account}.r2.cloudflarestorage.com");
        let normalized_url = format!("r2://{account}/{bucket}{}", normalized_prefix_path(&prefix));
        Self::new(RemoteCacheUrlParts {
            provider: Provider::CloudflareR2,
            normalized_url,
            endpoint,
            bucket,
            prefix,
            region: "auto".to_string(),
            expected_owner: None,
            addressing: "path",
        })
    }

    fn parse_loopback_fixture(authority: &str, path: &str, query: Option<&str>) -> RemoteStoreResult<Self> {
        let (host, port) = normalize_endpoint(authority)?;
        let loopback = host
            .trim_start_matches('[')
            .trim_end_matches(']')
            .parse::<IpAddr>()
            .is_ok_and(|address| address.is_loopback());
        if !loopback || port.is_none() {
            return Err(RemoteStoreError::configuration(
                "cleartext remote cache URLs require a loopback IP and explicit port",
            ));
        }
        let (bucket, prefix) = split_bucket_path(path)?;
        let bucket = normalize_compatible_bucket(bucket)?;
        let prefix = normalize_path(prefix)?;
        let query = parse_query(query, &["region"])?;
        let region = required_query(&query, "region")?;
        validate_region(region)?;
        let normalized_authority = match port {
            Some(port) => format!("{host}:{port}"),
            None => return Err(RemoteStoreError::configuration("loopback fixture port is missing")),
        };
        let endpoint = format!("http://{normalized_authority}");
        let normalized_url = format!(
            "s3+http://{normalized_authority}/{bucket}{}?region={region}",
            normalized_prefix_path(&prefix)
        );
        Self::new(RemoteCacheUrlParts {
            provider: Provider::S3Compatible,
            normalized_url,
            endpoint,
            bucket,
            prefix,
            region: region.to_string(),
            expected_owner: None,
            addressing: "path",
        })
    }
    fn new(parts: RemoteCacheUrlParts) -> RemoteStoreResult<Self> {
        let RemoteCacheUrlParts {
            provider,
            normalized_url,
            endpoint,
            bucket,
            prefix,
            region,
            expected_owner,
            addressing,
        } = parts;
        let identity = authority_id(
            provider,
            &endpoint,
            &bucket,
            &prefix,
            &region,
            expected_owner.as_deref(),
            addressing,
        )?;
        Ok(Self {
            provider,
            normalized_url,
            endpoint,
            bucket,
            prefix,
            region,
            expected_owner,
            addressing,
            identity,
        })
    }

    pub(super) fn identity(&self) -> &RemoteAuthorityId {
        &self.identity
    }

    pub(super) const fn provider_name(&self) -> &'static str {
        self.provider.as_str()
    }

    pub(super) const fn protocol_name(&self) -> &'static str {
        PROTOCOL
    }

    pub(super) fn normalized_url(&self) -> &str {
        &self.normalized_url
    }

    pub(super) const fn is_official_aws(&self) -> bool {
        matches!(self.provider, Provider::AwsS3)
    }

    pub(super) const fn is_azure_blob(&self) -> bool {
        matches!(self.provider, Provider::AzureBlob)
    }

    pub(super) fn is_loopback_fixture(&self) -> bool {
        matches!(self.provider, Provider::S3Compatible) && self.endpoint.starts_with("http://")
    }

    pub(super) const fn supports_s3_transport(&self) -> bool {
        matches!(
            self.provider,
            Provider::AwsS3 | Provider::CloudflareR2 | Provider::S3Compatible
        )
    }

    pub(super) fn endpoint(&self) -> &str {
        &self.endpoint
    }

    pub(super) fn bucket(&self) -> &str {
        &self.bucket
    }

    pub(super) fn prefix(&self) -> &str {
        &self.prefix
    }

    pub(super) fn region(&self) -> &str {
        &self.region
    }

    pub(super) fn expected_owner(&self) -> Option<&str> {
        self.expected_owner.as_deref()
    }

    pub(super) const fn addressing(&self) -> &'static str {
        self.addressing
    }
}

fn normalize_azure_container(value: &str) -> RemoteStoreResult<String> {
    let container = value.to_ascii_lowercase();
    if !(3..=63).contains(&container.len())
        || !container
            .bytes()
            .all(|byte| byte.is_ascii_lowercase() || byte.is_ascii_digit() || byte == b'-')
        || !container.as_bytes()[0].is_ascii_alphanumeric()
        || !container.as_bytes()[container.len() - 1].is_ascii_alphanumeric()
        || container.contains("--")
    {
        return Err(RemoteStoreError::configuration(
            "Azure Blob URL container is not a canonical container name",
        ));
    }
    Ok(container)
}

fn validate_url_bytes(value: &str) -> RemoteStoreResult<()> {
    if value.is_empty()
        || value.len() > MAX_URL_BYTES
        || !value.is_ascii()
        || value
            .bytes()
            .any(|byte| byte.is_ascii_control() || byte.is_ascii_whitespace() || byte == b'\\' || byte == b'#')
    {
        return Err(RemoteStoreError::configuration(
            "remote cache URL is empty, oversized, non-ASCII, or contains forbidden bytes",
        ));
    }
    Ok(())
}

fn split_query(value: &str) -> RemoteStoreResult<(&str, Option<&str>)> {
    let mut parts = value.split('?');
    let location = parts.next().unwrap_or_default();
    let query = parts.next();
    if parts.next().is_some() || query == Some("") {
        return Err(RemoteStoreError::configuration(
            "remote cache URL query is empty or ambiguous",
        ));
    }
    Ok((location, query))
}

fn parse_query<'a>(value: Option<&'a str>, expected: &[&str]) -> RemoteStoreResult<Vec<(&'a str, &'a str)>> {
    let value =
        value.ok_or_else(|| RemoteStoreError::configuration("remote cache URL is missing required query data"))?;
    let mut fields = Vec::new();
    for pair in value.split('&') {
        let (name, value) = pair
            .split_once('=')
            .filter(|(name, value)| !name.is_empty() && !value.is_empty())
            .ok_or_else(|| RemoteStoreError::configuration("remote cache URL query is malformed"))?;
        if name.contains('%') || value.contains('%') || !expected.contains(&name) {
            return Err(RemoteStoreError::configuration(
                "remote cache URL query contains encoded or unknown data",
            ));
        }
        if fields.iter().any(|(existing, _)| existing == &name) {
            return Err(RemoteStoreError::configuration(
                "remote cache URL query contains a duplicate key",
            ));
        }
        fields.push((name, value));
    }
    if fields.len() != expected.len() {
        return Err(RemoteStoreError::configuration(
            "remote cache URL query omits a required key",
        ));
    }
    fields.sort_unstable_by_key(|(name, _)| *name);
    Ok(fields)
}

fn required_query<'a>(fields: &[(&'a str, &'a str)], name: &str) -> RemoteStoreResult<&'a str> {
    fields
        .iter()
        .find_map(|(candidate, value)| (*candidate == name).then_some(*value))
        .ok_or_else(|| RemoteStoreError::configuration("remote cache URL omits a required query key"))
}

fn split_bucket_path(path: &str) -> RemoteStoreResult<(&str, &str)> {
    let (bucket, prefix) = path
        .split_once('/')
        .map_or((path, ""), |(bucket, prefix)| (bucket, prefix));
    if bucket.is_empty() {
        return Err(RemoteStoreError::configuration("remote cache URL has an empty bucket"));
    }
    Ok((bucket, prefix))
}

fn normalize_endpoint(authority: &str) -> RemoteStoreResult<(String, Option<u16>)> {
    let (host, port) = if let Some(value) = authority.strip_prefix('[') {
        let (host, suffix) = value
            .split_once(']')
            .ok_or_else(|| RemoteStoreError::configuration("remote cache endpoint has an invalid IPv6 host"))?;
        let port = if suffix.is_empty() {
            None
        } else {
            Some(parse_port(suffix.strip_prefix(':').ok_or_else(|| {
                RemoteStoreError::configuration("remote cache endpoint has an ambiguous port")
            })?)?)
        };
        let address = host
            .parse::<IpAddr>()
            .map_err(|_| RemoteStoreError::configuration("remote cache endpoint has an invalid IPv6 host"))?;
        (format!("[{address}]"), port)
    } else {
        let colon_count = authority.bytes().filter(|byte| *byte == b':').count();
        if colon_count > 1 {
            return Err(RemoteStoreError::configuration(
                "remote cache IPv6 endpoints require brackets",
            ));
        }
        let (host, port) = authority.split_once(':').map_or_else(
            || Ok((authority, None)),
            |(host, port)| Ok((host, Some(parse_port(port)?))),
        )?;
        let host = normalize_host(host)?;
        (host, port)
    };
    Ok((host, port))
}

fn parse_port(value: &str) -> RemoteStoreResult<u16> {
    if value.is_empty() || value.len() > 5 || value.starts_with('0') || !value.bytes().all(|byte| byte.is_ascii_digit())
    {
        return Err(RemoteStoreError::configuration(
            "remote cache endpoint port is invalid or noncanonical",
        ));
    }
    value
        .parse::<u16>()
        .ok()
        .filter(|port| *port != 0)
        .ok_or_else(|| RemoteStoreError::configuration("remote cache endpoint port is invalid"))
}

fn normalize_host(value: &str) -> RemoteStoreResult<String> {
    if value.is_empty() || value.len() > 253 || value.ends_with('.') || value.contains('%') {
        return Err(RemoteStoreError::configuration("remote cache endpoint host is invalid"));
    }
    if let Ok(address) = value.parse::<IpAddr>() {
        return Ok(address.to_string());
    }
    let value = value.to_ascii_lowercase();
    if value.split('.').any(|label| {
        label.is_empty()
            || label.len() > 63
            || !label.as_bytes()[0].is_ascii_alphanumeric()
            || !label.as_bytes()[label.len() - 1].is_ascii_alphanumeric()
            || !label.bytes().all(|byte| byte.is_ascii_alphanumeric() || byte == b'-')
    }) {
        return Err(RemoteStoreError::configuration("remote cache endpoint host is invalid"));
    }
    Ok(value)
}

fn normalize_aws_bucket(value: &str) -> RemoteStoreResult<String> {
    let bucket = value.to_ascii_lowercase();
    let valid_length = (3..=63).contains(&bucket.len());
    let valid_bytes = bucket
        .bytes()
        .all(|byte| byte.is_ascii_lowercase() || byte.is_ascii_digit() || matches!(byte, b'-' | b'.'));
    let valid_edges = bucket
        .as_bytes()
        .first()
        .zip(bucket.as_bytes().last())
        .is_some_and(|(first, last)| first.is_ascii_alphanumeric() && last.is_ascii_alphanumeric());
    let reserved = bucket.starts_with("xn--")
        || bucket.starts_with("sthree-")
        || bucket.starts_with("amzn_s3_demo_")
        || ["-s3alias", "--ol-s3", ".mrap", "--x-s3", "--table-s3"]
            .iter()
            .any(|suffix| bucket.ends_with(suffix));
    if !valid_length
        || !valid_bytes
        || !valid_edges
        || bucket.contains("..")
        || bucket
            .split('.')
            .any(|label| label.is_empty() || label.starts_with('-') || label.ends_with('-'))
        || bucket.parse::<std::net::Ipv4Addr>().is_ok()
        || reserved
    {
        return Err(RemoteStoreError::configuration(
            "official S3 URL bucket is not a classic DNS bucket name",
        ));
    }
    Ok(bucket)
}

fn normalize_compatible_bucket(value: &str) -> RemoteStoreResult<String> {
    let bucket = normalize_segment(value)?;
    if bucket.len() > MAX_BUCKET_BYTES || matches!(bucket.as_str(), "." | "..") {
        return Err(RemoteStoreError::configuration("remote cache URL bucket is invalid"));
    }
    Ok(bucket)
}

fn normalize_path(value: &str) -> RemoteStoreResult<String> {
    let value = value.trim_matches('/');
    if value.len() > MAX_PREFIX_BYTES {
        return Err(RemoteStoreError::configuration(
            "remote cache URL prefix exceeds its byte bound",
        ));
    }
    if value.is_empty() {
        return Ok(String::new());
    }
    value
        .split('/')
        .map(normalize_segment)
        .collect::<RemoteStoreResult<Vec<_>>>()
        .and_then(|segments| {
            if segments
                .iter()
                .any(|segment| segment.is_empty() || matches!(segment.as_str(), "." | ".."))
            {
                Err(RemoteStoreError::configuration(
                    "remote cache URL prefix contains an empty or dot segment",
                ))
            } else {
                Ok(segments.join("/"))
            }
        })
}

fn normalize_segment(value: &str) -> RemoteStoreResult<String> {
    let bytes = value.as_bytes();
    let mut normalized = String::with_capacity(bytes.len());
    let mut index = 0usize;
    while index < bytes.len() {
        let byte = bytes[index];
        if byte == b'%' {
            let high = bytes.get(index + 1).copied().and_then(hex_value);
            let low = bytes.get(index + 2).copied().and_then(hex_value);
            let decoded = high
                .zip(low)
                .map(|(high, low)| high * 16 + low)
                .ok_or_else(|| RemoteStoreError::configuration("remote cache URL contains invalid percent encoding"))?;
            if matches!(decoded, b'/' | b'\\' | 0) || decoded.is_ascii_control() {
                return Err(RemoteStoreError::configuration(
                    "remote cache URL contains an encoded separator or control byte",
                ));
            }
            if unreserved(decoded) {
                normalized.push(char::from(decoded));
            } else if pchar(decoded) {
                normalized.push('%');
                normalized.push(hex_digit(decoded >> 4));
                normalized.push(hex_digit(decoded & 0x0f));
            } else {
                return Err(RemoteStoreError::configuration(
                    "remote cache URL contains unsupported encoded data",
                ));
            }
            index += 3;
            continue;
        }
        if !pchar(byte) {
            return Err(RemoteStoreError::configuration(
                "remote cache URL path contains a noncanonical byte",
            ));
        }
        normalized.push(char::from(byte));
        index += 1;
    }
    Ok(normalized)
}

const fn unreserved(byte: u8) -> bool {
    byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'.' | b'_' | b'~')
}

const fn pchar(byte: u8) -> bool {
    unreserved(byte)
        || matches!(
            byte,
            b'!' | b'$' | b'&' | b'\'' | b'(' | b')' | b'*' | b'+' | b',' | b';' | b'=' | b':' | b'@'
        )
}

const fn hex_value(byte: u8) -> Option<u8> {
    match byte {
        b'0'..=b'9' => Some(byte - b'0'),
        b'a'..=b'f' => Some(byte - b'a' + 10),
        b'A'..=b'F' => Some(byte - b'A' + 10),
        _ => None,
    }
}

fn hex_digit(value: u8) -> char {
    match value {
        0..=9 => char::from(b'0' + value),
        _ => char::from(b'A' + value - 10),
    }
}

fn normalized_prefix_path(prefix: &str) -> String {
    if prefix.is_empty() {
        String::new()
    } else {
        format!("/{prefix}")
    }
}

fn validate_region(region: &str) -> RemoteStoreResult<()> {
    if region.is_empty()
        || region.len() > MAX_REGION_BYTES
        || !region
            .bytes()
            .all(|byte| byte.is_ascii_lowercase() || byte.is_ascii_digit() || byte == b'-')
        || !region.as_bytes()[0].is_ascii_alphanumeric()
        || !region.as_bytes()[region.len() - 1].is_ascii_alphanumeric()
        || region.contains("--")
    {
        return Err(RemoteStoreError::configuration("remote cache region is invalid"));
    }
    Ok(())
}

fn validate_expected_owner(owner: &str) -> RemoteStoreResult<()> {
    if owner.len() != 12 || !owner.bytes().all(|byte| byte.is_ascii_digit()) {
        return Err(RemoteStoreError::configuration(
            "official S3 URL owner must contain exactly 12 decimal digits",
        ));
    }
    Ok(())
}

fn authority_id(
    provider: Provider,
    endpoint: &str,
    bucket: &str,
    prefix: &str,
    region: &str,
    expected_owner: Option<&str>,
    addressing: &str,
) -> RemoteStoreResult<RemoteAuthorityId> {
    let mut hasher = Sha256::new();
    hasher.update(AUTHORITY_DOMAIN);
    for (tag, value) in [
        ("provider", provider.as_str()),
        ("protocol", PROTOCOL),
        ("endpoint", endpoint),
        ("bucket", bucket),
        ("prefix", prefix),
        ("region", region),
        ("expected-owner", expected_owner.unwrap_or("")),
        ("addressing", addressing),
    ] {
        let tag_length = u32::try_from(tag.len())
            .map_err(|_| RemoteStoreError::integrity("remote cache authority tag length is out of range"))?;
        hasher.update(&tag_length.to_le_bytes());
        hasher.update(tag.as_bytes());
        hasher.update(&(value.len() as u64).to_le_bytes());
        hasher.update(value.as_bytes());
    }
    RemoteAuthorityId::parse(format!(
        "remote-authority-v1-sha256-{}",
        ContentDigest::from_sha256_bytes(hasher.finalize())
    ))
    .map_err(|_| RemoteStoreError::integrity("remote cache authority identity could not be derived"))
}

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

    fn parse(value: &str) -> RemoteCacheAuthority {
        RemoteCacheAuthority::parse(value).expect("valid remote URL")
    }

    #[test]
    fn official_s3_normalizes_query_prefix_and_bucket_case() {
        let authority = parse("s3://Rail-Cache//team/%61?region=us-east-1&owner=123456789012");
        assert_eq!(
            authority.normalized_url(),
            "s3://rail-cache/team/a?owner=123456789012&region=us-east-1"
        );
        assert_eq!(authority.provider_name(), "aws-s3");
    }

    #[test]
    fn provider_authorities_are_distinct() {
        let aws = parse("s3://rail-cache/team?owner=123456789012&region=us-east-1");
        let r2 = parse("r2://0123456789abcdef0123456789abcdef/rail-cache/team");
        assert_ne!(aws.identity(), r2.identity());
    }

    #[test]
    fn r2_derives_only_the_account_endpoint_and_auto_region() {
        let authority = parse("r2://0123456789ABCDEF0123456789ABCDEF/cache/team");
        assert_eq!(
            authority.normalized_url(),
            "r2://0123456789abcdef0123456789abcdef/cache/team"
        );
        assert_eq!(authority.provider_name(), "cloudflare-r2");
        assert_eq!(
            authority.endpoint(),
            "https://0123456789abcdef0123456789abcdef.r2.cloudflarestorage.com"
        );
        assert_eq!(authority.region, "auto");
        assert_eq!(authority.addressing, "path");
    }

    #[test]
    fn r2_rejects_noncanonical_accounts_and_jurisdiction_queries() {
        for invalid in [
            "r2://account/cache/team",
            "r2://0123456789abcdef0123456789abcdeg/cache/team",
            "r2://0123456789abcdef0123456789abcdef:443/cache/team",
            "r2://0123456789abcdef0123456789abcdef/cache/team?jurisdiction=eu",
        ] {
            assert!(RemoteCacheAuthority::parse(invalid).is_err(), "accepted {invalid}");
        }
    }

    #[test]
    fn azure_derives_only_the_public_blob_endpoint() {
        let authority = parse("azure://RailCache01/Cargo-Rail//team/%61");
        assert_eq!(authority.normalized_url(), "azure://railcache01/cargo-rail/team/a");
        assert_eq!(authority.endpoint(), "https://railcache01.blob.core.windows.net");
        assert_eq!(authority.bucket(), "cargo-rail");
        assert_eq!(authority.provider_name(), "azure-blob");
        assert!(authority.is_azure_blob());
        assert!(!authority.supports_s3_transport());
    }

    #[test]
    fn cleartext_requires_explicit_loopback_authority() {
        for valid in [
            "s3+http://127.0.0.1:80/cache/team?region=test-1",
            "s3+http://127.0.0.1:9000/cache/team?region=test-1",
        ] {
            let authority = parse(valid);
            let reparsed = parse(authority.normalized_url());
            assert_eq!(reparsed.normalized_url(), authority.normalized_url());
            assert_eq!(reparsed.identity(), authority.identity());
        }
        for invalid in [
            "s3+http://127.0.0.1/cache/team?region=test-1",
            "s3+http://localhost:9000/cache/team?region=test-1",
            "s3+http://192.0.2.1:9000/cache/team?region=test-1",
        ] {
            assert!(RemoteCacheAuthority::parse(invalid).is_err(), "accepted {invalid}");
        }
    }

    #[test]
    fn parser_rejects_credential_and_ambiguity_surfaces() {
        for invalid in [
            "s3://user:secret@rail-cache/team?owner=123456789012&region=us-east-1",
            "s3://rail-cache/team#fragment?owner=123456789012&region=us-east-1",
            "s3://rail-cache/a%2fb?owner=123456789012&region=us-east-1",
            "s3://rail-cache/../team?owner=123456789012&region=us-east-1",
            "s3://rail-cache/team?owner=123456789012&owner=123456789012&region=us-east-1",
            "s3://rail-cache/team?owner=123456789012&region=us-east-1&token=secret",
            "s3+https://cache.example/bucket?region=us-east-1",
            "s3+https://cache.example:0443/bucket?region=us-east-1",
            "azure://account/container?token=secret",
            "azure://account/container--name",
            "azure://account/ab",
        ] {
            assert!(RemoteCacheAuthority::parse(invalid).is_err(), "accepted {invalid}");
        }
    }
}