Skip to content

Add an IP-based cache context#45

Closed
joecorall wants to merge 1 commit intodiscoverygarden:mainfrom
joecorall:main
Closed

Add an IP-based cache context#45
joecorall wants to merge 1 commit intodiscoverygarden:mainfrom
joecorall:main

Conversation

@joecorall
Copy link

@joecorall joecorall commented Sep 25, 2025

to ensure pages are cached separately for different IP addresses

Related discussion https://islandora.slack.com/archives/C019U12D44Q/p1758053670689559

Summary by CodeRabbit

  • Bug Fixes
    • Improved caching for embargo-based access checks by factoring in client IP ranges.
    • Prevents incorrect access outcomes caused by shared cache across different IPs.
    • Ensures embargoed content visibility behaves consistently and securely per user IP, reducing cache pollution and unintended exposure or blocking.

to ensure pages are cached separately for different IP addresses
@coderabbitai
Copy link

coderabbitai bot commented Sep 25, 2025

Walkthrough

The access check now appends a new cache context, ip.embargo_range, to the AccessResult in EmbargoAccessCheck::access(). No control-flow or method signatures changed; only the caching metadata was updated to vary by client IP embargo range.

Changes

Cohort / File(s) Summary
Embargo access caching
src/Access/EmbargoAccessCheck.php
Add cache context 'ip.embargo_range' to AccessResult; existing logic, conditions, and tags unchanged.

Sequence Diagram(s)

sequenceDiagram
    autonumber
    participant Client
    participant Router
    participant EmbargoAccessCheck as AccessCheck
    participant AccessResult

    Client->>Router: Request entity route
    Router->>AccessCheck: access(entity, user)
    AccessCheck->>AccessCheck: Evaluate embargo rules (unchanged)
    AccessCheck->>AccessResult: Build result with cache tags/contexts
    Note right of AccessResult: New cache context: ip.embargo_range
    AccessCheck-->>Router: AccessResult (vary by IP embargo range)
    Router-->>Client: Allow/Deny
Loading

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Poem

I twitch my whiskers, cache in sight,
A subtle context joins the night—
ip.embargo_range, hop so neat,
Keeps stale approvals from repeat.
With one small leap, I guard the gate,
Carrots secured—no cached debate! 🥕

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title “Add an IP-based cache context” succinctly identifies the primary change made in the pull request, namely the addition of a cache context based on IP address, and directly reflects the code modification without extraneous wording.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Warning

Review ran into problems

🔥 Problems

Errors were encountered while retrieving linked issues.

Errors (1)
  • JIRA integration encountered authorization issues. Please disconnect and reconnect the integration in the CodeRabbit UI.

Tip

👮 Agentic pre-merge checks are now available in preview!

Pro plan users can now enable pre-merge checks in their settings to enforce checklists before merging PRs.

  • Built-in checks – Quickly apply ready-made checks to enforce title conventions, require pull request descriptions that follow templates, validate linked issues for compliance, and more.
  • Custom agentic checks – Define your own rules using CodeRabbit’s advanced agentic capabilities to enforce organization-specific policies and workflows. For example, you can instruct CodeRabbit’s agent to verify that API documentation is updated whenever API schema files are modified in a PR. Note: Upto 5 custom checks are currently allowed during the preview period. Pricing for this feature will be announced in a few weeks.

Please see the documentation for more information.

Example:

reviews:
  pre_merge_checks:
    custom_checks:
      - name: "Undocumented Breaking Changes"
        mode: "warning"
        instructions: |
          Pass/fail criteria: All breaking changes to public APIs, CLI flags, environment variables, configuration keys, database schemas, or HTTP/GraphQL endpoints must be documented in the "Breaking Change" section of the PR description and in CHANGELOG.md. Exclude purely internal or private changes (e.g., code not exported from package entry points or explicitly marked as internal).

Please share your feedback with us on this Discord post.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

'@count embargoes preventing access.'
)->render()
));
)->addCacheContexts(['ip.embargo_range']));
Copy link
Contributor

Choose a reason for hiding this comment

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

This should not be necessary, as the embargo itself should bear the context definition:

if ($this->getExemptIps()) {
$contexts = Cache::mergeContexts($contexts, $this->getExemptIps()->getCacheContexts());
}

and so have already been merged into the caching result of the access result back on line 77.

@joecorall joecorall closed this Sep 25, 2025
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (2)
src/Access/EmbargoAccessCheck.php (2)

34-38: Typo in docblock

“Th current user.” → “The current user.”

-   * Th current user.
+   * The current user.

77-78: Avoid array_map for side effects

Use foreach for clarity.

-    array_map([$state, 'addCacheableDependency'], $related_embargoes);
+    foreach ($related_embargoes as $embargo) {
+      $state->addCacheableDependency($embargo);
+    }
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

Disabled knowledge base sources:

  • Jira integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between a481d32 and f3c7875.

📒 Files selected for processing (1)
  • src/Access/EmbargoAccessCheck.php (1 hunks)
🔇 Additional comments (2)
src/Access/EmbargoAccessCheck.php (2)

73-75: Confirm: no-IP variation when no embargo statements

This early return omits the ip.embargo_range context for entities without embargoes. Likely intentional to avoid unnecessary cache cardinality. Please confirm this matches the requirement.


85-93: LGTM: IP-based cache context registered and implemented
Ensure the cache_context.ip.embargo_range service (IpRangeCacheContext implements CacheContextInterface) buckets IPs into ranges (low-cardinality) and verify CDN/Varnish honors this variation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants