onnx-model-package 0.1.0-dev.6

Pure-Rust parsing, selection, resolution, and validation for ORT model-package directories
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
//! ORT model-package directory parsing, variant selection, and path resolution.

use std::collections::BTreeMap;
use std::fs;
use std::path::{Component as PathComponent, Path, PathBuf};

use indexmap::IndexMap;
use serde::Deserialize;
use serde_json::{Map, Value};
use thiserror::Error;

const MANIFEST_FILE: &str = "manifest.json";
const COMPONENT_FILE: &str = "component.json";
const MAX_JSON_BYTES: u64 = 4 * 1024 * 1024;

/// Errors produced while opening, selecting, resolving, or validating a package.
#[derive(Debug, Error)]
pub enum PackageError {
    #[error("failed to read {path}: {source}")]
    Io {
        path: PathBuf,
        #[source]
        source: std::io::Error,
    },
    #[error("invalid JSON in {path}: {source}")]
    Json {
        path: PathBuf,
        #[source]
        source: serde_json::Error,
    },
    #[error("invalid model package: {0}")]
    Invalid(String),
    #[error("component '{0}' was not found")]
    UnknownComponent(String),
    #[error("variant '{variant}' was not found in component '{component}'")]
    UnknownVariant { component: String, variant: String },
    #[error("no variant in component '{component}' matches {request}")]
    NoMatchingVariant { component: String, request: String },
}

/// ORT package layout policy declared inside the (untrusted) manifest.
#[derive(Debug, Clone, Copy, Default, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum PackageLayout {
    /// All references must remain under the package root or a shared asset.
    #[default]
    Portable,
    /// References may use absolute paths or parent traversal, but only when the
    /// embedding application has separately opted in via
    /// [`HostTrust::AllowInstalledLayout`]. The manifest declaring this layout
    /// is never, on its own, sufficient to escape the package root.
    Installed,
}

/// Caller-supplied trust policy governing whether an `installed`-layout package
/// may resolve references outside the package root.
///
/// Packages are untrusted input (see `docs/genai/MODEL_PACKAGE.md` ยง7), so the
/// manifest's own `layout` field must never be able to grant itself
/// filesystem-escape privileges. Confinement is therefore always enforced
/// unless the embedding application explicitly passes
/// [`HostTrust::AllowInstalledLayout`], which it should only do for packages it
/// has itself installed into a trusted location.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum HostTrust {
    /// Always confine every reference to the package root, regardless of the
    /// manifest `layout`. The safe default for downloaded/untrusted packages.
    #[default]
    Confined,
    /// Permit `installed`-layout manifests to use absolute paths and `..`.
    /// Only pass this for packages the host itself trusts.
    AllowInstalledLayout,
}

/// An ORT 1.x package manifest.
#[derive(Debug, Clone, Deserialize)]
pub struct Manifest {
    pub schema_version: String,
    pub package_name: Option<String>,
    pub package_version: Option<String>,
    pub description: Option<String>,
    #[serde(default)]
    pub layout: PackageLayout,
    pub components: IndexMap<String, ComponentReference>,
    #[serde(default)]
    pub shared_assets: BTreeMap<String, String>,
    #[serde(default)]
    pub additional_metadata: Map<String, Value>,
    #[serde(flatten)]
    pub unknown_fields: Map<String, Value>,
}

/// An inline component or a path to a component JSON file/directory.
#[derive(Debug, Clone, Deserialize)]
#[serde(untagged)]
pub enum ComponentReference {
    Inline(Box<Component>),
    External(String),
}

/// A package component containing named executable variants.
#[derive(Debug, Clone, Deserialize)]
pub struct Component {
    pub component_name: String,
    pub variants: IndexMap<String, Variant>,
    #[serde(default)]
    pub additional_metadata: Map<String, Value>,
    #[serde(flatten)]
    pub unknown_fields: Map<String, Value>,
}

/// A named component variant.
#[derive(Debug, Clone, Deserialize)]
pub struct Variant {
    pub variant_directory: String,
    pub ep: Option<String>,
    pub device: Option<String>,
    pub compatibility_string: Option<String>,
    #[serde(default)]
    pub executor_info: Map<String, Value>,
    #[serde(default)]
    pub additional_metadata: Map<String, Value>,
    #[serde(flatten)]
    pub unknown_fields: Map<String, Value>,
}

