Add warning when SVG with foreign object is included (#4932)

Co-authored-by: Laurenz <laurmaedje@gmail.com>
This commit is contained in:
Laurenz Stampfl 2024-09-10 19:18:40 +02:00 committed by GitHub
parent ac982f5856
commit 8501a65566
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 24 additions and 1 deletions

View File

@ -60,6 +60,13 @@ impl Readable {
Readable::Str(v) => v.as_bytes(),
}
}
pub(crate) fn as_str(&self) -> Option<&str> {
match self {
Readable::Str(v) => Some(v.as_str()),
Readable::Bytes(v) => std::str::from_utf8(v).ok(),
}
}
}
cast! {

View File

@ -13,7 +13,7 @@ use std::sync::Arc;
use comemo::Tracked;
use ecow::EcoString;
use crate::diag::{bail, At, SourceResult, StrResult};
use crate::diag::{bail, warning, At, SourceResult, StrResult};
use crate::engine::Engine;
use crate::foundations::{
cast, elem, func, scope, Bytes, Cast, Content, NativeElement, Packed, Show, Smart,
@ -190,6 +190,22 @@ fn layout_image(
Smart::Auto => determine_format(elem.path().as_str(), data).at(span)?,
};
// Warn the user if the image contains a foreign object. Not perfect
// because the svg could also be encoded, but that's an edge case.
if format == ImageFormat::Vector(VectorFormat::Svg) {
let has_foreign_object =
data.as_str().is_some_and(|s| s.contains("<foreignObject"));
if has_foreign_object {
engine.sink.warn(warning!(
span,
"image contains foreign object";
hint: "SVG images with foreign objects might render incorrectly in typst";
hint: "see https://github.com/typst/typst/issues/1421 for more information"
));
}
}
// Construct the image itself.
let image = Image::with_fonts(
data.clone().into(),