Skip to content

Commit e5fe441

Browse files
author
Ilia Alshanetsky
committed
Added support for httpOnly flag for session extension and cookie setting
functions. # Original patch by Scott MacVicar
1 parent 1ec10ac commit e5fe441

File tree

7 files changed

+38
-15
lines changed

7 files changed

+38
-15
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
PHP NEWS
22
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
33
?? Aug 2006, PHP 5.2.0RC2
4+
- Added support for httpOnly flag for session extension and cookie setting
5+
functions. (Scott MacVicar, Ilia)
46
- Added version specific registry keys to allow different configurations for
57
different php version. (Richard, Dmitry)
68
- In addition to path to php.ini, PHPRC now may specify full file name. (Dmitry)

ext/session/php_session.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ typedef struct _php_ps_globals {
103103
char *cookie_path;
104104
char *cookie_domain;
105105
zend_bool cookie_secure;
106+
zend_bool cookie_httponly;
106107
ps_module *mod;
107108
void *mod_data;
108109
php_session_status session_status;

ext/session/session.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ PHP_INI_BEGIN()
165165
STD_PHP_INI_ENTRY("session.cookie_path", "/", PHP_INI_ALL, OnUpdateString, cookie_path, php_ps_globals, ps_globals)
166166
STD_PHP_INI_ENTRY("session.cookie_domain", "", PHP_INI_ALL, OnUpdateString, cookie_domain, php_ps_globals, ps_globals)
167167
STD_PHP_INI_BOOLEAN("session.cookie_secure", "", PHP_INI_ALL, OnUpdateBool, cookie_secure, php_ps_globals, ps_globals)
168+
STD_PHP_INI_BOOLEAN("session.cookie_httponly", "", PHP_INI_ALL, OnUpdateBool, cookie_httponly, php_ps_globals, ps_globals)
168169
STD_PHP_INI_BOOLEAN("session.use_cookies", "1", PHP_INI_ALL, OnUpdateBool, use_cookies, php_ps_globals, ps_globals)
169170
STD_PHP_INI_BOOLEAN("session.use_only_cookies", "0", PHP_INI_ALL, OnUpdateBool, use_only_cookies, php_ps_globals, ps_globals)
170171
STD_PHP_INI_ENTRY("session.referer_check", "", PHP_INI_ALL, OnUpdateString, extern_referer_chk, php_ps_globals, ps_globals)
@@ -1012,6 +1013,7 @@ static int php_session_cache_limiter(TSRMLS_D)
10121013
#define COOKIE_PATH "; path="
10131014
#define COOKIE_DOMAIN "; domain="
10141015
#define COOKIE_SECURE "; secure"
1016+
#define COOKIE_HTTPONLY "; HttpOnly"
10151017

10161018
static void php_session_send_cookie(TSRMLS_D)
10171019
{
@@ -1065,6 +1067,10 @@ static void php_session_send_cookie(TSRMLS_D)
10651067
smart_str_appends(&ncookie, COOKIE_SECURE);
10661068
}
10671069

1070+
if (PS(cookie_httponly)) {
1071+
smart_str_appends(&ncookie, COOKIE_HTTPONLY);
1072+
}
1073+
10681074
smart_str_0(&ncookie);
10691075

10701076
/* 'replace' must be 0 here, else a previous Set-Cookie
@@ -1296,13 +1302,13 @@ static zend_bool php_session_destroy(TSRMLS_D)
12961302
Set session cookie parameters */
12971303
PHP_FUNCTION(session_set_cookie_params)
12981304
{
1299-
zval **lifetime, **path, **domain, **secure;
1305+
zval **lifetime, **path, **domain, **secure, **httponly;
13001306

13011307
if (!PS(use_cookies))
13021308
return;
13031309

1304-
if (ZEND_NUM_ARGS() < 1 || ZEND_NUM_ARGS() > 4 ||
1305-
zend_get_parameters_ex(ZEND_NUM_ARGS(), &lifetime, &path, &domain, &secure) == FAILURE)
1310+
if (ZEND_NUM_ARGS() < 1 || ZEND_NUM_ARGS() > 5 ||
1311+
zend_get_parameters_ex(ZEND_NUM_ARGS(), &lifetime, &path, &domain, &secure, &httponly) == FAILURE)
13061312
WRONG_PARAM_COUNT;
13071313

13081314
convert_to_string_ex(lifetime);
@@ -1319,6 +1325,10 @@ PHP_FUNCTION(session_set_cookie_params)
13191325
convert_to_long_ex(secure);
13201326
zend_alter_ini_entry("session.cookie_secure", sizeof("session.cookie_secure"), Z_BVAL_PP(secure)?"1":"0", 1, PHP_INI_USER, PHP_INI_STAGE_RUNTIME);
13211327
}
1328+
if (ZEND_NUM_ARGS() > 4) {
1329+
convert_to_long_ex(httponly);
1330+
zend_alter_ini_entry("session.cookie_httponly", sizeof("session.cookie_httponly"), Z_BVAL_PP(httponly)?"1":"0", 1, PHP_INI_USER, PHP_INI_STAGE_RUNTIME);
1331+
}
13221332
}
13231333
}
13241334
}
@@ -1338,6 +1348,7 @@ PHP_FUNCTION(session_get_cookie_params)
13381348
add_assoc_string(return_value, "path", PS(cookie_path), 1);
13391349
add_assoc_string(return_value, "domain", PS(cookie_domain), 1);
13401350
add_assoc_bool(return_value, "secure", PS(cookie_secure));
1351+
add_assoc_bool(return_value, "httponly", PS(cookie_httponly));
13411352
}
13421353
/* }}} */
13431354