/// Common executable fields understood from `executor_info["nxrt"]` or
/// `executor_info["ort"]`.
#[derive(Debug, Clone, Deserialize)]
pub struct ExecutorInfo {
    pub schema_version: Option<String>,
    pub model_file: String,
    pub genai_config: Option<String>,
    pub inference_metadata: Option<String>,
    pub tokenizer: Option<String>,
    #[serde(default)]
    pub session_options: BTreeMap<String, String>,
    #[serde(default)]
    pub provider_options: BTreeMap<String, String>,
    #[serde(flatten)]
    pub unknown_fields: Map<String, Value>,
}

/// Requested package variant attributes.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct SelectionRequest {
    pub variant: Option<String>,
    pub execution_provider: Option<String>,
    pub precision: Option<String>,
}

impl SelectionRequest {
    /// Select a specific named variant.
    #[must_use]
    pub fn named(name: impl Into<String>) -> Self {
        Self {
            variant: Some(name.into()),
            ..Self::default()
        }
    }

    /// Select the first manifest-ordered variant matching an execution provider.
    #[must_use]
    pub fn for_execution_provider(execution_provider: impl Into<String>) -> Self {
        Self {
            execution_provider: Some(execution_provider.into()),
            ..Self::default()
        }
    }
}

/// Fully resolved files for one selected variant.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SelectedVariant {
    pub component_name: String,
    pub variant_name: String,
    pub variant_directory: PathBuf,
    pub model_path: PathBuf,
    pub genai_config_path: Option<PathBuf>,
    pub inference_metadata_path: Option<PathBuf>,
    pub tokenizer_directory: Option<PathBuf>,
}

/// Open, parsed ORT model-package directory.
#[derive(Debug, Clone)]
pub struct ModelPackage {
    root: PathBuf,
    manifest: Manifest,
    trust: HostTrust,
}

impl ModelPackage {
    /// Open and structurally validate a package directory, confining every
    /// reference to the package root regardless of the manifest `layout`.
    ///
    /// This is the safe entry point for untrusted (e.g. downloaded) packages.
    pub fn open(root: impl AsRef<Path>) -> Result<Self, PackageError> {
        Self::open_with_trust(root, HostTrust::Confined)
    }

    /// Open a package with an explicit caller-supplied trust policy.
    ///
    /// Pass [`HostTrust::AllowInstalledLayout`] only for packages the embedding
    /// application itself trusts; with it, `installed`-layout manifests may
    /// resolve absolute paths and `..` outside the package root. The default
    /// [`HostTrust::Confined`] (used by [`ModelPackage::open`]) never permits
    /// such escapes, even when the manifest declares `"layout": "installed"`.
    pub fn open_with_trust(root: impl AsRef<Path>, trust: HostTrust) -> Result<Self, PackageError> {
        let root = root.as_ref();
        if !root.is_dir() {
            return Err(PackageError::Invalid(format!(
                "package directory does not exist: {}",
                root.display()
            )));
        }
        let manifest_path = root.join(MANIFEST_FILE);
        let manifest: Manifest = read_json(&manifest_path)?;
        validate_version(&manifest.schema_version, "package")?;
        if manifest.components.is_empty() {
            return Err(PackageError::Invalid(
                "manifest components must not be empty".to_string(),
            ));
        }
        let root = root.canonicalize().map_err(|source| PackageError::Io {
            path: root.to_path_buf(),
            source,
        })?;
        Ok(Self {
            root,
            manifest,
            trust,
        })
    }

    /// Return the parsed package manifest.
    #[must_use]
    pub fn manifest(&self) -> &Manifest {
        &self.manifest
    }

    /// Select and resolve one executable component variant.
    pub fn select(
        &self,
        component_name: &str,
        request: &SelectionRequest,
    ) -> Result<SelectedVariant, PackageError> {
        let component = self.load_component(component_name)?;
        let (variant_name, variant) = if let Some(requested_name) = &request.variant {
            component
                .variants
                .get_key_value(requested_name)
                .ok_or_else(|| PackageError::UnknownVariant {
                    component: component_name.to_string(),
                    variant: requested_name.clone(),
                })?
        } else {
            component
                .variants
                .iter()
                .find(|(_, variant)| variant_matches(variant, request))
                .ok_or_else(|| PackageError::NoMatchingVariant {
                    component: component_name.to_string(),
                    request: describe_request(request),
                })?
        };

        self.resolve_selected(component_name, variant_name, variant)
    }

    /// Validate every component, variant, executor payload, and referenced file.
    pub fn validate(&self) -> Result<(), PackageError> {
        for component_name in self.manifest.components.keys() {
            let component = self.load_component(component_name)?;
            if component.component_name != *component_name {
                return Err(PackageError::Invalid(format!(
                    "component map key '{component_name}' does not match component_name '{}'",
                    component.component_name
                )));
            }
            if component.variants.is_empty() {
                return Err(PackageError::Invalid(format!(
                    "component '{component_name}' has no variants"
                )));
            }
            for (variant_name, variant) in &component.variants {
                self.resolve_selected(component_name, variant_name, variant)?;
            }
        }
        Ok(())
    }

    fn load_component(&self, name: &str) -> Result<Component, PackageError> {
        let reference = self
            .manifest
            .components
            .get(name)
            .ok_or_else(|| PackageError::UnknownComponent(name.to_string()))?;
        match reference {
            ComponentReference::Inline(component) => Ok(component.as_ref().clone()),
            ComponentReference::External(reference) => {
                let mut path = self.resolve_path(&self.root, reference, true)?;
                if path.is_dir() {
                    // The implicit `component.json` under an external component
                    // directory must pass the same confinement check as any
                    // other reference; a symlinked component.json could
                    // otherwise escape the package root.
                    path = self.canonicalize_confined(&path.join(COMPONENT_FILE))?;
                }
                read_json(&path)
            }
        }
    }

    fn resolve_selected(
        &self,
        component_name: &str,
        variant_name: &str,
        variant: &Variant,
    ) -> Result<SelectedVariant, PackageError> {
        let variant_directory = self.resolve_path(&self.root, &variant.variant_directory, true)?;
        if !variant_directory.is_dir() {
            return Err(PackageError::Invalid(format!(
                "variant '{variant_name}' directory is not a directory: {}",
                variant_directory.display()
            )));
        }
        let executor_info = self.load_executor_info(variant_name, variant, &variant_directory)?;
        if let Some(version) = &executor_info.schema_version {
            validate_version(version, "executor")?;
        }
        let model_path = self.resolve_path(&variant_directory, &executor_info.model_file, true)?;
        ensure_file(&model_path, "model_file")?;
        let genai_config_path = self.resolve_optional_file(
            &variant_directory,
            executor_info.genai_config.as_deref(),
            "genai_config",
        )?;
        let inference_metadata_path = self.resolve_optional_file(
            &variant_directory,
            executor_info.inference_metadata.as_deref(),
            "inference_metadata",
        )?;
        let tokenizer_directory = executor_info
            .tokenizer
            .as_deref()
            .map(|reference| self.resolve_path(&variant_directory, reference, true))
            .transpose()?;
        if let Some(path) = &tokenizer_directory
            && !path.is_dir()
        {
            return Err(PackageError::Invalid(format!(
                "tokenizer reference is not a directory: {}",
                path.display()
            )));
        }
        for (key, reference) in &executor_info.session_options {
            if matches!(
                key.as_str(),
                "session.model_external_initializers_file_folder_path" | "ep.context_file_path"
            ) {
                self.resolve_path(&variant_directory, reference, true)?;
            }
        }
        Ok(SelectedVariant {
            component_name: component_name.to_string(),
            variant_name: variant_name.to_string(),
            variant_directory,
            model_path,
            genai_config_path,
            inference_metadata_path,
            tokenizer_directory,
        })
    }

