Skip to content

Commit

Permalink
Merge pull request #1821 from akto-api-security/feature/override_host…
Browse files Browse the repository at this point in the history
…_in_testing_url

Overriding host from url in test completed
  • Loading branch information
avneesh-akto authored Dec 13, 2024
2 parents cd5d9e8 + 9da67e5 commit 29b4093
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@ public TestingRunResult runTestNew(ApiInfo.ApiInfoKey apiInfoKey, ObjectId testR

String message = messages.get(messages.size() - 1);

RawApi rawApi = RawApi.buildFromMessage(message);
RawApi rawApi = RawApi.buildFromMessage(message, true);
int startTime = Context.now();

try {
Expand Down
17 changes: 16 additions & 1 deletion libs/dao/src/main/java/com/akto/dto/OriginalHttpRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.akto.util.HttpRequestResponseUtils;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.ctc.wstx.shaded.msv_core.util.Uri;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.mongodb.BasicDBObject;
Expand Down Expand Up @@ -48,6 +47,22 @@ public OriginalHttpRequest copy() {
);
}

public void buildFromSampleMessage(String message, boolean useUrlToFillHost) {
buildFromSampleMessage(message);
if(useUrlToFillHost){
try {
if(this.headers.getOrDefault("host", null) == null){
URI uri = new URI(this.url);
String calculatedHost = uri.getHost() != null ? uri.getHost() : "";
this.headers.put("host", Arrays.asList(calculatedHost));
}
} catch (Exception e) {
e.printStackTrace();
}

}
}

public void buildFromSampleMessage(String message) {
Map<String, Object> json = gson.fromJson(message, Map.class);

Expand Down
20 changes: 20 additions & 0 deletions libs/dao/src/main/java/com/akto/dto/RawApi.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.akto.dto;

import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand All @@ -25,6 +26,25 @@ public RawApi(OriginalHttpRequest request, OriginalHttpResponse response, String
this.originalMessage = originalMessage;
}

public static RawApi buildFromMessage(String message, boolean overrideHostHeader){
if(!overrideHostHeader){
return buildFromMessage(message);
}else{
try {
OriginalHttpRequest request = new OriginalHttpRequest();
request.buildFromSampleMessage(message, true);

OriginalHttpResponse response = new OriginalHttpResponse();
response.buildFromSampleMessage(message);

return new RawApi(request, response, message);
} catch (Exception e) {
e.printStackTrace();
return buildFromMessage(message);
}
}
}

public static RawApi buildFromMessage(String message) {
OriginalHttpRequest request = new OriginalHttpRequest();
request.buildFromSampleMessage(message);
Expand Down

0 comments on commit 29b4093

Please sign in to comment.