Skip to content

Commit e9cf83b

Browse files
committed
fixes request body fetch for x-www-form-urlencoded
1 parent 7635c91 commit e9cf83b

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

dongtai-core/src/main/java/io/dongtai/iast/core/handler/hookpoint/controller/impl/HttpImpl.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,34 @@ public static void solveHttpRequest(Object obj, Object req, Object resp, Map<Str
9595
} catch (Throwable ignore) {
9696
}
9797

98+
try {
99+
String contentType = ((Map<String, String>) requestMeta.get("headers")).get("Content-Type");
100+
String method = (String) requestMeta.get("method");
101+
if (("POST".equalsIgnoreCase(method) || "PUT".equalsIgnoreCase(method))
102+
&& !isRawBody(contentType)) {
103+
Method getParameterNamesMethod = ReflectUtils.getDeclaredMethodFromSuperClass(req.getClass(),
104+
"getParameterNames", null);
105+
Method getParameterMethod = ReflectUtils.getDeclaredMethodFromSuperClass(req.getClass(),
106+
"getParameter", new Class[]{String.class});
107+
Enumeration<?> parameterNames = (Enumeration<?>) getParameterNamesMethod.invoke(req);
108+
StringBuilder postBody = new StringBuilder();
109+
boolean first = true;
110+
while (parameterNames.hasMoreElements()) {
111+
String key = (String) parameterNames.nextElement();
112+
if (first) {
113+
first = false;
114+
postBody.append(key).append("=").append((String) getParameterMethod.invoke(req, key));
115+
} else {
116+
postBody.append("&").append(key).append("=").append((String) getParameterMethod.invoke(req, key));
117+
}
118+
}
119+
if (postBody.length() > 0) {
120+
requestMeta.put("body", postBody.toString());
121+
}
122+
}
123+
} catch (Throwable ignore) {
124+
}
125+
98126
EngineManager.enterHttpEntry(requestMeta);
99127
DongTaiLog.debug("HTTP Request:{} {} from: {}", requestMeta.get("method"), requestMeta.get("requestURI"),
100128
obj.getClass().getName());
@@ -111,6 +139,9 @@ public static Map<String, String> parseRequestHeaders(Object req, Enumeration<?>
111139
try {
112140
String key = (String) headerNames.nextElement();
113141
String val = (String) getHeaderMethod.invoke(req, key);
142+
if ("content-type".equalsIgnoreCase(key)) {
143+
key = "Content-Type";
144+
}
114145
headers.put(key, val);
115146
} catch (Throwable ignore) {
116147
}
@@ -119,6 +150,13 @@ public static Map<String, String> parseRequestHeaders(Object req, Enumeration<?>
119150
}
120151

121152
public static void onServletInputStreamRead(int ret, String desc, Object stream, byte[] bs, int offset, int len) {
153+
if (EngineManager.REQUEST_CONTEXT.get() != null
154+
&& EngineManager.REQUEST_CONTEXT.get().get("body") != null
155+
&& EngineManager.REQUEST_CONTEXT.get().get("body") != ""
156+
) {
157+
return;
158+
}
159+
122160
if ("()I".equals(desc)) {
123161
if (ret == -1) {
124162
return;
@@ -218,4 +256,9 @@ public static void onServletOutputStreamWrite(String desc, Object stream, int b,
218256
public static IastClassLoader getClassLoader() {
219257
return iastClassLoader;
220258
}
259+
260+
public static boolean isRawBody(String contentType) {
261+
return contentType != null
262+
&& (contentType.contains("application/json") || contentType.contains("application/xml"));
263+
}
221264
}

dongtai-core/src/main/java/io/dongtai/iast/core/handler/hookpoint/graphy/GraphBuilder.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,19 @@ public static String convertToReport(List<GraphNode> nodeList, Object request, O
8181
if (StringUtils.isEmpty(requestURI)) {
8282
return null;
8383
}
84+
8485
detail.put(ReportKey.URI, requestURI);
8586
detail.put(ReportKey.CLIENT_IP, requestMeta.getOrDefault("remoteAddr", ""));
8687
detail.put(ReportKey.QUERY_STRING, requestMeta.getOrDefault("queryString", ""));
8788
detail.put(ReportKey.REQ_HEADER, AbstractNormalVulScan.getEncodedHeader(
8889
(Map<String, String>) requestMeta.getOrDefault("headers", new HashMap<String, String>())));
89-
detail.put(ReportKey.REQ_BODY, EngineManager.BODY_BUFFER.getRequest().toString());
90+
91+
String reqBody = (String) requestMeta.get("body");
92+
if (StringUtils.isEmpty(reqBody)) {
93+
reqBody = EngineManager.BODY_BUFFER.getRequest().toString();
94+
}
95+
detail.put(ReportKey.REQ_BODY, reqBody);
96+
9097
detail.put(ReportKey.RES_HEADER, AbstractNormalVulScan.getEncodedResponseHeader(
9198
(String) requestMeta.get("responseStatus"),
9299
(Map<String, Collection<String>>) requestMeta.get("responseHeaders")));

0 commit comments

Comments
 (0)