Skip to content

Jackon 3: @JsonSubTypes does not affect validity checks #5320

@sdeleuze

Description

@sdeleuze

Search before asking

  • I searched in the issues and found nothing similar.

Describe the bug

Please find below a reproducer for a use case we have for Spring Security Jackson 3 support, where we would like to disable global default typing (for better security) and just rely on @JsonTypeInfo + @JsonSubTypes for polymorphic deserialization, and that seems not possible.

Despite the fact we register a strict set of sub types via @JsonSubTypes to deserialize Object principal, Jackson 3 requires a custom PolymorphicTypeValidator, which we would like to avoid because we try to remove mandatory mapper configuration.

Could DefaultBaseTypeLimitingValidator relax its checks in that case and take in account @JsonSubTypes configured in the mixins?

Version Information

3.0.0-rc10

Reproduction

import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import org.junit.jupiter.api.Test;
import tools.jackson.databind.json.JsonMapper;

import static org.assertj.core.api.Assertions.assertThat;

public class JacksonPTVTests {

	@Test
	void stringPrincipal() {
		JsonMapper mapper = new JsonMapper();
		String json = mapper.writeValueAsString(new User("bob"));
		User user = mapper.readValue(json, User.class);
		assertThat(user.principal()).isEqualTo("bob");
	}

	@Test
	void customPrincipal() {
		JsonMapper mapper = new JsonMapper();
		String json = mapper.writeValueAsString(new User(new CustomPrincipal("bob")));
		User user = mapper.readValue(json, User.class);
		assertThat(user.principal()).isInstanceOf(CustomPrincipal.class);
	}

	@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY)
	record User(
			@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY)
			@JsonSubTypes({ @JsonSubTypes.Type(CustomPrincipal.class)})
			Object principal) {
	}

	record CustomPrincipal(String login) {
	}
}

Generates the following error

Configured `PolymorphicTypeValidator` (of type `tools.jackson.databind.jsontype.DefaultBaseTypeLimitingValidator`) denies resolution of all subtypes of base type `java.lang.Object` as using too generic base type can open a security hole without checks on subtype: please configure a custom `PolymorphicTypeValidator` for this use case

Expected behavior

@JsonSubTypes({ @JsonSubTypes.Type(CustomPrincipal.class)}) is enough to make the Object principal deserialization safe and authorized by default.

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.xIssues to be only tackled for Jackson 3.x, not 2.xpolymorphic-handlingProblem with polymorphic type handling (`@JsonTypeInfo`, Default Typing)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions