Difference between revisions of "Struts"

From OWASP
Jump to: navigation, search
(Validation framework)
(Common errors and vulnerabilities)
 
(27 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
==Status==
 
==Status==
'''Content to be finalised.  First draft'''
+
'''Content to be finalized.  First draft'''
  
 +
==Overview==
 +
[https://www.owasp.org/index.php/Category:OWASP_Java_Project Struts] is an [[Apache]] framework aimed at simplifying the creation of dynamic web applications in [[Java]].
  
==Introduction ==
+
Struts is built on a MVC architecture, which means the application is arranged into 3 primary types of code. These are know as a Model, View and Controller.  The Model defines the structure of your data being processed.  The View defines everything that a end user can see.  The controller take the model as submitted from the page, performs business logic on the data, then decides what view should be responsible for displaying the result.
This article describes the web security implications for the Struts MVC framework, how Struts helps in securing your web applications and where special attention is needed. It will not describe the internal details of Struts.
+
  
==Architecture==
+
I will not spend any more time talking about the architecture of struts. If you would like to have more information on that topic, I suggest going to the [http://struts.apache.org/ official website].
The framework provides its own web Controller  component. This Controller acts as a bridge between the application's Model and the web View. When a request is received, the Controller invokes an Action class. The Action class interacts with the Model to examine or update the application's state. The framework provides an ActionForm class to help transfer data between Model and View.
+
  
==Components==
+
==Security in the Model==
===Action===
+
* No distinction is made between HTTP GET and POST method. Both methods are mapped to the same Action execute method.
+
  
=== ActionForm ===
+
===Validation===
* The ActionForm is much like a java bean. 
+
* There is at least one action for each action that contains post data.
+
* It defines the fields that are passed to the action. 
+
* It has pointers to or contains the validation that occurs before control makes it to the action. 
+
* It is very important that you validate every field no matter how certain you may be about it's inability to cause problems.
+
  
==== Validation in the ActionForm ====
+
The Struts Validation Framework is the primary method of validating a struts based application. Struts validation consists of a few elements to be setup. To properly use Struts validation your application should have the following...
* struts-config.xml
+
<pre>
+
    <struts-config>
+
        <form-beans>
+
            <form-bean name="logonForm" type="net.jcj.LogonForm"/>
+
        </form-beans>
+
        <action-mappings>
+
            <action path="/Logon" forward="/pages/Logon.jsp"/>
+
            <action path="/LogonSubmit" type="app.jcj.LogonAction" name="logonForm"
+
              scope="request" validate="true" input="/pages/Logon.jsp">
+
                <forward name="success" path="/pages/Welcome.jsp"/>
+
                <forward name="failure" path="/pages/Logon.jsp"/>
+
            </action>
+
        </action-mappings>
+
        <message-resources parameter="resources.application"/>
+
    </struts-config>
+
</pre>
+
* net.jcj.LogonForm
+
<pre>
+
package net.jcj;
+
  
import javax.servlet.http.HttpServletRequest;
+
<ul>
import org.apache.struts.action.*;
+
<li>A validator-rules.xml file in the WEB-INF folder.</li>
 +
<li>A validator.xml in the WEB-INF folder.</li>
 +
<li>All ActionForms should extend org.apache.struts.validator.ValidatorForm or org.apache.struts.validator.ValidatorActionForm instead of org.apache.struts.action.ActionForm.</li>
 +
<li>The commons-validator.jar in WEB-INF.  This can be obtained [http://commons.apache.org/validator/ here].</li>
 +
<li>The Validator plug-in should be enabled in struts-config.xml
 +
<p><pre>
 +
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
 +
    <set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validator.xml"/>
 +
</plug-in>
 +
</pre></p></li>
 +
</ul>
  
public class LogonForm extends ActionForm
+
====Examples====
{
+
  private String userId = null;
+
  private String password = null;
+
  
  public void setUserId (String userId){
+
[[Struts Validation in an ActionForm]]
    this.userId = userId ;
+
  }
+
  
  public String getUserId(){
+
[[Struts Validation in validator.xml using an ActionForm]]
    return this.userId ;
+
  }
+
  
  public void setPassword (String password){
+
[[Struts Validation in validator.xml using a DynaValidatorForm]]
    this.password = password;
+
  }
+
  
  public String getPassword(){
+
==Security in the View==
    return this.password;
+
  }
+
  
    /**
+
===Output Sanitation===
    * Resets all properties to their default values.
+
    */
+
    public void reset(ActionMapping mapping, HttpServletRequest request) {
+
      this.userId = null;
+
      this.password = null;
+
    }
+
  
    /**
+
[[Output sanitation]] is the process of ensuring that your output does not contain HTML or XML specific characters. So, for example a '<' becomes '&amp;lt;'.  This should be used as a secondary [[Cross-site Scripting (XSS)|XSS]] prevention method. Primary method of prevention should be validation. Luckily some Struts tags include output sanitation by default. If you're tag is not here, then you should implement sanitation manually.
    * Validates the form.  Returns a list of action
+
    * Of course in a production environment, your rules would be far more strict than this.
+
    */
+
  public ActionErrors validate(
+
      ActionMapping mapping, HttpServletRequest request ) {
+
      ActionErrors errors = new ActionErrors();
+
     
+
      if( getUserId() == null || getUserId().length() < 1 ) {
+
        errors.add("userId",new ActionMessage("error.userid.required"));
+
      }
+
      if( getPassword() == null || getPassword().length() < 1 ) {
+
        errors.add("password",new ActionMessage("error.password.required"));
+
      }
+
  
      return errors;
+
====Sanitized tags====
  }
+
*bean:Write (may be overwritten by setting filter to false)
 +
*html:Hidden
 +
*html:Messages (if the value is of type String)
 +
*html:Multibox
 +
*html:OptionsCollection (may be overwritten by setting filter to false)
 +
*html:Options (may be overwritten by setting filter to false)
 +
*html:Option '''(you must set filter to true)'''
 +
*html:Radio
 +
*html:TextArea
 +
*html:File
 +
*html:Hidden
 +
*html:Password
 +
*html:Text
  
}
+
==Security in the Controller==
</pre>
+
  
===Validation framework===
+
===Roles===
* Integration with commons validator
+
* A bit awkward, but it gets the job done.
+
  
 +
In the struts-config.xml configuration file it is possible to specify a roles attribute, a comma-delimited list of security role names that are allowed access to the ActionMapping object.  This is pretty much all that you get out of the box. 
  
* struts-config.xml
 
 
<pre>
 
<pre>
    <struts-config>
+
<action
      <form-beans>
+
    roles="administrator,contributor"
          <form-bean name="logonForm" type="net.jcj.LogonForm"/>
+
    path="/article/Edit"
      </form-beans>
+
    parameter="org.article.FindByArticle"
      <action-mappings>
+
    name="articleForm"
          <action path="/Logon" forward="/pages/Logon.jsp"/>
+
    scope="request">
          <action path="/LogonSubmit" type="app.jcj.LogonAction" name="logonForm"  
+
      <forward
            scope="request" validate="true" input="/pages/Logon.jsp">
+
            name="success"
              <forward name="success" path="/pages/Welcome.jsp"/>
+
            path="article.jsp"/>
              <forward name="failure" path="/pages/Logon.jsp"/>
+
</action>
          </action>
+
      </action-mappings>
+
      <message-resources parameter="resources.application"/>
+
        <plug-in className="org.apache.struts.validator.ValidatorPlugIn">
+
        <set-property property="pathnames" value="/technology/WEB-INF/validator-rules.xml, /WEB-INF/validation.xml"/>
+
      </plug-in>
+
    </struts-config>
+
 
</pre>
 
</pre>
* net.jcj.LogonForm
 
<pre>
 
package net.jcj;
 
  
import javax.servlet.http.HttpServletRequest;
+
===Custom Action Mappings===
import org.apache.struts.action.*;
+
  
public class LogonForm extends ActionForm
+
It is possible to implement far more complex security models if you extend the action mappings.
{
+
  private String userId = null;
+
  private String password = null;
+
  
  public void setUserId (String userId){
+
<pre>TODO: Lots more detail here.</pre>
    this.userId = userId ;
+
  }
+
  
  public String getUserId(){
+
===Error Handling===
    return this.userId ;
+
  }
+
  
  public void setPassword (String password){
+
<pre>TODO: Put some info here</pre>
    this.password = password;
+
  }
+
  
  public String getPassword(){
+
==Common errors and vulnerabilities==
    return this.password;
+
  }
+
  
    /**
+
[[Improper_Data_Validation#Struts:_Form_Field_Without_Validator|Form Field Without Validator]]
    * Resets all properties to their default values.
+
    */
+
    public void reset(ActionMapping mapping, HttpServletRequest request) {
+
      this.userId = null;
+
      this.password = null;
+
    }
+
  
    /**
+
[[Improper_Data_Validation#Struts:_Plug-in_Framework_Not_In_Use|Plug-in Framework Not In Use]]
    * Validates the form.  Returns a list of action
+
    * Of course in a production environment, your rules would be far more strict than this.
+
    */
+
  public ActionErrors validate(
+
      ActionMapping mapping, HttpServletRequest request ) {
+
      return new ActionErrors();
+
  }
+
  
}
+
[[Improper_Data_Validation#Struts:_Unused_Validation_Form|Unused Validation Form]]
</pre>
+
  
* validation.xml
+
[[Improper_Data_Validation#Struts:_Unvalidated_Action_Form|Unvalidated Action Form]]
  
<pre>
+
[[Improper_Data_Validation#Struts:_Validator_Turned_Off|Validator Turned Off]]
<form-validation>
+
  <formset>
+
    <form name="logonForm">
+
      <field property="userId" depends="required">
+
        <arg0 key="prompt.userId"/>
+
      </field>
+
      <field property="password" depends="required">
+
        <arg0 key="prompt.password"/>
+
      </field>
+
    </form>
+
  </formset>
+
</form-validation>
+
</pre>
+
  
==Configuration==
+
[[Improper_Data_Validation#Struts:_Validator_Without_Form_Field|Validator Without Form Field]]
  
==Security==
+
[[Improper_Data_Validation#Struts:_Form_Does_Not_Extend_Validation_Class|Form Does Not Extend Validation Class]]
===Roles===
+
In the struts-config.xml configuration file it is possible to specify a roles attribute, a comma-delimited list of security role names that are allowed access to the ActionMapping object.  This is pretty much all that you get out of the box. 
+
  
<pre>
+
[[Improper_Data_Validation#Struts:_Erroneous_validate.28.29_Method|Erroneous validate() Method]]
<action
+
    roles="administrator,contributor"
+
    path="/article/Edit"
+
    parameter="org.article.FindByArticle"
+
    name="articleForm" 
+
    scope="request">
+
      <forward
+
            name="success"
+
            path="article.jsp"/>
+
</action>
+
</pre>
+
  
===Extending action mappings===
+
[[Improper_Data_Validation#Struts:_Duplicate_Validation_Forms|Duplicate Validation Forms]]
If you extend the action mappings, you will be able to satisfy much more complicated security schemes.
+
  
<pre>
+
==Auditing Tools==
 +
 
 +
[[Struts XSLT Viewer]]
  
</pre>
 
 
[[Category:OWASP Java Project]]
 
[[Category:OWASP Java Project]]
 +
[[Category:Struts]]
 +
[[Category:Java]]

Latest revision as of 20:22, 17 February 2009

Contents

Status

Content to be finalized. First draft

Overview

Struts is an Apache framework aimed at simplifying the creation of dynamic web applications in Java.

Struts is built on a MVC architecture, which means the application is arranged into 3 primary types of code. These are know as a Model, View and Controller. The Model defines the structure of your data being processed. The View defines everything that a end user can see. The controller take the model as submitted from the page, performs business logic on the data, then decides what view should be responsible for displaying the result.

I will not spend any more time talking about the architecture of struts. If you would like to have more information on that topic, I suggest going to the official website.

Security in the Model

Validation

The Struts Validation Framework is the primary method of validating a struts based application. Struts validation consists of a few elements to be setup. To properly use Struts validation your application should have the following...

  • A validator-rules.xml file in the WEB-INF folder.
  • A validator.xml in the WEB-INF folder.
  • All ActionForms should extend org.apache.struts.validator.ValidatorForm or org.apache.struts.validator.ValidatorActionForm instead of org.apache.struts.action.ActionForm.
  • The commons-validator.jar in WEB-INF. This can be obtained here.
  • The Validator plug-in should be enabled in struts-config.xml

    <plug-in className="org.apache.struts.validator.ValidatorPlugIn">
        <set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validator.xml"/>
    </plug-in>
    

Examples

Struts Validation in an ActionForm

Struts Validation in validator.xml using an ActionForm

Struts Validation in validator.xml using a DynaValidatorForm

Security in the View

Output Sanitation

Output sanitation is the process of ensuring that your output does not contain HTML or XML specific characters. So, for example a '<' becomes '&lt;'. This should be used as a secondary XSS prevention method. Primary method of prevention should be validation. Luckily some Struts tags include output sanitation by default. If you're tag is not here, then you should implement sanitation manually.

Sanitized tags

  • bean:Write (may be overwritten by setting filter to false)
  • html:Hidden
  • html:Messages (if the value is of type String)
  • html:Multibox
  • html:OptionsCollection (may be overwritten by setting filter to false)
  • html:Options (may be overwritten by setting filter to false)
  • html:Option (you must set filter to true)
  • html:Radio
  • html:TextArea
  • html:File
  • html:Hidden
  • html:Password
  • html:Text

Security in the Controller

Roles

In the struts-config.xml configuration file it is possible to specify a roles attribute, a comma-delimited list of security role names that are allowed access to the ActionMapping object. This is pretty much all that you get out of the box.

<action
     roles="administrator,contributor"
     path="/article/Edit"
     parameter="org.article.FindByArticle"
     name="articleForm"  
     scope="request">
       <forward
             name="success"
             path="article.jsp"/>
</action>

Custom Action Mappings

It is possible to implement far more complex security models if you extend the action mappings.

TODO: Lots more detail here.

Error Handling

TODO: Put some info here

Common errors and vulnerabilities

Form Field Without Validator

Plug-in Framework Not In Use

Unused Validation Form

Unvalidated Action Form

Validator Turned Off

Validator Without Form Field

Form Does Not Extend Validation Class

Erroneous validate() Method

Duplicate Validation Forms

Auditing Tools

Struts XSLT Viewer