0

My understanding of @RequestScoped bean is that CDI creates a new instance for each request. We have a @Stateless bean and have @RequestScoped injected into it.

@Stateless
@Local(MyStateLessInterface.class)
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
@Interceptors({PerformanceLog.class, EntryExitLog.class})
public class MyStatelessBean implements MyStateLessInterface{

    @Inject
     private MyRequestScopedBean requestScopedBean;

}

@RequestScoped
public class MyRequestScopedBean {

private String jasperPath;

    public void setJasperPath(String jasperPath) {
        this.jasperPath = jasperPath;
    }

//Path will be set by MyStatelessBean and a different method in this class uses this.jasperPath to generate a report.

}

Functionally this seems to work fine, but our DevOps reports issue identified by fortify tool with below details :

The class MyRequestScopedBean is a singleton, so the member field jasperPath is shared between users. The result is that one user could see another user's data. Issue Category: Race Condition: Singleton Member Field

Application Name: Conexus-Reporting

Application Version Name: 1.0

Custom Tags:

Issue Detail: Many Servlet developers do not understand that a Servlet is a singleton. There is only one instance of the Servlet, and that single instance is used and re-used to handle multiple requests that are processed simultaneously by different threads.

A common result of this misunderstanding is that developers use Servlet member fields in such a way that one user may inadvertently see another user's data. In other words, storing user data in Servlet member fields introduces a data access race condition.

I have found some explanation here, but unable to reconcile my understanding. I am trying to understand why fortify is complaining and should we take it seriously.

rohith
  • 733
  • 4
  • 10
  • 24
  • 1
    Are you sure this Fortify tool understands the specifics of CDI? From a quick look at its supported languages it covers Java and JSP. How accurate is it? – Nikos Paraskevopoulos Mar 24 '21 at 21:07
  • @Nikos : That is what i am trying to understand. Do you see any problem with the way we are using it. – rohith Mar 25 '21 at 15:46
  • 1
    Hi @rohith, from what I see, I would say *you are using it right*! I might be missing something, or there may be some detail in the code not shown here. Or the tool simply does not cover this case. – Nikos Paraskevopoulos Mar 26 '21 at 09:14
  • Request scope by default behaves just like you said - new instance with a lifespan of a request meaning you create and destroy those beans for every request made. I see no problem with that so I'd say the security tool has no idea how CDI works and is just looking at pure code and trying to make assumptions. – Siliarus Mar 30 '21 at 07:31

0 Answers0