Skip to content

Commit d6c7a79

Browse files
authored
Rollup merge of rust-lang#81219 - joshtriplett:temp_dir-docs, r=sfackler
Document security implications of std::env::temp_dir Update the sample code to not create an insecure temporary file.
2 parents 2ebc036 + 27f3764 commit d6c7a79

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

library/std/src/env.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,13 @@ pub fn home_dir() -> Option<PathBuf> {
561561

562562
/// Returns the path of a temporary directory.
563563
///
564+
/// The temporary directory may be shared among users, or between processes
565+
/// with different privileges; thus, the creation of any files or directories
566+
/// in the temporary directory must use a secure method to create a uniquely
567+
/// named file. Creating a file or directory with a fixed or predictable name
568+
/// may result in "insecure temporary file" security vulnerabilities. Consider
569+
/// using a crate that securely creates temporary files or directories.
570+
///
564571
/// # Unix
565572
///
566573
/// Returns the value of the `TMPDIR` environment variable if it is
@@ -580,14 +587,10 @@ pub fn home_dir() -> Option<PathBuf> {
580587
///
581588
/// ```no_run
582589
/// use std::env;
583-
/// use std::fs::File;
584590
///
585-
/// fn main() -> std::io::Result<()> {
591+
/// fn main() {
586592
/// let mut dir = env::temp_dir();
587-
/// dir.push("foo.txt");
588-
///
589-
/// let f = File::create(dir)?;
590-
/// Ok(())
593+
/// println!("Temporary directory: {}", dir.display());
591594
/// }
592595
/// ```
593596
#[stable(feature = "env", since = "1.0.0")]

0 commit comments

Comments
 (0)