Skip to content

Commit

Permalink
Merge branch 'alibaba:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
Fatpandac authored Aug 4, 2023
2 parents d606713 + 4dc65ba commit 29aa028
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 28 deletions.
11 changes: 6 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
Expand All @@ -20,7 +21,7 @@
<connection>scm:git:[email protected]:alibaba/arthas.git</connection>
<developerConnection>scm:git:[email protected]:alibaba/arthas.git</developerConnection>
<url>https://github.com/alibaba/arthas</url>
<tag>HEAD</tag>
<tag>HEAD</tag>
</scm>

<developers>
Expand Down Expand Up @@ -81,7 +82,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<spring-boot.version>2.7.11</spring-boot.version>
<spring-boot.version>2.7.14</spring-boot.version>
<spring-boot3.version>3.0.6</spring-boot3.version>
<maven-invoker-plugin.version>3.0.0</maven-invoker-plugin.version>
<project.build.outputTimestamp>2020-09-27T15:10:43Z</project.build.outputTimestamp>
Expand All @@ -94,7 +95,7 @@
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>bytekit-core</artifactId>
<version>0.0.8</version>
<version>0.0.9</version>
</dependency>
<dependency>
<groupId>org.benf</groupId>
Expand Down Expand Up @@ -449,7 +450,7 @@
<artifactId>flatten-maven-plugin</artifactId>
<version>1.2.2</version>
<configuration>
<flattenMode>minimum</flattenMode>
<flattenMode>minimum</flattenMode>
</configuration>
<executions>
<execution>
Expand Down
12 changes: 10 additions & 2 deletions tunnel-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,16 @@
<url>https://github.com/alibaba/arthas</url>

<dependencies>


<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.InvalidClassException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectStreamClass;
import java.io.Serializable;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
Expand All @@ -16,9 +19,11 @@
*
*/
public class SimpleHttpResponse implements Serializable {

private static final long serialVersionUID = 1L;

private static final List<String> whitelist = Arrays.asList(byte[].class.getName(), String.class.getName(),
Map.class.getName(), HashMap.class.getName(), SimpleHttpResponse.class.getName());

private int status = 200;

private Map<String, String> headers = new HashMap<String, String>();
Expand Down Expand Up @@ -55,35 +60,25 @@ public void setStatus(int status) {

public static byte[] toBytes(SimpleHttpResponse response) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream out = null;
try {
out = new ObjectOutputStream(bos);
try (ObjectOutputStream out = new ObjectOutputStream(bos)) {
out.writeObject(response);
out.flush();
return bos.toByteArray();
} finally {
try {
bos.close();
} catch (IOException ex) {
// ignore close exception
}
}
}

public static SimpleHttpResponse fromBytes(byte[] bytes) throws IOException, ClassNotFoundException {
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
ObjectInput in = null;
try {
in = new ObjectInputStream(bis);
return (SimpleHttpResponse) in.readObject();
} finally {
try {
if (in != null) {
in.close();
try (ObjectInputStream in = new ObjectInputStream(bis) {
protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException {
if (!whitelist.contains(desc.getName())) {
throw new InvalidClassException("Unauthorized deserialization attempt", desc.getName());
}
} catch (IOException ex) {
// ignore close exception
return super.resolveClass(desc);
}
}) {
return (SimpleHttpResponse) in.readObject();
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.alibaba.arthas.tunnel.common;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InvalidClassException;
import java.io.ObjectOutputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.junit.Test;

public class SimpleHttpResponseTest {

@Test
public void testSerialization() throws IOException, ClassNotFoundException {
SimpleHttpResponse response = new SimpleHttpResponse();
response.setStatus(200);

Map<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "text/plain");
response.setHeaders(headers);

String content = "Hello, world!";
response.setContent(content.getBytes());

byte[] bytes = SimpleHttpResponse.toBytes(response);

SimpleHttpResponse deserializedResponse = SimpleHttpResponse.fromBytes(bytes);

assertEquals(response.getStatus(), deserializedResponse.getStatus());
assertEquals(response.getHeaders(), deserializedResponse.getHeaders());
assertArrayEquals(response.getContent(), deserializedResponse.getContent());
}

private static byte[] toBytes(Object object) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try (ObjectOutputStream out = new ObjectOutputStream(bos)) {
out.writeObject(object);
out.flush();
return bos.toByteArray();
}
}

@Test(expected = InvalidClassException.class)
public void testDeserializationWithUnauthorizedClass() throws IOException, ClassNotFoundException {
Date date = new Date();

byte[] bytes = toBytes(date);

// Try to deserialize the object with an unauthorized class
// This should throw an InvalidClassException
SimpleHttpResponse.fromBytes(bytes);
}

}

0 comments on commit 29aa028

Please sign in to comment.