Skip to content

Commit 4259495

Browse files
authored
Merge pull request #400 from Nizernizer/fix/parseCustomModel-exception
Fix/parse custom model exception
2 parents 565ef73 + cc41c68 commit 4259495

File tree

4 files changed

+54
-42
lines changed

4 files changed

+54
-42
lines changed

dongtai-core/src/main/java/io/dongtai/iast/core/EngineManager.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ public static void cleanThreadState() {
136136
EngineManager.TAINT_HASH_CODES.remove();
137137
EngineManager.TAINT_RANGES_POOL.remove();
138138
EngineManager.SCOPE_TRACKER.remove();
139+
EngineManager.ENTER_REPLAY_ENTRYPOINT.remove();
139140
FallbackSwitch.clearHeavyHookFallback();
140141
EngineManager.getFallbackManager().getHookRateLimiter().remove();
141142
ContextManager.getCONTEXT().remove();

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

Lines changed: 44 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -160,51 +160,57 @@ public static void handlerCustomModel(MethodEvent event) {
160160
* @return Set<Object>
161161
*/
162162
public static Set<Object> parseCustomModel(Object model) {
163-
Set<Object> modelValues = new HashSet<Object>();
164-
if (!TaintPoolUtils.isNotEmpty(model)) {
165-
return modelValues;
166-
}
167-
Class<?> sourceClass = model.getClass();
168-
if (sourceClass.getClassLoader() == null) {
169-
return modelValues;
170-
}
171-
String className = sourceClass.getName();
172-
if (className.startsWith("cn.huoxian.iast.api.") ||
173-
className.startsWith("io.dongtai.api.") ||
174-
className.startsWith(" org.apache.shiro.web.servlet".substring(1)) ||
175-
VALUES_ENUMERATOR.equals(className) ||
176-
className.startsWith(SPRING_OBJECT) ||
177-
className.endsWith("RequestWrapper") ||
178-
className.endsWith("ResponseWrapper")
179-
180-
) {
181-
return modelValues;
182-
}
183-
// getter methods
184-
Method[] methods = sourceClass.getMethods();
185-
Object itemValue = null;
186-
for (Method method : methods) {
187-
if (!TaintPoolUtils.isAllowTaintGetterMethod(method)) {
188-
continue;
163+
try{
164+
Set<Object> modelValues = new HashSet<Object>();
165+
if (!TaintPoolUtils.isNotEmpty(model)) {
166+
return modelValues;
189167
}
190-
191-
try {
192-
itemValue = method.invoke(model);
193-
if (!TaintPoolUtils.isNotEmpty(itemValue)) {
168+
Class<?> sourceClass = model.getClass();
169+
if (sourceClass.getClassLoader() == null) {
170+
return modelValues;
171+
}
172+
String className = sourceClass.getName();
173+
if (className.startsWith("cn.huoxian.iast.api.") ||
174+
className.startsWith("io.dongtai.api.") ||
175+
className.startsWith(" org.apache.tomcat".substring(1)) ||
176+
className.startsWith(" org.apache.catalina".substring(1)) ||
177+
className.startsWith(" org.apache.shiro.web.servlet".substring(1)) ||
178+
VALUES_ENUMERATOR.equals(className) ||
179+
className.startsWith(SPRING_OBJECT) ||
180+
className.contains("RequestWrapper") ||
181+
className.contains("ResponseWrapper")
182+
183+
) {
184+
return modelValues;
185+
}
186+
// getter methods
187+
Method[] methods = sourceClass.getMethods();
188+
Object itemValue = null;
189+
for (Method method : methods) {
190+
if (!TaintPoolUtils.isAllowTaintGetterMethod(method)) {
194191
continue;
195192
}
196-
modelValues.add(itemValue);
197-
if (itemValue instanceof List) {
198-
List<?> itemValueList = (List<?>) itemValue;
199-
for (Object listValue : itemValueList) {
200-
modelValues.addAll(parseCustomModel(listValue));
193+
194+
try {
195+
itemValue = method.invoke(model);
196+
if (!TaintPoolUtils.isNotEmpty(itemValue)) {
197+
continue;
198+
}
199+
modelValues.add(itemValue);
200+
if (itemValue instanceof List) {
201+
List<?> itemValueList = (List<?>) itemValue;
202+
for (Object listValue : itemValueList) {
203+
modelValues.addAll(parseCustomModel(listValue));
204+
}
201205
}
206+
} catch (Exception e) {
207+
DongTaiLog.error(e);
202208
}
203-
} catch (Exception e) {
204-
DongTaiLog.error(e);
205209
}
210+
return modelValues;
211+
}catch (Exception e){
212+
return new HashSet<Object>();
206213
}
207-
return modelValues;
208214
}
209215

210216
private static boolean allowCall(MethodEvent event) {

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ public static void buildAndReport(Object request, Object response) {
2727
List<GraphNode> nodeList = build();
2828
String report = convertToReport(nodeList, request, response);
2929
ThreadPools.sendPriorityReport(ApiPath.REPORT_UPLOAD, report);
30-
EngineManager.ENTER_REPLAY_ENTRYPOINT.remove();
3130
}
3231

3332
/**
@@ -75,7 +74,7 @@ public static List<GraphNode> build() {
7574
event.sourceTypes
7675
)
7776
);
78-
}catch (Exception e){
77+
} catch (Exception e) {
7978
DongTaiLog.debug(e);
8079
}
8180
}
@@ -99,8 +98,14 @@ public static String convertToReport(List<GraphNode> nodeList, Object request, O
9998
detail.put(ReportKey.METHOD, requestMeta.getOrDefault("method", ""));
10099
detail.put(ReportKey.SECURE, requestMeta.getOrDefault("secure", ""));
101100
String requestURL = requestMeta.getOrDefault("requestURL", "").toString();
101+
if (null == requestURL) {
102+
return null;
103+
}
102104
detail.put(ReportKey.URL, requestURL);
103105
String requestURI = requestMeta.getOrDefault("requestURI", "").toString();
106+
if (null == requestURI) {
107+
return null;
108+
}
104109
detail.put(ReportKey.URI, requestURI);
105110
setURL(requestURL);
106111
setURI(requestURI);
@@ -123,7 +128,6 @@ public static String convertToReport(List<GraphNode> nodeList, Object request, O
123128
for (GraphNode node : nodeList) {
124129
methodPool.put(node.toJson());
125130
}
126-
127131
return report.toString();
128132
}
129133

dongtai-core/src/main/java/io/dongtai/iast/core/utils/StackUtils.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
public class StackUtils {
99
public static StackTraceElement[] createCallStack(int stackStartPos) {
1010
StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
11-
StackTraceElement[] selfCallStack = new StackTraceElement[stackTraceElements.length - stackStartPos];
11+
StackTraceElement[] selfCallStack = new StackTraceElement[0];
1212
if (stackTraceElements.length - stackStartPos >= 0) {
13+
selfCallStack = new StackTraceElement[stackTraceElements.length - stackStartPos];
1314
System.arraycopy(stackTraceElements, stackStartPos, selfCallStack, 0, stackTraceElements.length - stackStartPos);
1415
}
1516
return selfCallStack;

0 commit comments

Comments
 (0)