Mitigation Strategy: Avoid Default Typing
Description:
- Identify Usage: Search the codebase for any instances of
ObjectMapper.enableDefaultTyping()
. This method call is the primary indicator of Default Typing being enabled. - Refactor to Explicit Type Information: Replace Default Typing with explicit type information using Jackson annotations. This means using
@JsonTypeInfo
,@JsonSubTypes
, and@JsonTypeName
on your classes and interfaces to define how type information is handled during serialization and deserialization. Jackson uses these annotations to determine the correct class to instantiate. - Remove
enableDefaultTyping()
: After refactoring, remove all calls toObjectMapper.enableDefaultTyping()
. - Test Thoroughly: Extensive testing is crucial after this refactoring to ensure correct behavior.
List of Threats Mitigated:
- Remote Code Execution (RCE) (Critical): Eliminates the primary mechanism for arbitrary class instantiation via malicious JSON.
- Denial of Service (DoS) (High): Reduces the attack surface related to type handling.
Impact:
- RCE: Risk is drastically reduced (almost eliminated with correct implementation).
- DoS: Risk is reduced.
Currently Implemented:
- Partially Implemented: Annotations are used in some areas (
com.example.models
), butenableDefaultTyping()
is still present incom.example.legacy.LegacyDataProcessor
.
Missing Implementation:
com.example.legacy.LegacyDataProcessor
: Requires refactoring to removeenableDefaultTyping()
and use explicit type annotations.
Mitigation Strategy: Use a Safe Type Validator (if Default Typing is unavoidable)
Description: (Only if removing Default Typing is impossible)
- Identify
enableDefaultTyping()
Usage: Locate all instances ofObjectMapper.enableDefaultTyping()
. - Create a
PolymorphicTypeValidator
:- Create a class extending
PolymorphicTypeValidator.Base
(or implementingPolymorphicTypeValidator
). - Override
validateSubClassName()
(and potentially others). - Implement a whitelist of allowed classes within
validateSubClassName()
. Never use a blacklist.
- Create a class extending
- Configure the
ObjectMapper
:- Instantiate your custom
PolymorphicTypeValidator
. - Use
JsonMapper.builder().activateDefaultTyping(ptv, ...)
to configure theObjectMapper
with your validator.
- Instantiate your custom
- Maintain the Whitelist: This is absolutely critical. The whitelist must be kept up-to-date.
- Alternatively, use
BasicPolymorphicTypeValidator
: For Jackson 2.10+, use the built-inBasicPolymorphicTypeValidator
for easier whitelist configuration.
List of Threats Mitigated:
- Remote Code Execution (RCE) (Critical): Restricts class instantiation to the whitelisted classes.
- Denial of Service (DoS) (High): Indirectly mitigated.
Impact:
- RCE: Risk is significantly reduced, dependent on whitelist accuracy.
- DoS: Risk is reduced.
Currently Implemented:
- Not Implemented: No custom
PolymorphicTypeValidator
is present.
Missing Implementation:
com.example.legacy.LegacyDataProcessor
: If Default Typing cannot be removed, aPolymorphicTypeValidator
must be implemented.
Mitigation Strategy: Limit Deserialization Depth and Data Size via StreamReadConstraints
Description:
- Determine Limits: Analyze your application to determine reasonable limits for JSON structure complexity.
- Implement
StreamReadConstraints
(Jackson 2.13+):- Create a
StreamReadConstraints
instance using its builder. - Set limits:
maxNestingDepth()
,maxStringLength()
,maxNumberLength()
,maxNameLength()
,setMaxArrayElements()
,setMaxObjectEntries()
. - Create a
JsonFactory
using its builder and set theStreamReadConstraints
. - Create an
ObjectMapper
using the configuredJsonFactory
.
- Create a
- Older Jackson Versions (Pre-2.13): This strategy is not directly available. You would need to resort to manual input size checks before passing data to Jackson, which is outside the scope of "directly involving jackson-core". The
JsonParser.Feature.STRICT_DUPLICATE_DETECTION
feature is available, but it only addresses duplicate keys, not size/depth limits.
List of Threats Mitigated:
- Denial of Service (DoS) (High): Prevents resource exhaustion from overly complex JSON.
- Algorithmic Complexity Attacks (High): A specific type of DoS.
Impact:
- DoS: Risk is significantly reduced.
- Algorithmic Complexity: Risk is significantly reduced.
Currently Implemented:
- Partially Implemented:
StreamReadConstraints
are used incom.example.api.ApiController
, but not comprehensively (only string length).
Missing Implementation:
com.example.services.DataImportService
: NeedsStreamReadConstraints
to handle potentially large JSON files.com.example.legacy.LegacyDataProcessor
: Also needs limits (ideallyStreamReadConstraints
if Jackson version allows).
Mitigation Strategy: Keep Jackson Up-to-Date
Description:
- Check Current Version: Find the
jackson-core
(andjackson-databind
) version in your dependency management file. - Identify Latest Stable Version: Check the Jackson project website for the latest release.
- Update Dependencies: Modify your dependency management file to use the latest version.
- Test Thoroughly: Run your test suite after updating.
List of Threats Mitigated:
- All Known Vulnerabilities (Variable Severity): Addresses known security issues in Jackson itself.
Impact:
- All Known Vulnerabilities: Risk is significantly reduced.
Currently Implemented:
- Partially Implemented: The project uses a recent, but not the absolute latest, version.
Missing Implementation:
- Project-Wide: Implement a more automated update process.
Mitigation Strategy: Use @JsonTypeInfo
Correctly
Description:
- Review Existing Usage: Examine all uses of
@JsonTypeInfo
. - Prefer
Id.NAME
: Useuse = JsonTypeInfo.Id.NAME
whenever possible. This uses logical type names, which are safer than class names. - Use
As.PROPERTY
: Generally, preferinclude = JsonTypeInfo.As.PROPERTY
. - Meaningful Property Name: Use a clear name for the
property
attribute (e.g.,@JsonTypeInfo(..., property = "classType")
). - Consider Custom Resolvers: If needed use custom
JsonTypeResolver
andJsonTypeIdResolver
.
List of Threats Mitigated:
- Remote Code Execution (RCE) (Critical): Reduces the risk of attackers controlling type information.
- Data Integrity (Medium): Ensures consistent type handling.
Impact:
- RCE: Risk is reduced.
- Data Integrity: Risk is reduced.
Currently Implemented:
- Partially Implemented:
@JsonTypeInfo
is used, but not always with the safest settings (some useId.CLASS
).
Missing Implementation:
com.example.models
: Review and update@JsonTypeInfo
usage to preferId.NAME
andAs.PROPERTY
.
Mitigation Strategy: Disable Problematic DeserializationFeatures
Description:
- Review
ObjectMapper
Configuration: Examine allObjectMapper
configurations. - Enable
FAIL_ON_UNKNOWN_PROPERTIES
: Add.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true)
. - Enable
FAIL_ON_INVALID_SUBTYPE
: Add.configure(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE, true)
. - Review Other Features: Carefully consider other
MapperFeature
andDeserializationFeature
options.
List of Threats Mitigated:
- Data Injection (Medium): Prevents injecting unexpected properties.
- Unexpected Behavior (Low): Improves predictability.
Impact:
- Data Injection: Risk is reduced.
- Unexpected Behavior: Risk is reduced.
Currently Implemented:
- Partially Implemented:
FAIL_ON_UNKNOWN_PROPERTIES
is enabled in some configurations, but not all.
Missing Implementation:
- Project-Wide: Ensure consistent use of
FAIL_ON_UNKNOWN_PROPERTIES
andFAIL_ON_INVALID_SUBTYPE
.