Skip to content

Commit c5f9a23

Browse files
committed
Streamlining of cookie handling in ext/session and setcookie
Up until now the session cookie used "HttpOnly" to indicate cookies only available through HTTP while setcookie() used "httponly". The relevant RFC 6265 claims that case does not matter for this token, but only explicitely mentions "HttpOnly". Thus this seems like a logical choice when streamlining the code. Also the setcookie implementation now uses the same string constants as the session extension for other tokens like Max-Age or the domain attribute. This change poses a slight risk of backwards incompatibility in places where people deliberately ignore chapter 5.2.5 of RFC 6265 and perform case-sensitive checks for the HttpOnly attribute.
1 parent c0f4e27 commit c5f9a23

File tree

4 files changed

+18
-17
lines changed

4 files changed

+18
-17
lines changed

ext/session/session.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
#include "ext/standard/php_smart_str.h"
5252
#include "ext/standard/url.h"
5353
#include "ext/standard/basic_functions.h"
54+
#include "ext/standard/head.h"
5455

5556
#include "mod_files.h"
5657
#include "mod_user.h"
@@ -1289,14 +1290,6 @@ static int php_session_cache_limiter(TSRMLS_D) /* {{{ */
12891290
* Cookie Management *
12901291
********************* */
12911292

1292-
#define COOKIE_SET_COOKIE "Set-Cookie: "
1293-
#define COOKIE_EXPIRES "; expires="
1294-
#define COOKIE_MAX_AGE "; Max-Age="
1295-
#define COOKIE_PATH "; path="
1296-
#define COOKIE_DOMAIN "; domain="
1297-
#define COOKIE_SECURE "; secure"
1298-
#define COOKIE_HTTPONLY "; HttpOnly"
1299-
13001293
/*
13011294
* Remove already sent session ID cookie.
13021295
* It must be directly removed from SG(sapi_header) because sapi_add_header_ex()

ext/standard/head.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,14 @@ PHPAPI int php_setcookie(char *name, int name_len, char *value, int value_len, t
117117
* pick an expiry date in the past
118118
*/
119119
dt = php_format_date("D, d-M-Y H:i:s T", sizeof("D, d-M-Y H:i:s T")-1, 1, 0 TSRMLS_CC);
120-
snprintf(cookie, len + 100, "Set-Cookie: %s=deleted; expires=%s; Max-Age=0", name, dt);
120+
snprintf(cookie, len + 100, "%s%s=deleted; expires=%s; Max-Age=0", COOKIE_SET_COOKIE, name, dt);
121121
efree(dt);
122122
} else {
123-
snprintf(cookie, len + 100, "Set-Cookie: %s=%s", name, value ? encoded_value : "");
123+
snprintf(cookie, len + 100, "%s%s=%s", COOKIE_SET_COOKIE, name, value ? encoded_value : "");
124124
if (expires > 0) {
125125
const char *p;
126126
char tsdelta[13];
127-
strlcat(cookie, "; expires=", len + 100);
127+
strlcat(cookie, COOKIE_EXPIRES, len + 100);
128128
dt = php_format_date("D, d-M-Y H:i:s T", sizeof("D, d-M-Y H:i:s T")-1, expires, 0 TSRMLS_CC);
129129
/* check to make sure that the year does not exceed 4 digits in length */
130130
p = zend_memrchr(dt, '-', strlen(dt));
@@ -139,7 +139,7 @@ PHPAPI int php_setcookie(char *name, int name_len, char *value, int value_len, t
139139
efree(dt);
140140

141141
snprintf(tsdelta, sizeof(tsdelta), "%li", (long) difftime(expires, time(NULL)));
142-
strlcat(cookie, "; Max-Age=", len + 100);
142+
strlcat(cookie, COOKIE_MAX_AGE, len + 100);
143143
strlcat(cookie, tsdelta, len + 100);
144144
}
145145
}
@@ -149,18 +149,18 @@ PHPAPI int php_setcookie(char *name, int name_len, char *value, int value_len, t
149149
}
150150

151151
if (path && path_len > 0) {
152-
strlcat(cookie, "; path=", len + 100);
152+
strlcat(cookie, COOKIE_PATH, len + 100);
153153
strlcat(cookie, path, len + 100);
154154
}
155155
if (domain && domain_len > 0) {
156-
strlcat(cookie, "; domain=", len + 100);
156+
strlcat(cookie, COOKIE_DOMAIN, len + 100);
157157
strlcat(cookie, domain, len + 100);
158158
}
159159
if (secure) {
160-
strlcat(cookie, "; secure", len + 100);
160+
strlcat(cookie, COOKIE_SECURE, len + 100);
161161
}
162162
if (httponly) {
163-
strlcat(cookie, "; httponly", len + 100);
163+
strlcat(cookie, COOKIE_HTTPONLY, len + 100);
164164
}
165165

166166
ctr.line = cookie;

ext/standard/head.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@
2121
#ifndef HEAD_H
2222
#define HEAD_H
2323

24+
#define COOKIE_SET_COOKIE "Set-Cookie: "
25+
#define COOKIE_EXPIRES "; expires="
26+
#define COOKIE_MAX_AGE "; Max-Age="
27+
#define COOKIE_DOMAIN "; domain="
28+
#define COOKIE_PATH "; path="
29+
#define COOKIE_SECURE "; secure"
30+
#define COOKIE_HTTPONLY "; HttpOnly"
31+
2432
extern PHP_RINIT_FUNCTION(head);
2533
PHP_FUNCTION(header);
2634
PHP_FUNCTION(header_remove);

ext/standard/tests/network/setcookie.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ $expected = array(
2929
'Set-Cookie: name=value; path=/path/',
3030
'Set-Cookie: name=value; domain=domain.tld',
3131
'Set-Cookie: name=value; secure',
32-
'Set-Cookie: name=value; httponly'
32+
'Set-Cookie: name=value; HttpOnly'
3333
);
3434

3535
$headers = headers_list();

0 commit comments

Comments
 (0)