This is an automated email from the ASF dual-hosted git repository.
markt-asf pushed a commit to branch 9.0.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git
The following commit(s) were added to refs/heads/9.0.x by this push:
new 4a5f5a1eaa Strip space around user names
4a5f5a1eaa is described below
commit 4a5f5a1eaaa4f644f206ff10fa201a75074a2b15
Author: Mark Thomas <[email protected]>
AuthorDate: Wed Oct 28 16:05:07 2026 +0000
Strip space around user names
---
java/org/apache/catalina/realm/LockOutRealm.java | 86 ++++++++++++++++++++++--
webapps/docs/changelog.xml | 4 ++
webapps/docs/config/realm.xml | 14 ++++
3 files changed, 98 insertions(+), 6 deletions(-)
diff --git a/java/org/apache/catalina/realm/LockOutRealm.java
b/java/org/apache/catalina/realm/LockOutRealm.java
index 90d3613759..40c4e04cb0 100644
--- a/java/org/apache/catalina/realm/LockOutRealm.java
+++ b/java/org/apache/catalina/realm/LockOutRealm.java
@@ -83,6 +83,15 @@ public class LockOutRealm extends CombinedRealm {
*/
private boolean caseSensitive = false;
+ /*
+ * Should user names (the keys) in the failedUsers Map have leading space
removed. The default is true.
+ */
+ private boolean stripLeadingSpace = true;
+
+ /*
+ * Should user names (the keys) in the failedUsers Map have trailing space
removed. The default is true.
+ */
+ private boolean stripTrailingSpace = true;
@Override
protected void startInternal() throws LifecycleException {
@@ -356,9 +365,9 @@ public class LockOutRealm extends CombinedRealm {
/**
- * Get the case sensitivity flag for username matching.
+ * Get the case sensitivity flag for user name matching.
*
- * @return the caseSensitive
+ * @return the current caseSensitive value
*/
public boolean getCaseSensitive() {
return caseSensitive;
@@ -366,7 +375,7 @@ public class LockOutRealm extends CombinedRealm {
/**
- * Set the case sensitivity flag for username matching.
+ * Set the case sensitivity flag for user name matching.
*
* @param caseSensitive the caseSensitive to set
*/
@@ -375,11 +384,76 @@ public class LockOutRealm extends CombinedRealm {
}
+ /**
+ * Get the case strip leading space flag for user name matching.
+ *
+ * @return the current stripLeadingSpace value
+ */
+ public boolean getStripLeadingSpace() {
+ return stripLeadingSpace;
+ }
+
+
+ /**
+ * Set the strip leading space flag for user name matching.
+ *
+ * @param stripLeadingSpace the stripLeadingSpace to set
+ */
+ public void setStripLeadingSpace(boolean stripLeadingSpace) {
+ this.stripLeadingSpace = stripLeadingSpace;
+ }
+
+
+ /**
+ * Get the case strip trailing space flag for user name matching.
+ *
+ * @return the current stripTrailingSpace value
+ */
+ public boolean getStripTrailingSpace() {
+ return stripTrailingSpace;
+ }
+
+
+ /**
+ * Set the strip trailing space flag for user name matching.
+ *
+ * @param stripTrailingSpace the stripTrailingSpace to set
+ */
+ public void setStripTrailingSpace(boolean stripTrailingSpace) {
+ this.stripTrailingSpace = stripTrailingSpace;
+ }
+
+
private String normalizeUsername(String username) {
- if (username != null && !getCaseSensitive()) {
- return username.toLowerCase(Locale.ROOT);
+ if (username == null) {
+ return null;
+ }
+ if (!getCaseSensitive()) {
+ username = username.toLowerCase(Locale.ROOT);
+ }
+
+ // String.strip() only covers Character.isWhitespace().
Character.isSpaceChar() is also needed here.
+ int start = 0;
+ int end = username.length();
+ if (getStripLeadingSpace()) {
+ while (start < end) {
+ char c = username.charAt(start);
+ if (!Character.isSpaceChar(c) && !Character.isWhitespace(c)) {
+ break;
+ }
+ start++;
+ }
+ }
+ if (getStripTrailingSpace()) {
+ while (end > start) {
+ char c = username.charAt(end - 1);
+ if (!Character.isSpaceChar(c) && !Character.isWhitespace(c)) {
+ break;
+ }
+ end--;
+ }
}
- return username;
+ return username.substring(start, end);
}
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index f08eb525ed..339217b48a 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -323,6 +323,10 @@
Do not depend on the content length being defined when processing
request bodies with the CGI Servlet. (csutherl)
</fix>
+ <fix>
+ Align LockoutRealm with some Realm implementations and ignore leading
+ and trailing space in user names by default. (markt)
+ </fix>
</changelog>
</subsection>
<subsection name="Coyote">
diff --git a/webapps/docs/config/realm.xml b/webapps/docs/config/realm.xml
index 9d03008e59..117089f5fd 100644
--- a/webapps/docs/config/realm.xml
+++ b/webapps/docs/config/realm.xml
@@ -1032,6 +1032,20 @@
will also not reset the lock out time.</p>
</attribute>
+ <attribute name="stripLeadingSpace" required="false">
+ <p>Will leading space (<code>Character.isWhitespace()</code> or
+ <code>Character.isSpaceChar()</code> returns <code>true</code>) be
+ removed from user names when making locking decisions. Defaults to
+ <code>true</code>, meaning leading space will be removed.</p>
+ </attribute>
+
+ <attribute name="stripTrailingSpace" required="false">
+ <p>Will trailing space (<code>Character.isWhitespace()</code> or
+ <code>Character.isSpaceChar()</code> returns <code>true</code>) be
+ removed from user names when making locking decisions. Defaults to
+ <code>true</code>, meaning leading space will be removed.</p>
+ </attribute>
+
<attribute name="transportGuaranteeRedirectStatus" required="false">
<p>The HTTP status code to use when the container needs to issue an
HTTP
redirect to meet the requirements of a configured transport
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]