    fn load_executor_info(
        &self,
        variant_name: &str,
        variant: &Variant,
        variant_directory: &Path,
    ) -> Result<ExecutorInfo, PackageError> {
        let (namespace, value) = variant
            .executor_info
            .get_key_value("nxrt")
            .or_else(|| variant.executor_info.get_key_value("ort"))
            .ok_or_else(|| {
                PackageError::Invalid(format!(
                    "variant '{variant_name}' has no executor_info.nxrt or executor_info.ort"
                ))
            })?;
        match value {
            Value::String(reference) => {
                let path = self.resolve_path(variant_directory, reference, true)?;
                read_json(&path)
            }
            Value::Object(_) => serde_json::from_value(value.clone()).map_err(|source| {
                PackageError::Json {
                    path: PathBuf::from(format!(
                        "{MANIFEST_FILE}:components.*.variants.{variant_name}.executor_info.{namespace}"
                    )),
                    source,
                }
            }),
            _ => Err(PackageError::Invalid(format!(
                "executor_info.{namespace} for variant '{variant_name}' must be an object or path"
            ))),
        }
    }

    fn resolve_optional_file(
        &self,
        base: &Path,
        reference: Option<&str>,
        field: &str,
    ) -> Result<Option<PathBuf>, PackageError> {
        reference
            .map(|reference| {
                let path = self.resolve_path(base, reference, true)?;
                ensure_file(&path, field)?;
                Ok(path)
            })
            .transpose()
    }

    /// Whether references must be confined to the package root. Confinement is
    /// always enforced unless the manifest declares the `installed` layout AND
    /// the caller explicitly opted in via [`HostTrust::AllowInstalledLayout`].
    fn confinement_enforced(&self) -> bool {
        !(self.manifest.layout == PackageLayout::Installed
            && self.trust == HostTrust::AllowInstalledLayout)
    }

    /// Canonicalize an existing path (resolving symlinks) and, when confinement
    /// is enforced, reject it if the real path escapes the package root.
    fn canonicalize_confined(&self, path: &Path) -> Result<PathBuf, PackageError> {
        if !path.exists() {
            return Err(PackageError::Invalid(format!(
                "referenced path does not exist: {}",
                path.display()
            )));
        }
        let canonical = path.canonicalize().map_err(|source| PackageError::Io {
            path: path.to_path_buf(),
            source,
        })?;
        if self.confinement_enforced() && !canonical.starts_with(&self.root) {
            return Err(PackageError::Invalid(format!(
                "package reference resolves outside package root: {}",
                path.display()
            )));
        }
        Ok(canonical)
    }

    fn resolve_path(
        &self,
        base: &Path,
        reference: &str,
        must_exist: bool,
    ) -> Result<PathBuf, PackageError> {
        let path = if let Some(asset_reference) = reference.strip_prefix("sha256:") {
            self.resolve_shared_asset(asset_reference)?
        } else {
            let reference_path = Path::new(reference);
            if self.confinement_enforced() {
                validate_portable_reference(reference_path)?;
            }
            if reference_path.is_absolute() {
                reference_path.to_path_buf()
            } else {
                base.join(reference_path)
            }
        };
        if must_exist {
            return self.canonicalize_confined(&path);
        }
        Ok(path)
    }

    fn resolve_shared_asset(&self, reference: &str) -> Result<PathBuf, PackageError> {
        let (digest, tail) = reference
            .split_once('/')
            .map_or((reference, None), |(digest, tail)| (digest, Some(tail)));
        if digest.len() != 64
            || !digest
                .bytes()
                .all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte))
        {
            return Err(PackageError::Invalid(format!(
                "invalid sha256 shared-asset reference: sha256:{reference}"
            )));
        }
        let override_path = self
            .manifest
            .shared_assets
            .get(&format!("sha256:{digest}"))
            .or_else(|| self.manifest.shared_assets.get(digest));
        let root = override_path.map_or_else(
            || {
                Ok(self
                    .root
                    .join("shared_assets")
                    .join(format!("sha256-{digest}")))
            },
            |reference| {
                let path = Path::new(reference);
                if self.confinement_enforced() {
                    validate_portable_reference(path)?;
                }
                Ok(if path.is_absolute() {
                    path.to_path_buf()
                } else {
                    self.root.join(path)
                })
            },
        )?;
        let path = tail.map_or(root.clone(), |tail| root.join(tail));
        self.canonicalize_confined(&path)
    }
}

