Skip to content
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

Spring Data REST #13373

Merged
merged 1 commit into from
Jan 4, 2021
Merged

Spring Data REST #13373

merged 1 commit into from
Jan 4, 2021

Conversation

gytis
Copy link

@gytis gytis commented Nov 19, 2020

cc @geoand

@ghost ghost added area/core area/dependencies Pull requests that update a dependency file area/documentation area/panache area/spring Issues relating to the Spring integration labels Nov 19, 2020
@gytis
Copy link
Author

gytis commented Nov 19, 2020

The quickstart PR quarkusio/quarkus-quickstarts#714

Copy link
Contributor

@geoand geoand left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job!!!

I've added some minor comments

getRepositoriesToImplement(index, PAGING_AND_SORTING_REPOSITORY_INTERFACE, JPA_REPOSITORY_INTERFACE));
}

private void implementResources(BuildProducer<GeneratedBeanBuildItem> implementationsProducer,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to have a comment on this method describing what steps it performs and why they are needed

@ghost ghost added the area/infra-automation anything related to CI, bots, etc. that are used to automated our infrastructure label Nov 20, 2020
@gytis gytis force-pushed the spring-data-rest branch 2 times, most recently from 6b8eed6 to fe0fc4d Compare November 20, 2020 09:13
Copy link
Contributor

@geoand geoand left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is awesome, thanks!

@geoand
Copy link
Contributor

geoand commented Nov 20, 2020

Mind rebasing onto master to pick up some CI fixes?

@gytis
Copy link
Author

gytis commented Nov 20, 2020

Done

@geoand geoand added the triage/waiting-for-ci Ready to merge when CI successfully finishes label Nov 20, 2020
@geoand geoand added this to the 1.11 - master milestone Nov 20, 2020
@geoand
Copy link
Contributor

geoand commented Nov 20, 2020

Seems like there is a compilation failure now

@geoand
Copy link
Contributor

geoand commented Nov 23, 2020

Also, it would be best to rebase onto master again, as there were more changes that should make CI happier.

Thanks

Copy link
Member

@gsmet gsmet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a bunch of comments, mostly on documentation.

<dependency><!-- TODO create Quarkus Spring Data REST API jar -->
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-rest-core</artifactId>
<version>3.3.0.RELEASE</version>
Copy link
Member

@gsmet gsmet Nov 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be in the BOM once you have created the artifact.


ResultHandle page = methodCreator.getMethodParam(0);
ResultHandle sort = methodCreator.getMethodParam(1);
ResultHandle pageable = toPageable(methodCreator, page, sort);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given we are taking the sort parameter from the REST URL, can we check it's not sensitive to SQL injections?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here page and sort are already io.quarkus.panache.common.Page and io.quarkus.panache.common.Sort. They're instantiated from the query parameters by rest-data-panache extension.
This extension (same as Hibernate and MongoDB Data REST alternatives) handle only data access logic. JAX-RS resource is handled in the same way for all of them by the rest-data-panache.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I disagree with that.

Your extension makes it visible and applies absolutely no checks on the content coming from the URL. It's different when it's just Panache API, people can do their own checks.

So we need to check it's not sensitive to SQL injections otherwise you have a big security risk.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not saying that it shouldn't be checked. I'm saying that this is not the place to do it. This extension does not handle the JAX-RS logic. It only implements user defined interface that extends https://github.com/quarkusio/quarkus/blob/master/extensions/panache/rest-data-panache/runtime/src/main/java/io/quarkus/rest/data/panache/RestDataResource.java.

rest-data-panache generates the JAX-RS resource and then calls spring-data-rest generated method for data access. So these two page and sort parameters are already instances of io.quarkus.panache.common.Page and io.quarkus.panache.common.Sort.

If we need to add query parameter validation we need to add it here https://github.com/quarkusio/quarkus/blob/master/extensions/panache/rest-data-panache/deployment/src/main/java/io/quarkus/rest/data/panache/deployment/methods/ListMethodImplementor.java#L104. However, wouldn't Panache Query builder escape all the query parameters automatically?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not saying that it shouldn't be checked. I'm saying that this is not the place to do it.

Well, I haven't really reviewed the other extension so I see it now for this one. I still think this should carefully be addressed.

However, wouldn't Panache Query builder escape all the query parameters automatically?

It didn't a while ago. That's why I think we should check if it's safe or not.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@FroMage maybe you know how unsafe it is to pass arbitrary parameters to the Panache Sort object? Are they somehow parsed down the line by Panache or Hibernate/Mongo? If this is unsafe, do you have a recommendation on what type of validation is needed?

In any case, I don't think that this issue should holdup this PR. If the validation has to be added, it will be added to the rest-data-panache extension.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's indeed unsafe because we end up generating an HQL query in PanacheJpaUtil.toOrderBy (not sure about Mongo but probably it is too).

Indeed the check really belongs in the REST layer as you say, especially wrt error handling. As to what you can check, here's what HQL allows:

orderByClause
// todo (6.0) : null precedence
	: ORDER BY sortSpecification (COMMA sortSpecification)*
	;

sortSpecification
	: expression collationSpecification? orderingSpecification?
	;

collationSpecification
	:	COLLATE collateName
	;

collateName
	:	dotIdentifierSequence
	;

orderingSpecification
	:	ASC
	|	DESC
	;

expression is particularly nasty because it's pretty much a free for all.

OTOH if your REST API specifies that the supported set of sort options is limited, then you can probably limit it to:

IDENTIFIER
	:	('a'..'z'|'A'..'Z'|'_'|'$'|'\u0080'..'\ufffe')('a'..'z'|'A'..'Z'|'_'|'$'|'0'..'9'|'\u0080'..'\ufffe')*

HTH :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, all this is from the panacheql extension, you have the lexer and parser files there with all the info you need ;)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @FroMage, this is very helpful. I'll add a query parameter validator to the rest-data-extension and will raise a new PR with it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've created a separate pull request to add a sort parameter validation #13514

