Dan*_*iel 4 viewstate myfaces csrf jsf-2 jsf-2.2
我正在使用MyFaces 2.2.3与客户端状态保存+ PrimeFaces
在询问如何阻止BalusC告诉我在不同会话中重用ViewState之后,我可以通过覆盖渲染器来注入我自己的CSRF令牌,让值为CSRF令牌,
我正在寻找一个不会强迫我修改我的xhtml页面的解决方案:)
BalusC提出了一种通过扩展来防止CSRF攻击的更好方法ViewHandlerWrapper,并且它工作得很好,我只需要通过restoreView以下方式修改一下
public UIViewRoot restoreView(FacesContext context, String viewId) {
UIViewRoot view = super.restoreView(context, viewId);
if (getCsrfToken(context).equals(view.getAttributes().get(CSRF_TOKEN_KEY))) {
return view;
} else {
HttpSession session = (HttpSession) context.getExternalContext().getSession(false);
if (session != null) {
session.invalidate(); //invalidate session so (my custom and unrelated) PhaseListener will notice that its a bad session now
}
try {
FacesContext.getCurrentInstance().getExternalContext().redirect("CSRF detected and blocked"); //better looking user feedback
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
老解决方案
到目前为止我尝试没有成功,
添加到faces-config.xml
<render-kit>
<renderer>
<component-family>javax.faces.Form</component-family>
<renderer-type>javax.faces.Form</renderer-type>
<renderer-class>com.communitake.mdportal.renderers.CTFormRenderer</renderer-class>
</renderer>
</render-kit>
Run Code Online (Sandbox Code Playgroud)
然后进去 CTFormRenderer.java
@Override
public void encodeEnd(FacesContext context, UIComponent arg1) throws IOException {
//how to set form value be a CSRF token?
}
@Override
public void decode(FacesContext context, UIComponent component) {
HttpSession session = (HttpSession) context.getExternalContext().getSession(false);
String token = (String) session.getAttribute(CSRFTOKEN_NAME);
String tokenFromForm = //how to get the value stored in form value attribute because (String) component.getAttributes().get("value"); return null
//check token against tokenFromForm...
}
Run Code Online (Sandbox Code Playgroud)
我不想为每一个添加自定义组件h:form,而是我想扩展form渲染器,以便我的所有表单都具有csrf token.
这种<h:form>渲染器覆盖方法对PrimeFaces来说是不安全的partialSubmit="true".此外,重用其隐藏字段标识提交的表单将是JSF实现特定的,因为这不是JSF API的一部分.
再想一想,将CSRF令牌直接存储在JSF视图状态本身要简单得多.您可以使用ViewHandler下面的自定义设置属性UIViewRoot(在JSF视图状态下自动保存):
public class CsrfViewHandler extends ViewHandlerWrapper {
private static final String CSRF_TOKEN_KEY = CsrfViewHandler.class.getName();
private ViewHandler wrapped;
public CsrfViewHandler(ViewHandler wrapped) {
this.wrapped = wrapped;
}
@Override
public UIViewRoot restoreView(FacesContext context, String viewId) {
UIViewRoot view = super.restoreView(context, viewId);
return getCsrfToken(context).equals(view.getAttributes().get(CSRF_TOKEN_KEY)) ? view : null;
}
@Override
public void renderView(FacesContext context, UIViewRoot view) throws IOException, FacesException {
view.getAttributes().put(CSRF_TOKEN_KEY, getCsrfToken(context));
super.renderView(context, view);
}
private String getCsrfToken(FacesContext context) {
String csrfToken = (String) context.getExternalContext().getSessionMap().get(CSRF_TOKEN_KEY);
if (csrfToken == null) {
csrfToken = UUID.randomUUID().toString();
context.getExternalContext().getSessionMap().put(CSRF_TOKEN_KEY, csrfToken);
}
return csrfToken;
}
@Override
public ViewHandler getWrapped() {
return wrapped;
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,当restoreView()返回时null,JSF会抛出ViewExpiredException"像往常一样".
要使其运行,请在以下位置注册faces-config.xml:
<application>
<view-handler>com.example.CsrfViewHandler</view-handler>
</application>
Run Code Online (Sandbox Code Playgroud)
因为它没有服务器端状态保存的附加值,所以如果当前的JSF应用程序配置了客户端状态保存,您可以根据需要在视图处理程序的构造函数中检测如下:
FacesContext context = FacesContext.getCurrentInstance();
if (!context.getApplication().getStateManager().isSavingStateInClient(context)) {
throw new IllegalStateException("This view handler is only applicable when JSF is configured with "
+ StateManager.STATE_SAVING_METHOD_PARAM_NAME + "=" + StateManager.STATE_SAVING_METHOD_CLIENT);
}
Run Code Online (Sandbox Code Playgroud)