ocilot 0.2.3

cli and library for interacting with OCI registries
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
use std::fmt::Debug;
use std::sync::Arc;

use crate::models::Token;
use crate::{Result, error};
use async_trait::async_trait;
use bytes::Bytes;
use reqwest::header::LOCATION;
use reqwest::{RequestBuilder, Response};
use snafu::{OptionExt, ResultExt};
use url::Url;

/// Accept header sent on manifest requests so the registry returns the
/// canonical manifest representation rather than an arbitrary default.
/// Includes both OCI and Docker media types for maximum compatibility.
pub(crate) const MANIFEST_ACCEPT: &str = concat!(
    "application/vnd.oci.image.index.v1+json,",
    "application/vnd.oci.image.manifest.v1+json,",
    "application/vnd.docker.distribution.manifest.list.v2+json,",
    "application/vnd.docker.distribution.manifest.v2+json",
);

/// A trait for a client implementing requests to an OCI registry.
///
/// This is primarily implemented to allow for ease of unit testing this crate.
#[async_trait]
pub(crate) trait RegistryClientImpl: Send + Sync + Debug {
    /// GET {uri}/v2/_catalog
    async fn catalog(&self, uri: &Url) -> Result<Response>;
    /// GET {uri}/v2/{repository}/tags/list
    async fn get_tags(&self, uri: &Url, repository: &str) -> Result<Response>;
    /// HEAD {uri}/v2/{repository}/blobs/{digest}
    async fn head_blob(&self, uri: &Url, repository: &str, digest: &str) -> Result<Response>;
    /// GET {uri}/v2/{repository}/blobs/{digest}
    async fn get_blob(&self, uri: &Url, repository: &str, digest: &str) -> Result<Response>;
    /// DELETE {uri}/v2/{repository}/blobs/{digest}
    async fn del_blob(&self, uri: &Url, repository: &str, digest: &str) -> Result<Response>;
    /// POST {url}/v2/{repository}/blobs/uploads/?digest={digest} (monolithic upload)
    async fn post_blob(
        &self,
        uri: &Url,
        repository: &str,
        data: Bytes,
        digest: &str,
    ) -> Result<Response>;
    /// POST {url}/v2/{repository}/blobs/uploads/ to start a chunked upload.
    async fn start_upload(&self, uri: &Url, repository: &str) -> Result<Response>;
    /// PATCH against an upload URL returned by the registry.
    async fn upload_part(
        &self,
        upload_url: &Url,
        data: Bytes,
        start: usize,
        end: usize,
    ) -> Result<Response>;
    /// PUT against an upload URL with the final digest query parameter.
    async fn finish_blob_upload(
        &self,
        upload_url: &Url,
        data: Bytes,
        digest: &str,
        start: usize,
        end: usize,
    ) -> Result<Response>;
    /// HEAD {uri}/v2/{repository}/manifests/{reference}
    async fn head_manifest(&self, uri: &Url, repository: &str, reference: &str)
    -> Result<Response>;
    /// GET {uri}/v2/{repository}/manifests/{reference}
    async fn get_manifest(&self, uri: &Url, repository: &str, reference: &str) -> Result<Response>;
    /// PUT {uri}/v2/{repository}/manifests/{reference}
    async fn put_manifest(
        &self,
        uri: &Url,
        repository: &str,
        reference: &str,
        content_type: &str,
        body: Bytes,
    ) -> Result<Response>;
    /// DELETE {uri}/v2/{repository}/manifests/{reference}
    async fn del_manifest(&self, uri: &Url, repository: &str, reference: &str) -> Result<Response>;
}

/// Implements a simple registry client using reqwest
#[derive(Debug)]
pub struct SimpleRegistryClient {
    client: reqwest::Client,
    auth: Option<Token>,
}

impl SimpleRegistryClient {
    pub fn new(auth: Option<Token>) -> Self {
        Self {
            client: reqwest::Client::new(),
            auth,
        }
    }