@gytis gytis force-pushed the spring-data-rest branch 3 times, most recently from 1e017ce to 16a005d Compare November 23, 2020 11:48
@geoand
Copy link
Contributor

geoand commented Nov 23, 2020

CI is reporting some NPEs

@gytis
Copy link
Author

gytis commented Nov 24, 2020

CI is reporting some NPEs

Seems that the problem is with java.lang.NoSuchMethodError: java.lang.Math.floorDiv(JI)J here https://github.com/quarkusio/quarkus/pull/13373/files#diff-d42992bf5a468b46cc7a587c9a189baa7b54fdf2c005159a4f38de943c5be6c7R68. I'll look into it.

@gytis
Copy link
Author

gytis commented Nov 24, 2020

Seems that the method is not available in Java 8. I'll replace it.

@gytis
Copy link
Author

gytis commented Nov 24, 2020

Minimised API JAR PR quarkusio/quarkus-spring-data-api#5

@gsmet
Copy link
Member

gsmet commented Nov 24, 2020

@gytis you haven't taken into account all my review comments (probably because some are hidden by GH by default). Could you adjust?

@gytis
Copy link
Author

gytis commented Nov 24, 2020

@gsmet you're right, I didn't see them. Thanks!

@gsmet gsmet modified the milestones: 1.11.0.Beta1, 1.11 - master Dec 17, 2020
@gsmet gsmet modified the milestones: 1.11.0.Beta2, 1.11 - master Dec 24, 2020
@gsmet gsmet merged commit 75510b1 into quarkusio:master Jan 4, 2021
@gsmet
Copy link
Member

gsmet commented Jan 4, 2021

Merged, thanks!

@gytis gytis deleted the spring-data-rest branch February 17, 2021 11:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/core area/dependencies Pull requests that update a dependency file area/documentation area/infra-automation anything related to CI, bots, etc. that are used to automated our infrastructure area/panache area/spring Issues relating to the Spring integration release/noteworthy-feature triage/waiting-for-ci Ready to merge when CI successfully finishes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants