Skip to content

Commit 55d1c09

Browse files
authored
Get bytes using UTF-8 charset (#15483)
1 parent c5656dd commit 55d1c09

17 files changed

Lines changed: 39 additions & 21 deletions

File tree

dubbo-common/src/main/java/org/apache/dubbo/common/io/Bytes.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.io.IOException;
2424
import java.io.InputStream;
2525
import java.io.OutputStream;
26+
import java.nio.charset.StandardCharsets;
2627
import java.security.MessageDigest;
2728
import java.security.NoSuchAlgorithmException;
2829
import java.util.Map;
@@ -823,7 +824,7 @@ public static byte[] unzip(byte[] bytes) throws IOException {
823824
* @return MD5 byte array.
824825
*/
825826
public static byte[] getMD5(String str) {
826-
return getMD5(str.getBytes());
827+
return getMD5(str.getBytes(StandardCharsets.UTF_8));
827828
}
828829

829830
/**

dubbo-common/src/main/java/org/apache/dubbo/common/utils/JVMUtil.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.lang.management.MonitorInfo;
2525
import java.lang.management.ThreadInfo;
2626
import java.lang.management.ThreadMXBean;
27+
import java.nio.charset.StandardCharsets;
2728

2829
import static java.lang.Thread.State.BLOCKED;
2930
import static java.lang.Thread.State.TIMED_WAITING;
@@ -33,7 +34,7 @@ public class JVMUtil {
3334
public static void jstack(OutputStream stream) throws Exception {
3435
ThreadMXBean threadMxBean = ManagementFactory.getThreadMXBean();
3536
for (ThreadInfo threadInfo : threadMxBean.dumpAllThreads(true, true)) {
36-
stream.write(getThreadDumpString(threadInfo).getBytes());
37+
stream.write(getThreadDumpString(threadInfo).getBytes(StandardCharsets.UTF_8));
3738
}
3839
}
3940

dubbo-maven-plugin/src/main/java/org/apache/dubbo/maven/plugin/protoc/DubboProtocCompilerMojo.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,7 @@ public String getOutput() {
533533
}
534534

535535
private static String fixUnicodeOutput(final String message) {
536+
// TODO default charset is not UTF-8 ?
536537
return new String(message.getBytes(), StandardCharsets.UTF_8);
537538
}
538539
}

dubbo-plugin/dubbo-auth/src/main/java/org/apache/dubbo/auth/BasicAuthenticator.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.apache.dubbo.common.URL;
2222
import org.apache.dubbo.rpc.Invocation;
2323

24+
import java.nio.charset.StandardCharsets;
2425
import java.util.Base64;
2526
import java.util.Objects;
2627

@@ -31,7 +32,7 @@ public void sign(Invocation invocation, URL url) {
3132
String username = url.getParameter(Constants.USERNAME_KEY);
3233
String password = url.getParameter(Constants.PASSWORD_KEY);
3334
String auth = username + ":" + password;
34-
String encodedAuth = Base64.getEncoder().encodeToString(auth.getBytes());
35+
String encodedAuth = Base64.getEncoder().encodeToString(auth.getBytes(StandardCharsets.UTF_8));
3536
String authHeaderValue = "Basic " + encodedAuth;
3637

3738
invocation.setAttachment(Constants.AUTHORIZATION_HEADER_LOWER, authHeaderValue);
@@ -42,7 +43,7 @@ public void authenticate(Invocation invocation, URL url) throws RpcAuthenticatio
4243
String username = url.getParameter(Constants.USERNAME_KEY);
4344
String password = url.getParameter(Constants.PASSWORD_KEY);
4445
String auth = username + ":" + password;
45-
String encodedAuth = Base64.getEncoder().encodeToString(auth.getBytes());
46+
String encodedAuth = Base64.getEncoder().encodeToString(auth.getBytes(StandardCharsets.UTF_8));
4647
String authHeaderValue = "Basic " + encodedAuth;
4748

4849
if (!Objects.equals(authHeaderValue, invocation.getAttachment(Constants.AUTHORIZATION_HEADER))

dubbo-plugin/dubbo-auth/src/main/java/org/apache/dubbo/auth/utils/SignatureUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ private static String sign(byte[] data, String key) throws RuntimeException {
6464
} catch (NoSuchAlgorithmException e) {
6565
throw new RuntimeException("Failed to generate HMAC: no such algorithm exception " + HMAC_SHA256_ALGORITHM);
6666
}
67-
SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), HMAC_SHA256_ALGORITHM);
67+
SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), HMAC_SHA256_ALGORITHM);
6868
try {
6969
mac.init(signingKey);
7070
} catch (InvalidKeyException e) {

dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/server/handler/ForeignHostPermitHandler.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import java.net.InetAddress;
2424
import java.net.InetSocketAddress;
25+
import java.nio.charset.StandardCharsets;
2526
import java.util.function.Predicate;
2627

2728
import io.netty.buffer.ByteBuf;
@@ -74,7 +75,7 @@ public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
7475

7576
ByteBuf cb = Unpooled.wrappedBuffer((QosConstants.BR_STR
7677
+ "Foreign Ip Not Permitted, Consider Config It In Whitelist." + QosConstants.BR_STR)
77-
.getBytes());
78+
.getBytes(StandardCharsets.UTF_8));
7879
ctx.writeAndFlush(cb).addListener(ChannelFutureListener.CLOSE);
7980
}
8081

dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/server/handler/HttpProcessHandler.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import org.apache.dubbo.qos.command.exception.PermissionDenyException;
2828
import org.apache.dubbo.rpc.model.FrameworkModel;
2929

30+
import java.nio.charset.StandardCharsets;
31+
3032
import io.netty.buffer.Unpooled;
3133
import io.netty.channel.ChannelFutureListener;
3234
import io.netty.channel.ChannelHandlerContext;
@@ -68,9 +70,11 @@ public HttpProcessHandler(FrameworkModel frameworkModel, QosConfiguration qosCon
6870

6971
private static FullHttpResponse http(int httpCode, String result) {
7072
FullHttpResponse response = new DefaultFullHttpResponse(
71-
HttpVersion.HTTP_1_1, HttpResponseStatus.valueOf(httpCode), Unpooled.wrappedBuffer(result.getBytes()));
73+
HttpVersion.HTTP_1_1,
74+
HttpResponseStatus.valueOf(httpCode),
75+
Unpooled.wrappedBuffer(result.getBytes(StandardCharsets.UTF_8)));
7276
HttpHeaders httpHeaders = response.headers();
73-
httpHeaders.set(HttpHeaderNames.CONTENT_TYPE, "text/plain");
77+
httpHeaders.set(HttpHeaderNames.CONTENT_TYPE, "text/plain; charset=utf-8");
7478
httpHeaders.set(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes());
7579
return response;
7680
}

dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/server/handler/QosProcessHandler.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.apache.dubbo.qos.api.QosConfiguration;
2121
import org.apache.dubbo.rpc.model.FrameworkModel;
2222

23+
import java.nio.charset.StandardCharsets;
2324
import java.util.List;
2425
import java.util.concurrent.TimeUnit;
2526

@@ -60,8 +61,8 @@ public void channelActive(final ChannelHandlerContext ctx) throws Exception {
6061
() -> {
6162
final String welcome = qosConfiguration.getWelcome();
6263
if (welcome != null) {
63-
ctx.write(Unpooled.wrappedBuffer(welcome.getBytes()));
64-
ctx.writeAndFlush(Unpooled.wrappedBuffer(PROMPT.getBytes()));
64+
ctx.write(Unpooled.wrappedBuffer(welcome.getBytes(StandardCharsets.UTF_8)));
65+
ctx.writeAndFlush(Unpooled.wrappedBuffer(PROMPT.getBytes(StandardCharsets.UTF_8)));
6566
}
6667
},
6768
500,

dubbo-plugin/dubbo-spring-security/src/main/java/org/apache/dubbo/spring/security/jackson/ObjectMapperCodec.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.apache.dubbo.common.utils.ClassUtils;
2323
import org.apache.dubbo.common.utils.StringUtils;
2424

25+
import java.nio.charset.StandardCharsets;
2526
import java.util.ArrayList;
2627
import java.util.List;
2728
import java.util.function.Consumer;
@@ -64,7 +65,7 @@ public <T> T deserialize(String content, Class<T> clazz) {
6465
if (StringUtils.isBlank(content)) {
6566
return null;
6667
}
67-
return deserialize(content.getBytes(), clazz);
68+
return deserialize(content.getBytes(StandardCharsets.UTF_8), clazz);
6869
}
6970

7071
public String serialize(Object object) {

dubbo-plugin/dubbo-triple-websocket/src/main/java/org/apache/dubbo/rpc/protocol/tri/websocket/TripleTextMessageHandler.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323

2424
import javax.websocket.MessageHandler;
2525

26+
import java.nio.charset.StandardCharsets;
27+
2628
public class TripleTextMessageHandler implements MessageHandler.Partial<String> {
2729

2830
private final WebSocketTransportListener webSocketTransportListener;
@@ -33,8 +35,8 @@ public TripleTextMessageHandler(WebSocketTransportListener webSocketTransportLis
3335

3436
@Override
3537
public void onMessage(String messagePart, boolean last) {
36-
Http2InputMessage http2InputMessage =
37-
new Http2InputMessageFrame(new FinalFragmentByteArrayInputStream(messagePart.getBytes(), last), false);
38+
Http2InputMessage http2InputMessage = new Http2InputMessageFrame(
39+
new FinalFragmentByteArrayInputStream(messagePart.getBytes(StandardCharsets.UTF_8), last), false);
3840
webSocketTransportListener.onData(http2InputMessage);
3941
}
4042
}

0 commit comments

Comments
 (0)