Skip to content

Commit

Permalink
Merge pull request #1274 from akto-api-security/hotfix/grpc_response_…
Browse files Browse the repository at this point in the history
…encoding_fix

response fix for grpc and ProtoBufUtils encoding handling for Integer…
  • Loading branch information
shivam-rawat-akto authored Jul 12, 2024
2 parents 9dfec7a + 5dcb66e commit 86aad3b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
6 changes: 4 additions & 2 deletions libs/dao/src/main/java/com/akto/util/grpc/ProtoBufUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,9 @@ private static void encodeMapToProto(Map<Object, Object> map, CodedOutputStream

codedOutputStream.writeTag(number, getWireType(value));

if (value instanceof Long) {
if (value instanceof Integer) {
codedOutputStream.writeInt64NoTag((int) value);
} else if (value instanceof Long) {
codedOutputStream.writeInt64NoTag((Long) value);
} else if (value instanceof Double) {
codedOutputStream.writeFixed64NoTag(Double.doubleToRawLongBits((Double) value));
Expand All @@ -172,7 +174,7 @@ private static void encodeMapToProto(Map<Object, Object> map, CodedOutputStream
}

private static int getWireType(Object value) {
if (value instanceof Long) {
if (value instanceof Long || value instanceof Integer) {
return WireFormat.WIRETYPE_VARINT;
} else if (value instanceof Double) {
return WireFormat.WIRETYPE_FIXED64;
Expand Down
18 changes: 12 additions & 6 deletions libs/utils/src/main/java/com/akto/testing/ApiExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@
import com.akto.log.LoggerMaker;
import com.akto.log.LoggerMaker.LogDb;
import com.akto.util.Constants;

import com.akto.util.HttpRequestResponseUtils;
import com.akto.util.grpc.ProtoBufUtils;
import kotlin.Pair;
import okhttp3.*;
import okio.BufferedSink;

import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
Expand Down Expand Up @@ -94,8 +92,14 @@ private static OriginalHttpResponse common(Request request, boolean followRedire
Headers headers = response.headers();

Map<String, List<String>> responseHeaders = generateHeadersMapFromHeadersObject(headers);

return new OriginalHttpResponse(body, responseHeaders, statusCode);
OriginalHttpResponse originalHttpResponse = new OriginalHttpResponse(body, responseHeaders, statusCode);
if (requestProtocol != null && requestProtocol.contains(HttpRequestResponseUtils.GRPC_CONTENT_TYPE)) {//GRPC request
//body will be binary,
String responseBase64Encoded = Base64.getEncoder().encodeToString(body.getBytes());
loggerMaker.infoAndAddToDb("grpc response base64 encoded:" + responseBase64Encoded, LogDb.TESTING);
originalHttpResponse.setBody(HttpRequestResponseUtils.convertGRPCEncodedToJson(responseBase64Encoded));
}
return originalHttpResponse;
}

public static Map<String, List<String>> generateHeadersMapFromHeadersObject(Headers headers) {
Expand Down Expand Up @@ -336,16 +340,18 @@ private static OriginalHttpResponse sendWithRequestBody(OriginalHttpRequest requ
if (!payload.startsWith("[") && !payload.startsWith("{")) payload = "{}";
} else if (contentType.contains(HttpRequestResponseUtils.GRPC_CONTENT_TYPE)) {
try {
loggerMaker.infoAndAddToDb("encoding to grpc payload:" + payload, LogDb.TESTING);
payload = ProtoBufUtils.base64EncodedJsonToProtobuf(payload);
} catch (Exception e) {
loggerMaker.errorAndAddToDb("Unable to encode payload:" + payload, LogDb.RUNTIME);
loggerMaker.errorAndAddToDb("Unable to encode grpc payload:" + payload, LogDb.TESTING);
payload = request.getBody();
}
try {// trying decoding payload
byte[] payloadByteArray = Base64.getDecoder().decode(payload);
loggerMaker.infoAndAddToDb("Final base64 encoded payload:"+ payload, LogDb.TESTING);
body = RequestBody.create(payloadByteArray, MediaType.parse(contentType));
} catch (Exception e) {
loggerMaker.errorAndAddToDb("Unable to decode payload:" + payload, LogDb.RUNTIME);
loggerMaker.errorAndAddToDb("Unable to decode grpc payload:" + payload, LogDb.TESTING);
}
}

Expand Down

0 comments on commit 86aad3b

Please sign in to comment.