From c2f7ce21c3fb12caefee87c517a8bb4f80700044 Mon Sep 17 00:00:00 2001 From: Mark Thomas Date: Tue, 3 Dec 2024 17:45:03 +0000 Subject: [PATCH] Limit to 10 attributes. Add option to delete attribute. Origin: https://github.com/apache/tomcat/commit/c2f7ce21c3fb12caefee87c517a8bb4f80700044 --- webapps/docs/changelog.xml | 5 ++ .../examples/jsp/security/protected/index.jsp | 49 ++++++++++++++++--- 2 files changed, 46 insertions(+), 8 deletions(-) diff --git a/webapps/examples/jsp/security/protected/index.jsp b/webapps/examples/jsp/security/protected/index.jsp index 09c23e721910..987a30fd1878 100644 --- a/webapps/examples/jsp/security/protected/index.jsp +++ b/webapps/examples/jsp/security/protected/index.jsp @@ -14,8 +14,10 @@ See the License for the specific language governing permissions and limitations under the License. --%> -<%@ page import="java.util.Enumeration" %> +<%@ page import="java.net.URLEncoder" %> +<%@ page import="java.nio.charset.StandardCharsets" %> <%@ page import="java.security.Principal" %> +<%@ page import="java.util.Enumeration" %> <%@ page import="org.apache.catalina.TomcatPrincipal" %> <% if (request.getParameter("logoff") != null) { @@ -121,31 +123,62 @@ enter it here: %>

+<% + // Count the existing attributes + int sessionAttributeCount = 0; + Enumeration names = session.getAttributeNames(); + while (names.hasMoreElements()) { + names.nextElement(); + sessionAttributeCount++; + } + + String dataName = request.getParameter("dataName"); + String dataValue = request.getParameter("dataValue"); + if (dataName != null) { + if (dataValue == null) { + session.removeAttribute(dataName); + sessionAttributeCount--; + } else if (sessionAttributeCount < 10) { + session.setAttribute(dataName, dataValue); + sessionAttributeCount++; + } else { +%> +

Session attribute [<%= util.HTMLFilter.filter(dataName) %>] not added as there are already 10 attributes in the +session. Delete an attribute before adding another.

+<% + } + } + + if (sessionAttributeCount < 10) { +%> To add some data to the authenticated session, enter it here:
-

- <% - String dataName = request.getParameter("dataName"); - if (dataName != null) { - session.setAttribute(dataName, request.getParameter("dataValue")); + } else { +%> +

You may not add more than 10 attributes to this session.

+<% } %> +

+

The authenticated session contains the following attributes:

<% - Enumeration names = session.getAttributeNames(); + names = session.getAttributeNames(); while (names.hasMoreElements()) { String name = names.nextElement(); + String value = session.getAttribute(name).toString(); %> - + + <% }
NameValue
<%= util.HTMLFilter.filter(name) %><%= util.HTMLFilter.filter(String.valueOf(session.getAttribute(name))) %><%= util.HTMLFilter.filter(value) %>delete