fix(platform): confine staging names; private temp dirs for tests and macOS signing (#5923)

This commit is contained in:
Dylan M. Taylor authored and GitHub committed 2026-11-16 21:59:18 -05:00
1 parent f8197e29ca
commit 9892160ed6
4 files changed
+49 -10

No files matched your search

+10 -3
View File
@@ -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();
+4
View File
@@ -43,6 +43,10 @@ fn create_staging_as(
suffixes: impl IntoIterator<Item = u64>,
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();
+29 -5
View File
@@ -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)]
+6 -2
View File
@@ -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"