magento / magento2

Prior to making any Submission(s), you must sign an Adobe Contributor License Agreement, available here at: https://opensource.adobe.com/cla.html. All Submissions you make to Adobe Inc. and its affiliates, assigns and subsidiaries (collectively “Adobe”) are subject to the terms of the Adobe Contributor License Agreement.
http://www.magento.com
Open Software License 3.0
11.55k stars 9.32k forks source link

GraphQL QueryComplexityLimiter queryComplexity is set too low value and queryDepth probably too high #32427

Open paales opened 3 years ago

paales commented 3 years ago

Preconditions (*)

  1. Magento 2.4.2

Steps to reproduce (*)

I'm trying to fetch all product information for the product page in one go:

When I run this query I get an error: Max query complexity should be 300 but got 388.

Expected result (*)

I'm not doing anything super special and I would expect this to succeed. Especially since the workaround is that I need to make two calls to the Magento backend which will invariably cause more load for the server.

Actual result (*)

The current complexity limit is set to 300: https://github.com/magento/magento2/blob/3ad7a1d3919095cfd432837c148573d5d5be66bf/app/code/Magento/GraphQl/etc/di.xml#L102-L103

There currently is a test set to validate that it isn't more than 300:

https://github.com/magento/magento2/blob/3ad7a1d3919095cfd432837c148573d5d5be66bf/dev/tests/api-functional/testsuite/Magento/GraphQl/Framework/QueryComplexityLimiterTest.php#L398

It seems that the queryDepth here is set relatively high, but the queryComplexity is set relatively low.

Proposed solution

Apollo

If we read this security post from Apollo https://www.apollographql.com/blog/securing-your-graphql-api-from-malicious-queries-16130a324a6b/, it seems more sensible values would be:

graphql-query-complexity library

If we follow the defaults mentioned here: https://github.com/slicknode/graphql-query-complexity/blob/95e2899dd9bc32600114dd04bef5996ceeba0f4a/README.md#usage, we get:

Hasura

Hasura doesn't even offer queryComplexity limits they only offer queryDepth limits:

https://hasura.io/docs/latest/graphql/cloud/api-limits.html#configuring-an-api-limit


Please provide Severity assessment for the Issue as Reporter. This information will help during Confirmation and Issue triage processes.

m2-assistant[bot] commented 3 years ago

Hi @paales. Thank you for your report. To help us process this issue please make sure that you provided the following information:

Please make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, please, add a comment to the issue:

@magento give me 2.4-develop instance - upcoming 2.4.x release

For more details, please, review the Magento Contributor Assistant documentation.

Please, add a comment to assign the issue: @magento I am working on this


:clock10: You can find the schedule on the Magento Community Calendar page.

:telephone_receiver: The triage of issues happens in the queue order. If you want to speed up the delivery of your contribution, please join the Community Contributions Triage session to discuss the appropriate ticket.

:movie_camera: You can find the recording of the previous Community Contributions Triage on the Magento Youtube Channel

:pencil2: Feel free to post questions/proposals/feedback related to the Community Contributions Triage process to the corresponding Slack Channel

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 14 days if no further activity occurs. Is this issue still relevant? If so, what is blocking it? Is there anything you can do to help move it forward? Thank you for your contributions!

paales commented 3 years ago

This isn't stale

m2-assistant[bot] commented 3 years ago

Hi @engcom-Lima. Thank you for working on this issue. In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:

engcom-Lima commented 3 years ago

Hi @paales ,

default query complexity and depth thresholds are configurable via the magento2/app/code/Magento/GraphQl/etc/di.xml file.

20 300

So you are free to adjust these values as deemed fit for your environment. This is the current approach we advocate to users and will have improved in the upcoming 2.4.3 release.

Thanks

paales commented 3 years ago

@engcom-Lima Thanks for the suggestion to modify those values! Changing the defaults would still make sense, right? Or is that what you mentioned for 2.4.3?

bloodf commented 3 years ago

This needs to be put in the core, we can have the configuration as it is. Doing an integration or consuming the GraphQL right now, makes the usage of the API totally worthless.

hnsr commented 2 years ago

@engcom-Lima Can you elaborate on the improvements for 2.4.3? I'm currently on this version, and still find myself running into the queryComplexity limit that seems to be set to 300.

For our application to remain compatible with standard Magento (that is, without requiring one to change the default limits through DI customisation) this would require splitting up such queries, which only introduces more overhead and takes away one of the benefits of GraphQL

joshdavenport commented 1 year ago

300 is still set as the default query complexity limit. This really ought to be assessed.

2.4-develop

https://github.com/magento/magento2/blob/647b34eb3d8bc13c02adca1262aaab389dec1324/app/code/Magento/GraphQl/etc/di.xml#L104

2.5-develop

https://github.com/magento/magento2/blob/6ac4cda5de1147c529fb8102bbe0cf6d59984153/app/code/Magento/GraphQl/etc/di.xml#L103

It's not difficult to change in a project, but also not difficult to set a more reasonable default that reduces the chance of devs chasing their tails because of this.