/// Probe whether a directory contains an ORT package manifest, while avoiding
/// unrelated fixture/application files also named `manifest.json`.
#[must_use]
pub fn is_model_package_directory(path: impl AsRef<Path>) -> bool {
    let path = path.as_ref();
    let Ok(bytes) = fs::read(path.join(MANIFEST_FILE)) else {
        return false;
    };
    let Ok(value) = serde_json::from_slice::<Value>(&bytes) else {
        return has_package_suffix(path);
    };
    value.get("schema_version").is_some() && value.get("components").is_some()
        || has_package_suffix(path)
}

fn has_package_suffix(path: &Path) -> bool {
    path.extension().is_some_and(|extension| {
        extension.eq_ignore_ascii_case("ortpackage") || extension.eq_ignore_ascii_case("nxpackage")
    })
}

fn read_json<T: for<'de> Deserialize<'de>>(path: &Path) -> Result<T, PackageError> {
    let metadata = fs::metadata(path).map_err(|source| PackageError::Io {
        path: path.to_path_buf(),
        source,
    })?;
    if metadata.len() > MAX_JSON_BYTES {
        return Err(PackageError::Invalid(format!(
            "JSON file exceeds {MAX_JSON_BYTES} bytes: {}",
            path.display()
        )));
    }
    let bytes = fs::read(path).map_err(|source| PackageError::Io {
        path: path.to_path_buf(),
        source,
    })?;
    serde_json::from_slice(&bytes).map_err(|source| PackageError::Json {
        path: path.to_path_buf(),
        source,
    })
}

fn validate_version(version: &str, kind: &str) -> Result<(), PackageError> {
    let (major, minor) = version.split_once('.').ok_or_else(|| {
        PackageError::Invalid(format!("{kind} schema_version must be '<major>.<minor>'"))
    })?;
    let major = major.parse::<u64>().map_err(|_| {
        PackageError::Invalid(format!("{kind} schema_version has a non-numeric major"))
    })?;
    minor.parse::<u64>().map_err(|_| {
        PackageError::Invalid(format!("{kind} schema_version has a non-numeric minor"))
    })?;
    if major != 1 {
        return Err(PackageError::Invalid(format!(
            "unsupported {kind} schema major version {major}; supported major is 1"
        )));
    }
    Ok(())
}

fn validate_portable_reference(path: &Path) -> Result<(), PackageError> {
    if path.as_os_str().is_empty()
        || path.is_absolute()
        || path.components().any(|component| {
            matches!(
                component,
                PathComponent::ParentDir | PathComponent::RootDir | PathComponent::Prefix(_)
            )
        })
    {
        return Err(PackageError::Invalid(format!(
            "portable package reference escapes its base: {}",
            path.display()
        )));
    }
    Ok(())
}

fn ensure_file(path: &Path, field: &str) -> Result<(), PackageError> {
    if path.is_file() {
        Ok(())
    } else {
        Err(PackageError::Invalid(format!(
            "{field} is not a file: {}",
            path.display()
        )))
    }
}

fn variant_matches(variant: &Variant, request: &SelectionRequest) -> bool {
    let execution_provider_matches = request.execution_provider.as_ref().is_none_or(|requested| {
        variant
            .ep
            .as_ref()
            .is_some_and(|actual| actual.eq_ignore_ascii_case(requested))
    });
    let precision_matches = request.precision.as_ref().is_none_or(|requested| {
        variant
            .additional_metadata
            .get("precision")
            .and_then(Value::as_str)
            .is_some_and(|actual| actual.eq_ignore_ascii_case(requested))
    });
    execution_provider_matches && precision_matches
}

fn describe_request(request: &SelectionRequest) -> String {
    let mut attributes = Vec::new();
    if let Some(execution_provider) = &request.execution_provider {
        attributes.push(format!("execution provider '{execution_provider}'"));
    }
    if let Some(precision) = &request.precision {
        attributes.push(format!("precision '{precision}'"));
    }
    if attributes.is_empty() {
        "the default selection".to_string()
    } else {
        attributes.join(" and ")
    }
}