Skip to content

Improve varnish config, add support for URIBAN by regex#495

Merged
Rade333 merged 8 commits into
release/2025-08-26from
feature/regex-uriban
Aug 26, 2025
Merged

Improve varnish config, add support for URIBAN by regex#495
Rade333 merged 8 commits into
release/2025-08-26from
feature/regex-uriban

Conversation

@MarttiR
Copy link
Copy Markdown
Contributor

@MarttiR MarttiR commented Aug 15, 2025

Changes

  • (drupal/varnish): Fix issue with Varnish varying cached static files by cookies
  • (drupal/varnish, frontend/varnish): Fix URIBAN behavior, add support for URIBAN by regex

Notes

  • Making a URIBAN request with a header like x-url-invalidate-pattern: ^/sites/default/files/foo/bar\.pdf will purge the cache from entries matching the pattern
  • Making a URIBAN request with no such header will purge cache from entries which exactly match the Host header and the URL, just as before
  • For the frontend chart, the BAN/URIBAN handling is moved before the allowed method checks (one would assume the previous placement made purging entirely non-functional)

@MarttiR
Copy link
Copy Markdown
Contributor Author

MarttiR commented Aug 15, 2025

The CircleCI failure does not seem related to this PR.


if (req.url ~ "\.(png|gif|jpg|tif|tiff|ico|webp|swf|css|js|pdf|doc|xls|ppt|zip)(\?.*)?$") {
// Forcing a lookup with static file requests
if (req.url ~ "\.(png|gif|jpg|svg|tif|tiff|ico|webp|swf|css|js|pdf|doc|xls|ppt|zip|woff|eot|ttf|bmp|bz2)$") {
Copy link
Copy Markdown
Contributor

@ragnarkurmwunder ragnarkurmwunder Aug 18, 2025

Choose a reason for hiding this comment

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

Sort the extensions alphabetically?

Copy link
Copy Markdown
Contributor Author

@MarttiR MarttiR Aug 18, 2025

Choose a reason for hiding this comment

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

Done in 9e62b7f.

Also noticed that the Accept-Encoding block tried to skip compressing already compressed files, but as there is significant overlap with the static files handling and the block was after the static files block, it was actually not doing anything for many types of static files. 6859a11 moves it to before the static files block.

if (req.method == "URIBAN") {
# Admin port is only exposed to internal network
if (!client.ip ~ purge) {
return (synth(403, "Not allowed."));
Copy link
Copy Markdown
Contributor

@ragnarkurmwunder ragnarkurmwunder Aug 25, 2025

Choose a reason for hiding this comment

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

Consider "IP not allowed".
Reasoning: suppose someone gets the error, and has no clue of this code here, it could be a guesswork why the error.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

AFAIK, the message set here is only logged in varnishlog, not transmitted via HTTP.

if (req.url ~ "\.(png|gif|jpg|tif|tiff|ico|webp|swf|css|js|pdf|doc|xls|ppt|zip)(\?.*)?$") {
// Forcing a lookup with static file requests
if (req.http.Accept-Encoding) {
if (req.url ~ "\.(bz2|eot|gif|gz|ico|jpg|mp3|ogg|pdf|png|svg|swf|tbz|tgz|tif|tiff|ttf|webp|woff|zip)(\?.*)?$") {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can we use non-capturing regex: (?:...)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Varnish uses PCRE so it would probably work, I will try it out.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Changed regexps using groups to use non-capturing groups in a3e28a4 and 2294948.

}

if (req.url ~ "\.(bmp|bz2|css|doc|eot|gif|ico|jpg|js|pdf|png|ppt|svg|swf|tif|tiff|ttf|webp|woff|xls|zip)$") {
# Static file request do not vary on cookies
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Good point. I like this.

return (hash);
}
elseif (req.http.Cookie ~ "(SESS[a-z0-9]+|SSESS[a-z0-9]+)") {
if (req.http.Cookie ~ "(SESS[a-z0-9]+|SSESS[a-z0-9]+)") {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

simplify?
``"S?SESS[a-z0-9]+="`

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good idea, done in cba1254.

@ragnarkurmwunder
Copy link
Copy Markdown
Contributor

I reviewed it to my capacity, but I didn't have the full understanding of it, but I have it roughly, so I could not really review the algorithmic aspect. I did not test it, but reviewed it visually.

@Rade333 Rade333 requested a review from Copilot August 26, 2025 09:00
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR improves Varnish caching configuration by fixing static file caching issues and enhancing URIBAN functionality with regex pattern support. The changes address cookie-based cache variations for static files and add more flexible cache invalidation capabilities.

Key changes:

  • Fixed static file cache variation issue by removing cookies from static file requests
  • Enhanced URIBAN method to support regex-based cache invalidation via x-url-invalidate-pattern header
  • Moved BAN/URIBAN handling to occur before method validation checks

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.

File Description
frontend/templates/varnish-configmap-vcl.yaml Moved BAN/URIBAN logic earlier in request processing, added regex URIBAN support, improved regex patterns
drupal/templates/varnish-configmap-vcl.yaml Split BAN/URIBAN into separate handlers, added regex URIBAN support, fixed static file cookie handling, reorganized request processing flow

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

# If x-url-invalidate-pattern header is present,
# use it to match URLs in stored objects. (ban by regex pattern)
if (req.http.x-url-invalidate-pattern) {
ban("obj.http.x-url ~ " + req.http.x-url-invalidate-pattern);
Copy link

Copilot AI Aug 26, 2025

Choose a reason for hiding this comment

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

The ban statements reference obj.http.x-url but there's no evidence that this header is set during cache storage. The original code used req.url for URIBAN matching. If x-url is not properly set on cached objects, these ban statements will not match any cached entries.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

obj.http.x-url is stored in the cached objects here

Comment thread drupal/templates/varnish-configmap-vcl.yaml
Comment thread frontend/templates/varnish-configmap-vcl.yaml
Comment thread frontend/templates/varnish-configmap-vcl.yaml
return (hash);
}
elseif (req.http.Cookie ~ "(SESS[a-z0-9]+|SSESS[a-z0-9]+)") {
if (req.http.Cookie ~ "S?SESS[a-z0-9]+=") {
Copy link

Copilot AI Aug 26, 2025

Choose a reason for hiding this comment

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

The regex pattern S?SESS[a-z0-9]+= is incorrect. The S? makes the 'S' optional, which would match both 'SESS' and 'ESS' patterns. Based on the original code that checked for (SESS[a-z0-9]+|SSESS[a-z0-9]+), this should be (S?SESS[a-z0-9]+=) or (?:S?SESS[a-z0-9]+=) to properly match both SESS and SSESS cookies.

Suggested change
if (req.http.Cookie ~ "S?SESS[a-z0-9]+=") {
if (req.http.Cookie ~ "(S?SESS[a-z0-9]+=)") {

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

That's not true, the pattern is correct. This can be verified using regexr.com:

Screenshot 2025-08-26 at 12 15 29

@Rade333 Rade333 changed the base branch from master to release/2025-08-26 August 26, 2025 09:18
@Rade333 Rade333 merged commit 8eae1d5 into release/2025-08-26 Aug 26, 2025
2 of 13 checks passed
@Rade333 Rade333 deleted the feature/regex-uriban branch August 26, 2025 09:19
@Rade333 Rade333 mentioned this pull request Aug 26, 2025
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.

4 participants