fix(cli): reject --pdf-options that is not a JSON object instead of panicking (#731)

This commit is contained in:
MarekSpodBiedry
2026-10-13 18:42:57 +02:00
parent e862e0a613
commit dfb41f5aea
2 changed files with 23 additions and 1 deletions
+5 -1
View File
@@ -261,7 +261,11 @@ fn run(args: &[String]) -> Result<(), String> {
"--page" => page = val()?.parse().map_err(|_| "bad --page")?,
"--scale" => scale = val()?.parse().map_err(|_| "bad --scale")?,
"--pdf-options" => {
pdf_opts = serde_json::from_str(&val()?).map_err(|e| format!("--pdf-options: {e}"))?;
pdf_opts = match serde_json::from_str(&val()?).map_err(|e| format!("--pdf-options: {e}"))? {
Value::Null => json!({}),
o @ Value::Object(_) => o,
_ => return Err("--pdf-options: expected a JSON object".to_string()),
};
}
"--export" => {
let out = val()?;
+18
View File
@@ -138,6 +138,24 @@ fn run_export_options_come_before_their_export() {
let _ = std::fs::remove_dir_all(&dir);
}
/// `--pdf-options` must be a JSON object; any other JSON value used to panic when the PDF path was set on it.
#[test]
fn pdf_options_that_are_not_an_object_are_an_error_not_a_panic() {
let dir = std::env::temp_dir().join(format!("dc-cli-pdfopts-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let pdf = dir.join("out.pdf");
let pdf = pdf.to_str().unwrap();
for val in ["[]", "true", "1", r#""x""#] {
let out = cli().args(["run", "--sample", "--pdf-options", val, "--export", pdf]).output().unwrap();
let err = String::from_utf8_lossy(&out.stderr);
assert_eq!(out.status.code(), Some(1), "{val}: {err}");
assert!(err.contains("--pdf-options") && !err.contains("panicked"), "{val}: {err}");
}
let out = cli().args(["run", "--sample", "--pdf-options", "null", "--export", pdf]).output().unwrap();
assert!(out.status.success(), "{}", String::from_utf8_lossy(&out.stderr));
let _ = std::fs::remove_dir_all(&dir);
}
#[cfg(unix)]
#[test]
fn raster_export_preserves_an_existing_reader_and_private_symlink_target() {