-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Quarkus REST Jackson: Improve detection of generic fields annotated with the @SecureField
and allow to explicitly enable secure serialization
#44669
Merged
geoand
merged 1 commit into
quarkusio:main
from
michalvavrik:feature/improve-secure-field-detection
Nov 25, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
.../io/quarkus/resteasy/reactive/jackson/deployment/test/DisableSecureSerializationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package io.quarkus.resteasy.reactive.jackson.deployment.test; | ||
|
||
import jakarta.ws.rs.Consumes; | ||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.Path; | ||
import jakarta.ws.rs.Produces; | ||
import jakarta.ws.rs.core.MediaType; | ||
|
||
import org.hamcrest.Matchers; | ||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.resteasy.reactive.jackson.DisableSecureSerialization; | ||
import io.quarkus.resteasy.reactive.jackson.EnableSecureSerialization; | ||
import io.quarkus.resteasy.reactive.jackson.SecureField; | ||
import io.quarkus.security.test.utils.TestIdentityController; | ||
import io.quarkus.security.test.utils.TestIdentityProvider; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.restassured.RestAssured; | ||
import io.restassured.response.ValidatableResponse; | ||
|
||
public class DisableSecureSerializationTest { | ||
|
||
@RegisterExtension | ||
static QuarkusUnitTest test = new QuarkusUnitTest() | ||
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class) | ||
.addClasses(TestIdentityProvider.class, TestIdentityController.class)); | ||
|
||
@Test | ||
public void testDisablingOfSecureSerialization() { | ||
request("disabled", "user").body("secretField", Matchers.is("secret")); | ||
request("disabled", "admin").body("secretField", Matchers.is("secret")); | ||
request("enabled", "user").body("secretField", Matchers.nullValue()); | ||
request("enabled", "admin").body("secretField", Matchers.is("secret")); | ||
} | ||
|
||
private static ValidatableResponse request(String subPath, String user) { | ||
TestIdentityController.resetRoles().add(user, user, user); | ||
return RestAssured | ||
.with() | ||
.auth().preemptive().basic(user, user) | ||
.get("/test/" + subPath) | ||
.then() | ||
.statusCode(200) | ||
.body("publicField", Matchers.is("public")); | ||
} | ||
|
||
@DisableSecureSerialization | ||
@Produces(MediaType.APPLICATION_JSON) | ||
@Consumes(MediaType.APPLICATION_JSON) | ||
@Path("test") | ||
public static class GreetingsResource { | ||
|
||
@Path("disabled") | ||
@GET | ||
public Dto disabled() { | ||
return Dto.createDto(); | ||
} | ||
|
||
@EnableSecureSerialization | ||
@Path("enabled") | ||
@GET | ||
public Dto enabled() { | ||
return Dto.createDto(); | ||
} | ||
} | ||
|
||
public static class Dto { | ||
|
||
public Dto(String secretField, String publicField) { | ||
this.secretField = secretField; | ||
this.publicField = publicField; | ||
} | ||
|
||
@SecureField(rolesAllowed = "admin") | ||
private final String secretField; | ||
|
||
private final String publicField; | ||
|
||
public String getSecretField() { | ||
return secretField; | ||
} | ||
|
||
public String getPublicField() { | ||
return publicField; | ||
} | ||
|
||
private static Dto createDto() { | ||
return new Dto("secret", "public"); | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
.../deployment/src/test/java/io/quarkus/resteasy/reactive/jackson/deployment/test/Fruit.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package io.quarkus.resteasy.reactive.jackson.deployment.test; | ||
|
||
import java.util.List; | ||
|
||
public class Fruit { | ||
|
||
public String name; | ||
|
||
public List<Price> prices; | ||
|
||
public Fruit(String name, Float price) { | ||
this.name = name; | ||
this.prices = List.of(new Price("USD", price)); | ||
} | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
...nt/src/test/java/io/quarkus/resteasy/reactive/jackson/deployment/test/GenericWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package io.quarkus.resteasy.reactive.jackson.deployment.test; | ||
|
||
public class GenericWrapper<T> { | ||
|
||
public String name; | ||
|
||
public T entity; | ||
|
||
public GenericWrapper(String name, T entity) { | ||
this.name = name; | ||
this.entity = entity; | ||
} | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
.../deployment/src/test/java/io/quarkus/resteasy/reactive/jackson/deployment/test/Price.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package io.quarkus.resteasy.reactive.jackson.deployment.test; | ||
|
||
import io.quarkus.resteasy.reactive.jackson.SecureField; | ||
|
||
public class Price { | ||
|
||
@SecureField(rolesAllowed = "admin") | ||
public Float price; | ||
|
||
public String currency; | ||
|
||
public Price(String currency, Float price) { | ||
this.currency = currency; | ||
this.price = price; | ||
} | ||
|
||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have better type mapping functions already in place that do better than just handling simple cases like the field type being a type variable. Probably this could be improved.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you be more specific? Both in scenarios you have in mind and where these mapping functions are already in place, because I don't know what you mean.
Also, I didn't want to do better because last time I improved this detection there was a complain that it prolongs build-time execution. So if your proposal won't have negative effect, I'm happy to apply it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Few hints should do, I just want to better understand what do you mean and make sure it wouldn't lead to increased build-time execution.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking of the logic we already do to resolve type parameters in resteasy reactive. I think via
JandexUtil
andTypeMapper
, but I can only find usages of them to obtain method parameter or return type signatures or the list of type parameters. I can't find anywhere where we resolve an entireType
with type parameters substituted. Probably I dreamed this :(There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
np, if you run into this or @geoand knows, please let me know. it's not on the top of my list tbh, so no hurry
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AFAIR, the cases we've it for are method parameters and method return types