Skip to content

Cache key contains reserved characters aka IPv6 support #115

@drzraf

Description

@drzraf

I'm using the following RateLimit for the Route of one of my controllers:
@RateLimit(methods={"PUT", "POST"}, limit=35, period=3600)

and use the following storage backend:

## config/services.yaml
    rate_limit_cache_storage:
        class: Symfony\Component\Cache\Simple\FilesystemCache
        arguments: ['ratelimit', 0, '%kernel.cache_dir%']

## config/packages/noxlogic_rate_limit.yaml
noxlogic_rate_limit:
    storage_engine: simple_cache
    simple_cache_service: rate_limit_cache_storage
    rate_response_code: 429
    rate_response_message: 'You exceeded the rate limit'

Sadly, once I started getting IPv6 flowing my controller started failing badly:

Uncaught PHP Exception Symfony\Component\Cache\Exception\InvalidArgumentException:
"Cache key "PUT.POST.api_foo.2800:xxx:yyyy:zzzz:d141:zzzz:xxx:yyyy" contains reserved characters "{}()/\@:"." at
vendor/symfony/cache/CacheItem.php line 177
{"exception":"[object] (Symfony\\Component\\Cache\\Exception\\InvalidArgumentException(code: 0): Cache key \"PUT.POST.api_foo.2800:xxx:yyyy:zzzz:d141:zzzz:xxx:yyyy\" contains reserved characters \"{}()/\\@:\". at vendor/symfony/cache/CacheItem.php:177)"} []

This is really bad because:

  1. This basically kept requests from fullfiling even though such an exceptions must have failed safely (A failure of the RateLimit is not something that must block normal processing!).
  2. I've no (simple) way to create filesystem-compatible cache-key for IPv6 without rolling a custom cache-key generator.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions