Skip to content

Commit

Permalink
Revert the vcl_hash() logic. Authenticated (X)HTML requests are not…
Browse files Browse the repository at this point in the history
… cacheable (since they're user-specific)
  • Loading branch information
namedgraph committed Nov 22, 2024
1 parent d5fc56d commit 4d6164d
Showing 1 changed file with 6 additions and 37 deletions.
43 changes: 6 additions & 37 deletions platform/varnish.vcl.template
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ sub vcl_recv {
return (pass);
}

if (req.http.Client-Cert && (req.http.Accept ~ "text/html" || req.http.Accept ~ "application/xhtml+xml")) {
/* Authenticated (X)HTML requests are not cacheable (since they're user-specific) */
return (pass);
}

if (req.http.Cookie) {
# explicitly allow only cookies required by LDH server-side
set req.http.Cookie = ";" + req.http.Cookie;
Expand All @@ -58,11 +63,6 @@ sub vcl_recv {
set req.http.Cookie = regsuball(req.http.Cookie, ";[^ ][^;]*", "");
set req.http.Cookie = regsuball(req.http.Cookie, "^[; ]+|[; ]+$", "");

if (req.http.Cookie ~ "LinkedDataHub\.id_token=") {
# Extract the LinkedDataHub.id_token value
set req.http.X-LinkedDataHub-Id-Token = regsub(req.http.Cookie, ".*LinkedDataHub\.id_token=([^; ]+).*", "\1");
}

if (req.http.cookie ~ "^\s*$") {
unset req.http.cookie;
}
Expand All @@ -71,43 +71,12 @@ sub vcl_recv {
return (hash);
}

sub vcl_hash {
hash_data(req.url);
hash_data(req.http.Host);

# static resources are not user-dependent
if (req.url ~ "^/static/") {
return (lookup);
}

# include user identifiers if flagged for user-specific content
if (req.http.X-User-Specific == "true") {
# include the client certificate in the hash, if it exists
if (req.http.Client-Cert) {
hash_data(req.http.Client-Cert);
}

# include LinkedDataHub.id_token cookie value in the hash, if it exists
if (req.http.X-LinkedDataHub-Id-Token) {
hash_data(req.http.X-LinkedDataHub-Id-Token);
}
}

return (lookup);
}

sub vcl_backend_response {
/* flag user-specific content only when (X)HTML is returned */
if (beresp.http.Content-Type ~ "text/html" || beresp.http.Content-Type ~ "application/xhtml+xml") {
set beresp.http.X-User-Specific = "true";
set req.http.X-User-Specific = "true"; # pass to req for hash calculation
}

/* purge URLs after updates */
if ((beresp.status == 200 || beresp.status == 201 || beresp.status == 204) && bereq.method ~ "POST|PUT|DELETE|PATCH") {
set beresp.http.X-LinkedDataHub = "Banned";
ban("req.url == " + bereq.url + " && req.http.host == " + bereq.http.host);
}

return (deliver);
}
}

0 comments on commit 4d6164d

Please sign in to comment.