Skip to content

Fix #172: Invalidate corrupted cache entries across all storage adapters#216

Merged
Kevinrob merged 2 commits into
masterfrom
fix-issue-172-stream-typeerror
Mar 11, 2026
Merged

Fix #172: Invalidate corrupted cache entries across all storage adapters#216
Kevinrob merged 2 commits into
masterfrom
fix-issue-172-stream-typeerror

Conversation

@Kevinrob

@Kevinrob Kevinrob commented Mar 8, 2026

Copy link
Copy Markdown
Owner

Description

This PR addresses issue #172, where corrupted or incompatible cache entries (often caused by upgrades to Guzzle or this library) would trigger a fatal TypeError
during unserialization.

Instead of crashing the application or returning malformed responses, corrupted entries are now strictly validated and invalidated, resulting in a clean Cache
MISS.

Changes

  • Core Robustness: Wrapped the entire CacheEntry::__unserialize method in a try-catch block.
  • Strict Validation: Added checks for mandatory properties (request, response, staleAt, dateCreated) during unserialization. Any failure now throws an
    InvalidArgumentException.
  • Storage Protection: Updated all storage adapters (Flysystem, Laravel, WordPress, PSR-6, PSR-16) to catch deserialization exceptions during retrieval and
    return null (Cache MISS).
  • Early Evaluation Support: Moved PSR-6 getItem() inside the try-catch block to support implementations that deserialize data immediately.
  • Regression Test: Added CorruptedCacheTest to verify that invalid stream data no longer crashes the application.

Impact

  • Prevents production outages caused by incompatible cached objects after updates.
  • Ensures data integrity by never serving partially restored cache entries.

Verification

  • Successfully ran make test.
  • Verified the fix with the new CorruptedCacheTest which simulates the exact TypeError reported in the issue.
  • All 82 tests passed on PHP 8.5.

@gemini-code-assist

Copy link
Copy Markdown

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the resilience of the caching mechanism by preventing application crashes caused by corrupted or incompatible cache entries. It introduces robust error handling during deserialization, ensuring that malformed data leads to a cache miss rather than a fatal error, thereby improving stability and data integrity across various storage adapters.

Highlights

  • Core Robustness: The CacheEntry::__unserialize method is now wrapped in a try-catch block to prevent fatal errors during deserialization.
  • Strict Validation: Mandatory properties (request, response, staleAt, dateCreated) are strictly validated during unserialization, throwing an InvalidArgumentException if missing or invalid.
  • Storage Protection: All storage adapters (Flysystem, Laravel, WordPress, PSR-6, PSR-16) now catch deserialization exceptions during retrieval and return null (Cache MISS) instead of crashing.
  • Early Evaluation Support: The PSR-6 getItem() call has been moved inside the try-catch block to support implementations that deserialize data immediately.
  • Regression Test: A new CorruptedCacheTest has been added to verify that invalid stream data no longer crashes the application.
Changelog
  • src/CacheEntry.php
    • Implemented a try-catch block around the __unserialize method to handle deserialization errors.
    • Added strict validation for request, response, staleAt, and dateCreated properties, throwing InvalidArgumentException if they are null or invalid.
    • Modified the __unserialize method to throw a generic InvalidArgumentException with a "Corrupted cache entry" message if any Throwable occurs during deserialization.
  • src/Storage/FlysystemStorage.php
    • Wrapped the unserialize call in a try-catch block to gracefully handle exceptions during deserialization.
    • Ensured null is returned if deserialization fails due to a corrupted cache entry.
  • src/Storage/LaravelCacheStorage.php
    • Changed the exception type caught from \Exception to \Throwable for broader error handling.
    • Used the error suppression operator (@) with unserialize to prevent warnings from becoming errors.
    • Ensured null is returned if deserialization fails.
  • src/Storage/Psr16CacheStorage.php
    • Added a try-catch block around the cache->get($key) call to handle potential exceptions during cache retrieval or deserialization by the underlying PSR-16 implementation.
    • Ensured null is returned if an exception occurs.
  • src/Storage/Psr6CacheStorage.php
    • Wrapped the cachePool->getItem($key) and item->get() calls in a try-catch block to handle exceptions, including those from immediate deserialization.
    • Ensured null is returned if an exception occurs during item retrieval or deserialization.
  • src/Storage/VolatileRuntimeStorage.php
    • Explicitly returned null when a key is not found, aligning with other storage adapters.
  • src/Storage/WordPressObjectCacheStorage.php
    • Changed the exception type caught from \Exception to \Throwable.
    • Used the error suppression operator (@) with unserialize.
  • tests/CorruptedCacheTest.php
    • Added a new test file CorruptedCacheTest.php.
    • Included testRestoreStreamBodyWithBrokenMessageThrowsTypeError to verify TypeError handling during stream restoration.
    • Included testUnserializeThrowsExceptionWhenStreamIsCorrupted to confirm CacheEntry::__unserialize throws InvalidArgumentException for corrupted entries.
Activity
  • The author successfully ran make test.
  • The fix was verified with the new CorruptedCacheTest, simulating the exact TypeError reported in the issue.
  • All 82 tests passed on PHP 8.5.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request effectively addresses the issue of corrupted cache entries causing fatal errors by introducing robust error handling in the CacheEntry::__unserialize method and across all storage adapters, ensuring deserialization failures result in a clean cache miss. The addition of CorruptedCacheTest is excellent for verifying the fix. However, a high-severity security risk remains due to the use of unserialize() without the allowed_classes option in several storage adapters, which could lead to Remote Code Execution (RCE) if the underlying cache storage is compromised. Implementing class whitelisting for all unserialize() calls is recommended to harden the library's security posture. Additionally, there is a minor suggestion for improvement to ensure consistency across the storage adapters.

Comment thread src/Storage/FlysystemStorage.php
Comment thread src/Storage/LaravelCacheStorage.php Outdated
Comment thread src/Storage/WordPressObjectCacheStorage.php Outdated
Comment thread src/Storage/WordPressObjectCacheStorage.php
@Legion112

Copy link
Copy Markdown
Contributor

LGTM

@Kevinrob Kevinrob merged commit c2f752a into master Mar 11, 2026
16 checks passed
@Kevinrob Kevinrob deleted the fix-issue-172-stream-typeerror branch March 11, 2026 06:58
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