You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by ta...@apache.org on 2026/03/26 12:42:49 UTC

(myfaces) branch main updated: MYFACES-4748 / MYFACES-4747: CSP script rendering

This is an automated email from the ASF dual-hosted git repository.

tandraschko pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/myfaces.git


The following commit(s) were added to refs/heads/main by this push:
     new da9617717 MYFACES-4748 / MYFACES-4747: CSP script rendering
da9617717 is described below

commit da96177178b7567525715fbfe19d2283ce027b59
Author: tandraschko <ta...@apache.org>
AuthorDate: Thu Mar 26 13:42:40 2026 +0100

    MYFACES-4748 / MYFACES-4747: CSP script rendering
---
 .../context/servlet/PartialViewContextImpl.java    |   3 +
 .../renderkit/html/HtmlResponseWriterImpl.java     |   2 +
 .../renderkit/html/base/HtmlBodyRendererBase.java  |   2 +
 .../html/base/HtmlButtonRendererBase.java          |  13 +-
 .../renderkit/html/base/HtmlLinkRendererBase.java  |   9 +-
 .../renderkit/html/util/CommonHtmlEventsUtil.java  | 284 ++++++++++++++++++++-
 .../renderkit/html/util/HtmlRendererUtils.java     | 112 +-------
 integration-tests/csp/pom.xml                      |  34 +++
 .../core/integrationtests/csp/TestBean.java        |  46 ++++
 .../csp/src/main/webapp/WEB-INF/beans.xml          |  25 ++
 .../csp/src/main/webapp/WEB-INF/web.xml            |  56 ++++
 integration-tests/csp/src/main/webapp/index.xhtml  |  43 ++++
 integration-tests/pom.xml                          |   1 +
 13 files changed, 516 insertions(+), 114 deletions(-)

diff --git a/impl/src/main/java/org/apache/myfaces/context/servlet/PartialViewContextImpl.java b/impl/src/main/java/org/apache/myfaces/context/servlet/PartialViewContextImpl.java
index e70307083..37174bcf3 100644
--- a/impl/src/main/java/org/apache/myfaces/context/servlet/PartialViewContextImpl.java
+++ b/impl/src/main/java/org/apache/myfaces/context/servlet/PartialViewContextImpl.java
@@ -59,6 +59,7 @@
 import org.apache.myfaces.context.PartialResponseWriterImpl;
 import org.apache.myfaces.context.RequestViewContext;
 import org.apache.myfaces.renderkit.html.HtmlResponseStateManager;
+import org.apache.myfaces.renderkit.html.util.CommonHtmlEventsUtil;
 import org.apache.myfaces.renderkit.html.util.ResourceUtils;
 import org.apache.myfaces.util.lang.StringUtils;
 import org.apache.myfaces.component.visit.MyFacesVisitHints;
@@ -572,6 +573,8 @@ else if (viewRoot.isTransient())
                 writer.writeText(cw.getId(), null);
                 writer.endUpdate();
             }
