Skip to content

Commit 49962a0

Browse files
authored
Merge branch '2.4-develop' into cia-2.4-develop-bugfixes-09222021
2 parents 46f0941 + 192b280 commit 49962a0

File tree

4 files changed

+403
-34
lines changed

4 files changed

+403
-34
lines changed

app/code/Magento/PageCache/etc/varnish4.vcl

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -108,16 +108,16 @@ sub vcl_recv {
108108
#unset req.http.Cookie;
109109
}
110110

111-
# Authenticated GraphQL requests should not be cached by default
112-
if (req.url ~ "/graphql" && req.http.Authorization ~ "^Bearer") {
111+
# Bypass authenticated GraphQL requests without a X-Magento-Cache-Id
112+
if (req.url ~ "/graphql" && !req.http.X-Magento-Cache-Id && req.http.Authorization ~ "^Bearer") {
113113
return (pass);
114114
}
115115

116116
return (hash);
117117
}
118118

119119
sub vcl_hash {
120-
if (req.http.cookie ~ "X-Magento-Vary=") {
120+
if ((req.url !~ "/graphql" || !req.http.X-Magento-Cache-Id) && req.http.cookie ~ "X-Magento-Vary=") {
121121
hash_data(regsub(req.http.cookie, "^.*?X-Magento-Vary=([^;]+);*.*$", "\1"));
122122
}
123123

@@ -133,9 +133,19 @@ sub vcl_hash {
133133
}
134134

135135
sub process_graphql_headers {
136+
if (req.http.X-Magento-Cache-Id) {
137+
hash_data(req.http.X-Magento-Cache-Id);
138+
139+
# When the frontend stops sending the auth token, make sure users stop getting results cached for logged-in users
140+
if (req.http.Authorization ~ "^Bearer") {
141+
hash_data("Authorized");
142+
}
143+
}
144+
136145
if (req.http.Store) {
137146
hash_data(req.http.Store);
138147
}
148+
139149
if (req.http.Content-Currency) {
140150
hash_data(req.http.Content-Currency);
141151
}
@@ -184,19 +194,21 @@ sub vcl_backend_response {
184194
set beresp.uncacheable = true;
185195
}
186196

197+
# If the cache key in the Magento response doesn't match the one that was sent in the request, don't cache under the request's key
198+
if (bereq.url ~ "/graphql" && bereq.http.X-Magento-Cache-Id && bereq.http.X-Magento-Cache-Id != beresp.http.X-Magento-Cache-Id) {
199+
set beresp.ttl = 0s;
200+
set beresp.uncacheable = true;
201+
}
202+
187203
return (deliver);
188204
}
189205

190206
sub vcl_deliver {
191-
if (resp.http.X-Magento-Debug) {
192-
if (resp.http.x-varnish ~ " ") {
193-
set resp.http.X-Magento-Cache-Debug = "HIT";
194-
set resp.http.Grace = req.http.grace;
195-
} else {
196-
set resp.http.X-Magento-Cache-Debug = "MISS";
197-
}
207+
if (resp.http.x-varnish ~ " ") {
208+
set resp.http.X-Magento-Cache-Debug = "HIT";
209+
set resp.http.Grace = req.http.grace;
198210
} else {
199-
unset resp.http.Age;
211+
set resp.http.X-Magento-Cache-Debug = "MISS";
200212
}
201213

202214
# Not letting browser to cache non-static files.
@@ -206,6 +218,9 @@ sub vcl_deliver {
206218
set resp.http.Cache-Control = "no-store, no-cache, must-revalidate, max-age=0";
207219
}
208220

221+
if (!resp.http.X-Magento-Debug) {
222+
unset resp.http.Age;
223+
}
209224
unset resp.http.X-Magento-Debug;
210225
unset resp.http.X-Magento-Tags;
211226
unset resp.http.X-Powered-By;

app/code/Magento/PageCache/etc/varnish5.vcl

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -109,16 +109,16 @@ sub vcl_recv {
109109
#unset req.http.Cookie;
110110
}
111111

112-
# Authenticated GraphQL requests should not be cached by default
113-
if (req.url ~ "/graphql" && req.http.Authorization ~ "^Bearer") {
112+
# Bypass authenticated GraphQL requests without a X-Magento-Cache-Id
113+
if (req.url ~ "/graphql" && !req.http.X-Magento-Cache-Id && req.http.Authorization ~ "^Bearer") {
114114
return (pass);
115115
}
116116

117117
return (hash);
118118
}
119119

120120
sub vcl_hash {
121-
if (req.http.cookie ~ "X-Magento-Vary=") {
121+
if ((req.url !~ "/graphql" || !req.http.X-Magento-Cache-Id) && req.http.cookie ~ "X-Magento-Vary=") {
122122
hash_data(regsub(req.http.cookie, "^.*?X-Magento-Vary=([^;]+);*.*$", "\1"));
123123
}
124124

@@ -134,9 +134,19 @@ sub vcl_hash {
134134
}
135135

136136
sub process_graphql_headers {
137+
if (req.http.X-Magento-Cache-Id) {
138+
hash_data(req.http.X-Magento-Cache-Id);
139+
140+
# When the frontend stops sending the auth token, make sure users stop getting results cached for logged-in users
141+
if (req.http.Authorization ~ "^Bearer") {
142+
hash_data("Authorized");
143+
}
144+
}
145+
137146
if (req.http.Store) {
138147
hash_data(req.http.Store);
139148
}
149+
140150
if (req.http.Content-Currency) {
141151
hash_data(req.http.Content-Currency);
142152
}
@@ -183,19 +193,21 @@ sub vcl_backend_response {
183193
set beresp.uncacheable = true;
184194
}
185195

196+
# If the cache key in the Magento response doesn't match the one that was sent in the request, don't cache under the request's key
197+
if (bereq.url ~ "/graphql" && bereq.http.X-Magento-Cache-Id && bereq.http.X-Magento-Cache-Id != beresp.http.X-Magento-Cache-Id) {
198+
set beresp.ttl = 0s;
199+
set beresp.uncacheable = true;
200+
}
201+
186202
return (deliver);
187203
}
188204

189205
sub vcl_deliver {
190-
if (resp.http.X-Magento-Debug) {
191-
if (resp.http.x-varnish ~ " ") {
192-
set resp.http.X-Magento-Cache-Debug = "HIT";
193-
set resp.http.Grace = req.http.grace;
194-
} else {
195-
set resp.http.X-Magento-Cache-Debug = "MISS";
196-
}
206+
if (resp.http.x-varnish ~ " ") {
207+
set resp.http.X-Magento-Cache-Debug = "HIT";
208+
set resp.http.Grace = req.http.grace;
197209
} else {
198-
unset resp.http.Age;
210+
set resp.http.X-Magento-Cache-Debug = "MISS";
199211
}
200212

201213
# Not letting browser to cache non-static files.
@@ -205,6 +217,9 @@ sub vcl_deliver {
205217
set resp.http.Cache-Control = "no-store, no-cache, must-revalidate, max-age=0";
206218
}
207219

220+
if (!resp.http.X-Magento-Debug) {
221+
unset resp.http.Age;
222+
}
208223
unset resp.http.X-Magento-Debug;
209224
unset resp.http.X-Magento-Tags;
210225
unset resp.http.X-Powered-By;

app/code/Magento/PageCache/etc/varnish6.vcl

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -113,16 +113,16 @@ sub vcl_recv {
113113
#unset req.http.Cookie;
114114
}
115115

116-
# Authenticated GraphQL requests should not be cached by default
117-
if (req.url ~ "/graphql" && req.http.Authorization ~ "^Bearer") {
116+
# Bypass authenticated GraphQL requests without a X-Magento-Cache-Id
117+
if (req.url ~ "/graphql" && !req.http.X-Magento-Cache-Id && req.http.Authorization ~ "^Bearer") {
118118
return (pass);
119119
}
120120

121121
return (hash);
122122
}
123123

124124
sub vcl_hash {
125-
if (req.http.cookie ~ "X-Magento-Vary=") {
125+
if ((req.url !~ "/graphql" || !req.http.X-Magento-Cache-Id) && req.http.cookie ~ "X-Magento-Vary=") {
126126
hash_data(regsub(req.http.cookie, "^.*?X-Magento-Vary=([^;]+);*.*$", "\1"));
127127
}
128128

@@ -138,9 +138,19 @@ sub vcl_hash {
138138
}
139139

140140
sub process_graphql_headers {
141+
if (req.http.X-Magento-Cache-Id) {
142+
hash_data(req.http.X-Magento-Cache-Id);
143+
144+
# When the frontend stops sending the auth token, make sure users stop getting results cached for logged-in users
145+
if (req.http.Authorization ~ "^Bearer") {
146+
hash_data("Authorized");
147+
}
148+
}
149+
141150
if (req.http.Store) {
142151
hash_data(req.http.Store);
143152
}
153+
144154
if (req.http.Content-Currency) {
145155
hash_data(req.http.Content-Currency);
146156
}
@@ -185,21 +195,23 @@ sub vcl_backend_response {
185195
# Mark as Hit-For-Pass for the next 2 minutes
186196
set beresp.ttl = 120s;
187197
set beresp.uncacheable = true;
188-
}
198+
}
199+
200+
# If the cache key in the Magento response doesn't match the one that was sent in the request, don't cache under the request's key
201+
if (bereq.url ~ "/graphql" && bereq.http.X-Magento-Cache-Id && bereq.http.X-Magento-Cache-Id != beresp.http.X-Magento-Cache-Id) {
202+
set beresp.ttl = 0s;
203+
set beresp.uncacheable = true;
204+
}
189205

190206
return (deliver);
191207
}
192208

193209
sub vcl_deliver {
194-
if (resp.http.X-Magento-Debug) {
195-
if (resp.http.x-varnish ~ " ") {
196-
set resp.http.X-Magento-Cache-Debug = "HIT";
197-
set resp.http.Grace = req.http.grace;
198-
} else {
199-
set resp.http.X-Magento-Cache-Debug = "MISS";
200-
}
210+
if (resp.http.x-varnish ~ " ") {
211+
set resp.http.X-Magento-Cache-Debug = "HIT";
212+
set resp.http.Grace = req.http.grace;
201213
} else {
202-
unset resp.http.Age;
214+
set resp.http.X-Magento-Cache-Debug = "MISS";
203215
}
204216

205217
# Not letting browser to cache non-static files.
@@ -209,6 +221,9 @@ sub vcl_deliver {
209221
set resp.http.Cache-Control = "no-store, no-cache, must-revalidate, max-age=0";
210222
}
211223

224+
if (!resp.http.X-Magento-Debug) {
225+
unset resp.http.Age;
226+
}
212227
unset resp.http.X-Magento-Debug;
213228
unset resp.http.X-Magento-Tags;
214229
unset resp.http.X-Powered-By;

0 commit comments

Comments
 (0)