    pub(crate) fn auth(&self, request: RequestBuilder) -> RequestBuilder {
        if let Some(token) = self.auth.as_ref() {
            match token {
                Token::Bearer(t) => request.bearer_auth(t),
                Token::Basic { username, password } => request.basic_auth(username, Some(password)),
            }
        } else {
            request
        }
    }
}

/// Build a `/v2/...` URL from literal + dynamic path segments.
///
/// Unlike `format!("/v2/{repo}/blobs/{digest}")` followed by `Url::join`,
/// this percent-encodes each segment independently via `path_segments_mut`
/// and never interprets `/`, `..`, `?`, or `#` inside a segment specially.
/// That matters because `repository`/`digest`/`reference` values can
/// originate from untrusted registry responses (e.g. a manifest digest read
/// back from a third-party registry during `copy`); without this, such a
/// value could inject extra path segments or otherwise redirect the request
/// to an unintended endpoint on the same host.
fn v2_url<'a>(base: &Url, segments: impl IntoIterator<Item = &'a str>) -> Result<Url> {
    let mut url = base.join("/v2/").context(error::UrlSnafu)?;
    {
        let mut path = url
            .path_segments_mut()
            .map_err(|_| error::Error::Internal {
                context: "registry url cannot be used as a base for path segments",
            })?;
        path.pop_if_empty();
        path.extend(segments);
    }
    Ok(url)
}

#[async_trait]
impl RegistryClientImpl for SimpleRegistryClient {
    async fn catalog(&self, uri: &Url) -> Result<Response> {
        let request = self
            .client
            .get(uri.join("/v2/_catalog").context(error::UrlSnafu)?);
        self.auth(request).send().await.context(error::RequestSnafu)
    }

    async fn head_blob(&self, uri: &Url, repository: &str, digest: &str) -> Result<Response> {
        let request = self
            .client
            .head(v2_url(uri, repository.split('/').chain(["blobs", digest]))?);
        self.auth(request).send().await.context(error::RequestSnafu)
    }

    async fn get_blob(&self, uri: &Url, repository: &str, digest: &str) -> Result<Response> {
        let request = self
            .client
            .get(v2_url(uri, repository.split('/').chain(["blobs", digest]))?);
        self.auth(request).send().await.context(error::RequestSnafu)
    }

    async fn del_blob(&self, uri: &Url, repository: &str, digest: &str) -> Result<Response> {
        let request = self
            .client
            .delete(v2_url(uri, repository.split('/').chain(["blobs", digest]))?);
        self.auth(request).send().await.context(error::RequestSnafu)
    }

    async fn get_tags(&self, uri: &Url, repository: &str) -> Result<Response> {
        let request = self
            .client
            .get(v2_url(uri, repository.split('/').chain(["tags", "list"]))?);
        self.auth(request).send().await.context(error::RequestSnafu)
    }

    async fn post_blob(
        &self,
        uri: &Url,
        repository: &str,
        data: Bytes,
        digest: &str,
    ) -> Result<Response> {
        let mut uri = v2_url(uri, repository.split('/').chain(["blobs", "uploads", ""]))?;
        uri.query_pairs_mut().append_pair("digest", digest);
        let request = self.client.post(uri);
        self.auth(request)
            .header("Content-Type", "application/octet-stream")
            .header("Content-Length", data.len())
            .body(data)
            .send()
            .await
            .context(error::RequestSnafu)
    }

    async fn start_upload(&self, uri: &Url, repository: &str) -> Result<Response> {
        let request = self.client.post(v2_url(
            uri,
            repository.split('/').chain(["blobs", "uploads", ""]),
        )?);
        self.auth(request)
            .header("Content-Length", 0)
            .send()
            .await
            .context(error::RequestSnafu)
    }

    async fn upload_part(
        &self,
        upload_url: &Url,
        data: Bytes,
        start: usize,
        end: usize,
    ) -> Result<Response> {
        // Content-Range uses inclusive endpoints per RFC 7233; callers pass the
        // exclusive end (`start + len`) so subtract 1 for the wire format.
        let last = end.saturating_sub(1);
        let request = self.client.patch(upload_url.clone());
        self.auth(request)
            .header("Content-Type", "application/octet-stream")
            .header("Content-Length", data.len())
            .header("Content-Range", format!("{}-{}", start, last))
            .body(data)
            .send()
            .await
            .context(error::RequestSnafu)
    }

