diff --git a/crates/export/src/pptx/tests.rs b/crates/export/src/pptx/tests.rs index 5e619b32f0..06cd21ad5f 100644 --- a/crates/export/src/pptx/tests.rs +++ b/crates/export/src/pptx/tests.rs @@ -379,9 +379,16 @@ fn pptx_table_cell_with_largest_font_stays_in_table_and_not_in_title() { fn pptx_opens_in_libreoffice() { let p = sample_page(); let bytes = pptx(&[p], "LibreOffice Check"); - let dir = std::env::temp_dir().join(format!("pdfcraft-pptx-lo-{}", std::process::id())); - let _ = std::fs::remove_dir_all(&dir); - let _ = std::fs::create_dir_all(&dir); + // A new folder of our own, never one left (or planted) at a guessable name. + let nanos = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map_or(0, |d| d.as_nanos()); + let dir = std::env::temp_dir().join(format!("pdfcraft-pptx-lo-{}-{nanos:x}", std::process::id())); + let mut made = std::fs::DirBuilder::new(); + #[cfg(unix)] + { + use std::os::unix::fs::DirBuilderExt as _; + made.mode(0o700); + } + made.create(&dir).expect("a fresh test folder"); let pptx_path = dir.join("test.pptx"); std::fs::write(&pptx_path, &bytes).unwrap(); diff --git a/crates/platform/src/staging.rs b/crates/platform/src/staging.rs index a604dd58b5..95f879054d 100644 --- a/crates/platform/src/staging.rs +++ b/crates/platform/src/staging.rs @@ -43,6 +43,10 @@ fn create_staging_as( suffixes: impl IntoIterator, keep: Option<&std::fs::Metadata>, ) -> std::io::Result<(PathBuf, std::fs::File)> { + // One name, in `dir`: a separator in it would put the staging file somewhere else. + if name.is_empty() || name.chars().any(std::path::is_separator) { + return Err(std::io::Error::new(std::io::ErrorKind::InvalidInput, "the file name is not a single name")); + } // At most 128 bytes of the target's name, cut between characters, so the staging name fits // the 255-byte (Linux, macOS) and 255-unit (Windows) limits however long that name is. let mut stem = String::new(); diff --git a/crates/platform/src/staging/tests.rs b/crates/platform/src/staging/tests.rs index 699ba9bbf9..d489c4551a 100644 --- a/crates/platform/src/staging/tests.rs +++ b/crates/platform/src/staging/tests.rs @@ -1,11 +1,22 @@ use super::*; -/// A fresh, empty folder for one test. +/// A fresh, empty folder for one test: made new under a name nobody can guess, never reused, +/// so nothing planted there beforehand (a symbolic link, say) is written through. fn test_dir(test: &str) -> PathBuf { - let dir = std::env::temp_dir().join(format!("pdfcraft-platform-staging-{}-{test}", std::process::id())); - let _ = std::fs::remove_dir_all(&dir); - std::fs::create_dir_all(&dir).unwrap(); - dir + let seed = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map_or(0, |d| d.as_nanos()); + for n in 0..64u32 { + let dir = std::env::temp_dir().join(format!("pdfcraft-platform-staging-{}-{test}-{:x}", std::process::id(), seed.wrapping_add(n.into()))); + let mut made = std::fs::DirBuilder::new(); + #[cfg(unix)] + { + use std::os::unix::fs::DirBuilderExt as _; + made.mode(0o700); + } + if made.create(&dir).is_ok() { + return dir; + } + } + panic!("no fresh test folder could be made"); } fn file_name(p: &Path) -> String { @@ -451,6 +462,19 @@ fn a_replaced_link_keeps_its_own_attributes_not_its_targets() { let _ = std::fs::remove_dir_all(&dir); } +/// A name with a path separator in it would stage the file outside `dir`: it is refused. +#[test] +fn a_name_with_path_components_is_refused() { + let dir = test_dir("components"); + let windows_only: &[&str] = if cfg!(windows) { &["sub\\out.pdf"] } else { &[] }; + for &name in ["../out.pdf", "sub/out.pdf", "/out.pdf", ""].iter().chain(windows_only) { + let err = create_staging(&dir, name, StagingName::SuffixThenTag, [0xab]).unwrap_err(); + assert_eq!(err.kind(), std::io::ErrorKind::InvalidInput, "{name:?}"); + } + assert!(std::fs::read_dir(&dir).unwrap().next().is_none(), "nothing was created"); + let _ = std::fs::remove_dir_all(&dir); +} + /// #5001, #4593: a save overwriting an existing file preserves its original attributes even if /// the server (such as Samba/QNAP NAS hiding dot-files) marks the temporary staging file hidden. #[cfg(windows)] diff --git a/packaging/macos/import-cert.sh b/packaging/macos/import-cert.sh index b9825da35d..de16f22d43 100755 --- a/packaging/macos/import-cert.sh +++ b/packaging/macos/import-cert.sh @@ -17,12 +17,16 @@ if [[ -z "${APPLE_CERTIFICATE:-}" ]]; then exit 0 fi -TMP="${RUNNER_TEMP:-${TMPDIR:-/tmp}}" +# A new, owner-only folder of its own (umask 077): the certificate and keychain are never +# written through a path someone else could have made first in a shared temporary folder. +TMP="$(mktemp -d "${RUNNER_TEMP:-${TMPDIR:-/tmp}}/pdfcraft-signing.XXXXXXXX")" KEYCHAIN="$TMP/pdfcraft-signing.keychain-db" CERT="$TMP/pdfcraft-signing.p12" KC_PASS="${KEYCHAIN_PASSWORD:-$(openssl rand -hex 24)}" -trap 'rm -f "$CERT"' EXIT +# Every exit path removes the keychain and the folder holding it: the keychain keeps the signing +# identity, and each run makes a new folder under a temporary parent that can outlive the job. +trap 'rm -f "$CERT"; security delete-keychain "$KEYCHAIN" 2>/dev/null || true; rm -rf "$TMP"' EXIT printf '%s' "$APPLE_CERTIFICATE" | base64 --decode >"$CERT" security create-keychain -p "$KC_PASS" "$KEYCHAIN" security set-keychain-settings -lut 21600 "$KEYCHAIN"