nora-registry 1.2.2

Cloud-Native Artifact Registry - Fast, lightweight, multi-protocol
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
#![deny(clippy::unwrap_used)]
#![forbid(unsafe_code)]
// Test code is linted under `--all-targets` but is not held to the production
// restriction/style lints: `.unwrap()`/`.expect()` are idiomatic in tests, and
// the small style nits (redundant clone/field names, `>= x + 1`, etc.) are not
// worth contorting assertions over. Scoped to `cfg(test)` so production keeps
// the strict profile (incl. the workspace `redundant_clone = deny`).
#![cfg_attr(
    test,
    allow(
        clippy::unwrap_used,
        clippy::redundant_clone,
        clippy::int_plus_one,
        clippy::field_reassign_with_default,
        clippy::unnecessary_get_then_check,
        clippy::single_match,
        clippy::redundant_field_names,
        clippy::len_zero,
        clippy::items_after_test_module
    )
)]
//! NORA Registry — library interface for fuzzing and testing

pub mod validation;
pub mod verified;

/// Re-export Docker manifest parsing for fuzz targets
pub mod docker_fuzz {
    pub fn detect_manifest_media_type(data: &[u8]) -> String {
        let Ok(value) = serde_json::from_slice::<serde_json::Value>(data) else {
            return "application/octet-stream".to_string();
        };
        if let Some(mt) = value.get("mediaType").and_then(|v| v.as_str()) {
            return mt.to_string();
        }
        if value.get("manifests").is_some() {
            return "application/vnd.oci.image.index.v1+json".to_string();
        }
        if value.get("schemaVersion").and_then(|v| v.as_i64()) == Some(2) {
            if value.get("layers").is_some() {
                return "application/vnd.oci.image.manifest.v1+json".to_string();
            }
            return "application/vnd.docker.distribution.manifest.v2+json".to_string();
        }
        if value.get("schemaVersion").and_then(|v| v.as_i64()) == Some(1) {
            return "application/vnd.docker.distribution.manifest.v1+json".to_string();
        }
        "application/vnd.docker.distribution.manifest.v2+json".to_string()
    }
}

/// Re-export npm metadata rewriting for fuzz targets
pub mod npm_fuzz {
    #[allow(clippy::result_unit_err)]
    pub fn rewrite_tarball_urls(
        data: &[u8],
        nora_base: &str,
        upstream_url: &str,
    ) -> Result<Vec<u8>, ()> {
        let mut json: serde_json::Value = serde_json::from_slice(data).map_err(|_| ())?;

        let upstream_trimmed = upstream_url.trim_end_matches('/');
        let nora_npm_base = format!("{}/npm", nora_base.trim_end_matches('/'));

        if let Some(versions) = json.get_mut("versions").and_then(|v| v.as_object_mut()) {
            for (_ver, version_data) in versions.iter_mut() {
                if let Some(tarball_url) = version_data
                    .get("dist")
                    .and_then(|d| d.get("tarball"))
                    .and_then(|t| t.as_str())
                    .map(|s| s.to_string())
                {
                    let rewritten = tarball_url.replace(upstream_trimmed, &nora_npm_base);
                    if let Some(dist) = version_data.get_mut("dist") {
                        dist["tarball"] = serde_json::Value::String(rewritten);
                    }
                }
            }
        }

        let output = serde_json::to_vec(&json).map_err(|_| ())?;

        // Safety net: byte-level replace of any remaining upstream URL prefix (#439)
        Ok(replace_upstream_bytes(
            &output,
            upstream_trimmed,
            &nora_npm_base,
        ))
    }

    /// Byte-level replace of upstream URL prefix (safety net for fuzz targets).
    fn replace_upstream_bytes(data: &[u8], upstream_url: &str, nora_npm_base: &str) -> Vec<u8> {
        if upstream_url.is_empty() {
            return data.to_vec();
        }
        let needle = upstream_url.as_bytes();
        if memchr::memmem::find(data, needle).is_none() {
            return data.to_vec();
        }
        let replacement = nora_npm_base.as_bytes();
        let mut result = Vec::with_capacity(data.len());
        let mut start = 0;
        let finder = memchr::memmem::Finder::new(needle);
        while let Some(pos) = finder.find(&data[start..]) {
            result.extend_from_slice(&data[start..start + pos]);
            result.extend_from_slice(replacement);
            start += pos + needle.len();
        }
        result.extend_from_slice(&data[start..]);
        result
    }

    #[cfg(test)]
    mod url_leak_tests {
        //! #385: a rewrite must never leak the configured upstream host into
        //! client-facing output. The fuzz target `fuzz_url_leak` explores this;
        //! these deterministic cases enforce it on every CI run (there is no
        //! fuzz CI job, so this is the front-loop guard).
        use super::rewrite_tarball_urls;

        const UPSTREAM: &str = "https://upstream-host.invalid";
        const NORA: &str = "http://nora.test";

        #[test]
        fn tarball_rewrite_drops_upstream_host() {
            let meta = br#"{"versions":{"1.0.0":{"dist":{"tarball":"https://upstream-host.invalid/p/-/p-1.0.0.tgz"}}}}"#;
            let out = rewrite_tarball_urls(meta, NORA, UPSTREAM).expect("valid json");
            let out = String::from_utf8(out).expect("json is utf-8");
            assert!(!out.contains(UPSTREAM), "upstream host leaked: {out}");
            assert!(
                out.contains("http://nora.test/npm/p/-/p-1.0.0.tgz"),
                "tarball not rewritten to nora base: {out}"
            );
        }

        #[test]
        fn byte_safety_net_scrubs_upstream_outside_dist() {
            // An upstream URL hidden in a non-`dist` field must still be removed
            // by the #439 byte-level safety-net before the client sees it.
            let meta = br#"{"_note":"mirror of https://upstream-host.invalid/x","versions":{}}"#;
            let out = rewrite_tarball_urls(meta, NORA, UPSTREAM).expect("valid json");
            assert!(
                !String::from_utf8_lossy(&out).contains(UPSTREAM),
                "byte safety-net let the upstream host through"
            );
        }
    }
}

/// Re-export PyPI HTML parsing for fuzz targets
pub mod pypi_fuzz {
    use crate::validation::ends_with_ci;

    pub fn extract_filename(url: &str) -> Option<String> {
        let url = url.split('#').next()?;
        let filename = url.rsplit('/').next()?;

        if ends_with_ci(filename, ".tar.gz")
            || ends_with_ci(filename, ".tgz")
            || ends_with_ci(filename, ".whl")
            || ends_with_ci(filename, ".zip")
            || ends_with_ci(filename, ".egg")
        {
            Some(filename.to_string())
        } else {
            None
        }
    }

    pub fn parse_upstream_html(html: &str) -> Vec<(String, Option<String>)> {
        let mut files = Vec::new();
        let mut remaining = html;

        while let Some(href_start) = remaining.find("href=\"") {
            remaining = &remaining[href_start + 6..];
            if let Some(href_end) = remaining.find('"') {
                let url = &remaining[..href_end];
                if let Some(filename) = extract_filename(url) {
                    let sha256 = url.find("#sha256=").map(|pos| url[pos + 8..].to_string());
                    files.push((filename, sha256));
                }
                remaining = &remaining[href_end..];
            }
        }
        files
    }
}

/// Re-export Maven path classification for fuzz targets
pub mod maven_fuzz {
    use crate::validation::ends_with_ci;

    #[derive(Debug, PartialEq)]
    pub enum MavenPathKind {
        VersionFile {
            group_path: String,
            artifact_id: String,
            version: String,
            filename: String,
        },
        ArtifactMeta {
            group_path: String,
            artifact_id: String,
            filename: String,
        },
        Opaque,
    }

