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

Various improvements to ROOT_MUTATION caching #8280

Merged
merged 6 commits into from
May 26, 2021

Conversation

benjamn
Copy link
Member

@benjamn benjamn commented May 24, 2021

Fixes #7665 by allowing field policy read functions to run for fields within mutation results, making it easier for mutation result data to use custom scalar types (to borrow @stephenh's use case from #7665).

In order to keep each ROOT_MUTATION write isolated from others that might be happening at the same time, this PR also removes the temporary ROOT_MUTATION object from the cache immediately after the update function runs, so information like arguments passed to mutation root fields will not be retained in the cache longer than need be, thereby resolving #3592. To be clear, the Apollo Client team does not believe the previous behavior of leaving ROOT_MUTATION in the cache created any meaningful opportunities for attackers to read cached data, but we believe in defense in depth and agree with the principles articulated by @danilobuerger in #3592 (comment).

The removal/impermanence of ROOT_MUTATION could be a disruptive change for anyone who depends on reading ROOT_MUTATION data from the cache after the mutation has finished, but that's not encouraged or well supported right now, so we think/hope the potential disruption is justified in order to achieve better security by default. We can revisit this before Apollo Client v3.4 is released, if anyone reports problems.

src/core/QueryManager.ts Outdated Show resolved Hide resolved
Copy link
Member

@hwillson hwillson 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 @benjamn! And as discussed earlier today, I'm 👍 on this going into 3.4.

@brainkim brainkim requested review from brainkim and removed request for brainkim May 26, 2021 00:40
Should alleviate #3592, while also helping to isolate mutations from one
another, so multiple mutation results never overlap in the cache.
While I was working with this code, I noticed that the fetchPolicy:
"no-cache" option for mutations was not always respected (cache writes
would happen anyway), so I fixed that.
I suspect this change will be slightly less disruptive than completely
removing the ROOT_MUTATION object from the cache after each mutation.

It's safe to keep __typename because it isn't sensitive information,
like the arguments of root mutation fields.

It's convenient to keep __typename because that data (and the enclosing
object) would otherwise have to be recreated the next time a mutation
writes its results into the cache.

We could use the mutation.document root fields to perform this removal
more precisely, but that's more work for no clear gain.
This option should limit the disruptiveness of PR #8280 (even though
removing ROOT_MUTATION fields is the default behavior), because at least
there's an easy way to achieve the old behavior again (that is, by
passing `keepRootFields: true` to `client.mutate`).
@benjamn benjamn merged commit 244e635 into release-3.4 May 26, 2021
@benjamn benjamn deleted the ROOT_MUTATION-caching-adjustments branch May 26, 2021 16:12
@benjamn
Copy link
Member Author

benjamn commented May 26, 2021

@hwillson @brainkim Heads up: I added a new mutation option called keepRootFields?: boolean to allow callers of client.mutate or useMutation to preserve the ROOT_MUTATION cache data. The default is still false, but I think this escape valve will help limit the severity of any breaking changes.

@hwillson
Copy link
Member

Great plan @benjamn - thanks!

@hwillson hwillson removed this from the MM-2021-06 milestone Jul 29, 2021
@benjamn benjamn mentioned this pull request Aug 20, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants