title: Remove passwords from (PHP) stream wrapper error messages?
body: <p>On production servers, error output is surely be off as a matter of routine. Still, I'm feeling jumpy when I see error messages like this even on my dev screen:</p>
  
  <pre><code>stat(): stat failed for ftp://user:pass@127.0.0.1:21/dir-de-nada
  </code></pre>
  
  <p>This is in the context of a class I wrote around the <code>ftp://</code> stream. Of course PHP can't filter out passwords from any arbitrary location. But in this case (as with URL wrappers and wherever else), the presence of a password is standard and obvious.</p>
  
  <p>I'm already catching the errors. However I found myself wondering if I need to X these out myself, just to be on the safe side in all contexts, <strong>or if there's a way I can have PHP to do it automatically and globally.</strong> (That's the question here.) I'm guessing not, but no harm in tossing in a probe here.</p>
  
  <p><em>As to what me worry, it's only on my dev screen.</em> However error reports will also be relevant to site managers etc., who may not need to know the FTP password. Having the FTP password blurted into the error log is less of a concern, but for reports saved into a database etc. -- I really don't want this sort of information propagating anywhere, ever, for obvious reasons. Prevention pointers please?</p>
  
  <p>And if anyone feels like contributing related notes on keeping PHP from blurting out passwords etc. sensitive info in whatever context, that's also welcome. Aside "don't output anything anywhere".</p>
  
  <hr>
  
  <p><strong>Edit:</strong> Better options pending, the message-part of my error and exception handlers now runs this:</p>
  
  <pre><code>$msg = preg_replace('#((ftp|http)://)([^@]+?)@#', '$1*:*@', $msg);
  </code></pre>
  
  <p>If working with SSH2 streams, add in regex for <code>ssh2\.(shell|exec|sftp|scp)</code> in case concerned. And for good measure, if you use stack traces that show arguments, sanitize those too.</p>
  
  <hr>
  
  <p><strong>Edit2:</strong> On a general note on experiences with PHP's FTP stream context wrapper. </p>
  
  <ol>
  <li>Performs lousy in particular if there are any failing requests, giving repeated script timeouts.</li>
  <li>Stream callback (defined in <code>create_stream_context</code> / <code>$param['notification']</code>) logs mostly blanks/partials instead of complete responses from the FTP server.</li>
  <li>Filesystem functions using stream contexts may or may not return proper/consistent errors, or log anything to the callback. (If someone wants a breakdown of malfunctions, ask away.)</li>
  <li>It seems there's no option for a persistent connection: PHP re-logs-in in for every filesys call in the same script even if I recycle the <code>stream_context_create</code> resource. </li>
  </ol>
  
  <p>Suppose FTP stream contexts are fine for one-off basic transactions, but a serious no for trying to do anything more in one go, feels a bit like an immature alt. rig for the filesys functions. Next...</p>