Skip to content

Commit

Permalink
fix content negotiation (pluginkollektiv#265) (pluginkollektiv#273)
Browse files Browse the repository at this point in the history
Add conditions for the HTTP "Accept" header to both cache generation and
webserver configuration so that only HTML content is served from cache.
  • Loading branch information
Ancocodet authored Jan 17, 2023
1 parent 38345db commit aa835ce
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 0 deletions.
7 changes: 7 additions & 0 deletions inc/class-cachify.php
Original file line number Diff line number Diff line change
Expand Up @@ -1339,6 +1339,13 @@ private static function _skip_cache() {
return true;
}

/* Content Negotiation */

// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
if ( isset( $_SERVER['HTTP_ACCEPT'] ) && false === strpos( $_SERVER['HTTP_ACCEPT'], 'text/html' ) ) {
return true;
}

return false;
}

Expand Down
1 change: 1 addition & 0 deletions inc/setup/cachify.hdd.htaccess.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
RewriteRule .* - [E=CACHIFY_DIR:/]
{{GZIP}}
# Main Rules
RewriteCond %{HTTP_ACCEPT} .*text/html.*
RewriteCond %{REQUEST_METHOD} GET
RewriteCond %{QUERY_STRING} =""
RewriteCond %{REQUEST_URI} !^/(wp-admin|wp-content/cache)/.*
Expand Down
3 changes: 3 additions & 0 deletions inc/setup/cachify.hdd.nginx.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
if ( $query_string ) {
return 405;
}
if ( $http_accept !~* "text/html" ) {
return 405;
}
if ( $request_method = POST ) {
return 405;
}
Expand Down
5 changes: 5 additions & 0 deletions inc/setup/cachify.memcached.nginx.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
if ( $query_string ) {
return 405;
}

if ( $http_accept !~* "text/html" ) {
return 405;
}

if ( $request_method = POST ) {
return 405;
}
Expand Down

0 comments on commit aa835ce

Please sign in to comment.