    async fn finish_blob_upload(
        &self,
        upload_url: &Url,
        data: Bytes,
        digest: &str,
        start: usize,
        end: usize,
    ) -> Result<Response> {
        let mut uri = upload_url.clone();
        // Append digest while preserving any pre-existing query params placed
        // there by the registry (some, like ECR, embed signature material).
        let new_query = match uri.query() {
            Some(existing) if !existing.is_empty() => format!("{existing}&digest={digest}"),
            _ => format!("digest={digest}"),
        };
        uri.set_query(Some(&new_query));
        let request = self.client.put(uri);
        let request = if data.is_empty() {
            self.auth(request).header("Content-Length", 0)
        } else {
            // Inclusive end byte per RFC 7233 / OCI distribution spec.
            let last = end.saturating_sub(1);
            self.auth(request)
                .header("Content-Type", "application/octet-stream")
                .header("Content-Length", data.len())
                .header("Content-Range", format!("{}-{}", start, last))
                .body(data)
        };
        request.send().await.context(error::RequestSnafu)
    }

    async fn head_manifest(
        &self,
        uri: &Url,
        repository: &str,
        reference: &str,
    ) -> Result<Response> {
        let request = self.client.head(v2_url(
            uri,
            repository.split('/').chain(["manifests", reference]),
        )?);
        self.auth(request)
            .header("Accept", MANIFEST_ACCEPT)
            .send()
            .await
            .context(error::RequestSnafu)
    }

    async fn get_manifest(&self, uri: &Url, repository: &str, reference: &str) -> Result<Response> {
        let request = self.client.get(v2_url(
            uri,
            repository.split('/').chain(["manifests", reference]),
        )?);
        self.auth(request)
            .header("Accept", MANIFEST_ACCEPT)
            .send()
            .await
            .context(error::RequestSnafu)
    }

    async fn put_manifest(
        &self,
        uri: &Url,
        repository: &str,
        reference: &str,
        content_type: &str,
        body: Bytes,
    ) -> Result<Response> {
        let request = self.client.put(v2_url(
            uri,
            repository.split('/').chain(["manifests", reference]),
        )?);
        self.auth(request)
            .header("Content-Type", content_type)
            .header("Content-Length", body.len())
            .body(body)
            .send()
            .await
            .context(error::RequestSnafu)
    }

    async fn del_manifest(&self, uri: &Url, repository: &str, reference: &str) -> Result<Response> {
        let request = self.client.delete(v2_url(
            uri,
            repository.split('/').chain(["manifests", reference]),
        )?);
        self.auth(request).send().await.context(error::RequestSnafu)
    }
}

/// Handle to OCI registry HTTP operations.
///
/// Wraps the underlying client implementation to enable dependency injection
/// and unit testing. The inner trait already requires `Send + Sync + Debug`,
/// so `Arc<dyn ...>` provides safe sharing without any `unsafe impl`.
#[derive(Clone, Debug)]
pub struct RegistryClient {
    client: Arc<dyn RegistryClientImpl>,
}

impl RegistryClient {
    pub fn new(auth: Option<Token>) -> Self {
        Self {
            client: Arc::new(SimpleRegistryClient::new(auth)),
        }
    }

    pub async fn catalog(&self, uri: Url) -> Result<Response> {
        self.client.catalog(&uri).await
    }

    pub async fn head_blob(
        &self,
        uri: Url,
        repository: String,
        digest: String,
    ) -> Result<Response> {
        self.client
            .head_blob(&uri, repository.as_str(), digest.as_str())
            .await
    }

    pub async fn get_blob(&self, uri: Url, repository: String, digest: String) -> Result<Response> {
        self.client
            .get_blob(&uri, repository.as_str(), digest.as_str())
            .await
    }