+
+            CommonHtmlEventsUtil.flushDeferredCspBehaviorScripts(context, writer);
         }
         catch (IOException ex)
         {
diff --git a/impl/src/main/java/org/apache/myfaces/renderkit/html/HtmlResponseWriterImpl.java b/impl/src/main/java/org/apache/myfaces/renderkit/html/HtmlResponseWriterImpl.java
index e00c11bdc..b9f455c96 100644
--- a/impl/src/main/java/org/apache/myfaces/renderkit/html/HtmlResponseWriterImpl.java
+++ b/impl/src/main/java/org/apache/myfaces/renderkit/html/HtmlResponseWriterImpl.java
@@ -35,6 +35,7 @@
 import jakarta.faces.render.Renderer;
 
 import org.apache.myfaces.config.webparameters.MyfacesConfig;
+import org.apache.myfaces.renderkit.html.util.CommonHtmlEventsUtil;
 import org.apache.myfaces.core.api.shared.ComponentUtils;
 import org.apache.myfaces.renderkit.ContentTypeUtils;
 import org.apache.myfaces.renderkit.html.util.UnicodeEncoder;
@@ -306,6 +307,7 @@ public void startDocument()
     public void endDocument() throws IOException
     {
         FacesContext facesContext = getFacesContext();
+        CommonHtmlEventsUtil.flushDeferredCspBehaviorScripts(facesContext, this);
         MyfacesConfig myfacesConfig = MyfacesConfig.getCurrentInstance(facesContext);
         if (myfacesConfig.isEarlyFlushEnabled())
         {
diff --git a/impl/src/main/java/org/apache/myfaces/renderkit/html/base/HtmlBodyRendererBase.java b/impl/src/main/java/org/apache/myfaces/renderkit/html/base/HtmlBodyRendererBase.java
index 77d41ebbe..6faacc36c 100644
--- a/impl/src/main/java/org/apache/myfaces/renderkit/html/base/HtmlBodyRendererBase.java
+++ b/impl/src/main/java/org/apache/myfaces/renderkit/html/base/HtmlBodyRendererBase.java
@@ -158,6 +158,8 @@ public void encodeEnd(FacesContext facesContext, UIComponent component) throws I
                 child.encodeAll(facesContext);
             }
         }
+
+        CommonHtmlEventsUtil.flushDeferredCspBehaviorScripts(facesContext, writer);
         
         // render all unhandled FacesMessages when ProjectStage is Development
         if (facesContext.isProjectStage(ProjectStage.Development))
diff --git a/impl/src/main/java/org/apache/myfaces/renderkit/html/base/HtmlButtonRendererBase.java b/impl/src/main/java/org/apache/myfaces/renderkit/html/base/HtmlButtonRendererBase.java
index a476e1b71..ee3ef0b48 100644
--- a/impl/src/main/java/org/apache/myfaces/renderkit/html/base/HtmlButtonRendererBase.java
+++ b/impl/src/main/java/org/apache/myfaces/renderkit/html/base/HtmlButtonRendererBase.java
@@ -44,6 +44,7 @@
 
 import org.apache.myfaces.renderkit.ClientBehaviorEvents;
 import org.apache.myfaces.renderkit.RendererUtils;
+import org.apache.myfaces.renderkit.html.util.CommonHtmlEventsUtil;
 import org.apache.myfaces.renderkit.html.util.JavascriptUtils;
 import org.apache.myfaces.renderkit.html.util.ResourceUtils;
 import org.apache.myfaces.renderkit.html.util.HTML;
@@ -187,7 +188,11 @@ public void encodeBegin(FacesContext facesContext, UIComponent uiComponent) thro
                         form, validParams);
                 if (onClick.length() != 0)
                 {
-                    writer.writeAttribute(HTML.ONCLICK_ATTR, onClick, null);
+                    if (!CommonHtmlEventsUtil.deferClientBehaviorScriptIfCspNonceActive(
+                            facesContext, clientId, HTML.ONCLICK_ATTR, onClick))
+                    {
+                        writer.writeAttribute(HTML.ONCLICK_ATTR, onClick, null);
+                    }
                 }
             }
             else
@@ -201,7 +206,11 @@ public void encodeBegin(FacesContext facesContext, UIComponent uiComponent) thro
                         commandOnclick , null);
                 if (onClick.length() != 0)
                 {
-                    writer.writeAttribute(HTML.ONCLICK_ATTR, onClick, null);
+                    if (!CommonHtmlEventsUtil.deferClientBehaviorScriptIfCspNonceActive(
+                            facesContext, clientId, HTML.ONCLICK_ATTR, onClick))
+                    {
+                        writer.writeAttribute(HTML.ONCLICK_ATTR, onClick, null);
+                    }
                 }
             }
             
diff --git a/impl/src/main/java/org/apache/myfaces/renderkit/html/base/HtmlLinkRendererBase.java b/impl/src/main/java/org/apache/myfaces/renderkit/html/base/HtmlLinkRendererBase.java
index 2da044825..93601128e 100644
--- a/impl/src/main/java/org/apache/myfaces/renderkit/html/base/HtmlLinkRendererBase.java
+++ b/impl/src/main/java/org/apache/myfaces/renderkit/html/base/HtmlLinkRendererBase.java
@@ -549,7 +549,14 @@ protected void renderBehaviorizedJavaScriptAnchorStart(FacesContext facesContext
         
         writer.startElement(HTML.ANCHOR_ELEM, component);
         writer.writeURIAttribute(HTML.HREF_ATTR, "#", null);
-        writer.writeAttribute(HTML.ONCLICK_ATTR, onclick, null);
+        if (onclick != null && !onclick.isEmpty())
+        {
+            if (!CommonHtmlEventsUtil.deferClientBehaviorScriptIfCspNonceActive(
+                    facesContext, clientId, HTML.ONCLICK_ATTR, onclick))
+            {
+                writer.writeAttribute(HTML.ONCLICK_ATTR, onclick, null);
+            }
+        }
     }
 
     private boolean hasSubmittingBehavior(Map<String, List<ClientBehavior>> clientBehaviors, String eventName)
