Skip to content

Commit

Permalink
feat: sanitize SVGs even though only admins can upload them (#615)
Browse files Browse the repository at this point in the history
* sanitize SVGs even though only admins can upload them

* move sanitization into its own function
  • Loading branch information
sebadob authored Nov 12, 2024
1 parent ecd3600 commit 6e20e77
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 4 deletions.
28 changes: 28 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ serde_with = { version = "3.8.1", features = ["macros"] }
spow = { version = "0.4", features = ["server"] }
sqlx = { version = "0.8.2", features = ["macros", "migrate", "postgres", "runtime-tokio", "sqlite", "tls-rustls", "uuid"] }
strum = { version = "0.26.3", features = ["derive"] }
svg-hush = "0.9.4"
time = { version = "0.3", features = ["formatting", "local-offset", "macros", "parsing", "serde"] }
tracing = { version = "0.1", features = ["attributes"] }
tracing-subscriber = { version = "0.3", features = ["env-filter", "json", "tracing"] }
Expand Down
1 change: 1 addition & 0 deletions src/error/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ serde_json = { workspace = true }
serde_json_path = { workspace = true }
spow = { workspace = true }
sqlx = { workspace = true }
svg-hush = { workspace = true }
time = { workspace = true }
tracing = { workspace = true }
tokio = { workspace = true }
Expand Down
11 changes: 11 additions & 0 deletions src/error/src/error_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use serde_json_path::ParseError;
use spow::pow::PowError;
use std::borrow::Cow;
use std::string::FromUtf8Error;
use svg_hush::FError;
use time::OffsetDateTime;
use tracing::{debug, error, trace};

Expand Down Expand Up @@ -473,3 +474,13 @@ impl From<ruma::client::Error<reqwest::Error, ruma::api::client::Error>> for Err
)
}
}

impl From<svg_hush::FError> for ErrorResponse {
fn from(value: FError) -> Self {
trace!("{:?}", value);
ErrorResponse::new(
ErrorResponseType::BadRequest,
format!("svg sanitization error: {:?}", value),
)
}
}
1 change: 1 addition & 0 deletions src/models/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ serde_json_path = { workspace = true }
serde_with = { workspace = true }
spow = { workspace = true }
sqlx = { workspace = true }
svg-hush = { workspace = true }
strum = { workspace = true }
time = { workspace = true }
tracing = { workspace = true }
Expand Down
2 changes: 0 additions & 2 deletions src/models/src/entity/auth_providers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1077,8 +1077,6 @@ impl AuthProviderCallback {
pub struct AuthProviderTemplate {
pub id: String,
pub name: String,
// pub logo: Option<Vec<u8>>,
// pub logo_type: Option<String>,
}

impl AuthProviderTemplate {
Expand Down
15 changes: 13 additions & 2 deletions src/models/src/entity/logos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::database::{Cache, DB};
use actix_web::web;
use hiqlite::{params, Param, Row};
use image::imageops::FilterType;
use image::ImageFormat;
use image::{EncodableLayout, ImageFormat};
use jwt_simple::prelude::{Deserialize, Serialize};
use rauthy_common::constants::{
CACHE_TTL_APP, CONTENT_TYPE_WEBP, IDX_AUTH_PROVIDER_LOGO, IDX_CLIENT_LOGO,
Expand All @@ -11,6 +11,7 @@ use rauthy_common::is_hiqlite;
use rauthy_error::{ErrorResponse, ErrorResponseType};
use sqlx::{query, query_as};
use std::io::Cursor;
use svg_hush::data_url_filter;
use tracing::debug;

// The default height a client logo will be resized to
Expand Down Expand Up @@ -204,7 +205,7 @@ impl Logo {
id,
res: LogoRes::Svg,
content_type,
data: logo,
data: Self::sanitize_svg(&mut logo.as_bytes())?,
};
slf.upsert_self(typ, true).await
}
Expand Down Expand Up @@ -452,4 +453,14 @@ impl Logo {
LogoType::AuthProvider => format!("{}_{}", IDX_AUTH_PROVIDER_LOGO, id),
}
}

fn sanitize_svg(source: &mut [u8]) -> Result<Vec<u8>, ErrorResponse> {
let mut filter = svg_hush::Filter::new();
filter.set_data_url_filter(data_url_filter::allow_standard_images);

let mut sanitized = Vec::with_capacity(source.len());
filter.filter(&mut source.as_bytes(), &mut sanitized)?;

Ok(sanitized)
}
}

0 comments on commit 6e20e77

Please sign in to comment.