    pub async fn del_blob(&self, uri: Url, repository: String, digest: String) -> Result<Response> {
        self.client
            .del_blob(&uri, repository.as_str(), digest.as_str())
            .await
    }

    pub async fn get_tags(&self, uri: &Url, repository: &str) -> Result<Response> {
        self.client.get_tags(uri, repository).await
    }

    pub async fn post_blob(
        &self,
        uri: Url,
        repository: String,
        data: Bytes,
        digest: String,
    ) -> Result<Response> {
        self.client
            .post_blob(&uri, repository.as_str(), data, digest.as_str())
            .await
    }

    pub async fn start_upload(&self, uri: Url, repository: String) -> Result<Response> {
        self.client.start_upload(&uri, repository.as_str()).await
    }

    pub async fn upload_part(
        &self,
        upload_url: Url,
        data: Bytes,
        start: usize,
        end: usize,
    ) -> Result<Response> {
        self.client.upload_part(&upload_url, data, start, end).await
    }

    pub async fn finish_blob_upload(
        &self,
        upload_url: Url,
        data: Bytes,
        digest: String,
        start: usize,
        end: usize,
    ) -> Result<Response> {
        self.client
            .finish_blob_upload(&upload_url, data, digest.as_str(), start, end)
            .await
    }

    pub async fn head_manifest(
        &self,
        uri: Url,
        repository: String,
        reference: String,
    ) -> Result<Response> {
        self.client
            .head_manifest(&uri, repository.as_str(), reference.as_str())
            .await
    }

    pub async fn get_manifest(
        &self,
        uri: Url,
        repository: String,
        reference: String,
    ) -> Result<Response> {
        self.client
            .get_manifest(&uri, repository.as_str(), reference.as_str())
            .await
    }

    pub async fn put_manifest(
        &self,
        uri: Url,
        repository: String,
        reference: String,
        content_type: &str,
        body: Bytes,
    ) -> Result<Response> {
        self.client
            .put_manifest(
                &uri,
                repository.as_str(),
                reference.as_str(),
                content_type,
                body,
            )
            .await
    }

    pub async fn del_manifest(
        &self,
        uri: Url,
        repository: String,
        reference: String,
    ) -> Result<Response> {
        self.client
            .del_manifest(&uri, repository.as_str(), reference.as_str())
            .await
    }
}

/// Extract the `Location` header from a response and resolve it against the
/// request base URL. Registries are allowed to return either an absolute URL
/// (ECR commonly does this with a different host) or a relative path.
pub(crate) fn extract_location(response: &Response, base: &Url) -> crate::Result<Url> {
    let header = response
        .headers()
        .get(LOCATION)
        .context(error::StartBlobNoLocationSnafu)?
        .to_str()
        .context(error::ImproperHeaderSnafu)?;
    base.join(header).context(error::UrlSnafu)
}

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

    #[test]
    fn v2_url_builds_expected_path() {
        let base = Url::parse("https://registry.example.com").unwrap();
        let url = v2_url(&base, "org/app".split('/').chain(["blobs", "sha256:abc"])).unwrap();
        assert_eq!(
            url.as_str(),
            "https://registry.example.com/v2/org/app/blobs/sha256:abc"
        );
    }

    #[test]
    fn v2_url_percent_encodes_untrusted_segments() {
        // A malicious/compromised registry could return a digest-shaped
        // value containing path-traversal or path-injection characters.
        // Each segment must stay confined to a single percent-encoded path
        // component rather than being able to introduce extra segments.
        let base = Url::parse("https://registry.example.com").unwrap();
        let malicious_digest = "sha256:../../../etc/passwd";
        let url = v2_url(
            &base,
            "org/app".split('/').chain(["blobs", malicious_digest]),
        )
        .unwrap();
        // The traversal characters are percent-encoded within the final
        // path segment, so the request cannot escape /v2/org/app/blobs/.
        assert!(url.path().starts_with("/v2/org/app/blobs/"));
        assert_eq!(url.path_segments().unwrap().count(), 5);
        assert!(!url.path().contains("/etc/passwd"));
    }
}