diff --git a/impl/src/main/java/org/apache/myfaces/renderkit/html/util/CommonHtmlEventsUtil.java b/impl/src/main/java/org/apache/myfaces/renderkit/html/util/CommonHtmlEventsUtil.java
index e725f5585..71ce0a8dc 100644
--- a/impl/src/main/java/org/apache/myfaces/renderkit/html/util/CommonHtmlEventsUtil.java
+++ b/impl/src/main/java/org/apache/myfaces/renderkit/html/util/CommonHtmlEventsUtil.java
@@ -19,6 +19,7 @@
 package org.apache.myfaces.renderkit.html.util;
 
 import java.io.IOException;
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
 import java.util.Map;
@@ -26,14 +27,20 @@
 import jakarta.faces.component.behavior.ClientBehavior;
 import jakarta.faces.component.behavior.ClientBehaviorContext;
 import jakarta.faces.context.FacesContext;
+import jakarta.faces.context.PartialResponseWriter;
 import jakarta.faces.context.ResponseWriter;
 import org.apache.myfaces.core.api.shared.CommonHtmlEvents;
 import org.apache.myfaces.core.api.shared.CommonHtmlAttributes;
+import org.apache.myfaces.config.webparameters.MyfacesConfig;
 import org.apache.myfaces.renderkit.ClientBehaviorEvents;
 import org.apache.myfaces.renderkit.RendererUtils;
