Skip to content

Commit c104ec5

Browse files
authored
Merge pull request #369 from sebastianfrey/SHIRO-887-do-not-trim-password-strings
[SHIRO-887] Do not trim passwords in FormAuthenticationFilter
2 parents 404dbf5 + e47feeb commit c104ec5

File tree

4 files changed

+75
-5
lines changed

4 files changed

+75
-5
lines changed

lang/src/main/java/org/apache/shiro/util/StringUtils.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public static boolean startsWithIgnoreCase(String str, String prefix) {
130130
* <p/>
131131
* <ol>
132132
* <li>If the specified <code>String</code> is <code>null</code>, return <code>null</code></li>
133-
* <li>If not <code>null</code>, {@link String#trim() trim()} it.</li>
133+
* <li>If not <code>null</code>, {@link String#trim() trim()} it, when the trim param is set to <code>true</code>.</li>
134134
* <li>If the trimmed string is equal to the empty String (i.e. &quot;&quot;), return <code>null</code></li>
135135
* <li>If the trimmed string is not the empty string, return the trimmed version</li>.
136136
* </ol>
@@ -139,13 +139,16 @@ public static boolean startsWithIgnoreCase(String str, String prefix) {
139139
* is returned.
140140
*
141141
* @param in the input String to clean.
142+
* @param trim specifies whether the input String should be trimmed or not
142143
* @return a populated-but-trimmed String or <code>null</code> otherwise
143144
*/
144-
public static String clean(String in) {
145+
public static String clean(String in, boolean trim) {
145146
String out = in;
146147

147148
if (in != null) {
148-
out = in.trim();
149+
if (trim) {
150+
out = in.trim();
151+
}
149152
if (out.equals(EMPTY_STRING)) {
150153
out = null;
151154
}
@@ -154,6 +157,26 @@ public static String clean(String in) {
154157
return out;
155158
}
156159

160+
/**
161+
* Returns a 'cleaned' representation of the specified argument. 'Cleaned' is defined as the following:
162+
* <p/>
163+
* <ol>
164+
* <li>If the specified <code>String</code> is <code>null</code>, return <code>null</code></li>
165+
* <li>If not <code>null</code>, {@link String#trim() trim()} it.</li>
166+
* <li>If the trimmed string is equal to the empty String (i.e. &quot;&quot;), return <code>null</code></li>
167+
* <li>If the trimmed string is not the empty string, return the trimmed version</li>.
168+
* </ol>
169+
* <p/>
170+
* Therefore this method always ensures that any given string has trimmed text, and if it doesn't, <code>null</code>
171+
* is returned.
172+
*
173+
* @param in the input String to clean.
174+
* @return a populated-but-trimmed String or <code>null</code> otherwise
175+
*/
176+
public static String clean(String in) {
177+
return clean(in, true);
178+
}
179+
157180
/**
158181
* Returns the specified array as a comma-delimited (',') string.
159182
*
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.shiro.lang.util;
20+
21+
import org.apache.shiro.util.StringUtils;
22+
import org.junit.Test;
23+
24+
import static org.junit.Assert.assertEquals;
25+
26+
public class StringUtilsTest {
27+
28+
@Test
29+
public void testClean() {
30+
assertEquals(StringUtils.clean(" abc "), "abc");
31+
assertEquals(StringUtils.clean(" abc ", true), "abc");
32+
assertEquals(StringUtils.clean(" abc ", false), " abc ");
33+
}
34+
}

web/src/main/java/org/apache/shiro/web/filter/authc/FormAuthenticationFilter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ protected String getUsername(ServletRequest request) {
220220
}
221221

222222
protected String getPassword(ServletRequest request) {
223-
return WebUtils.getCleanParam(request, getPasswordParam());
223+
return WebUtils.getCleanParam(request, getPasswordParam(), false);
224224
}
225225

226226

web/src/main/java/org/apache/shiro/web/util/WebUtils.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,20 @@ public static boolean isTrue(ServletRequest request, String paramName) {
616616
* @return the clean param value, or null if the param does not exist or is empty.
617617
*/
618618
public static String getCleanParam(ServletRequest request, String paramName) {
619-
return StringUtils.clean(request.getParameter(paramName));
619+
return getCleanParam(request, paramName, true);
620+
}
621+
622+
/**
623+
* Convenience method that returns a request parameter value, first running it through
624+
* {@link StringUtils#clean(String)}.
625+
*
626+
* @param request the servlet request.
627+
* @param paramName the parameter name.
628+
* @param trim specifies whether the parameter value should be trimmed or not
629+
* @return the clean param value, or null if the param does not exist or is empty.
630+
*/
631+
public static String getCleanParam(ServletRequest request, String paramName, boolean trim) {
632+
return StringUtils.clean(request.getParameter(paramName), trim);
620633
}
621634

622635
public static void saveRequest(ServletRequest request) {

0 commit comments

Comments
 (0)