Skip to content

cache_keys directive not working in Caddy plugin - JSON parsing bug #688

@ishworgiri1999

Description

@ishworgiri1999

Description
The cache_keys configuration directive in the Caddy plugin does not work. The regex patterns defined in cache_keys are not applied, and the cache key is generated with default settings regardless of the configuration.

Environment
Souin version: latest (main branch)

Using this file.
handle @invalidPath {
rewrite * /{lang}/404
}

	cache {
		ttl 30s
		stale 30s
		default_cache_control public, s-maxage=30,stale-while-revalidate=30
		# key {
		# 	disable_query
		# }
		cache_keys {
			.+\/404$ {
				disable_host
			}
		}
	}

I always get cache key as domain.com/-en/404 instead of en/404.

Some analysis

The bug seems to be coming from parseJSON, but I can't confirm

func (c *CacheKeys) parseJSON(rootDecoder *json.Decoder) {
_, _ = rootDecoder.Token() // skips token 1
_, _ = rootDecoder.Token() // skips token 2
_, _ = rootDecoder.Token() // skips token 3

For {".":{"disable_host":true}} Regex pattern becomes disable_host instead of .

After adding some debug logs

[DEBUG] cache_keys parsing: regex pattern = .*
[DEBUG] CacheKeys.MarshalJSON: {".": {"disable_host":true}}
[DEBUG] CacheKeys.UnmarshalJSON: {".
":{"disable_host":true}}
[DEBUG] GetCacheKeys: [map[disable_host:{DisableHost:false}]]

hasOverride := false
for _, current := range g.overrides {
for k, v := range current {
fmt.Printf("[DEBUG] computeKey: testing regex=%s against URI=%s\n", k.String(), req.RequestURI)
if k.MatchString(req.RequestURI) {

[DEBUG] computeKey: testing regex=disable_host against URI=/de

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