|
| 1 | +/* |
| 2 | + * Copyright 2026, Google Inc. All rights reserved. |
| 3 | + * |
| 4 | + * Redistribution and use in source and binary forms, with or without |
| 5 | + * modification, are permitted provided that the following conditions are |
| 6 | + * met: |
| 7 | + * |
| 8 | + * * Redistributions of source code must retain the above copyright |
| 9 | + * notice, this list of conditions and the following disclaimer. |
| 10 | + * * Redistributions in binary form must reproduce the above |
| 11 | + * copyright notice, this list of conditions and the following disclaimer |
| 12 | + * in the documentation and/or other materials provided with the |
| 13 | + * distribution. |
| 14 | + * |
| 15 | + * * Neither the name of Google Inc. nor the names of its |
| 16 | + * contributors may be used to endorse or promote products derived from |
| 17 | + * this software without specific prior written permission. |
| 18 | + * |
| 19 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | + */ |
| 31 | + |
| 32 | +package com.google.auth.appengine; |
| 33 | + |
| 34 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 35 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 36 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 37 | + |
| 38 | +import java.io.ByteArrayInputStream; |
| 39 | +import java.io.ByteArrayOutputStream; |
| 40 | +import java.io.IOException; |
| 41 | +import java.io.ObjectInputStream; |
| 42 | +import java.io.ObjectOutputStream; |
| 43 | +import java.lang.reflect.Field; |
| 44 | +import java.util.Collections; |
| 45 | +import org.junit.jupiter.api.Test; |
| 46 | + |
| 47 | +class AppEngineDeserializationSecurityTest { |
| 48 | + |
| 49 | + /** A class that does not implement HttpTransportFactory. */ |
| 50 | + static class ArbitraryClass {} |
| 51 | + |
| 52 | + @Test |
| 53 | + void testArbitraryClassInstantiationPrevented() throws Exception { |
| 54 | + AppEngineCredentials credentials = |
| 55 | + AppEngineCredentials.newBuilder().setScopes(Collections.singleton("scope")).build(); |
| 56 | + |
| 57 | + // Use reflection to set appIdentityServiceClassName to ArbitraryClass |
| 58 | + // as the setter must be of AppIdentityService |
| 59 | + Field classNameField = |
| 60 | + AppEngineCredentials.class.getDeclaredField("appIdentityServiceClassName"); |
| 61 | + classNameField.setAccessible(true); |
| 62 | + classNameField.set(credentials, ArbitraryClass.class.getName()); |
| 63 | + |
| 64 | + ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
| 65 | + ObjectOutputStream oos = new ObjectOutputStream(bos); |
| 66 | + oos.writeObject(credentials); |
| 67 | + |
| 68 | + ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); |
| 69 | + ObjectInputStream ois = new ObjectInputStream(bis); |
| 70 | + |
| 71 | + assertThrows(IOException.class, ois::readObject); |
| 72 | + |
| 73 | + bos.close(); |
| 74 | + oos.close(); |
| 75 | + bis.close(); |
| 76 | + ois.close(); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + void testValidServiceDeserialization() throws Exception { |
| 81 | + MockAppIdentityService mockService = new MockAppIdentityService(); |
| 82 | + AppEngineCredentials credentials = |
| 83 | + AppEngineCredentials.newBuilder() |
| 84 | + .setScopes(Collections.singleton("scope")) |
| 85 | + .setAppIdentityService(mockService) |
| 86 | + .build(); |
| 87 | + |
| 88 | + ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
| 89 | + ObjectOutputStream oos = new ObjectOutputStream(bos); |
| 90 | + oos.writeObject(credentials); |
| 91 | + oos.close(); |
| 92 | + |
| 93 | + ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); |
| 94 | + ObjectInputStream ois = new ObjectInputStream(bis); |
| 95 | + |
| 96 | + AppEngineCredentials deserialized = (AppEngineCredentials) ois.readObject(); |
| 97 | + |
| 98 | + assertNotNull(deserialized); |
| 99 | + Field serviceField = AppEngineCredentials.class.getDeclaredField("appIdentityService"); |
| 100 | + serviceField.setAccessible(true); |
| 101 | + Object service = serviceField.get(deserialized); |
| 102 | + assertEquals(MockAppIdentityService.class, service.getClass()); |
| 103 | + |
| 104 | + bos.close(); |
| 105 | + oos.close(); |
| 106 | + bis.close(); |
| 107 | + ois.close(); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + void testNonExistentClassDeserialization() throws Exception { |
| 112 | + AppEngineCredentials credentials = |
| 113 | + AppEngineCredentials.newBuilder().setScopes(Collections.singleton("scope")).build(); |
| 114 | + |
| 115 | + // 2. Use reflection to set appIdentityServiceClassName to non-existent class |
| 116 | + Field classNameField = |
| 117 | + AppEngineCredentials.class.getDeclaredField("appIdentityServiceClassName"); |
| 118 | + classNameField.setAccessible(true); |
| 119 | + classNameField.set(credentials, "com.google.nonexistent.Class"); |
| 120 | + |
| 121 | + ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
| 122 | + ObjectOutputStream oos = new ObjectOutputStream(bos); |
| 123 | + oos.writeObject(credentials); |
| 124 | + oos.close(); |
| 125 | + |
| 126 | + ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); |
| 127 | + ObjectInputStream ois = new ObjectInputStream(bis); |
| 128 | + |
| 129 | + assertThrows(ClassNotFoundException.class, ois::readObject); |
| 130 | + |
| 131 | + bos.close(); |
| 132 | + oos.close(); |
| 133 | + bis.close(); |
| 134 | + ois.close(); |
| 135 | + } |
| 136 | +} |
0 commit comments