class Transformer extends RefType { Transformer() { this.getASupertype().hasQualifiedName("org.apache.commons.collections", "Transformer") and exists(Method m | m = this.getAMethod() and m.getName() = "transform" ) }
Method getTransformMethod() { result= this.getAMethod() and result.getName() = "transform" }
}
from Transformer t select t, t.getTransformMethod()
class TransformerCall extends MethodCall { TransformerCall() { this.getMethod().getDeclaringType().hasQualifiedName("org.apache.commons.collections", "Transformer") and this.getMethod().getName() = "transform" and exists(Method m | m = this.getEnclosingCallable() and m.getName() != "transform" ) } }
predicate isSerializable(RefType rt) { exists(RefType st | rt.hasSupertype(st) and st.hasQualifiedName("java.io", "Serializable") ) }
from TransformerCall tc where isSerializable(tc.getEnclosingCallable().getDeclaringType()) select tc, tc.getEnclosingCallable(), tc.getEnclosingCallable().getDeclaringType()
/** * 检查字段初始化是否为Map接口类型 */ predicate fieldInitializedWithMapInterface() { this.isFieldAccess() and exists(Field field | field = this.getAccessedField() | // 字段初始化为Map接口 exists(Expr init | init = field.getInitializer() | exists(MethodCall mc | mc = init and isMapInterfaceReturnType(mc.getMethod()) ) or exists(RefType flowType | exprTypeFlow(init, flowType, true) and isMapInterfaceType(flowType) ) ) ) }
/** * 检查字段是否在任何方法中被赋值为具体实现类(关键新增功能) */ predicate fieldContaminatedByConcreteAssignment() { this.isFieldAccess() and exists(Field field, Assignment assign | field = this.getAccessedField() and assign.getDest().(FieldAccess).getField() = field and ( // 直接赋值为具体实现类构造函数 exists(ClassInstanceExpr cie | cie = assign.getSource() and isConcreteMapImplementation(cie.getConstructedType()) ) or // 赋值为返回具体实现类的方法调用 exists(MethodCall mc | mc = assign.getSource() and isConcreteMapImplementation(mc.getMethod().getReturnType()) ) or // 使用类型流分析检测具体实现类 exists(RefType flowType | exprTypeFlow(assign.getSource(), flowType, true) and isConcreteMapImplementation(flowType) ) ) ) }
/** * 检查字段是否在构造函数中被赋值为具体实现类 */ predicate fieldContaminatedInConstructor() { this.isFieldAccess() and exists(Field field, Constructor constructor, Assignment assign | field = this.getAccessedField() and constructor.getDeclaringType() = field.getDeclaringType() and assign.getEnclosingCallable() = constructor and assign.getDest().(FieldAccess).getField() = field and ( exists(ClassInstanceExpr cie | cie = assign.getSource() and isConcreteMapImplementation(cie.getConstructedType()) ) or exists(RefType flowType | exprTypeFlow(assign.getSource(), flowType, true) and isConcreteMapImplementation(flowType) ) ) ) }
/** * 检查字段是否被任何形式的具体实现类污染 */ predicate fieldIsContaminated() { this.fieldContaminatedByConcreteAssignment() or this.fieldContaminatedInConstructor() or // 字段初始化就是具体实现类 ( this.isFieldAccess() and exists(Field field | field = this.getAccessedField() | exists(Expr init | init = field.getInitializer() | exists(ClassInstanceExpr cie | cie = init and isConcreteMapImplementation(cie.getConstructedType()) ) or exists(RefType flowType | exprTypeFlow(init, flowType, true) and isConcreteMapImplementation(flowType) ) ) ) ) }
/** * 检查是否所有流入的定义都是Map接口类型 */ predicate allDefinitionsAreMapInterface() { // 情况1:变量访问且所有SSA定义都是Map接口 ( not this.isFieldAccess() and exists(this.getAFlowingDefinition()) and forall(SsaVariable def | def = this.getAFlowingDefinition() | this.ssaDefinesMapInterface(def) ) ) or // 情况2:字段访问且字段类型是Map接口,且没有被具体实现类污染 ( this.isFieldAccess() and this.fieldIsMapInterface() and not this.fieldIsContaminated() and (this.fieldInitializedWithMapInterface() or not exists(this.getAccessedField().getInitializer())) ) }
/** * 检查是否为Map接口类型(而不是具体实现类) */ predicate isMapInterfaceType(Type type) { // 直接是Map接口 type.(RefType).getSourceDeclaration().hasQualifiedName("java.util", "Map") or // 参数化的Map接口,如Map<String, Object> exists(ParameterizedType pt | pt = type and pt.getSourceDeclaration().hasQualifiedName("java.util", "Map") ) }
/** * 检查是否为具体的Map实现类 */ predicate isConcreteMapImplementation(RefType type) { type.getSourceDeclaration().hasQualifiedName("java.util", "HashMap") or type.getSourceDeclaration().hasQualifiedName("java.util", "LinkedHashMap") or type.getSourceDeclaration().hasQualifiedName("java.util", "TreeMap") or type.getSourceDeclaration().hasQualifiedName("java.util", "ConcurrentHashMap") or type.getSourceDeclaration().hasQualifiedName("java.util", "WeakHashMap") or type.getSourceDeclaration().hasQualifiedName("java.util", "IdentityHashMap") or type.getSourceDeclaration().hasQualifiedName("java.util", "EnumMap") or // 其他常见Map实现 (type instanceof MapType and not type.getSourceDeclaration().hasQualifiedName("java.util", "Map") and type.getSourceDeclaration().getASourceSupertype*().hasQualifiedName("java.util", "Map")) }
/** * 检查方法是否返回Map接口类型(常见的工厂方法) */ predicate isMapInterfaceReturnType(Method method) { // Collections类的静态方法 method.getDeclaringType().hasQualifiedName("java.util", "Collections") and method.getName().regexpMatch("(empty|singleton|unmodifiable|synchronized).*Map.*") and isMapInterfaceType(method.getReturnType()) or // 其他返回Map接口的工厂方法 method.getReturnType().(RefType).getSourceDeclaration().hasQualifiedName("java.util", "Map") or // Map接口的默认方法或静态方法 method.getDeclaringType().hasQualifiedName("java.util", "Map") and isMapInterfaceType(method.getReturnType()) }
/** * 辅助谓词:检查是否为序列化类 */ predicate isSerializableClass(RefType rt) { exists(RefType st | rt.hasSupertype(st) and st.hasQualifiedName("java.io", "Serializable") ) }
// 主查询 from AdvancedMapCall amc where // 声明类型是Map接口 amc.hasDeclaredMapInterfaceType() and // 所有定义都是Map接口类型(包括字段访问且未被污染) amc.allDefinitionsAreMapInterface() and // 在序列化类中(可选过滤条件) isSerializableClass(amc.getEnclosingCallable().getDeclaringType()) select amc, "Map 使用点", amc.getMapInterfaceInfo(), "映射接口信息", amc.getEnclosingCallable(), "方法所在的位置", amc.getEnclosingCallable().getDeclaringType(), "所属类"