title: Security: CWE-201: What is the correct way to securely read a properties file using openStream?
body: <p>I'm working on coming up with a solution for CWE-201 that is flagged from Veracode. </p>
  
  <p><strong>Background:</strong> </p>
  
  <p><strong>CWE-201: Information Exposure Through Sent Data</strong></p>
  
  <p><strong>Information Exposure Through Sent Data</strong>
  Weakness ID: 201 (Weakness Variant)     Status: Draft
  <strong>+ Description</strong>
  Description Summary
  The accidental exposure of sensitive information through sent data refers to the transmission of data which are either sensitive in and of itself or useful in the further exploitation of the system through standard data channels.</p>
  
  <p><strong>Phase: Architecture and Design</strong>
  Strategy: Separation of Privilege
  Compartmentalize the system to have "safe" areas where trust boundaries can be unambiguously drawn. Do not allow sensitive data to go outside of the trust boundary and always be careful when interfacing with a compartment outside of the safe area.
  Ensure that appropriate compartmentalization is built into the system design and that the compartmentalization serves to allow for and further reinforce privilege separation functionality. Architects and designers should rely on the principle of least privilege to decide when it is appropriate to use and to drop system privileges.</p>
  
  <p>Besides...what the heck does that mean for people who code, I'm trying to find some practical solutions using java to resolve this problem. </p>
  
  <p>What I can tell is that the following code will cause veracode to flag the cwe-201:</p>
  
  <pre><code>public void init(URL filePath) {
      try {
          load(new BufferedInputStream(filePath.openStream()));
      } catch (java.io.IOException e) {           
          Log.error("Could not load server properties file!", e);                     
      } 
  }
  </code></pre>
  
  <p>More information:</p>
  
  <p><strong>Phase: Implementation</strong>
  Ensure that any possibly sensitive data specified in the requirements is verified with designers to ensure that it is either a calculated risk or mitigated elsewhere. Any information that is not necessary to the functionality should be removed in order to lower both the overhead and the possibility of security sensitive data being sent.</p>
  
  <p><strong>Phase: System Configuration</strong>
  <strong>Setup default error messages so that unexpected errors do not disclose sensitive information.</strong></p>
  
  <p><strong>I have done the recommendation stated in the System Configuration by creating a custom runtime exception which swallows the the IOException here...but Veracode still flagged it.</strong> </p>
  
  <p>Here's that that code looks like:</p>
  
  <pre><code>public class CWE201Exception extends RuntimeException {
  
  private static Logger log = ESAPI.getLogger(CWE201Exception .class.getName());
  
  public CWE201Exception(String identifer, Throwable t){  
      log.error(Logger.SECURITY_AUDIT, identifer);
  }
  </code></pre>
  
  <p>}</p>
  
  <p>And updated the method to look like this:</p>
  
  <pre><code>public void init(URL filePath) {
      try {
          load(new BufferedInputStream(filePath.openStream()));
      } catch (java.io.IOException e) {           
          throw new CWE201Exception("omgStillDoingThis", e);                      
      } 
  }
  </code></pre>
  
  <p>Looking through the veracode report, I came across the following:</p>
  
  <p><strong>Attack Vector: java.net.URL.openStream</strong></p>
  
  <p>Description:  The application calls the java.net.URL.openStream() function, which will result in data being transferred out of the application (via the network or another medium). This data contains sensitive information. openStream() was called on the filePath object, which contains potentially sensitive data. The potentially sensitive data originated from an earlier call to <strong>java.lang.system.getproperty</strong>.</p>
  
  <p>Remediation: <strong>Ensure that the transfer of the sensitive data is intended and that it does not violate application security policy.</strong> This flaw is categorized as low severity because it only impacts confidentiality, not integrity or availability. However, in the context of a mobile application, the significance of an information leak may be much greater, especially if misaligned with user expectations or data privacy policies.</p>
  
  <p><strong>QUESTION</strong>
  It turns out that when you read a property file that resides on your server in this way, you are using System.getProperties() indirectly. </p>
  
  <p><strong>Exposing this as a stream is viewed as the security threat</strong></p>
  
  <p>With that said, what is the correct way to load a property file so that your application can load environment configuration informationin what a manner that veracode considers a "safe"?</p>