T09 · Insecure Skill Coding Practices
Error
- Location
- scripts/generate_structure.py:252
- Finding
- Unsafe Jackson Polymorphic Deserialization in Redis Templates<![CDATA[ ## Vulnerability Details **File Location**: `scripts/generate_structure.py:252-277`; duplicated in `references/redis-config.md:28-53` **Vulnerability Type**: Unsafe polymorphic deserialization **Risk Level**: High ### Vulnerable Code ```java import com.fasterxml.jackson.annotation.JsonTypeInfo; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) { RedisTemplate<String, Object> template = new RedisTemplate<>(); template.setConnectionFactory(connectionFactory); Jackson2JsonRedisSerializer<Object> serializer = new Jackson2JsonRedisSerializer<>(Object.class); ObjectMapper mapper = new ObjectMapper(); mapper.activateDefaultTyping( LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY ); serializer.setObjectMapper(mapper); StringRedisSerializer stringSerializer = new StringRedisSerializer(); template.setKeySerializer(stringSerializer); template.setHashKeySerializer(stringSerializer); template.setValueSerializer(serializer); template.setHashValueSerializer(serializer); template.afterPropertiesSet(); return template; } ``` ### Technical Analysis The generated Redis configuration enables Jackson default typing for all non-final classes and uses `LaissezFaireSubTypeValidator`, which does not impose a meaningful restriction on polymorphic subtypes. Serialized values may consequently include type metadata instructing Jackson which application or dependency class to instantiate. When an application reads an attacker-controlled Redis value through this `RedisTemplate`, Jackson may instantiate a class selected by the attacker. If the generated application's classpath co ...[truncated 2071 chars]
- Remediation
- <![CDATA[ ## Remediation Suggestions 1. Remove `activateDefaultTyping` and `LaissezFaireSubTypeValidator`. 2. Serialize explicitly declared DTO types instead of arbitrary `Object` values. 3. Configure separate, strongly typed Redis serializers for each cache or value category. 4. If polymorphism is unavoidable, use `BasicPolymorphicTypeValidator` with a narrow allowlist of specific packages or classes: ```java BasicPolymorphicTypeValidator validator = BasicPolymorphicTypeValidator.builder() .allowIfSubType("com.example.project.dto.") .build(); ObjectMapper mapper = new ObjectMapper(); mapper.activateDefaultTyping( validator, ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY ); ``` 5. Do not allow framework, JDK, third-party, or general application implementation classes through the validator. 6. Restrict Redis network access, require authentication and transport encryption where supported, and ensure unrelated services do not share writable Redis namespaces. 7. Replace the unsafe example in `references/redis-config.md` so developers do not reintroduce the issue manually. 8. Add tests that reject serialized values containing unauthorized type identifiers. ]]>
