Skip to content

Commit

Permalink
[ISSUE apache#7226] Filter tlvs in ppv2 which contents not are spec-c…
Browse files Browse the repository at this point in the history
…ompliant ASCII characters and space (apache#7227)

Filter tlvs in ppv2 which not are spec-compliant ASCII characters and space
  • Loading branch information
dingshuangxi888 authored Aug 29, 2023
1 parent fa54915 commit 9f34f55
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,21 @@ public static String generateMd5(byte[] content) {
byte[] bytes = calculateMd5(content);
return Hex.encodeHexString(bytes, false);
}

/**
* Returns true if subject contains only bytes that are spec-compliant ASCII characters.
* @param subject
* @return
*/
public static boolean isAscii(byte[] subject) {
if (subject == null) {
return false;
}
for (byte b : subject) {
if ((b & 0x80) != 0) {
return false;
}
}
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import io.grpc.netty.shaded.io.grpc.netty.InternalProtocolNegotiators;
import io.grpc.netty.shaded.io.grpc.netty.ProtocolNegotiationEvent;
import io.grpc.netty.shaded.io.netty.buffer.ByteBuf;
import io.grpc.netty.shaded.io.netty.buffer.ByteBufUtil;
import io.grpc.netty.shaded.io.netty.channel.ChannelHandler;
import io.grpc.netty.shaded.io.netty.channel.ChannelHandlerContext;
import io.grpc.netty.shaded.io.netty.channel.ChannelInboundHandlerAdapter;
Expand All @@ -44,6 +45,7 @@
import org.apache.commons.lang3.StringUtils;
import org.apache.rocketmq.common.constant.HAProxyConstants;
import org.apache.rocketmq.common.constant.LoggerName;
import org.apache.rocketmq.common.utils.BinaryUtil;
import org.apache.rocketmq.logging.org.slf4j.Logger;
import org.apache.rocketmq.logging.org.slf4j.LoggerFactory;
import org.apache.rocketmq.proxy.config.ConfigurationManager;
Expand Down Expand Up @@ -191,9 +193,13 @@ private void handleWithMessage(HAProxyMessage msg) {
}
if (CollectionUtils.isNotEmpty(msg.tlvs())) {
msg.tlvs().forEach(tlv -> {
byte[] valueBytes = ByteBufUtil.getBytes(tlv.content());
if (!BinaryUtil.isAscii(valueBytes)) {
return;
}
Attributes.Key<String> key = AttributeKeys.valueOf(
HAProxyConstants.PROXY_PROTOCOL_TLV_PREFIX + String.format("%02x", tlv.typeByteValue()));
String value = StringUtils.trim(tlv.content().toString(CharsetUtil.UTF_8));
String value = StringUtils.trim(new String(valueBytes, CharsetUtil.UTF_8));
builder.set(key, value);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.PooledByteBufAllocator;
import io.netty.channel.Channel;
import io.netty.channel.ChannelDuplexHandler;
Expand Down Expand Up @@ -58,6 +59,7 @@
import org.apache.rocketmq.common.ThreadFactoryImpl;
import org.apache.rocketmq.common.constant.HAProxyConstants;
import org.apache.rocketmq.common.constant.LoggerName;
import org.apache.rocketmq.common.utils.BinaryUtil;
import org.apache.rocketmq.common.utils.NetworkUtil;
import org.apache.rocketmq.logging.org.slf4j.Logger;
import org.apache.rocketmq.logging.org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -787,9 +789,13 @@ private void handleWithMessage(HAProxyMessage msg, Channel channel) {
}
if (CollectionUtils.isNotEmpty(msg.tlvs())) {
msg.tlvs().forEach(tlv -> {
byte[] valueBytes = ByteBufUtil.getBytes(tlv.content());
if (!BinaryUtil.isAscii(valueBytes)) {
return;
}
AttributeKey<String> key = AttributeKeys.valueOf(
HAProxyConstants.PROXY_PROTOCOL_TLV_PREFIX + String.format("%02x", tlv.typeByteValue()));
String value = StringUtils.trim(tlv.content().toString(CharsetUtil.UTF_8));
String value = StringUtils.trim(new String(valueBytes, CharsetUtil.UTF_8));
channel.attr(key).set(value);
});
}
Expand Down

0 comments on commit 9f34f55

Please sign in to comment.