File tree Expand file tree Collapse file tree 2 files changed +42
-2
lines changed
main/java/org/apache/shiro/web/mgt
test/java/org/apache/shiro/web/mgt Expand file tree Collapse file tree 2 files changed +42
-2
lines changed Original file line number Diff line number Diff line change @@ -212,9 +212,21 @@ protected byte[] getRememberedSerializedIdentity(SubjectContext subjectContext)
212
212
if (log .isTraceEnabled ()) {
213
213
log .trace ("Acquired Base64 encoded identity [" + base64 + "]" );
214
214
}
215
- byte [] decoded = Base64 .decode (base64 );
215
+ byte [] decoded ;
216
+ try {
217
+ decoded = Base64 .decode (base64 );
218
+ } catch (RuntimeException rtEx ) {
219
+ /*
220
+ * https://issues.apache.org/jira/browse/SHIRO-766:
221
+ * If the base64 string cannot be decoded, just assume there is no valid cookie value.
222
+ * */
223
+ getCookie ().removeFrom (request , response );
224
+ log .warn ("Unable to decode existing base64 encoded entity: [" + base64 + "]." , rtEx );
225
+ return null ;
226
+ }
227
+
216
228
if (log .isTraceEnabled ()) {
217
- log .trace ("Base64 decoded byte array length: " + ( decoded != null ? decoded .length : 0 ) + " bytes." );
229
+ log .trace ("Base64 decoded byte array length: " + decoded .length + " bytes." );
218
230
}
219
231
return decoded ;
220
232
} else {
Original file line number Diff line number Diff line change 35
35
import javax .servlet .http .HttpServletRequest ;
36
36
import javax .servlet .http .HttpServletResponse ;
37
37
38
+ import java .util .UUID ;
39
+
38
40
import static org .easymock .EasyMock .*;
39
41
import static org .junit .Assert .*;
40
42
@@ -244,4 +246,30 @@ public void onLogout() {
244
246
verify (mockResponse );
245
247
verify (cookie );
246
248
}
249
+
250
+ @ Test
251
+ public void shouldIgnoreInvalidCookieValues () {
252
+ // given
253
+ HttpServletRequest mockRequest = createMock (HttpServletRequest .class );
254
+ HttpServletResponse mockResponse = createMock (HttpServletResponse .class );
255
+ WebSubjectContext context = new DefaultWebSubjectContext ();
256
+ context .setServletRequest (mockRequest );
257
+ context .setServletResponse (mockResponse );
258
+
259
+ CookieRememberMeManager mgr = new CookieRememberMeManager ();
260
+ Cookie [] cookies = new Cookie []{
261
+ new Cookie (CookieRememberMeManager .DEFAULT_REMEMBER_ME_COOKIE_NAME , UUID .randomUUID ().toString () + "%%ldapRealm" )
262
+ };
263
+
264
+ expect (mockRequest .getAttribute (ShiroHttpServletRequest .IDENTITY_REMOVED_KEY )).andReturn (null );
265
+ expect (mockRequest .getContextPath ()).andReturn (null );
266
+ expect (mockRequest .getCookies ()).andReturn (cookies );
267
+ replay (mockRequest );
268
+
269
+ // when
270
+ final byte [] rememberedSerializedIdentity = mgr .getRememberedSerializedIdentity (context );
271
+
272
+ // then
273
+ assertNull ("should ignore invalid cookie values" , rememberedSerializedIdentity );
274
+ }
247
275
}
You can’t perform that action at this time.
0 commit comments