Skip to content

Commit 456759e

Browse files
authored
Merge pull request #1279 from microsoft/littleaj/fix-object-deserialization
Object deserialization improvements
2 parents 163c57f + c2aa751 commit 456759e

File tree

5 files changed

+45
-4
lines changed

5 files changed

+45
-4
lines changed

ApplicationInsightsInternalLogger/src/main/java/com/microsoft/applicationinsights/internal/logger/InternalLogger.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,11 @@ public void stop() {
204204
loggingLevel = LoggingLevel.OFF;
205205
}
206206
}
207+
/* VisibleForTesting */
208+
void reset() {
209+
INSTANCE.stop();
210+
INSTANCE.initialized = false;
211+
}
207212

208213
public boolean isTraceEnabled() {
209214
return loggingLevel.getValue() <= LoggingLevel.TRACE.getValue();

core/src/main/java/com/microsoft/applicationinsights/internal/channel/common/TransmissionFileSystemOutput.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@
2323

2424
import java.io.File;
2525
import java.io.InputStream;
26+
import java.io.InvalidClassException;
2627
import java.io.ObjectInput;
2728
import java.io.FileInputStream;
2829
import java.io.BufferedInputStream;
2930
import java.io.ObjectInputStream;
3031
import java.io.FileNotFoundException;
3132
import java.io.IOException;
3233
import java.io.FileOutputStream;
33-
import java.io.OutputStream;
34+
import java.io.ObjectStreamClass;
3435
import java.io.BufferedOutputStream;
3536
import java.io.ObjectOutput;
3637
import java.io.ObjectOutputStream;
@@ -244,7 +245,7 @@ private Optional<Transmission> loadTransmission(File file) {
244245
if (file == null) {
245246
return Optional.absent();
246247
}
247-
try (ObjectInput input = new ObjectInputStream(new BufferedInputStream(new FileInputStream(file)))) {
248+
try (ObjectInput input = new SafeObjectInputStream(new BufferedInputStream(new FileInputStream(file)))) {
248249
transmission = (Transmission)input.readObject();
249250
} catch (FileNotFoundException e) {
250251
InternalLogger.INSTANCE.error("Failed to load transmission, file not found, exception: %s", e.toString());
@@ -257,6 +258,21 @@ private Optional<Transmission> loadTransmission(File file) {
257258
return Optional.fromNullable(transmission);
258259
}
259260

261+
private final static class SafeObjectInputStream extends ObjectInputStream {
262+
263+
public SafeObjectInputStream(InputStream in) throws IOException {
264+
super(in);
265+
}
266+
267+
protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException {
268+
if (!desc.getName().equals(Transmission.class.getName()) && !desc.getName().equals(byte[].class.getName())) {
269+
throw new InvalidClassException("Cannot deserialize "+desc.getName());
270+
} else {
271+
return super.resolveClass(desc);
272+
}
273+
}
274+
}
275+
260276
private boolean renameToPermanentName(File tempTransmissionFile) {
261277
File transmissionFile = new File(folder, FilenameUtils.getBaseName(tempTransmissionFile.getName()) + TRANSMISSION_FILE_EXTENSION);
262278
try {

core/src/test/java/com/microsoft/applicationinsights/internal/channel/common/ActiveTransmissionLoaderTest.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,13 @@
2323

2424
import java.io.File;
2525
import java.io.IOException;
26+
import java.util.HashMap;
2627
import java.util.concurrent.TimeUnit;
2728

2829
import com.microsoft.applicationinsights.internal.channel.TransmissionDispatcher;
29-
import org.junit.Test;
30+
import com.microsoft.applicationinsights.internal.logger.InternalLogger;
31+
import com.microsoft.applicationinsights.internal.logger.LoggerTestHelper;
32+
import org.junit.*;
3033
import org.mockito.Mockito;
3134

3235
import org.apache.commons.io.FileUtils;
@@ -38,6 +41,16 @@
3841
public class ActiveTransmissionLoaderTest {
3942
private final static String TEMP_TEST_FOLDER = "TransmissionTests";
4043

44+
@BeforeClass
45+
public static void initLogger() {
46+
InternalLogger.INSTANCE.initialize("CONSOLE", new HashMap<String, String>());
47+
}
48+
49+
@AfterClass
50+
public static void tearDownLogger() {
51+
LoggerTestHelper.resetInternalLogger();
52+
}
53+
4154
@Test(expected = NullPointerException.class)
4255
public void testNullFileSystem() throws Exception {
4356
new ActiveTransmissionLoader(null, Mockito.mock(TransmissionDispatcher.class), mockStateFetcher(), 1);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.microsoft.applicationinsights.internal.logger;
2+
3+
public class LoggerTestHelper {
4+
public static void resetInternalLogger() {
5+
InternalLogger.INSTANCE.reset();
6+
}
7+
}

gradle/common-java.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ if (hasProperty("JDKToUse")) {
110110
options.with {
111111
fork = true
112112
forkOptions.executable = javaExecutables.javac
113-
forkOptions.javaHome = JDKToUse
113+
forkOptions.javaHome = file(JDKToUse)
114114
}
115115
}
116116
tasks.withType(Javadoc) {

0 commit comments

Comments
 (0)