Skip to content

Commit cfa2fca

Browse files
author
Chris Gårdenberg
committed
fix(Security): Hardened cookies with HttpOnly
1 parent 5438067 commit cfa2fca

1 file changed

Lines changed: 31 additions & 25 deletions

File tree

libraries/class-wp-session.php

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -131,35 +131,41 @@ protected function set_expiration() {
131131
protected function set_cookie() {
132132
$is_secure = ( ! empty( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] !== "off" ) || $_SERVER['SERVER_PORT'] === 443;
133133
$cookie_value = $this->session_id . '||' . $this->expires . '||' . $this->exp_variant;
134-
if ( PHP_VERSION_ID >= 70300 ) {
134+
if ( ! headers_sent() ) {
135+
if ( PHP_VERSION_ID >= 70300 ) {
136+
@setcookie(
137+
WP_SESSION_COOKIE,
138+
$cookie_value,
139+
array(
140+
'expires' => $this->expires,
141+
'path' => COOKIEPATH,
142+
'domain' => COOKIE_DOMAIN,
143+
'secure' => $is_secure,
144+
'samesite' => 'None',
145+
'httponly' => true,
146+
)
147+
);
148+
} else {
149+
@header(
150+
"Set-Cookie: " . urlencode( WP_SESSION_COOKIE ) . "=" . urlencode( $cookie_value ) . "; " .
151+
"Expires=" . date( "D, d M Y H:i:s", $this->expires ) . "; " .
152+
"Path=" . COOKIEPATH . "; " .
153+
"Domain=" . COOKIE_DOMAIN . "; " .
154+
"HttpOnly; " .
155+
( $is_secure ? "Secure; SameSite=None" : "" )
156+
);
157+
}
158+
135159
@setcookie(
136-
WP_SESSION_COOKIE,
160+
WP_SESSION_COOKIE . '-legacy',
137161
$cookie_value,
138-
array(
139-
'expires' => $this->expires,
140-
'path' => COOKIEPATH,
141-
'domain' => COOKIE_DOMAIN,
142-
'secure' => $is_secure,
143-
'samesite' => 'None',
144-
)
145-
);
146-
} else {
147-
@header(
148-
"Set-Cookie: " . urlencode( WP_SESSION_COOKIE ) . "=" . urlencode( $cookie_value ) . "; " .
149-
"Expires=" . date( "D, d M Y H:i:s", $this->expires ) . "; " .
150-
"Path=" . COOKIEPATH . "; " .
151-
"Domain=" . COOKIE_DOMAIN . "; " .
152-
( $is_secure ? "Secure; SameSite=None" : "" )
162+
$this->expires,
163+
COOKIEPATH,
164+
COOKIE_DOMAIN,
165+
$is_secure,
166+
true
153167
);
154168
}
155-
156-
@setcookie(
157-
WP_SESSION_COOKIE . '-legacy',
158-
$cookie_value,
159-
$this->expires,
160-
COOKIEPATH,
161-
COOKIE_DOMAIN
162-
);
163169
}
164170

165171
/**

0 commit comments

Comments
 (0)