Skip to content

Commit c7c1806

Browse files
authored
test: add YamlCodec security tests (#16282)
1 parent 18bd169 commit c7c1806

1 file changed

Lines changed: 78 additions & 0 deletions

File tree

  • dubbo-remoting/dubbo-remoting-http12/src/test/java/org/apache/dubbo/remoting/http12/message/codec
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.dubbo.remoting.http12.message.codec;
18+
19+
import org.apache.dubbo.common.utils.SerializeCheckStatus;
20+
import org.apache.dubbo.common.utils.SerializeSecurityManager;
21+
import org.apache.dubbo.remoting.http12.exception.DecodeException;
22+
import org.apache.dubbo.remoting.http12.message.HttpMessageCodec;
23+
import org.apache.dubbo.rpc.model.FrameworkModel;
24+
25+
import java.io.ByteArrayInputStream;
26+
import java.nio.charset.StandardCharsets;
27+
28+
import org.junit.jupiter.api.AfterEach;
29+
import org.junit.jupiter.api.Assertions;
30+
import org.junit.jupiter.api.BeforeEach;
31+
import org.junit.jupiter.api.Test;
32+
33+
class YamlCodecTest {
34+
35+
private HttpMessageCodec codec;
36+
37+
@BeforeEach
38+
void setUp() {
39+
FrameworkModel.destroyAll();
40+
codec = new YamlCodecFactory().createCodec(null, FrameworkModel.defaultModel(), null);
41+
}
42+
43+
@AfterEach
44+
void tearDown() {
45+
FrameworkModel.destroyAll();
46+
}
47+
48+
@Test
49+
void testDecodeYamlToPojo() {
50+
setSerializeCheckStatus(SerializeCheckStatus.WARN);
51+
String content = "username: JohnDoe\nlocation: New York\n";
52+
53+
User user = (User) codec.decode(newInputStream(content), User.class);
54+
55+
Assertions.assertEquals("JohnDoe", user.getUsername());
56+
Assertions.assertEquals("New York", user.getLocation());
57+
}
58+
59+
@Test
60+
void testDecodeRejectsUnsafeYamlClassTag() {
61+
setSerializeCheckStatus(SerializeCheckStatus.STRICT);
62+
63+
String content = "!!java.net.Socket {}";
64+
65+
Assertions.assertThrows(DecodeException.class, () -> codec.decode(newInputStream(content), Object.class));
66+
}
67+
68+
private void setSerializeCheckStatus(SerializeCheckStatus status) {
69+
FrameworkModel.defaultModel()
70+
.getBeanFactory()
71+
.getBean(SerializeSecurityManager.class)
72+
.setCheckStatus(status);
73+
}
74+
75+
private ByteArrayInputStream newInputStream(String content) {
76+
return new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8));
77+
}
78+
}

0 commit comments

Comments
 (0)