Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

grpc: add websocket support #5509

Merged
merged 9 commits into from
Jul 2, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.parosproxy.paros.Constant;
import org.parosproxy.paros.extension.ExtensionAdaptor;
import org.parosproxy.paros.extension.ExtensionHook;
import org.zaproxy.addon.grpc.internal.DecoderUtils;
import org.zaproxy.addon.grpc.internal.HttpPanelGrpcView;
import org.zaproxy.addon.grpc.internal.VariantGrpc;
import org.zaproxy.zap.extension.httppanel.component.split.request.RequestSplitComponent;
Expand Down Expand Up @@ -98,7 +99,9 @@ public String getName() {

@Override
public HttpPanelView getNewView() {
return new HttpPanelGrpcView(new ResponseBodyByteHttpPanelViewModel());
return new HttpPanelGrpcView(
new ResponseBodyByteHttpPanelViewModel(),
DecoderUtils.DecodingMethod.BASE64_ENCODED);
}

@Override
Expand All @@ -118,7 +121,9 @@ public String getName() {

@Override
public HttpPanelView getNewView() {
return new HttpPanelGrpcView(new RequestBodyByteHttpPanelViewModel());
return new HttpPanelGrpcView(
new RequestBodyByteHttpPanelViewModel(),
DecoderUtils.DecodingMethod.BASE64_ENCODED);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ public class DecoderUtils {
public static final int LENGTH_DELIMITED_WIRE_TYPE = 2;
public static final int BIT32_WIRE_TYPE = 5;

public static enum DecodingMethod {
thc202 marked this conversation as resolved.
Show resolved Hide resolved
BASE64_ENCODED,
DIRECT
}

static boolean isGraphic(byte ch) {
// Check if the character is printable
// Printable characters have unicode values greater than 32 (excluding control
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ public class HttpPanelGrpcView implements HttpPanelView, HttpPanelViewModelListe
private ProtoBufMessageEncoder protoBufMessageEncoder;
private AbstractByteHttpPanelViewModel model;

public HttpPanelGrpcView(AbstractByteHttpPanelViewModel model) {
private DecoderUtils.DecodingMethod decodingMethod;
thc202 marked this conversation as resolved.
Show resolved Hide resolved

public HttpPanelGrpcView(
AbstractByteHttpPanelViewModel model, DecoderUtils.DecodingMethod decodingMethod) {
httpPanelGrpcArea = new HttpPanelGrpcArea();
RTextScrollPane scrollPane = new RTextScrollPane(httpPanelGrpcArea);
scrollPane.setLineNumbersEnabled(false);
Expand All @@ -60,6 +63,7 @@ public HttpPanelGrpcView(AbstractByteHttpPanelViewModel model) {
model.addHttpPanelViewModelListener(this);
protoBufMessageDecoder = new ProtoBufMessageDecoder();
protoBufMessageEncoder = new ProtoBufMessageEncoder();
this.decodingMethod = decodingMethod;
}

@Override
Expand Down Expand Up @@ -126,7 +130,11 @@ public void save() {
try {
protoBufMessageEncoder.encode(EncoderUtils.parseIntoList(text));
byte[] encodedMessage = protoBufMessageEncoder.getOutputEncodedMessage();
this.model.setData(Base64.getEncoder().encode(encodedMessage));
if (decodingMethod == DecoderUtils.DecodingMethod.BASE64_ENCODED) {
this.model.setData(Base64.getEncoder().encode(encodedMessage));
} else {
this.model.setData(encodedMessage);
}
thc202 marked this conversation as resolved.
Show resolved Hide resolved
} catch (Exception e) {
showInvalidMessageFormatError(e.getMessage());
}
Expand All @@ -147,8 +155,13 @@ public void dataChanged(HttpPanelViewModelEvent e) {
httpPanelGrpcArea.setBorder(null);
try {
body = DecoderUtils.splitMessageBodyAndStatusCode(body);
body = Base64.getDecoder().decode(body);
byte[] payload = DecoderUtils.extractPayload(body);
byte[] payload;
if (decodingMethod == DecoderUtils.DecodingMethod.BASE64_ENCODED) {
body = Base64.getDecoder().decode(body);
payload = DecoderUtils.extractPayload(body);
} else {
payload = body;
}
if (payload.length == 0) {
httpPanelGrpcArea.setText("");
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.parosproxy.paros.extension.ExtensionAdaptor;
import org.parosproxy.paros.extension.ExtensionHook;
import org.zaproxy.addon.grpc.ExtensionGrpc;
import org.zaproxy.addon.grpc.internal.DecoderUtils;
import org.zaproxy.addon.grpc.internal.HttpPanelGrpcView;
import org.zaproxy.zap.extension.httppanel.view.HttpPanelView;
import org.zaproxy.zap.extension.websocket.ExtensionWebSocket;
Expand Down Expand Up @@ -89,7 +90,8 @@ public String getName() {

@Override
public HttpPanelView getNewView() {
return new HttpPanelGrpcView(new ByteWebSocketPanelViewModel());
return new HttpPanelGrpcView(
new ByteWebSocketPanelViewModel(), DecoderUtils.DecodingMethod.DIRECT);
}

@Override
Expand Down
Loading