通过客户端状态保存防止 JSF2 中的 CSRF

2023-12-06

我正在使用带有客户端状态保存的 MyFaces 2.2.3 + PrimeFaces

问完后如何防止在不同会话中重复使用 ViewState I was 由 BalusC 讲述,我可以inject my own CSRF代币 by 重写 from 渲染器,让该值成为 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;
    }
}

旧解决方案

I tried without success so far,

添加到 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>   

Then in 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...
}

This <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;
    }

}

请注意,当restoreView()回报null, JSF 将抛出一个ViewExpiredException“照常”。

要让它运行,请按以下方式注册faces-config.xml:

<application>
    <view-handler>com.example.CsrfViewHandler</view-handler>    
</application>

因为它在服务器端状态保存方面没有附加值,所以如果需要,您可以在视图处理程序的构造函数中检测当前 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);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

通过客户端状态保存防止 JSF2 中的 CSRF 的相关文章

随机推荐