+import org.apache.myfaces.util.lang.StringUtils;
 
 public class CommonHtmlEventsUtil
 {
+    private static final String CSP_DEFERRED_BEHAVIOR_SCRIPTS_KEY =
+            CommonHtmlEventsUtil.class.getName() + ".DEFERRED_CSP_BEHAVIOR_SCRIPTS";
+
     public static long getMarkedEvents(UIComponent component)
     {
         return CommonHtmlEvents.getMarkedEvents(component);
@@ -93,30 +100,291 @@ public static boolean renderBehaviorizedAttribute(
         List<ClientBehavior> cbl = (clientBehaviors != null) ? clientBehaviors.get(eventName) : null;
         if (cbl == null || cbl.isEmpty())
         {
-            return HtmlRendererUtils.renderHTMLStringAttribute(writer, componentProperty,
+            return HtmlRendererUtils.renderHTMLAttribute(writer, componentProperty,
                     htmlAttrName, attributeValue);
         }
 
+        String targetElementId = sourceId != null ? sourceId : component.getClientId(facesContext);
+
         if (cbl.size() > 1 || (cbl.size() == 1 && attributeValue != null))
         {
-            return HtmlRendererUtils.renderHTMLStringAttribute(writer, componentProperty, htmlAttrName,
-                    ClientBehaviorRendererUtils.buildBehaviorChain(facesContext,
-                            component, sourceId, eventName,
-                            eventParameters, clientBehaviors, attributeValue,
-                            RendererUtils.EMPTY_STRING));
+            String chain = ClientBehaviorRendererUtils.buildBehaviorChain(facesContext,
+                    component, sourceId, eventName,
+                    eventParameters, clientBehaviors, attributeValue,
+                    RendererUtils.EMPTY_STRING);
+            if (StringUtils.isNotBlank(chain)
+                    && deferClientBehaviorScriptIfCspNonceActive(facesContext, targetElementId, htmlAttrName, chain))
+            {
+                return true;
+            }
+            return HtmlRendererUtils.renderHTMLAttribute(writer, componentProperty, htmlAttrName, chain);
         }
         else
         {
             //Only 1 behavior and attrValue == null, so just render it directly
             ClientBehaviorContext ctx = ClientBehaviorContext.createClientBehaviorContext(
                                     facesContext, component, eventName,sourceId, eventParameters);
-            return HtmlRendererUtils.renderHTMLStringAttribute(
+            String script = cbl.get(0).getScript(ctx);
+            if (deferClientBehaviorScriptIfCspNonceActive(facesContext, targetElementId, htmlAttrName, script))
+            {
+                return true;
+            }
+            return HtmlRendererUtils.renderHTMLAttribute(
                     writer,
                     componentProperty,
                     htmlAttrName,
-                    cbl.get(0).getScript(ctx));
+                    script);
+        }
+    }
+
+    /**
+     * When JSF CSP support is enabled ({@link jakarta.faces.application.ResourceHandler#ENABLE_CSP_NONCE_PARAM_NAME})
+     * or {@link jakarta.faces.application.ResourceHandler#getCurrentNonce} is non-null, inline {@code on*} event
+     * handler attributes must not carry behavior scripts. This method enqueues the same script to be emitted
+     * later (nonce-bearing {@code <script>} on a full response, or {@code <eval>} on Ajax) that assigns the
+     * handler on the element via the DOM ({@code element.onclick = function(event) { ... }}).
+     *
+     * @return {@code true} if the script was deferred and must not be rendered as an attribute
+     */
+    public static boolean deferClientBehaviorScriptIfCspNonceActive(
+            FacesContext facesContext,
+            String targetElementId,
+            String htmlAttrName,
+            String scriptBody)
+    {
+        if (!isCspDeferClientBehaviorInlineHandlers(facesContext))
+        {
+            return false;
+        }
+        if (StringUtils.isBlank(scriptBody))
+        {
+            return false;
+        }
+        String escapedId = escapeJsStringForSingleQuotes(targetElementId);
+        StringBuilder sb = new StringBuilder(64 + scriptBody.length() + escapedId.length());
+        sb.append("(function(el){if(!el)return;el.");
+        sb.append(htmlAttrName);
+        sb.append("=function(event){");
+        sb.append(scriptBody);
+        sb.append("};})(document.getElementById('");
+        sb.append(escapedId);
+        sb.append("'));");
+
+        List<String> queue = (List<String>) facesContext.getAttributes().computeIfAbsent(
+                CSP_DEFERRED_BEHAVIOR_SCRIPTS_KEY, k -> new ArrayList<>());
+        queue.add(sb.toString());
+        return true;
+    }
+
+    /**
+     * @return true when inline {@code on*} attributes must not carry client behavior scripts: JSF CSP is enabled
+     *         in configuration, or the resource handler already exposes a view nonce.
+     */
+    public static boolean isCspDeferClientBehaviorInlineHandlers(FacesContext facesContext)
+    {
+        MyfacesConfig cfg = MyfacesConfig.getCurrentInstance(facesContext);
+        if (cfg != null && cfg.isCspEnabled())
+        {
+            return true;
+        }
+        return facesContext.getApplication().getResourceHandler().getCurrentNonce(facesContext) != null;
+    }
+
+    private static String escapeJsStringForSingleQuotes(String s)
+    {
+        if (s == null)
+        {
+            return "";
+        }
+        StringBuilder out = null;
+        for (int i = 0; i < s.length(); i++)
+        {
+            char c = s.charAt(i);
+            if (c == '\\' || c == '\'')
+            {
+                if (out == null)
+                {
+                    out = new StringBuilder(s.length() + 8);
+                    out.append(s, 0, i);
+                }
+                out.append('\\');
+            }
+            if (out != null)
+            {
+                out.append(c);
+            }
+        }
+        return out == null ? s : out.toString();
+    }
+
+    /**
+     * Writes all scripts enqueued by {@link #deferClientBehaviorScriptIfCspNonceActive} in one
+     * nonce-bearing {@code <script>} block. Safe to call multiple times; only the first flush emits output.
+     */
+    public static void flushDeferredCspBehaviorScripts(FacesContext facesContext, ResponseWriter writer)
+            throws IOException
+    {
+        List<String> queue = (List<String>) facesContext.getAttributes().remove(CSP_DEFERRED_BEHAVIOR_SCRIPTS_KEY);
+        if (queue == null || queue.isEmpty())
+        {
+            return;
+        }
+        if (writer instanceof PartialResponseWriter prw)
+        {
+            prw.startEval();
+            for (int i = 0, n = queue.size(); i < n; i++)
+            {
+                prw.write(queue.get(i));
+            }
+            prw.endEval();
+        }
+        else
+        {
+            writer.startElement(HTML.SCRIPT_ELEM, null);
+            HtmlRendererUtils.renderScriptType(facesContext, writer);
+            HtmlRendererUtils.renderNonce(facesContext, writer);
+            for (int i = 0, n = queue.size(); i < n; i++)
+            {
+                writer.write(queue.get(i));
+            }
+            writer.endElement(HTML.SCRIPT_ELEM);
+        }
+    }
+
+    // CHECKSTYLE:OFF (ParameterNumber — mirrors jakarta.faces renderBehaviorizedAttribute overloads)
+    public static boolean renderBehaviorizedAttribute(
+            FacesContext facesContext, ResponseWriter writer,
+            String componentProperty, UIComponent component,
+            String sourceId, String eventName,
+            Collection<ClientBehaviorContext.Parameter> eventParameters,
+            Map<String, List<ClientBehavior>> clientBehaviors,
+            String htmlAttrName, String attributeValue, String serverSideScript) throws IOException
+    {
+
+        List<ClientBehavior> cbl = (clientBehaviors != null) ? clientBehaviors.get(eventName) : null;
+        String targetElementId = sourceId != null ? sourceId : component.getClientId(facesContext);
+        if (((cbl != null) ? cbl.size() : 0) + (attributeValue != null ? 1 : 0)
+                + (serverSideScript != null ? 1 : 0) <= 1)
+        {
+            if (cbl == null || cbl.isEmpty())
+            {
+                if (attributeValue != null)
+                {
+                    return HtmlRendererUtils.renderHTMLStringAttribute(writer, componentProperty, htmlAttrName,
+                            attributeValue);
+                }
+                else
+                {
+                    return HtmlRendererUtils.renderHTMLStringAttribute(writer, componentProperty, htmlAttrName,
+                            serverSideScript);
+                }
+            }
+            else
+            {
+                String script = cbl.get(0).getScript(
+                        ClientBehaviorContext
+                                .createClientBehaviorContext(
+                                        facesContext, component,
+                                        eventName, sourceId,
+                                        eventParameters));
+                if (deferClientBehaviorScriptIfCspNonceActive(facesContext, targetElementId, htmlAttrName, script))
+                {
+                    return true;
+                }
+                return HtmlRendererUtils.renderHTMLStringAttribute(
+                        writer, componentProperty, htmlAttrName,
+                        script);
+            }
+        }
+        else
+        {
+            String chain = ClientBehaviorRendererUtils.buildBehaviorChain(facesContext,
+                    component, sourceId, eventName,
+                    eventParameters, clientBehaviors, attributeValue,
+                    serverSideScript);
+            if (StringUtils.isNotBlank(chain)
+                    && deferClientBehaviorScriptIfCspNonceActive(facesContext, targetElementId, htmlAttrName, chain))
+            {
+                return true;
+            }
+            return HtmlRendererUtils.renderHTMLStringAttribute(writer, componentProperty, htmlAttrName,
+                    chain);
+        }
+    }
+
+    public static boolean renderBehaviorizedAttribute(
+            FacesContext facesContext, ResponseWriter writer,
+            String componentProperty, UIComponent component,
+            String sourceId, String eventName,
+            Collection<ClientBehaviorContext.Parameter> eventParameters,
+            String eventName2,
+            Collection<ClientBehaviorContext.Parameter> eventParameters2,
+            Map<String, List<ClientBehavior>> clientBehaviors,
+            String htmlAttrName, String attributeValue, String serverSideScript) throws IOException
+    {
+        List<ClientBehavior> cb1 = (clientBehaviors != null) ? clientBehaviors.get(eventName) : null;
+        List<ClientBehavior> cb2 = (clientBehaviors != null) ? clientBehaviors.get(eventName2) : null;
+        String targetElementId = sourceId != null ? sourceId : component.getClientId(facesContext);
+        if (((cb1 != null) ? cb1.size() : 0) + ((cb2 != null) ? cb2.size() : 0)
+                + (attributeValue != null ? 1 : 0) <= 1)
+        {
+            if (attributeValue != null)
+            {
+                return HtmlRendererUtils.renderHTMLStringAttribute(writer, componentProperty, htmlAttrName,
+                        attributeValue);
+            }
+            else if (serverSideScript != null)
+            {
+                return HtmlRendererUtils.renderHTMLStringAttribute(writer, componentProperty, htmlAttrName,
+                        serverSideScript);
+            }
+            else if (((cb1 != null) ? cb1.size() : 0) > 0)
+            {
+                String script = cb1.get(0).getScript(ClientBehaviorContext
+                        .createClientBehaviorContext(
+                                facesContext, component,
+                                eventName, sourceId,
+                                eventParameters));
+                if (deferClientBehaviorScriptIfCspNonceActive(facesContext, targetElementId, htmlAttrName, script))
+                {
+                    return true;
+                }
+                return HtmlRendererUtils.renderHTMLStringAttribute(
+                        writer, componentProperty, htmlAttrName,
+                        script);
+            }
+            else
+            {
+                String script = cb2.get(0).getScript(ClientBehaviorContext
+                        .createClientBehaviorContext(
+                                facesContext, component,
+                                eventName2, sourceId,
+                                eventParameters2));
+                if (deferClientBehaviorScriptIfCspNonceActive(facesContext, targetElementId, htmlAttrName, script))
+                {
+                    return true;
+                }
+                return HtmlRendererUtils.renderHTMLStringAttribute(
+                        writer, componentProperty, htmlAttrName,
+                        script);
+            }
+        }
+        else
+        {
+            String chain = ClientBehaviorRendererUtils.buildBehaviorChain(facesContext,
+                    component, sourceId, eventName,
+                    eventParameters, eventName2, eventParameters2,
+                    clientBehaviors, attributeValue, serverSideScript);
+            if (StringUtils.isNotBlank(chain)
+                    && deferClientBehaviorScriptIfCspNonceActive(facesContext, targetElementId, htmlAttrName, chain))
+            {
+                return true;
+            }
+            return HtmlRendererUtils.renderHTMLStringAttribute(writer, componentProperty, htmlAttrName,
+                    chain);
         }
     }
+    // CHECKSTYLE:ON
 
     public static void renderBehaviorizedEventHandlers(
             FacesContext facesContext, ResponseWriter writer,
diff --git a/impl/src/main/java/org/apache/myfaces/renderkit/html/util/HtmlRendererUtils.java b/impl/src/main/java/org/apache/myfaces/renderkit/html/util/HtmlRendererUtils.java
index d78634bb1..65ffc4063 100644
--- a/impl/src/main/java/org/apache/myfaces/renderkit/html/util/HtmlRendererUtils.java
+++ b/impl/src/main/java/org/apache/myfaces/renderkit/html/util/HtmlRendererUtils.java
@@ -1274,31 +1274,9 @@ public static boolean renderBehaviorizedAttribute(
             Map<String, List<ClientBehavior>> clientBehaviors,
             String htmlAttrName, String attributeValue) throws IOException
     {
-
-        List<ClientBehavior> cbl = (clientBehaviors != null) ? clientBehaviors
-                .get(eventName) : null;
-        if (cbl == null || cbl.isEmpty())
-        {
-            return renderHTMLAttribute(writer, componentProperty, htmlAttrName, attributeValue);
-        }
-        if (cbl.size() > 1 || (cbl.size() == 1 && attributeValue != null))
-        {
-            return renderHTMLAttribute(writer, componentProperty, htmlAttrName,
-                    ClientBehaviorRendererUtils.buildBehaviorChain(facesContext,
-                            component, sourceId, eventName,
-                            eventParameters, clientBehaviors, attributeValue,
-                            RendererUtils.EMPTY_STRING));
-        }
-        else
-        {
-            //Only 1 behavior and attrValue == null, so just render it directly
-            return renderHTMLAttribute(
-                    writer, componentProperty, htmlAttrName,
-                    cbl.get(0).getScript(
-                            ClientBehaviorContext.createClientBehaviorContext(
-                                    facesContext, component, eventName,
-                                    sourceId, eventParameters)));
-        }
+        return CommonHtmlEventsUtil.renderBehaviorizedAttribute(facesContext, writer,
+                componentProperty, component, sourceId, eventName, eventParameters,
+                clientBehaviors, htmlAttrName, attributeValue);
     }
 
     /**
@@ -1344,42 +1322,9 @@ public static boolean renderBehaviorizedAttribute(
             String htmlAttrName, String attributeValue, String serverSideScript)
             throws IOException
     {
-
-        List<ClientBehavior> cbl = (clientBehaviors != null) ? clientBehaviors.get(eventName) : null;
-        if (((cbl != null) ? cbl.size() : 0) + (attributeValue != null ? 1 : 0)
-                + (serverSideScript != null ? 1 : 0) <= 1)
-        {
-            if (cbl == null || cbl.isEmpty())
-            {
-                if (attributeValue != null)
-                {
-                    return renderHTMLStringAttribute(writer, componentProperty, htmlAttrName, attributeValue);
-                }
-                else
-                {
-                    return renderHTMLStringAttribute(writer, componentProperty, htmlAttrName, serverSideScript);
-                }
-            }
-            else
-            {
-                return renderHTMLStringAttribute(
-                        writer, componentProperty, htmlAttrName,
-                        cbl.get(0).getScript(
-                                ClientBehaviorContext
-                                        .createClientBehaviorContext(
-                                                facesContext, component,
-                                                eventName, sourceId,
-                                                eventParameters)));
-            }
-        }
-        else
-        {
-            return renderHTMLStringAttribute(writer, componentProperty, htmlAttrName,
-                    ClientBehaviorRendererUtils.buildBehaviorChain(facesContext,
-                            component, sourceId, eventName,
-                            eventParameters, clientBehaviors, attributeValue,
-                            serverSideScript));
-        }
+        return CommonHtmlEventsUtil.renderBehaviorizedAttribute(facesContext, writer,
+                componentProperty, component, sourceId, eventName, eventParameters,
+                clientBehaviors, htmlAttrName, attributeValue, serverSideScript);
     }
 
     public static boolean renderBehaviorizedAttribute(
@@ -1410,48 +1355,9 @@ public static boolean renderBehaviorizedAttribute(
             String htmlAttrName, String attributeValue, String serverSideScript)
             throws IOException
     {
-        List<ClientBehavior> cb1 = (clientBehaviors != null) ? clientBehaviors.get(eventName) : null;
-        List<ClientBehavior> cb2 = (clientBehaviors != null) ? clientBehaviors.get(eventName2) : null;
-        if (((cb1 != null) ? cb1.size() : 0) + ((cb2 != null) ? cb2.size() : 0)
-                + (attributeValue != null ? 1 : 0) <= 1)
-        {
-            if (attributeValue != null)
-            {
-                return renderHTMLStringAttribute(writer, componentProperty, htmlAttrName, attributeValue);
-            }
-            else if (serverSideScript != null)
-            {
-                return renderHTMLStringAttribute(writer, componentProperty, htmlAttrName, serverSideScript);
-            }
-            else if (((cb1 != null) ? cb1.size() : 0) > 0)
-            {
-                return renderHTMLStringAttribute(
-                        writer, componentProperty, htmlAttrName,
-                        cb1.get(0).getScript(ClientBehaviorContext
-                                        .createClientBehaviorContext(
-                                                facesContext, component,
-                                                eventName, sourceId,
-                                                eventParameters)));
-            }
-            else
-            {
-                return renderHTMLStringAttribute(
-                        writer, componentProperty, htmlAttrName,
-                        cb2.get(0).getScript(ClientBehaviorContext
-                                        .createClientBehaviorContext(
-                                                facesContext, component,
-                                                eventName2, sourceId,
-                                                eventParameters2)));
-            }
-        }
-        else
-        {
-            return renderHTMLStringAttribute(writer, componentProperty, htmlAttrName,
-                    ClientBehaviorRendererUtils.buildBehaviorChain(facesContext,
-                            component, sourceId, eventName,
-                            eventParameters, eventName2, eventParameters2,
-                            clientBehaviors, attributeValue, serverSideScript));
-        }
+        return CommonHtmlEventsUtil.renderBehaviorizedAttribute(facesContext, writer,
+                componentProperty, component, sourceId, eventName, eventParameters,
+                eventName2, eventParameters2, clientBehaviors, htmlAttrName, attributeValue, serverSideScript);
     }
     // CHECKSTYLE: ON
 
diff --git a/integration-tests/csp/pom.xml b/integration-tests/csp/pom.xml
new file mode 100644
index 000000000..c2f286052
--- /dev/null
+++ b/integration-tests/csp/pom.xml
@@ -0,0 +1,34 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+
+    <parent>
+        <groupId>org.apache.myfaces.core</groupId>
+        <artifactId>myfaces-integration-tests</artifactId>
+        <version>5.0.0-SNAPSHOT</version>
+        <relativePath>../pom.xml</relativePath>
+    </parent>
+
+    <modelVersion>4.0.0</modelVersion>
+
+    <groupId>org.apache.myfaces.core.integration-tests</groupId>
+    <artifactId>csp</artifactId>
+    <name>Apache MyFaces Core 5.0 - Integration Tests - CSP</name>
+    <packaging>war</packaging>
+
+</project>
diff --git a/integration-tests/csp/src/main/java/org/apache/myfaces/core/integrationtests/csp/TestBean.java b/integration-tests/csp/src/main/java/org/apache/myfaces/core/integrationtests/csp/TestBean.java
new file mode 100644
index 000000000..83ed3b224
--- /dev/null
+++ b/integration-tests/csp/src/main/java/org/apache/myfaces/core/integrationtests/csp/TestBean.java
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.myfaces.core.integrationtests.ajax.basic;
+
+import jakarta.enterprise.context.RequestScoped;
+import jakarta.faces.annotation.FacesConfig;
+import jakarta.inject.Named;
+
+@Named
+@RequestScoped
+@FacesConfig
+public class TestBean
+{
+    private String sayHello = "hello world from bean";
+
+    public String getSayHello()
+    {
+        return sayHello;
+    }
+
+    public void setSayHello(String sayHello)
+    {
+        this.sayHello = sayHello;
+    }
+
+    public void doAction()
+    {
+        sayHello = "Action Performed";
+    }
+}
diff --git a/integration-tests/csp/src/main/webapp/WEB-INF/beans.xml b/integration-tests/csp/src/main/webapp/WEB-INF/beans.xml
new file mode 100644
index 000000000..9bcebd865
--- /dev/null
+++ b/integration-tests/csp/src/main/webapp/WEB-INF/beans.xml
@@ -0,0 +1,25 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+    Licensed to the Apache Software Foundation (ASF) under one
+    or more contributor license agreements.  See the NOTICE file
+    distributed with this work for additional information
+    regarding copyright ownership.  The ASF licenses this file
+    to you under the Apache License, Version 2.0 (the
+    "License"); you may not use this file except in compliance
+    with the License.  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing,
+    software distributed under the License is distributed on an
+    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+    KIND, either express or implied.  See the License for the
+    specific language governing permissions and limitations
+    under the License.
+-->
+<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+       xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
+       version="1.2" bean-discovery-mode="all">
+
+</beans>
\ No newline at end of file
diff --git a/integration-tests/csp/src/main/webapp/WEB-INF/web.xml b/integration-tests/csp/src/main/webapp/WEB-INF/web.xml
new file mode 100644
index 000000000..19b579425
--- /dev/null
+++ b/integration-tests/csp/src/main/webapp/WEB-INF/web.xml
@@ -0,0 +1,56 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+    Licensed to the Apache Software Foundation (ASF) under one
+    or more contributor license agreements.  See the NOTICE file
+    distributed with this work for additional information
+    regarding copyright ownership.  The ASF licenses this file
+    to you under the Apache License, Version 2.0 (the
+    "License"); you may not use this file except in compliance
+    with the License.  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing,
+    software distributed under the License is distributed on an
+    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+    KIND, either express or implied.  See the License for the
+    specific language governing permissions and limitations
+    under the License.
+-->
+<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
+         version="3.1">
+
+    <context-param>
+        <param-name>jakarta.faces.PROJECT_STAGE</param-name>
+        <param-value>Production</param-value>
+    </context-param>
+    <context-param>
+        <param-name>org.apache.myfaces.LOG_WEB_CONTEXT_PARAMS</param-name>
+        <param-value>false</param-value>
+    </context-param>
+    <context-param>
+        <param-name>org.apache.myfaces.SUPPORT_JSP_AND_FACES_EL</param-name>
+        <param-value>false</param-value>
+    </context-param>
+    <context-param>
+        <param-name>jakarta.faces.ENABLE_CSP_NONCE</param-name>
+        <param-value>true</param-value>
+    </context-param>
+
+    <listener>
+        <listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
+    </listener>
+    <listener>
+        <listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class>
+    </listener>
+
+    <welcome-file-list>
+        <welcome-file>index.jsf</welcome-file>
+        <welcome-file>index.xhtml</welcome-file>
+    </welcome-file-list>
+    <session-config>
+        <tracking-mode>COOKIE</tracking-mode>
+    </session-config>
+</web-app>
diff --git a/integration-tests/csp/src/main/webapp/index.xhtml b/integration-tests/csp/src/main/webapp/index.xhtml
new file mode 100644
index 000000000..ce3ce7ae1
--- /dev/null
+++ b/integration-tests/csp/src/main/webapp/index.xhtml
@@ -0,0 +1,43 @@
+<!--
+    Licensed to the Apache Software Foundation (ASF) under one
+    or more contributor license agreements.  See the NOTICE file
+    distributed with this work for additional information
+    regarding copyright ownership.  The ASF licenses this file
+    to you under the Apache License, Version 2.0 (the
+    "License"); you may not use this file except in compliance
+    with the License.  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing,
+    software distributed under the License is distributed on an
+    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+    KIND, either express or implied.  See the License for the
+    specific language governing permissions and limitations
+    under the License.
+-->
+<!DOCTYPE html>
+<html xmlns="http://www.w3.org/1999/xhtml"
+      xmlns:h="http://xmlns.jcp.org/jsf/html"
+      xmlns:pt="http://xmlns.jcp.org/jsf/passthrough"
+      xmlns:f="http://xmlns.jcp.org/jsf/core"
+      xmlns:jsf="http://xmlns.jcp.org/jsf"
+      xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
+
+    <h:head>
+        <title>test</title>
+        <h:outputScript library="jakarta.faces" name="faces.js" />
+    </h:head>
+
+    <h:body>
+        <h:form id="mainForm">
+            #{testBean.sayHello}
+
+            <h:commandLink id="press" action="#{testBean.doAction}" styleClass="press">Press Me
+                <f:ajax execute="@this" render="@form"></f:ajax>
+            </h:commandLink>
+        </h:form>
+    </h:body>
+
+</html>
+
diff --git a/integration-tests/pom.xml b/integration-tests/pom.xml
index 32246f15a..bb2ea138a 100644
--- a/integration-tests/pom.xml
+++ b/integration-tests/pom.xml
@@ -224,6 +224,7 @@
         <module>ajax</module>
         <module>automaticExtensionlessMapping</module>
         <module>myfaces4623</module>
+        <module>csp</module>
     </modules>
 
     <profiles>