    pub fn classify_path(path: &str) -> MavenPathKind {
        let segments: Vec<&str> = path.split('/').filter(|s| !s.is_empty()).collect();

        if segments.len() < 2 {
            return MavenPathKind::Opaque;
        }

        let last = segments[segments.len() - 1];

        if (last == "maven-metadata.xml" || last.starts_with("maven-metadata.xml."))
            && segments.len() >= 2
        {
            return MavenPathKind::ArtifactMeta {
                group_path: segments[..segments.len() - 2].join("/"),
                artifact_id: segments[segments.len() - 2].to_string(),
                filename: last.to_string(),
            };
        }

        if segments.len() >= 4 {
            return MavenPathKind::VersionFile {
                group_path: segments[..segments.len() - 3].join("/"),
                artifact_id: segments[segments.len() - 3].to_string(),
                version: segments[segments.len() - 2].to_string(),
                filename: last.to_string(),
            };
        }

        MavenPathKind::Opaque
    }

    pub fn is_checksum_file(filename: &str) -> bool {
        ends_with_ci(filename, ".md5")
            || ends_with_ci(filename, ".sha1")
            || ends_with_ci(filename, ".sha256")
            || ends_with_ci(filename, ".sha512")
    }

    pub fn is_snapshot(version: &str) -> bool {
        version.ends_with("-SNAPSHOT")
    }

    pub fn compare_maven_versions(a: &str, b: &str) -> std::cmp::Ordering {
        let a_base = a.strip_suffix("-SNAPSHOT").unwrap_or(a);
        let b_base = b.strip_suffix("-SNAPSHOT").unwrap_or(b);

        let a_parts: Vec<&str> = a_base.split(['.', '-']).collect();
        let b_parts: Vec<&str> = b_base.split(['.', '-']).collect();

        for (ap, bp) in a_parts.iter().zip(b_parts.iter()) {
            let ord = match (ap.parse::<u64>(), bp.parse::<u64>()) {
                (Ok(an), Ok(bn)) => an.cmp(&bn),
                _ => ap.cmp(bp),
            };
            if ord != std::cmp::Ordering::Equal {
                return ord;
            }
        }

        let base_ord = a_parts.len().cmp(&b_parts.len());
        if base_ord != std::cmp::Ordering::Equal {
            return base_ord;
        }

        let a_snap = a.ends_with("-SNAPSHOT");
        let b_snap = b.ends_with("-SNAPSHOT");
        b_snap.cmp(&a_snap)
    }

    pub fn xml_escape(s: &str) -> String {
        s.replace('&', "&amp;")
            .replace('<', "&lt;")
            .replace('>', "&gt;")
            .replace('"', "&quot;")
    }
}

/// Re-export version parsing for fuzz targets
pub mod version_fuzz {
    use crate::validation::ends_with_ci;

    pub fn parse_npm_tarball_version(package_name: &str, filename: &str) -> Option<String> {
        let filename = filename.strip_suffix(".tgz")?;
        let name_part = if package_name.contains('/') {
            package_name.rsplit('/').next()?
        } else {
            package_name
        };
        let version = filename.strip_prefix(name_part)?.strip_prefix('-')?;
        if version.is_empty() {
            return None;
        }
        Some(version.to_string())
    }

    pub fn parse_pypi_version(normalized_name: &str, filename: &str) -> Option<String> {
        let base = filename
            .strip_suffix(".tar.gz")
            .or_else(|| filename.strip_suffix(".tgz"))
            .or_else(|| filename.strip_suffix(".zip"))
            .or_else(|| filename.strip_suffix(".whl"))
            .or_else(|| filename.strip_suffix(".egg"))?;

        let name_underscore = normalized_name.replace('-', "_");
        let base_lower = base.to_lowercase();
        let prefix = format!("{}-", name_underscore.to_lowercase());

        let rest = base_lower.strip_prefix(&prefix)?;
        let version = if ends_with_ci(filename, ".whl") {
            rest.split('-').next()?
        } else {
            rest
        };
        if version.is_empty() {
            return None;
        }
        Some(version.to_string())
    }
}

