Skip to main content

qualia_core_db/net/qdnf/policy_labels/
projection.rs

1//! Conservative sensitivity projection. Missing labels fail closed.
2
3use super::flow::JobLabelContext;
4use super::types::{Confidentiality, LabelFields};
5use crate::net::qdnf::errors::QdnfError;
6
7/// Derived output sinks that must inherit the job context. None may lower handling.
8#[repr(u8)]
9#[derive(Clone, Copy, Debug, PartialEq, Eq)]
10pub enum DerivedSink {
11    Reply = 1,
12    Summary = 2,
13    Translation = 3,
14    Embedding = 4,
15    ModelContext = 5,
16    QueryResult = 6,
17    Export = 7,
18    Log = 8,
19    Receipt = 9,
20    Backup = 10,
21}
22
23/// Core sensitivity cache is a projection, not the full label.
24/// Unknown has no rank and never becomes Public.
25pub fn project_sensitivity(fields: &LabelFields) -> Result<u8, QdnfError> {
26    fields.confidentiality.lattice_rank()
27}
28
29/// `?::require_labelled`.
30pub fn require_labelled(fields: &LabelFields) -> Result<(), QdnfError> {
31    if fields.confidentiality == Confidentiality::Unknown {
32        return Err(QdnfError::Denied);
33    }
34    if fields.issuer.is_zero() {
35        return Err(QdnfError::Unauthorized);
36    }
37    Ok(())
38}
39
40/// Protected content with a missing or unknown label fails closed.
41pub fn require_labelled_content(
42    protected_content: bool,
43    fields: Option<&LabelFields>,
44) -> Result<Confidentiality, QdnfError> {
45    match (protected_content, fields) {
46        (true, None) => Err(QdnfError::Incomplete),
47        (false, None) => Ok(Confidentiality::C0Public),
48        (_, Some(fields)) => {
49            if fields.confidentiality.is_unknown() {
50                return Err(QdnfError::Denied);
51            }
52            Ok(fields.confidentiality)
53        }
54    }
55}
56
57/// Inherit the current job label for any derived sink. Unknown fails closed.
58pub fn project_sink(ctx: &JobLabelContext, _sink: DerivedSink) -> Result<LabelFields, QdnfError> {
59    let current = *ctx.current();
60    if current.confidentiality.is_unknown() {
61        return Err(QdnfError::Incomplete);
62    }
63    Ok(current)
64}