|
| 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.mgt; |
| 20 | + |
| 21 | +import org.apache.shiro.lang.io.DefaultSerializer; |
| 22 | +import org.apache.shiro.lang.io.Serializer; |
| 23 | +import org.apache.shiro.subject.PrincipalCollection; |
| 24 | +import org.apache.shiro.subject.SimplePrincipalCollection; |
| 25 | +import org.apache.shiro.subject.Subject; |
| 26 | +import org.apache.shiro.subject.SubjectContext; |
| 27 | +import org.apache.shiro.subject.support.DefaultSubjectContext; |
| 28 | +import org.junit.jupiter.api.Test; |
| 29 | + |
| 30 | +import java.io.ByteArrayOutputStream; |
| 31 | +import java.io.IOException; |
| 32 | +import java.io.InvalidClassException; |
| 33 | +import java.io.ObjectInputFilter; |
| 34 | +import java.io.ObjectOutputStream; |
| 35 | +import java.io.Serializable; |
| 36 | +import java.util.ArrayList; |
| 37 | +import java.util.List; |
| 38 | + |
| 39 | +import static org.assertj.core.api.Assertions.assertThat; |
| 40 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 41 | + |
| 42 | +/** |
| 43 | + * Test cases proving {@link AbstractRememberMeManager}'s RememberMe cookie deserialization path is |
| 44 | + * protected by a <a href="https://openjdk.org/jeps/290">JEP-290</a> {@link ObjectInputFilter} by default, |
| 45 | + * without breaking legitimate {@link PrincipalCollection} round-tripping. |
| 46 | + */ |
| 47 | +class AbstractRememberMeManagerObjectInputFilterTest { |
| 48 | + |
| 49 | + /** Deeper than the default filter's {@code maxdepth=30}, to trigger a resource-limit rejection. */ |
| 50 | + private static final int DEEP_CHAIN_LENGTH = 60; |
| 51 | + |
| 52 | + @Test |
| 53 | + void testLegitimatePrincipalsRoundTripUnderDefaultFilter() { |
| 54 | + InMemoryRememberMeManager rmm = new InMemoryRememberMeManager(); |
| 55 | + PrincipalCollection principals = new SimplePrincipalCollection("joecool", "myRealm"); |
| 56 | + |
| 57 | + rmm.rememberIdentity(null, principals); |
| 58 | + PrincipalCollection remembered = rmm.getRememberedPrincipals(new DefaultSubjectContext()); |
| 59 | + |
| 60 | + assertThat(remembered).isNotNull(); |
| 61 | + assertThat(remembered.getPrimaryPrincipal()).isEqualTo("joecool"); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + void testDefaultFilterRejectsOversizedPayloadBeforeFullConstruction() { |
| 66 | + // The default filter (see AbstractRememberMeManager.DEFAULT_OBJECT_INPUT_FILTER_PATTERN) bounds the |
| 67 | + // object graph depth/array size/reference count/byte count of the deserialized payload. This is |
| 68 | + // denial-of-service-shaped-payload hardening: it does not restrict classes and does not stop RCE |
| 69 | + // gadget chains (which are shallow and small). Feed an oversized/deeply nested payload that exceeds |
| 70 | + // maxdepth, encrypted the way a real RememberMe cookie is, and confirm getRememberedPrincipals() fails |
| 71 | + // closed with a JEP-290 filter rejection (InvalidClassException) before the graph is materialized. The |
| 72 | + // InvalidClassException cause is the discriminating assertion: without the filter this same payload |
| 73 | + // deserializes fully and fails only later with an unrelated ClassCastException, so asserting the cause |
| 74 | + // is what proves the filter itself fired. |
| 75 | + InMemoryRememberMeManager rmm = new InMemoryRememberMeManager(); |
| 76 | + |
| 77 | + List<Object> deepChain = new ArrayList<>(); |
| 78 | + List<Object> cursor = deepChain; |
| 79 | + for (int i = 0; i < DEEP_CHAIN_LENGTH; i++) { |
| 80 | + List<Object> next = new ArrayList<>(); |
| 81 | + cursor.add(next); |
| 82 | + cursor = next; |
| 83 | + } |
| 84 | + |
| 85 | + byte[] serialized = plainJdkSerialize(deepChain); |
| 86 | + byte[] encrypted = rmm.encryptForTest(serialized); |
| 87 | + rmm.injectRawSerializedIdentity(encrypted); |
| 88 | + |
| 89 | + assertThatThrownBy(() -> rmm.getRememberedPrincipals(new DefaultSubjectContext())) |
| 90 | + .isInstanceOf(RuntimeException.class) |
| 91 | + .hasCauseInstanceOf(InvalidClassException.class); |
| 92 | + // onRememberedPrincipalFailure must have run its "forget" cleanup path. |
| 93 | + assertThat(rmm.forgetCount).isEqualTo(1); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + @SuppressWarnings("unchecked") |
| 98 | + void testCustomStricterAllowListFilterCanBeConfigured() { |
| 99 | + // Documented override path (see AbstractRememberMeManager#getSerializer javadoc): cast the default |
| 100 | + // DefaultSerializer and replace its filter with a strict class allow-list. Only SimplePrincipalCollection, |
| 101 | + // AbstractRememberMeManager.RememberedIdentity, and JDK collection/primitive/java.time plumbing are let |
| 102 | + // through. Note: java.time types (e.g. Instant) don't serialize themselves directly - they writeReplace() |
| 103 | + // to an internal java.time serialization proxy class, which is what actually appears in the stream. |
| 104 | + InMemoryRememberMeManager rmm = new InMemoryRememberMeManager(); |
| 105 | + ((DefaultSerializer<AbstractRememberMeManager.RememberedIdentity>) rmm.getSerializer()) |
| 106 | + .setObjectInputFilter(ObjectInputFilter.Config.createFilter( |
| 107 | + "org.apache.shiro.subject.SimplePrincipalCollection;" |
| 108 | + + "org.apache.shiro.mgt.AbstractRememberMeManager$RememberedIdentity;" |
| 109 | + + "java.time.*;java.util.*;java.lang.*;!*")); |
| 110 | + |
| 111 | + PrincipalCollection principals = new SimplePrincipalCollection("joecool", "myRealm"); |
| 112 | + rmm.rememberIdentity(null, principals); |
| 113 | + PrincipalCollection remembered = rmm.getRememberedPrincipals(new DefaultSubjectContext()); |
| 114 | + assertThat(remembered.getPrimaryPrincipal()).isEqualTo("joecool"); |
| 115 | + |
| 116 | + // A disallowed class must now be rejected outright (not merely resource-limited). |
| 117 | + byte[] disallowed = plainJdkSerialize(new NotAllowlisted()); |
| 118 | + rmm.injectRawSerializedIdentity(rmm.encryptForTest(disallowed)); |
| 119 | + |
| 120 | + assertThatThrownBy(() -> rmm.getRememberedPrincipals(new DefaultSubjectContext())) |
| 121 | + .isInstanceOf(RuntimeException.class); |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + void testCustomSerializerIsUnaffectedByDefaultFilterMachinery() { |
| 126 | + // A caller-supplied Serializer implementation (not a DefaultSerializer) must keep working exactly as |
| 127 | + // before this feature existed - AbstractRememberMeManager only touches the filter on its own default |
| 128 | + // DefaultSerializer instance, never on a replaced Serializer. |
| 129 | + InMemoryRememberMeManager rmm = new InMemoryRememberMeManager(); |
| 130 | + rmm.setSerializer(new Serializer<AbstractRememberMeManager.RememberedIdentity>() { |
| 131 | + @Override |
| 132 | + public byte[] serialize(AbstractRememberMeManager.RememberedIdentity o) { |
| 133 | + return plainJdkSerialize(o); |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + @SuppressWarnings("unchecked") |
| 138 | + public AbstractRememberMeManager.RememberedIdentity deserialize(byte[] serialized) { |
| 139 | + try (var ois = new java.io.ObjectInputStream(new java.io.ByteArrayInputStream(serialized))) { |
| 140 | + return (AbstractRememberMeManager.RememberedIdentity) ois.readObject(); |
| 141 | + } catch (IOException | ClassNotFoundException e) { |
| 142 | + throw new RuntimeException(e); |
| 143 | + } |
| 144 | + } |
| 145 | + }); |
| 146 | + |
| 147 | + PrincipalCollection principals = new SimplePrincipalCollection("joecool", "myRealm"); |
| 148 | + rmm.rememberIdentity(null, principals); |
| 149 | + PrincipalCollection remembered = rmm.getRememberedPrincipals(new DefaultSubjectContext()); |
| 150 | + |
| 151 | + assertThat(remembered.getPrimaryPrincipal()).isEqualTo("joecool"); |
| 152 | + } |
| 153 | + |
| 154 | + private static byte[] plainJdkSerialize(Object o) { |
| 155 | + try { |
| 156 | + ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 157 | + try (ObjectOutputStream oos = new ObjectOutputStream(baos)) { |
| 158 | + oos.writeObject(o); |
| 159 | + } |
| 160 | + return baos.toByteArray(); |
| 161 | + } catch (IOException e) { |
| 162 | + throw new RuntimeException(e); |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + public static class NotAllowlisted implements Serializable { |
| 167 | + private static final long serialVersionUID = 1L; |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * Minimal in-memory RememberMeManager test double: stores the "persisted" (encrypted+serialized) bytes |
| 172 | + * in a field instead of a cookie, and tracks how many times identity was forgotten. |
| 173 | + */ |
| 174 | + private static final class InMemoryRememberMeManager extends AbstractRememberMeManager { |
| 175 | + private byte[] stored; |
| 176 | + private int forgetCount; |
| 177 | + |
| 178 | + @Override |
| 179 | + protected void forgetIdentity(Subject subject) { |
| 180 | + stored = null; |
| 181 | + forgetCount++; |
| 182 | + } |
| 183 | + |
| 184 | + public void forgetIdentity(SubjectContext subjectContext) { |
| 185 | + stored = null; |
| 186 | + forgetCount++; |
| 187 | + } |
| 188 | + |
| 189 | + @Override |
| 190 | + protected void rememberSerializedIdentity(Subject subject, byte[] serialized) { |
| 191 | + this.stored = serialized; |
| 192 | + } |
| 193 | + |
| 194 | + @Override |
| 195 | + protected byte[] getRememberedSerializedIdentity(SubjectContext subjectContext) { |
| 196 | + return stored; |
| 197 | + } |
| 198 | + |
| 199 | + void injectRawSerializedIdentity(byte[] raw) { |
| 200 | + this.stored = raw; |
| 201 | + } |
| 202 | + |
| 203 | + byte[] encryptForTest(byte[] plain) { |
| 204 | + return encrypt(plain); |
| 205 | + } |
| 206 | + } |
| 207 | +} |
0 commit comments