ext/standard/head.c

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ PHPAPI int php_header(TSRMLS_D)
6060
}
6161

6262

63-
PHPAPI int php_setcookie(char *name, int name_len, char *value, int value_len, time_t expires, char *path, int path_len, char *domain, int domain_len, int secure, int url_encode TSRMLS_DC)
63+
PHPAPI int php_setcookie(char *name, int name_len, char *value, int value_len, time_t expires, char *path, int path_len, char *domain, int domain_len, int secure, int url_encode, int httponly TSRMLS_DC)
6464
{
6565
char *cookie, *encoded_value = NULL;
6666
int len=sizeof("Set-Cookie: ");
@@ -131,6 +131,9 @@ PHPAPI int php_setcookie(char *name, int name_len, char *value, int value_len, t
131131
if (secure) {
132132
strcat(cookie, "; secure");
133133
}
134+
if (httponly) {
135+
strcat(cookie, "; httponly");
136+
}
134137

135138
ctr.line = cookie;
136139
ctr.line_len = strlen(cookie);
@@ -142,45 +145,45 @@ PHPAPI int php_setcookie(char *name, int name_len, char *value, int value_len, t
142145

143146

144147
/* php_set_cookie(name, value, expires, path, domain, secure) */
145-
/* {{{ proto bool setcookie(string name [, string value [, int expires [, string path [, string domain [, bool secure]]]]])
148+
/* {{{ proto bool setcookie(string name [, string value [, int expires [, string path [, string domain [, bool secure[, bool httponly]]]]]])
146149
Send a cookie */
147150
PHP_FUNCTION(setcookie)
148151
{
149152
char *name, *value = NULL, *path = NULL, *domain = NULL;
150153
long expires = 0;
151-
zend_bool secure = 0;
154+
zend_bool secure = 0, httponly = 0;
152155
int name_len, value_len, path_len, domain_len;
153156

154-
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|slssb", &name,
157+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|slssbb", &name,
155158
&name_len, &value, &value_len, &expires, &path,
156-
&path_len, &domain, &domain_len, &secure) == FAILURE) {
159+
&path_len, &domain, &domain_len, &secure, &httponly) == FAILURE) {
157160
return;
158161
}
159162

160-
if (php_setcookie(name, name_len, value, value_len, expires, path, path_len, domain, domain_len, secure, 1 TSRMLS_CC) == SUCCESS) {
163+
if (php_setcookie(name, name_len, value, value_len, expires, path, path_len, domain, domain_len, secure, 1, httponly TSRMLS_CC) == SUCCESS) {
161164
RETVAL_TRUE;
162165
} else {
163166
RETVAL_FALSE;
164167
}
165168
}
166169
/* }}} */
167170

168-
/* {{{ proto bool setrawcookie(string name [, string value [, int expires [, string path [, string domain [, bool secure]]]]])
171+
/* {{{ proto bool setrawcookie(string name [, string value [, int expires [, string path [, string domain [, bool secure[, bool httponly]]]]]])
169172
Send a cookie with no url encoding of the value */
170173
PHP_FUNCTION(setrawcookie)
171174
{
172175
char *name, *value = NULL, *path = NULL, *domain = NULL;
173176
long expires = 0;
174-
zend_bool secure = 0;
177+
zend_bool secure = 0, httponly = 0;
175178
int name_len, value_len, path_len, domain_len;
176179

177-
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|slssb", &name,
180+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|slssbb", &name,
178181
&name_len, &value, &value_len, &expires, &path,
179-
&path_len, &domain, &domain_len, &secure) == FAILURE) {
182+
&path_len, &domain, &domain_len, &secure, &httponly) == FAILURE) {
180183
return;
181184
}
182185

183-
if (php_setcookie(name, name_len, value, value_len, expires, path, path_len, domain, domain_len, secure, 0 TSRMLS_CC) == SUCCESS) {
186+
if (php_setcookie(name, name_len, value, value_len, expires, path, path_len, domain, domain_len, secure, 0, httponly TSRMLS_CC) == SUCCESS) {
184187
RETVAL_TRUE;
185188
} else {
186189
RETVAL_FALSE;

ext/standard/head.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ PHP_FUNCTION(headers_sent);
2929
PHP_FUNCTION(headers_list);
3030

3131
PHPAPI int php_header(TSRMLS_D);
32-
PHPAPI int php_setcookie(char *name, int name_len, char *value, int value_len, time_t expires, char *path, int path_len, char *domain, int domain_len, int secure, int url_encode TSRMLS_DC);
32+
PHPAPI int php_setcookie(char *name, int name_len, char *value, int value_len, time_t expires, char *path, int path_len, char *domain, int domain_len, int secure, int url_encode, int httponly TSRMLS_DC);
3333

3434
#endif

php.ini-dist

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,9 @@ session.cookie_path = /
915915
; The domain for which the cookie is valid.
916916
session.cookie_domain =
917917

918+
; Whether or not to add the httpOnly flag to the cookie, which makes it inaccessible to browser scripting languages such as JavaScript.
919+
session.cookie_httponly =
920+
918921
; Handler used to serialize data. php is the standard serializer of PHP.
919922
session.serialize_handler = php
920923

php.ini-recommended

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -970,6 +970,9 @@ session.cookie_path = /
970970
; The domain for which the cookie is valid.
971971
session.cookie_domain =
972972

973+
; Whether or not to add the httpOnly flag to the cookie, which makes it inaccessible to browser scripting languages such as JavaScript.
974+
session.cookie_httponly =
975+
973976
; Handler used to serialize data. php is the standard serializer of PHP.
974977
session.serialize_handler = php
975978

0 commit comments

Comments
 (0)