Skip to content
This repository has been archived by the owner on Jul 25, 2024. It is now read-only.

Allow custom regex via "vhp-ban-regex" query parameter #97

Closed
wants to merge 1 commit into from

Conversation

lukasbesch
Copy link

My approach for #96.

Similar to query parameter vhp-regex, you can now add a custom regex using the query parameter vhp-ban-regex to the purge_urls.
If it is set, the PURGE request contains an additional header X-Ban-Regex and the X-Purge-Method is ban-regex.
If the VCL contains this functionality (described below), it will be used. Otherwise – to preserve backwards compatibility – only the request url itself is purged.

The VCL now looks like this:

sub vcl_recv {
    if (req.method == "PURGE" || req.method == "BAN") {

        # If the IP isn't a match, we can't flush cache.
        if (!client.ip ~ purge) {
            return (synth(405, "This IP is not allowed to send PURGE requests."));
        }

        # If we're trying to custom regex, then we need to use the ban calls:
        if (req.http.X-Purge-Method == "ban-regex" && req.http.X-Ban-Regex) {
            ban("obj.http.x-url ~ " + req.http.X-Ban-Regex + " && obj.http.x-host ~ " + req.http.host);
            return (synth(200, "Banned"));
        }

        # If we're trying to regex, then we need to use the ban calls:
        if (req.http.X-Purge-Method == "regex") {
            ban("obj.http.x-url ~ " + req.url + " && obj.http.x-host ~ " + req.http.host);
            return (synth(200, "Purged"));
        }

        # Backported catch for exact cache flush calls:
        if (req.http.X-Purge-Method == "exact") {
            ban("obj.http.x-url == " + req.url + " && obj.http.X-Req-Host == " + req.http.host);
            return (synth(200, "Purged"));
        }

        # Otherwise, we strip the query params and flush as is:
        set req.url = regsub(req.url, "\?.*$", "");
        return (purge);
    }
}

What I don't like in this approach is that X-Purge-Method: regex and X-Purge-Method: ban-regex as well as $pregex and $bregex might be confusing (but is needed for backwards compatibility or for people who cannot change their VCL).

@lukasbesch
Copy link
Author

#98 will provide a solution for custom invalidation strategies so I close this

@lukasbesch lukasbesch closed this Apr 3, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant