-
Notifications
You must be signed in to change notification settings - Fork 7
DGI9-610: Prevent page_cache caching where an ip cache context is relevant.
#46
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c342205
Prevent page_cache caching where an `ip` cache context is relevant fo…
adam-vessey 4a75b1f
Refactor out to a constant.
adam-vessey 6facd09
Misc linting issues.
adam-vessey 742bbd3
Fix list syntax.
adam-vessey d683061
Simplify request access.
adam-vessey 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,59 @@ | ||
| <?php | ||
|
|
||
| namespace Drupal\embargo\PageCache; | ||
|
|
||
| use Drupal\Core\Cache\RefinableCacheableDependencyInterface; | ||
| use Drupal\Core\PageCache\ResponsePolicyInterface; | ||
| use Drupal\Core\Routing\AccessAwareRouterInterface; | ||
| use Symfony\Component\HttpFoundation\Request; | ||
| use Symfony\Component\HttpFoundation\Response; | ||
|
|
||
| /** | ||
| * Avoid page_cache when IP embargoes are in play. | ||
| * | ||
| * IP embargoes break the assumption made by page_cache that all anonymous | ||
| * requests are equivalent, so let's avoid allowing things to go to the | ||
| * page_cache for anonymous users when an access result has the | ||
| * `ip.embargo_range` (or wider `ip`) context. | ||
| */ | ||
| class DenyIpDependentResponse implements ResponsePolicyInterface { | ||
|
|
||
| /** | ||
| * Cache contexts of which the presence should suppress page_cache. | ||
| */ | ||
| protected const IP_CONTEXTS = [ | ||
| 'ip.embargo_range', | ||
| // XXX: `ip.embargo_range` could be optimized away if the `ip` context | ||
| // itself is added, so let's also account for it. | ||
| 'ip', | ||
| ]; | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| */ | ||
| public function check(Response $response, Request $request) : ?string { | ||
| if (!$request->attributes->has(AccessAwareRouterInterface::ACCESS_RESULT)) { | ||
| // No access result; unable to check. | ||
| return NULL; | ||
| } | ||
|
|
||
| /** @var \Drupal\Core\Access\AccessResultInterface $access_result */ | ||
| $access_result = $request->attributes->get(AccessAwareRouterInterface::ACCESS_RESULT); | ||
|
|
||
| if (!($access_result instanceof RefinableCacheableDependencyInterface)) { | ||
| // Access result is not cacheable; unable to check cache contexts. | ||
| return NULL; | ||
| } | ||
|
|
||
| $cache_contexts = $access_result->getCacheContexts(); | ||
|
|
||
| if (array_intersect(static::IP_CONTEXTS, $cache_contexts)) { | ||
| // Access result has relevant context; avoiding page cache. | ||
| return ResponsePolicyInterface::DENY; | ||
| } | ||
|
|
||
| // No candidate IP cache contexts present on access result; passing. | ||
| return NULL; | ||
| } | ||
|
|
||
| } |
This file contains hidden or 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 hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.