Lukasz
Lukasz

Reputation: 11

Problem with CsrfGuard. Despite configuration from official site, csrf throw a problem


I have a problem with configuration CsrfGuard. I use configuration from github (which is in here -> https://github.com/aramrami/OWASP-CSRFGuard/tree/d197506c122aefa09af807ac48e944d778bf624c/csrfguard-test).
I try implement token synchronizer pattern. When I used this, still I have warning: potential cross-site request forgery (CSRF) attack thwarted (user:, ip:0:0:0:0:0:0:0:1, error:required token is missing from the request).
I understand what is a problem but i don' know how to add token into my 2 html files.
I don't use a jsp because I have old app which has so many html files and I can't use jsp. I try this on simply login project I don't know what am I doing wrong.

When I add this:
<script src="/JavaScriptServlet"></script>
I got this in my console:
Failed to load resource: the server responded with a status of 404 () JavaScriptServlet:1
When I copy js file into my project (path : /WEB-INF/Owasp.CsrfGuard.js) I got a problem with variables like this one: %DOMAIN_STRICT%. I know that variable is retrieve from some file but I don't know which one. I have no idea what am I doing wrong? Should I write some code in Java or it doesn't work in html?
This is my code in index.html (whole a body tag):

<form method="POST" action="login">
    <label for="username">Username:</label>
    <input type="text" name="username" id="username">
    <label for="password">Password:</label>
    <input type="text" name="password" id="password">
    <button>Submit</button>
</form>
<script src="/JavaScriptServlet"></script>

These are all properties (little modify to my code, maybe wrong):

org.owasp.csrfguard.Logger=org.owasp.csrfguard.log.JavaLogger
org.owasp.csrfguard.NewTokenLandingPage=
org.owasp.csrfguard.configuration.provider.factory = org.owasp.csrfguard.config.overlay.ConfigurationAutodetectProviderFactory
org.owasp.csrfguard.Enabled = true
org.owasp.csrfguard.ValidateWhenNoSessionExists = true
org.owasp.csrfguard.TokenPerPage=true
org.owasp.csrfguard.TokenPerPagePrecreate=true
org.owasp.csrfguard.Ajax=true
org.owasp.csrfguard.protected.Protected=/hello.html
org.owasp.csrfguard.unprotected.Index=%servletContext%/index.html
org.owasp.csrfguard.action.Log=org.owasp.csrfguard.action.Log
org.owasp.csrfguard.action.Log.Message=potential cross-site request forgery (CSRF) attack thwarted (user:%user%, ip:%remote_ip%, uri:%request_uri%, error:%exception_message%)
org.owasp.csrfguard.action.Redirect=org.owasp.csrfguard.action.Redirect
org.owasp.csrfguard.action.Redirect.Page=%servletContext%/error.html
org.owasp.csrfguard.action.Rotate=org.owasp.csrfguard.action.Rotate
org.owasp.csrfguard.TokenName=OWASP-CSRFTOKEN
org.owasp.csrfguard.PRNG=SHA1PRNG
org.owasp.csrfguard.PRNG.Provider=SUN
org.owasp.csrfguard.Config.Print = true
org.owasp.csrfguard.JavascriptServlet.sourceFile = /script/Owasp.CsrfGuard.js
org.owasp.csrfguard.JavascriptServlet.domainStrict = true
org.owasp.csrfguard.JavascriptServlet.cacheControl = private, maxage=28800
org.owasp.csrfguard.JavascriptServlet.refererPattern = .*
org.owasp.csrfguard.JavascriptServlet.refererMatchDomain = true
org.owasp.csrfguard.JavascriptServlet.injectIntoForms = true
org.owasp.csrfguard.JavascriptServlet.injectGetForms = false
org.owasp.csrfguard.JavascriptServlet.injectFormAttributes = true
org.owasp.csrfguard.JavascriptServlet.injectIntoAttributes = true
org.owasp.csrfguard.JavascriptServlet.xRequestedWith = OWASP CSRFGuard Project
org.owasp.csrfguard.configOverlay.hierarchy = classpath:Owasp.CsrfGuard.properties, classpath:Owasp.CsrfGuard.overlay.properties
org.owasp.csrfguard.configOverlay.secondsBetweenUpdateChecks = 60

Upvotes: 0

Views: 5795

Answers (1)

lukpaw
lukpaw

Reputation: 1613

You don't have a web.xml configuration:

Failed to load resource: the server responded with a status of 404 ()   JavaScriptServlet:1

Add this to web.xml:

  <servlet>
    <servlet-name>JavaScriptServlet</servlet-name>
    <servlet-class>org.owasp.csrfguard.servlet.JavaScriptServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>JavaScriptServlet</servlet-name>
    <url-pattern>/JavaScriptServlet</url-pattern>
  </servlet-mapping>

You probably don't have a web.xml configuration for all CSRF Guard.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">

  <context-param>
    <param-name>Owasp.CsrfGuard.Config</param-name>
    <param-value>csrfguard.properties</param-value>
  </context-param>

  <listener>
    <listener-class>org.owasp.csrfguard.CsrfGuardServletContextListener</listener-class>
  </listener>
  
  <listener>
    <listener-class>org.owasp.csrfguard.CsrfGuardHttpSessionListener</listener-class>
  </listener>

  <filter>
    <filter-name>CSRFGuardFilter</filter-name>
    <filter-class>org.owasp.csrfguard.CsrfGuardFilter</filter-class>
  </filter>

  <filter-mapping>
    <filter-name>CSRFGuardFilter</filter-name>
    <url-pattern>/hello.html</url-pattern>
  </filter-mapping>

  <servlet>
    <servlet-name>JavaScriptServlet</servlet-name>
    <servlet-class>org.owasp.csrfguard.servlet.JavaScriptServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>JavaScriptServlet</servlet-name>
    <url-pattern>/JavaScriptServlet</url-pattern>
  </servlet-mapping>

</web-app>

Add to your project into WEB-INF/classes file csrfguard.properties and into WEB-INF/lib file csrfguard.jar

Add to your hello.html:

<script type="text/javascript" src="/JavaScriptServlet"></script>

Upvotes: 2

Related Questions