<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	
	xmlns:georss="http://www.georss.org/georss"
	xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
	
	>
<channel>
	<title>
	Comments on: Best practices for temp files	</title>
	<atom:link href="https://www.robg3d.com/2013/01/best-practices-for-temp-files/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.robg3d.com/2013/01/best-practices-for-temp-files/</link>
	<description>Blog of Rob Galanakis (@robgalanakis)</description>
	<lastBuildDate>Tue, 15 Jan 2013 22:13:50 +0000</lastBuildDate>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.4.1</generator>
	<item>
		<title>
		By: chris		</title>
		<link>https://www.robg3d.com/2013/01/best-practices-for-temp-files/#comment-54194</link>

		<dc:creator><![CDATA[chris]]></dc:creator>
		<pubDate>Tue, 15 Jan 2013 22:13:50 +0000</pubDate>
		<guid isPermaLink="false">http://www.robg3d.com/?p=1060#comment-54194</guid>

					<description><![CDATA[&lt;a href=&quot;#comment-54053&quot; rel=&quot;nofollow&quot;&gt;@Adam Skutt&lt;/a&gt; 
You know what&#039;s also unsafe? Parsing and executing Python through an application like Maya.. You guys should quit.]]></description>
			<content:encoded><![CDATA[<p><a href="#comment-54053" rel="nofollow">@Adam Skutt</a><br />
You know what&#8217;s also unsafe? Parsing and executing Python through an application like Maya.. You guys should quit.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Adam Skutt		</title>
		<link>https://www.robg3d.com/2013/01/best-practices-for-temp-files/#comment-54053</link>

		<dc:creator><![CDATA[Adam Skutt]]></dc:creator>
		<pubDate>Mon, 14 Jan 2013 12:53:39 +0000</pubDate>
		<guid isPermaLink="false">http://www.robg3d.com/?p=1060#comment-54053</guid>

					<description><![CDATA[There&#039;s lots of questionable advice here.  I&#039;m not sure you fully understand temporary file-race issues, and why they are so dangerous.

You should never, ever just generate a temporary file name, unless you&#039;re going to write into a secure directory.  A secure directory is one that no one else can write into (except root) and the same holds for all parent directories (or the sticky bit is set).  Otherwise, you open yourself up to race attacks.

Temporary filenames in sticky directories (e.g., /tmp) are only safe to use as long as the file itself exists.  Once the file is gone, the name isn&#039;t usable either.  Suitably paranoid code would check that the directory is sticky before creating the file and using the name.

In general, all of this means you should write code that never looks at, or cares about, the name of a temporary file.     If you care about the name, you must be very careful.  In those cases, it&#039;s probably easiest to securely create a temporary directory, then create the files in there.

This code:
“mkstemp(dir=os.path.join(gettempdir(), ‘myscratchfiles’))“ is insecure because someone else could make myscratchfiles into a symlink.  It could be used to write a temporary file into an attacker controlled place, which opens you to attack if you reuse the filename (i.e., close the descriptor and open the same name again).  Likewise, your last paragraph is only safe because you use mkstemp and not one of the other functions.

The simplest way to ensure temporary files and directories get cleaned up is to just use tempfile.TemporaryFile where appropriate, or a context manager when not appropriate.  If you&#039;re finding management of temporary files difficult, then you&#039;re most likely structuring your code incorrectly.]]></description>
			<content:encoded><![CDATA[<p>There&#8217;s lots of questionable advice here.  I&#8217;m not sure you fully understand temporary file-race issues, and why they are so dangerous.</p>
<p>You should never, ever just generate a temporary file name, unless you&#8217;re going to write into a secure directory.  A secure directory is one that no one else can write into (except root) and the same holds for all parent directories (or the sticky bit is set).  Otherwise, you open yourself up to race attacks.</p>
<p>Temporary filenames in sticky directories (e.g., /tmp) are only safe to use as long as the file itself exists.  Once the file is gone, the name isn&#8217;t usable either.  Suitably paranoid code would check that the directory is sticky before creating the file and using the name.</p>
<p>In general, all of this means you should write code that never looks at, or cares about, the name of a temporary file.     If you care about the name, you must be very careful.  In those cases, it&#8217;s probably easiest to securely create a temporary directory, then create the files in there.</p>
<p>This code:<br />
“mkstemp(dir=os.path.join(gettempdir(), ‘myscratchfiles’))“ is insecure because someone else could make myscratchfiles into a symlink.  It could be used to write a temporary file into an attacker controlled place, which opens you to attack if you reuse the filename (i.e., close the descriptor and open the same name again).  Likewise, your last paragraph is only safe because you use mkstemp and not one of the other functions.</p>
<p>The simplest way to ensure temporary files and directories get cleaned up is to just use tempfile.TemporaryFile where appropriate, or a context manager when not appropriate.  If you&#8217;re finding management of temporary files difficult, then you&#8217;re most likely structuring your code incorrectly.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Antonio		</title>
		<link>https://www.robg3d.com/2013/01/best-practices-for-temp-files/#comment-54044</link>

		<dc:creator><![CDATA[Antonio]]></dc:creator>
		<pubDate>Mon, 14 Jan 2013 10:37:22 +0000</pubDate>
		<guid isPermaLink="false">http://www.robg3d.com/?p=1060#comment-54044</guid>

					<description><![CDATA[I think a context manager is a better overall solution:

class cdinto(object):
  def __enter__(...)
    -&#062; create the temp dir
  def __exit__(...)
    -&#062; remove the leftovers

and wrapping the main with:
  with cdinto() as tmp:
    main()

Much easier;)]]></description>
			<content:encoded><![CDATA[<p>I think a context manager is a better overall solution:</p>
<p>class cdinto(object):<br />
  def __enter__(&#8230;)<br />
    -&gt; create the temp dir<br />
  def __exit__(&#8230;)<br />
    -&gt; remove the leftovers</p>
<p>and wrapping the main with:<br />
  with cdinto() as tmp:<br />
    main()</p>
<p>Much easier;)</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Roger		</title>
		<link>https://www.robg3d.com/2013/01/best-practices-for-temp-files/#comment-54009</link>

		<dc:creator><![CDATA[Roger]]></dc:creator>
		<pubDate>Mon, 14 Jan 2013 04:17:04 +0000</pubDate>
		<guid isPermaLink="false">http://www.robg3d.com/?p=1060#comment-54009</guid>

					<description><![CDATA[Always use the prefix option when making temp files giving the name of your script/module/library/function as appropriate.  That way examination of the temp directory will tell you who is to blame for files and makes manual cleanup easier, not to mention detecting bugs in the cleanup of files when you do an &quot;ls&quot; and see many files with your prefix!]]></description>
			<content:encoded><![CDATA[<p>Always use the prefix option when making temp files giving the name of your script/module/library/function as appropriate.  That way examination of the temp directory will tell you who is to blame for files and makes manual cleanup easier, not to mention detecting bugs in the cleanup of files when you do an &#8220;ls&#8221; and see many files with your prefix!</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Mumm		</title>
		<link>https://www.robg3d.com/2013/01/best-practices-for-temp-files/#comment-53984</link>

		<dc:creator><![CDATA[Mumm]]></dc:creator>
		<pubDate>Sun, 13 Jan 2013 21:47:54 +0000</pubDate>
		<guid isPermaLink="false">http://www.robg3d.com/?p=1060#comment-53984</guid>

					<description><![CDATA[I would also recommend keeping temp files around for helping with debugging.]]></description>
			<content:encoded><![CDATA[<p>I would also recommend keeping temp files around for helping with debugging.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Adam		</title>
		<link>https://www.robg3d.com/2013/01/best-practices-for-temp-files/#comment-53969</link>

		<dc:creator><![CDATA[Adam]]></dc:creator>
		<pubDate>Sun, 13 Jan 2013 17:39:12 +0000</pubDate>
		<guid isPermaLink="false">http://www.robg3d.com/?p=1060#comment-53969</guid>

					<description><![CDATA[Thanks! I actually never knew about the tempfile module.]]></description>
			<content:encoded><![CDATA[<p>Thanks! I actually never knew about the tempfile module.</p>
]]></content:encoded>
		
			</item>
	</channel>
</rss>
