Skip to content

FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust

security(tts): verify Kokoro checkpoint before parsing (#2918)

Earl Cod4595df9c442
diff --git a/Cargo.lock b/Cargo.lock
index 9ff01577b..96f4338d0 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -2324,6 +2324,7 @@ dependencies = [
"filmcraft-tts-text",
"serde",
"serde_json",
+ "sha2",
"thiserror 2.0.21",
"zip",
]
diff --git a/crates/tts/Cargo.toml b/crates/tts/Cargo.toml
index 5100d70db..fffdb622d 100644
--- a/crates/tts/Cargo.toml
+++ b/crates/tts/Cargo.toml
@@ -12,7 +12,7 @@ workspace = true
[features]
default = []
# Kokoro-82M neural voices on the CPU via candle (pure Rust). Off by default and in wasm builds.
-kokoro = ["dep:candle-core", "dep:candle-nn", "dep:zip", "dep:serde_json", "dep:filmcraft-tts-text", "dep:filmcraft-audio-dsp"]
+kokoro = ["dep:candle-core", "dep:candle-nn", "dep:zip", "dep:serde_json", "dep:sha2", "dep:filmcraft-tts-text", "dep:filmcraft-audio-dsp"]
[dependencies]
filmcraft-tts-text = { workspace = true, optional = true }
@@ -23,6 +23,7 @@ candle-core = { version = "0.9", default-features = false, optional = true }
candle-nn = { version = "0.9", default-features = false, optional = true }
zip = { version = "7", default-features = false, optional = true }
serde_json = { workspace = true, optional = true }
+sha2 = { version = "0.10", optional = true }
[[example]]
name = "kokoro_say"
diff --git a/crates/tts/src/kokoro/weights.rs b/crates/tts/src/kokoro/weights.rs
index b65e8de49..12c759c6a 100644
--- a/crates/tts/src/kokoro/weights.rs
+++ b/crates/tts/src/kokoro/weights.rs
@@ -12,6 +12,7 @@ use std::io::Read;
use std::path::Path;
use candle_core::{DType, Device, Tensor};
+use sha2::{Digest, Sha256};
use crate::TtsError;
@@ -83,6 +84,7 @@ const PARTS: [&str; 5] = ["bert", "bert_encoder", "predictor", "text_encoder", "
impl Weights {
pub(crate) fn load(path: &Path) -> Result<Weights, TtsError> {
+ verify_checkpoint(path)?;
let mut raw: HashMap<String, Tensor> = HashMap::new();
for part in PARTS {
let tensors = candle_core::pickle::read_all_with_key(path, Some(part)).map_err(|e| err(format!("{}: {e}", path.display())))?;
@@ -126,6 +128,34 @@ impl Weights {
}
}
+/// Candle's pickle reader is used only for the fixed, catalogued checkpoint.
+/// Verify its immutable size and digest before any pickle object is expanded.
+fn verify_checkpoint(path: &Path) -> Result<(), TtsError> {
+ let file = crate::catalog::FILES
+ .iter()
+ .find(|file| file.name == "kokoro-v1_0.pth")
+ .ok_or_else(|| err("Kokoro checkpoint is missing from the package catalogue"))?;
+ let metadata = std::fs::metadata(path).map_err(|e| err(format!("{}: {e}", path.display())))?;
+ if metadata.len() != file.size {
+ return Err(err(format!("{}: checkpoint size does not match the official package", path.display())));
+ }
+ let mut reader = std::fs::File::open(path).map_err(|e| err(format!("{}: {e}", path.display())))?;
+ let mut hasher = Sha256::new();
+ let mut buf = [0u8; 64 * 1024];
+ loop {
+ let n = reader.read(&mut buf).map_err(|e| err(format!("{}: {e}", path.display())))?;
+ if n == 0 {
+ break;
+ }
+ hasher.update(&buf[..n]);
+ }
+ let actual = format!("{:x}", hasher.finalize());
+ if actual != file.sha256 {
+ return Err(err(format!("{}: checkpoint digest does not match the official package", path.display())));
+ }
+ Ok(())
+}
+
/// Number of style vectors in a voice pack (one per input length 1..=510).
pub(crate) const PACK_LEN: usize = 510;
pub(crate) const STYLE: usize = 256;