use std::collections::{BTreeMap, BTreeSet};
use crate::diagnostics::DiagnosticSource;
use crate::ir::{Fingerprint, SourceLocation, SourceSpan};
use super::assets::{AssetError, PageAssetStore};
use super::{ExecutionAsset, ExecutionFailure, ExecutionFailureKind};
mod diagnostics;
mod fragment;
mod html;
mod markdown;
mod urls;
mod warnings;
pub use diagnostics::*;
pub use fragment::*;
pub(crate) use html::restore_html_content;
pub use html::{
DecodedHtml, HtmlAttribute, HtmlNode, ValidatedHtml, restore_html, validate_html_live,
};
pub use markdown::{
ImageBinding, NodeAddress, NodeEdge, ValidatedMarkdown, restore_markdown,
validate_markdown_live,
};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AuthoredOutputContext {
source: SourceLocation,
collection: String,
anchors: BTreeSet<String>,
}
impl AuthoredOutputContext {
pub fn new(mut source: SourceLocation, collection: String, anchors: BTreeSet<String>) -> Self {
source.span = None;
Self {
source,
collection,
anchors,
}
}
pub fn source(&self) -> &SourceLocation {
&self.source
}
pub fn collection(&self) -> &str {
&self.collection
}
pub fn anchors(&self) -> &BTreeSet<String> {
&self.anchors
}
fn diagnostic_source(&self) -> DiagnosticSource {
DiagnosticSource::Repository {
repository: self.source.repository.clone(),
path: self.source.path.clone(),
}
}
fn owns_asset(&self, asset: &ExecutionAsset) -> bool {
let mut identity = b"diplodocus/execution-assets-v1\0".to_vec();
for field in [
self.source.repository.as_str(),
self.collection.as_str(),
self.source.path.as_str(),
] {
identity.extend_from_slice(&(field.len() as u64).to_be_bytes());
identity.extend_from_slice(field.as_bytes());
}
let namespace = crate::provenance::fingerprint_bytes(&identity).value;
valid_digest(&asset.reference.fingerprint)
&& asset.reference.path.as_str()
== format!(
"execution-assets/{namespace}/sha256/{}",
asset.reference.fingerprint.value
)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(deny_unknown_fields)]
pub struct FragmentIdentity {
pub ordinal: usize,
pub byte_length: usize,
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(deny_unknown_fields)]
pub struct OutputOrigin {
pub cell: usize,
pub slot: usize,
pub cell_span: SourceSpan,
pub fragment: Option<FragmentIdentity>,
}
#[derive(Debug)]
pub enum Validation<T> {
Accepted {
value: T,
diagnostics: Vec<ExecutionDiagnostic>,
},
Rejected {
diagnostics: Vec<ExecutionDiagnostic>,
},
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AssetUse {
pub digest: Fingerprint,
pub media_type: String,
pub byte_size: u64,
}
impl From<&ExecutionAsset> for AssetUse {
fn from(asset: &ExecutionAsset) -> Self {
Self {
digest: asset.reference.fingerprint.clone(),
media_type: asset.media_type.clone(),
byte_size: asset.byte_size,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
pub enum RestoreRejection {
#[error("Invalid generated-content structure.")]
Structure,
#[error("Unsafe generated-content URL.")]
Url,
#[error("Generated content references an unverified asset.")]
UnboundAsset,
#[error("Generated-content asset metadata does not match.")]
AssetMismatch,
#[error("Generated content is not canonical.")]
NonCanonical,
}
#[derive(Debug, Default)]
pub struct VerifiedAssets {
assets: BTreeMap<String, ExecutionAsset>,
}
impl VerifiedAssets {
pub(crate) fn from_store(
store: &mut PageAssetStore,
page: &super::ExecutionPage,
expected: &[ExecutionAsset],
) -> Result<Self, AssetError> {
store.verify_assets(page, expected)?;
Ok(Self {
assets: expected
.iter()
.map(|asset| (asset.reference.fingerprint.value.clone(), asset.clone()))
.collect(),
})
}
pub fn new() -> Self {
Self::default()
}
pub fn verify_bytes(
&mut self,
context: &AuthoredOutputContext,
expected: &ExecutionAsset,
bytes: &[u8],
) -> Result<(), RestoreRejection> {
if !context.owns_asset(expected)
|| expected.reference.fingerprint != crate::provenance::fingerprint_bytes(bytes)
|| expected.byte_size != bytes.len() as u64
|| super::assets::validate_image_bytes(&expected.media_type, bytes).is_err()
{
return Err(RestoreRejection::AssetMismatch);
}
let digest = &expected.reference.fingerprint.value;
if self.assets.get(digest).is_some_and(|old| old != expected) {
return Err(RestoreRejection::AssetMismatch);
}
self.assets.insert(digest.clone(), expected.clone());
Ok(())
}
pub fn stage(
&mut self,
store: &mut PageAssetStore,
expected: &ExecutionAsset,
bytes: &[u8],
) -> Result<(), AssetError> {
let asset = store.stage_cached(expected, bytes)?;
if self
.assets
.get(&asset.reference.fingerprint.value)
.is_some_and(|old| old != &asset)
{
return Err(AssetError::Collision);
}
self.assets
.insert(asset.reference.fingerprint.value.clone(), asset);
Ok(())
}
pub fn assets(&self) -> impl Iterator<Item = &ExecutionAsset> {
self.assets.values()
}
fn resolve(
&self,
usage: &AssetUse,
context: &AuthoredOutputContext,
) -> Result<ExecutionAsset, RestoreRejection> {
if !valid_digest(&usage.digest) {
return Err(RestoreRejection::AssetMismatch);
}
let asset = self
.assets
.get(&usage.digest.value)
.ok_or(RestoreRejection::UnboundAsset)?;
if AssetUse::from(asset) != *usage || !context.owns_asset(asset) {
return Err(RestoreRejection::AssetMismatch);
}
Ok(asset.clone())
}
}
fn valid_digest(digest: &Fingerprint) -> bool {
digest.algorithm == "sha256"
&& digest.value.len() == 64
&& digest
.value
.bytes()
.all(|c| c.is_ascii_digit() || (b'a'..=b'f').contains(&c))
}
fn failure(
kind: ExecutionFailureKind,
context: &AuthoredOutputContext,
origin: &OutputOrigin,
) -> ExecutionFailure {
ExecutionFailure {
kind,
diagnostics: vec![kind.to_diagnostic(
context.collection.clone(),
SourceLocation {
span: Some(origin.cell_span),
..context.source.clone()
},
)],
cleanup_diagnostics: Vec::new(),
}
}