/// Re-export of the escape-aware raw-JSON URL rewrites for fuzz targets (#385).
///
/// These MIRROR the private `registry::{ansible,nuget}` rewrites, which live in the
/// binary crate and are unreachable from this library — so a fuzz-friendly copy is
/// the only option (same constraint as `npm_fuzz`). The *real* functions are enforced
/// on every CI run by their in-module `#[cfg(test)]` leak tests; these byte-faithful
/// copies let `cargo fuzz` explore the encoding-space for new dodge forms. A finding
/// here must be reproduced against the real function before it counts.
pub mod rewrite_fuzz {
    /// Mirror of `registry::replace_url_escape_aware`.
    fn replace_url_escape_aware(text: &str, from: &str, to: &str) -> String {
        let plain = text.replace(from, to);
        let esc_from = from.replace('/', "\\/");
        if !plain.contains(&esc_from) {
            return plain;
        }
        let esc_to = to.replace('/', "\\/");
        plain.replace(&esc_from, &esc_to)
    }

    /// Mirror of `registry::ansible::rewrite_ansible_urls`.
    pub fn rewrite_ansible_urls(json_text: &str, upstream_url: &str, base_url: &str) -> String {
        const API_PREFIX: &str = "/api/v3/plugin/ansible/content/published/collections/index";
        let upstream = upstream_url.trim_end_matches('/');
        let base = base_url.trim_end_matches('/');
        let nora_ansible = format!("{}/ansible", base);
        let s = replace_url_escape_aware(
            json_text,
            &format!("{}/download/", upstream),
            &format!("{}/download/", nora_ansible),
        );
        let s = replace_url_escape_aware(
            &s,
            &format!(
                "{}/api/v3/plugin/ansible/content/published/collections/artifacts/",
                upstream
            ),
            &format!("{}/download/", nora_ansible),
        );
        let s = replace_url_escape_aware(
            &s,
            &format!("{}{}/", upstream, API_PREFIX),
            &format!("{}/v3/collections/", nora_ansible),
        );
        replace_url_escape_aware(&s, upstream, &nora_ansible)
    }

    /// Mirror of `registry::nuget::rewrite_registration_urls`.
    pub fn rewrite_registration_urls(
        json_text: &str,
        upstream_url: &str,
        base_url: &str,
    ) -> String {
        let upstream = upstream_url.trim_end_matches('/');
        let nora_nuget = format!("{}/nuget", base_url.trim_end_matches('/'));
        let nora_reg = format!("{}/v3/registration/", nora_nuget);
        let s = replace_url_escape_aware(
            json_text,
            &format!("{}/v3/registration5-semver1/", upstream),
            &nora_reg,
        );
        let s = replace_url_escape_aware(
            &s,
            &format!("{}/v3/registration5-gz-semver1/", upstream),
            &nora_reg,
        );
        let s = replace_url_escape_aware(
            &s,
            &format!("{}/v3/registration5-gz-semver2/", upstream),
            &nora_reg,
        );
        replace_url_escape_aware(
            &s,
            &format!("{}/v3-flatcontainer/", upstream),
            &format!("{}/v3/flatcontainer/", nora_nuget),
        )
    }

    #[cfg(test)]
    mod url_leak_tests {
        //! Deterministic front-loop cases (no fuzz CI job) — the fuzz targets
        //! `fuzz_rewrite_ansible` / `fuzz_rewrite_nuget` explore the rest.
        use super::*;
        const UP: &str = "https://origin-host.invalid";
        const NORA: &str = "http://nora.test";

        #[test]
        fn ansible_escaped_slash_scrubbed() {
            let out = rewrite_ansible_urls(
                r#"{"d":"https:\/\/origin-host.invalid\/download\/a.tar.gz"}"#,
                UP,
                NORA,
            );
            assert!(!out.contains("origin-host.invalid"), "leak: {out}");
        }

        #[test]
        fn nuget_escaped_slash_scrubbed() {
            let out = rewrite_registration_urls(
                r#"{"@id":"https:\/\/origin-host.invalid\/v3\/registration5-semver1\/p\/i.json"}"#,
                UP,
                NORA,
            );
            assert!(!out.contains("origin-host.invalid"), "leak: {out}");
        }
    }
}