66import io .dongtai .iast .core .handler .hookpoint .models .policy .SourceNode ;
77import io .dongtai .iast .core .handler .hookpoint .models .taint .range .*;
88import io .dongtai .log .DongTaiLog ;
9- import io .dongtai .log .ErrorCode ;
109
1110import java .lang .reflect .Array ;
12- import java .lang .reflect .Method ;
11+ import java .lang .reflect .Field ;
1312import java .math .BigDecimal ;
1413import java .util .*;
1514
@@ -24,16 +23,6 @@ public class TaintPoolUtils {
2423 private static final String VALUES_ENUMERATOR = " org.apache.tomcat.util.http.ValuesEnumerator" .substring (1 );
2524 private static final String SPRING_OBJECT = " org.springframework." .substring (1 );
2625
27- /**
28- * 判断 obj 对象是否为 java 的内置数据类型,包括:string、array、list、map、enum 等
29- *
30- * @param obj Object
31- * @return boolean
32- */
33- public static boolean isJdkType (Object obj ) {
34- return obj instanceof String || obj instanceof Map || obj instanceof List ;
35- }
36-
3726 public static boolean poolContains (Object obj , MethodEvent event ) {
3827 if (obj == null ) {
3928 return false ;
@@ -146,86 +135,7 @@ public static boolean isAllowTaintType(Object obj) {
146135 return isAllowTaintType (obj .getClass ());
147136 }
148137
149- public static boolean isAllowTaintGetterModel (Object model ) {
150- if (!TaintPoolUtils .isNotEmpty (model )) {
151- return false ;
152- }
153- Class <?> sourceClass = model .getClass ();
154- if (sourceClass .getClassLoader () == null ) {
155- return false ;
156- }
157- if (!TaintPoolUtils .isAllowTaintGetterClass (sourceClass )) {
158- return false ;
159- }
160- return true ;
161- }
162-
163- public static boolean isAllowTaintGetterClass (Class <?> clazz ) {
164- String className = clazz .getName ();
165- if (className .startsWith ("cn.huoxian.iast.api." ) ||
166- className .startsWith ("io.dongtai.api." ) ||
167- className .startsWith (" org.apache.tomcat" .substring (1 )) ||
168- className .startsWith (" org.apache.catalina" .substring (1 )) ||
169- className .startsWith (" org.apache.shiro.web.servlet" .substring (1 )) ||
170- className .startsWith (" org.eclipse.jetty" .substring (1 )) ||
171- VALUES_ENUMERATOR .equals (className ) ||
172- className .startsWith (SPRING_OBJECT ) ||
173- className .contains ("RequestWrapper" ) ||
174- className .contains ("ResponseWrapper" )
175-
176- ) {
177- return false ;
178- }
179-
180- List <Class <?>> interfaces = ReflectUtils .getAllInterfaces (clazz );
181- for (Class <?> inter : interfaces ) {
182- if (inter .getName ().endsWith (".servlet.ServletRequest" )
183- || inter .getName ().endsWith (".servlet.ServletResponse" )) {
184- return false ;
185- }
186- }
187-
188- return true ;
189- }
190-
191- public static boolean isAllowTaintGetterMethod (Method method ) {
192- String methodName = method .getName ();
193- if (!methodName .startsWith ("get" )
194- || "getClass" .equals (methodName )
195- || "getParserForType" .equals (methodName )
196- || "getDefaultInstance" .equals (methodName )
197- || "getDefaultInstanceForType" .equals (methodName )
198- || "getDescriptor" .equals (methodName )
199- || "getDescriptorForType" .equals (methodName )
200- || "getAllFields" .equals (methodName )
201- || "getInitializationErrorString" .equals (methodName )
202- || "getUnknownFields" .equals (methodName )
203- || "getDetailOrBuilderList" .equals (methodName )
204- || "getAllFieldsMutable" .equals (methodName )
205- || "getAllFieldsRaw" .equals (methodName )
206- || "getOneofFieldDescriptor" .equals (methodName )
207- || "getField" .equals (methodName )
208- || "getFieldRaw" .equals (methodName )
209- || "getRepeatedFieldCount" .equals (methodName )
210- || "getRepeatedField" .equals (methodName )
211- || "getSerializedSize" .equals (methodName )
212- || "getMethodOrDie" .equals (methodName )
213- || "getReader" .equals (methodName )
214- || "getInputStream" .equals (methodName )
215- || "getWriter" .equals (methodName )
216- || "getOutputStream" .equals (methodName )
217- || "getParameterNames" .equals (methodName )
218- || "getParameterMap" .equals (methodName )
219- || "getHeaderNames" .equals (methodName )
220- || methodName .endsWith ("Bytes" )
221- || method .getParameterCount () != 0 ) {
222- return false ;
223- }
224-
225- return isAllowTaintType (method .getReturnType ());
226- }
227-
228- public static void trackObject (MethodEvent event , PolicyNode policyNode , Object obj , int depth ) {
138+ public static void trackObject (MethodEvent event , PolicyNode policyNode , Object obj , int depth , Boolean isMicroservice ) {
229139 if (depth >= 10 || !TaintPoolUtils .isNotEmpty (obj ) || !TaintPoolUtils .isAllowTaintType (obj )) {
230140 return ;
231141 }
@@ -241,21 +151,21 @@ public static void trackObject(MethodEvent event, PolicyNode policyNode, Object
241151
242152 Class <?> cls = obj .getClass ();
243153 if (cls .isArray () && !cls .getComponentType ().isPrimitive ()) {
244- trackArray (event , policyNode , obj , depth );
154+ trackArray (event , policyNode , obj , depth , isMicroservice );
245155 } else if (obj instanceof Iterator && !(obj instanceof Enumeration )) {
246- trackIterator (event , policyNode , (Iterator <?>) obj , depth );
156+ trackIterator (event , policyNode , (Iterator <?>) obj , depth , isMicroservice );
247157 } else if (obj instanceof Map ) {
248- trackMap (event , policyNode , (Map <?, ?>) obj , depth );
158+ trackMap (event , policyNode , (Map <?, ?>) obj , depth , isMicroservice );
249159 } else if (obj instanceof Map .Entry ) {
250- trackMapEntry (event , policyNode , (Map .Entry <?, ?>) obj , depth );
160+ trackMapEntry (event , policyNode , (Map .Entry <?, ?>) obj , depth , isMicroservice );
251161 } else if (obj instanceof Collection && !(obj instanceof Enumeration )) {
252162 if (obj instanceof List ) {
253- trackList (event , policyNode , (List <?>) obj , depth );
163+ trackList (event , policyNode , (List <?>) obj , depth , isMicroservice );
254164 } else {
255- trackIterator (event , policyNode , ((Collection <?>) obj ).iterator (), depth );
165+ trackIterator (event , policyNode , ((Collection <?>) obj ).iterator (), depth , isMicroservice );
256166 }
257167 } else if ("java.util.Optional" .equals (obj .getClass ().getName ())) {
258- trackOptional (event , policyNode , obj , depth );
168+ trackOptional (event , policyNode , obj , depth , isMicroservice );
259169 } else {
260170 if (isSourceNode ) {
261171 int len = TaintRangesBuilder .getLength (obj );
@@ -276,6 +186,17 @@ public static void trackObject(MethodEvent event, PolicyNode policyNode, Object
276186 EngineManager .TAINT_HASH_CODES .add (hash );
277187 event .addTargetHash (hash );
278188 EngineManager .TAINT_RANGES_POOL .add (hash , tr );
189+ if (isMicroservice && !(obj instanceof String )) {
190+ try {
191+ Field [] declaredFields = ReflectUtils .getDeclaredFieldsSecurity (cls );
192+ for (Field field : declaredFields ) {
193+ trackObject (event , policyNode , field .get (obj ), depth + 1 , isMicroservice );
194+ }
195+ } catch (Throwable e ) {
196+ DongTaiLog .debug ("solve model failed: {}, {}" ,
197+ e .getMessage (), e .getCause () != null ? e .getCause ().getMessage () : "" );
198+ }
199+ }
279200 } else {
280201 hash = System .identityHashCode (obj );
281202 if (EngineManager .TAINT_HASH_CODES .contains (hash )) {
@@ -285,41 +206,41 @@ public static void trackObject(MethodEvent event, PolicyNode policyNode, Object
285206 }
286207 }
287208
288- private static void trackArray (MethodEvent event , PolicyNode policyNode , Object arr , int depth ) {
209+ private static void trackArray (MethodEvent event , PolicyNode policyNode , Object arr , int depth , Boolean isMicroservice ) {
289210 int length = Array .getLength (arr );
290211 for (int i = 0 ; i < length ; i ++) {
291- trackObject (event , policyNode , Array .get (arr , i ), depth + 1 );
212+ trackObject (event , policyNode , Array .get (arr , i ), depth + 1 , isMicroservice );
292213 }
293214 }
294215
295- private static void trackIterator (MethodEvent event , PolicyNode policyNode , Iterator <?> it , int depth ) {
216+ private static void trackIterator (MethodEvent event , PolicyNode policyNode , Iterator <?> it , int depth , Boolean isMicroservice ) {
296217 while (it .hasNext ()) {
297- trackObject (event , policyNode , it .next (), depth + 1 );
218+ trackObject (event , policyNode , it .next (), depth + 1 , isMicroservice );
298219 }
299220 }
300221
301- private static void trackMap (MethodEvent event , PolicyNode policyNode , Map <?, ?> map , int depth ) {
222+ private static void trackMap (MethodEvent event , PolicyNode policyNode , Map <?, ?> map , int depth , Boolean isMicroservice ) {
302223 for (Object key : map .keySet ()) {
303- trackObject (event , policyNode , key , depth + 1 );
304- trackObject (event , policyNode , map .get (key ), depth + 1 );
224+ trackObject (event , policyNode , key , depth + 1 , isMicroservice );
225+ trackObject (event , policyNode , map .get (key ), depth + 1 , isMicroservice );
305226 }
306227 }
307228
308- private static void trackMapEntry (MethodEvent event , PolicyNode policyNode , Map .Entry <?, ?> entry , int depth ) {
309- trackObject (event , policyNode , entry .getKey (), depth + 1 );
310- trackObject (event , policyNode , entry .getValue (), depth + 1 );
229+ private static void trackMapEntry (MethodEvent event , PolicyNode policyNode , Map .Entry <?, ?> entry , int depth , Boolean isMicroservice ) {
230+ trackObject (event , policyNode , entry .getKey (), depth + 1 , isMicroservice );
231+ trackObject (event , policyNode , entry .getValue (), depth + 1 , isMicroservice );
311232 }
312233
313- private static void trackList (MethodEvent event , PolicyNode policyNode , List <?> list , int depth ) {
234+ private static void trackList (MethodEvent event , PolicyNode policyNode , List <?> list , int depth , Boolean isMicroservice ) {
314235 for (Object obj : list ) {
315- trackObject (event , policyNode , obj , depth + 1 );
236+ trackObject (event , policyNode , obj , depth + 1 , isMicroservice );
316237 }
317238 }
318239
319- private static void trackOptional (MethodEvent event , PolicyNode policyNode , Object obj , int depth ) {
240+ private static void trackOptional (MethodEvent event , PolicyNode policyNode , Object obj , int depth , Boolean isMicroservice ) {
320241 try {
321242 Object v = ((Optional <?>) obj ).orElse (null );
322- trackObject (event , policyNode , v , depth + 1 );
243+ trackObject (event , policyNode , v , depth + 1 , isMicroservice );
323244 } catch (Throwable ignore ) {
324245 }
325246 }
0 commit comments