Coverage Report

Created: 2026-07-25 06:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/php-src/ext/standard/head.c
Line
Count
Source
1
/*
2
   +----------------------------------------------------------------------+
3
   | Copyright © The PHP Group and Contributors.                          |
4
   +----------------------------------------------------------------------+
5
   | This source file is subject to the Modified BSD License that is      |
6
   | bundled with this package in the file LICENSE, and is available      |
7
   | through the World Wide Web at <https://www.php.net/license/>.        |
8
   |                                                                      |
9
   | SPDX-License-Identifier: BSD-3-Clause                                |
10
   +----------------------------------------------------------------------+
11
   | Author: Rasmus Lerdorf <rasmus@lerdorf.on.ca>                        |
12
   +----------------------------------------------------------------------+
13
 */
14
15
#include <stdio.h>
16
#include "php.h"
17
#include "ext/standard/php_standard.h"
18
#include "ext/date/php_date.h"
19
#include "SAPI.h"
20
#include "php_main.h"
21
#include "head.h"
22
#include <time.h>
23
24
#include "php_globals.h"
25
#include "zend_smart_str.h"
26
27
28
/* Implementation of the language Header() function */
29
/* {{{ Sends a raw HTTP header */
30
PHP_FUNCTION(header)
31
303
{
32
303
  bool rep = 1;
33
303
  sapi_header_line ctr = {0};
34
303
  char *line;
35
303
  size_t len;
36
37
909
  ZEND_PARSE_PARAMETERS_START(1, 3)
38
1.21k
    Z_PARAM_STRING(line, len)
39
303
    Z_PARAM_OPTIONAL
40
642
    Z_PARAM_BOOL(rep)
41
54
    Z_PARAM_LONG(ctr.response_code)
42
303
  ZEND_PARSE_PARAMETERS_END();
43
44
303
  ctr.line = line;
45
303
  ctr.line_len = len;
46
303
  sapi_header_op(rep ? SAPI_HEADER_REPLACE:SAPI_HEADER_ADD, &ctr);
47
303
}
48
/* }}} */
49
50
/* {{{ Removes an HTTP header previously set using header() */
51
PHP_FUNCTION(header_remove)
52
0
{
53
0
  sapi_header_line ctr = {0};
54
0
  char *line = NULL;
55
0
  size_t len = 0;
56
57
0
  ZEND_PARSE_PARAMETERS_START(0, 1)
58
0
    Z_PARAM_OPTIONAL
59
0
    Z_PARAM_STRING_OR_NULL(line, len)
60
0
  ZEND_PARSE_PARAMETERS_END();
61
62
0
  ctr.line = line;
63
0
  ctr.line_len = len;
64
0
  sapi_header_op(line == NULL ? SAPI_HEADER_DELETE_ALL : SAPI_HEADER_DELETE, &ctr);
65
0
}
66
/* }}} */
67
68
PHPAPI bool php_header(void)
69
300k
{
70
300k
  if (sapi_send_headers()==FAILURE || SG(request_info).headers_only) {
71
0
    return false; /* don't allow output */
72
300k
  } else {
73
300k
    return true; /* allow output */
74
300k
  }
75
300k
}
76
77
PHPAPI bool php_is_valid_samesite_value(zend_string *value)
78
0
{
79
0
  return zend_string_equals_literal_ci(value, "Strict")
80
0
    || zend_string_equals_literal_ci(value, "Lax")
81
0
    || zend_string_equals_literal_ci(value, "None");
82
0
}
83
84
0
#define ILLEGAL_COOKIE_CHARACTER "\",\", \";\", \" \", \"\\t\", \"\\r\", \"\\n\", \"\\013\", or \"\\014\""
85
PHPAPI zend_result php_setcookie(zend_string *name, zend_string *value, time_t expires,
86
  zend_string *path, zend_string *domain, bool secure, bool httponly,
87
  zend_string *samesite, bool partitioned, bool url_encode)
88
0
{
89
0
  zend_string *dt;
90
0
  sapi_header_line ctr = {0};
91
0
  zend_result result;
92
0
  smart_str buf = {0};
93
94
0
  if (!ZSTR_LEN(name)) {
95
0
    zend_argument_must_not_be_empty_error(1);
96
0
    return FAILURE;
97
0
  }
98
0
  if (strpbrk(ZSTR_VAL(name), "=,; \t\r\n\013\014") != NULL) {   /* man isspace for \013 and \014 */
99
0
    zend_argument_value_error(1, "cannot contain \"=\", " ILLEGAL_COOKIE_CHARACTER);
100
0
    return FAILURE;
101
0
  }
102
0
  if (!url_encode && value &&
103
0
      strpbrk(ZSTR_VAL(value), ",; \t\r\n\013\014") != NULL) { /* man isspace for \013 and \014 */
104
0
    zend_argument_value_error(2, "cannot contain " ILLEGAL_COOKIE_CHARACTER);
105
0
    return FAILURE;
106
0
  }
107
108
0
  if (path && strpbrk(ZSTR_VAL(path), ",; \t\r\n\013\014") != NULL) { /* man isspace for \013 and \014 */
109
0
    zend_value_error("%s(): \"path\" option cannot contain " ILLEGAL_COOKIE_CHARACTER,
110
0
      get_active_function_name());
111
0
    return FAILURE;
112
0
  }
113
0
  if (domain && strpbrk(ZSTR_VAL(domain), ",; \t\r\n\013\014") != NULL) { /* man isspace for \013 and \014 */
114
0
    zend_value_error("%s(): \"domain\" option cannot contain " ILLEGAL_COOKIE_CHARACTER,
115
0
      get_active_function_name());
116
0
    return FAILURE;
117
0
  }
118
0
#ifdef ZEND_ENABLE_ZVAL_LONG64
119
0
  if (expires >= 253402300800) {
120
0
    zend_value_error("%s(): \"expires\" option cannot have a year greater than 9999",
121
0
      get_active_function_name());
122
0
    return FAILURE;
123
0
  }
124
0
#endif
125
0
  if (partitioned && !secure) {
126
0
    zend_value_error("%s(): \"partitioned\" option cannot be used without \"secure\" option",
127
0
      get_active_function_name());
128
0
    return FAILURE;
129
0
  }
130
131
0
  if (samesite && ZSTR_LEN(samesite) > 0 && !php_is_valid_samesite_value(samesite)) {
132
0
    zend_value_error("%s(): \"samesite\" option must be \"Strict\", \"Lax\", \"None\", or \"\"",
133
0
      get_active_function_name());
134
0
    return FAILURE;
135
0
  }
136
137
0
  if (value == NULL || ZSTR_LEN(value) == 0) {
138
    /*
139
     * MSIE doesn't delete a cookie when you set it to a null value
140
     * so in order to force cookies to be deleted, even on MSIE, we
141
     * pick an expiry date in the past
142
     */
143
0
    smart_str_appends(&buf, "Set-Cookie: ");
144
0
    smart_str_append(&buf, name);
145
0
    smart_str_appends(&buf, "=deleted; expires=Thu, 01 Jan 1970 00:00:01 GMT; Max-Age=0");
146
0
  } else {
147
0
    smart_str_appends(&buf, "Set-Cookie: ");
148
0
    smart_str_append(&buf, name);
149
0
    smart_str_appendc(&buf, '=');
150
0
    if (url_encode) {
151
0
      zend_string *encoded_value = php_raw_url_encode(ZSTR_VAL(value), ZSTR_LEN(value));
152
0
      smart_str_append(&buf, encoded_value);
153
0
      zend_string_release_ex(encoded_value, 0);
154
0
    } else {
155
0
      smart_str_append(&buf, value);
156
0
    }
157
158
0
    if (expires > 0) {
159
0
      double diff;
160
161
0
      smart_str_appends(&buf, COOKIE_EXPIRES);
162
0
      dt = php_format_date("D, d M Y H:i:s \\G\\M\\T", sizeof("D, d M Y H:i:s \\G\\M\\T")-1, expires, 0);
163
164
0
      smart_str_append(&buf, dt);
165
0
      zend_string_free(dt);
166
167
0
      diff = difftime(expires, php_time());
168
0
      if (diff < 0) {
169
0
        diff = 0;
170
0
      }
171
172
0
      smart_str_appends(&buf, COOKIE_MAX_AGE);
173
0
      smart_str_append_long(&buf, (zend_long) diff);
174
0
    }
175
0
  }
176
177
0
  if (path && ZSTR_LEN(path)) {
178
0
    smart_str_appends(&buf, COOKIE_PATH);
179
0
    smart_str_append(&buf, path);
180
0
  }
181
0
  if (domain && ZSTR_LEN(domain)) {
182
0
    smart_str_appends(&buf, COOKIE_DOMAIN);
183
0
    smart_str_append(&buf, domain);
184
0
  }
185
0
  if (secure) {
186
0
    smart_str_appends(&buf, COOKIE_SECURE);
187
0
  }
188
0
  if (httponly) {
189
0
    smart_str_appends(&buf, COOKIE_HTTPONLY);
190
0
  }
191
0
  if (samesite && ZSTR_LEN(samesite)) {
192
0
    smart_str_appends(&buf, COOKIE_SAMESITE);
193
0
    smart_str_append(&buf, samesite);
194
0
  }
195
0
  if (partitioned) {
196
0
    smart_str_appends(&buf, COOKIE_PARTITIONED);
197
0
  }
198
199
0
  ctr.line = ZSTR_VAL(buf.s);
200
0
  ctr.line_len = (uint32_t) ZSTR_LEN(buf.s);
201
202
0
  result = sapi_header_op(SAPI_HEADER_ADD, &ctr);
203
0
  zend_string_release(buf.s);
204
0
  return result;
205
0
}
206
207
static zend_result php_head_parse_cookie_options_array(HashTable *options, zend_long *expires, zend_string **path,
208
    zend_string **domain, bool *secure, bool *httponly, zend_string **samesite, bool *partitioned)
209
0
{
210
0
  zend_string *key;
211
0
  zval *value;
212
213
0
  ZEND_HASH_FOREACH_STR_KEY_VAL(options, key, value) {
214
0
    if (!key) {
215
0
      zend_value_error("%s(): option array cannot have numeric keys", get_active_function_name());
216
0
      return FAILURE;
217
0
    }
218
0
    if (zend_string_equals_literal_ci(key, "expires")) {
219
0
      *expires = zval_get_long(value);
220
0
    } else if (zend_string_equals_literal_ci(key, "path")) {
221
0
      if (*path) {
222
0
        zend_string_release(*path);
223
0
      }
224
0
      *path = zval_get_string(value);
225
0
    } else if (zend_string_equals_literal_ci(key, "domain")) {
226
0
      if (*domain) {
227
0
        zend_string_release(*domain);
228
0
      }
229
0
      *domain = zval_get_string(value);
230
0
    } else if (zend_string_equals_literal_ci(key, "secure")) {
231
0
      *secure = zend_is_true(value);
232
0
    } else if (zend_string_equals_literal_ci(key, "httponly")) {
233
0
      *httponly = zend_is_true(value);
234
0
    } else if (zend_string_equals_literal_ci(key, "samesite")) {
235
0
      if (*samesite) {
236
0
        zend_string_release(*samesite);
237
0
      }
238
0
      *samesite = zval_get_string(value);
239
0
    } else if (zend_string_equals_literal_ci(key, "partitioned")) {
240
0
      *partitioned = zend_is_true(value);
241
0
    } else {
242
0
      zend_value_error("%s(): option \"%s\" is invalid", get_active_function_name(), ZSTR_VAL(key));
243
0
      return FAILURE;
244
0
    }
245
0
  } ZEND_HASH_FOREACH_END();
246
0
  return SUCCESS;
247
0
}
248
249
static void php_setcookie_common(INTERNAL_FUNCTION_PARAMETERS, bool is_raw)
250
0
{
251
0
  HashTable *options = NULL;
252
0
  zend_long expires = 0;
253
0
  zend_string *name, *value = NULL, *path = NULL, *domain = NULL, *samesite = NULL;
254
0
  bool secure = 0, httponly = 0, partitioned = false;
255
256
0
  ZEND_PARSE_PARAMETERS_START(1, 7)
257
0
    Z_PARAM_STR(name)
258
0
    Z_PARAM_OPTIONAL
259
0
    Z_PARAM_STR(value)
260
0
    Z_PARAM_ARRAY_HT_OR_LONG(options, expires)
261
0
    Z_PARAM_STR(path)
262
0
    Z_PARAM_STR(domain)
263
0
    Z_PARAM_BOOL(secure)
264
0
    Z_PARAM_BOOL(httponly)
265
0
  ZEND_PARSE_PARAMETERS_END();
266
267
0
  if (options) {
268
0
    if (UNEXPECTED(ZEND_NUM_ARGS() > 3)) {
269
0
      zend_argument_count_error("%s(): Expects exactly 3 arguments when argument #3 "
270
0
        "($expires_or_options) is an array", get_active_function_name());
271
0
      RETURN_THROWS();
272
0
    }
273
274
0
    if (FAILURE == php_head_parse_cookie_options_array(options, &expires, &path,
275
0
      &domain, &secure, &httponly, &samesite, &partitioned)
276
0
    ) {
277
0
      goto cleanup;
278
0
    }
279
0
  }
280
281
0
  RETVAL_BOOL(php_setcookie(name, value, expires, path, domain, secure, httponly, samesite, partitioned, !is_raw) == SUCCESS);
282
283
0
  if (options) {
284
0
cleanup:
285
0
    if (path) {
286
0
      zend_string_release(path);
287
0
    }
288
0
    if (domain) {
289
0
      zend_string_release(domain);
290
0
    }
291
0
    if (samesite) {
292
0
      zend_string_release(samesite);
293
0
    }
294
0
  }
295
0
}
296
297
/* {{{ setcookie(string name [, string value [, array options]])
298
   Send a cookie */
299
PHP_FUNCTION(setcookie)
300
0
{
301
0
  php_setcookie_common(INTERNAL_FUNCTION_PARAM_PASSTHRU, false);
302
0
}
303
/* }}} */
304
305
/* {{{ setrawcookie(string name [, string value [, array options]])
306
   Send a cookie with no url encoding of the value */
307
PHP_FUNCTION(setrawcookie)
308
0
{
309
0
  php_setcookie_common(INTERNAL_FUNCTION_PARAM_PASSTHRU, true);
310
0
}
311
/* }}} */
312
313
314
/* {{{ Returns true if headers have already been sent, false otherwise */
315
PHP_FUNCTION(headers_sent)
316
17
{
317
17
  zval *by_ref_filename = NULL;
318
17
  zval *by_ref_line = NULL;
319
17
  const char *file = "";
320
17
  int line = 0;
321
322
51
  ZEND_PARSE_PARAMETERS_START(0, 2)
323
51
    Z_PARAM_OPTIONAL
324
68
    Z_PARAM_ZVAL(by_ref_filename)
325
68
    Z_PARAM_ZVAL(by_ref_line)
326
46
  ZEND_PARSE_PARAMETERS_END();
327
328
17
  if (SG(headers_sent)) {
329
0
    line = php_output_get_start_lineno();
330
0
    file = php_output_get_start_filename();
331
0
  }
332
333
17
  if (by_ref_filename) {
334
17
    if (file) {
335
17
      ZEND_TRY_ASSIGN_REF_STRING(by_ref_filename, file);
336
17
    } else {
337
0
      ZEND_TRY_ASSIGN_REF_EMPTY_STRING(by_ref_filename);
338
0
    }
339
17
  }
340
17
  if (by_ref_line) {
341
6
    ZEND_TRY_ASSIGN_REF_LONG(by_ref_line, line);
342
6
  }
343
344
17
  RETURN_BOOL(SG(headers_sent));
345
17
}
346
/* }}} */
347
348
/* {{{ php_head_apply_header_list_to_hash
349
   Turn an llist of sapi_header_struct headers into a numerically indexed zval hash */
350
static void php_head_apply_header_list_to_hash(void *data, void *arg)
351
0
{
352
0
  sapi_header_struct *sapi_header = (sapi_header_struct *)data;
353
354
0
  if (arg && sapi_header) {
355
0
    add_next_index_string((zval *)arg, (char *)(sapi_header->header));
356
0
  }
357
0
}
358
359
/* {{{ Return list of headers to be sent / already sent */
360
PHP_FUNCTION(headers_list)
361
0
{
362
0
  ZEND_PARSE_PARAMETERS_NONE();
363
364
0
  array_init(return_value);
365
0
  zend_llist_apply_with_argument(&SG(sapi_headers).headers, php_head_apply_header_list_to_hash, return_value);
366
0
}
367
/* }}} */
368
369
/* {{{ Sets a response code, or returns the current HTTP response code */
370
PHP_FUNCTION(http_response_code)
371
0
{
372
0
  zend_long response_code = 0;
373
374
0
  ZEND_PARSE_PARAMETERS_START(0, 1)
375
0
    Z_PARAM_OPTIONAL
376
0
    Z_PARAM_LONG(response_code)
377
0
  ZEND_PARSE_PARAMETERS_END();
378
379
0
  if (response_code)
380
0
  {
381
0
    if (SG(headers_sent) && !SG(request_info).no_headers) {
382
0
      const char *output_start_filename = php_output_get_start_filename();
383
0
      int output_start_lineno = php_output_get_start_lineno();
384
385
0
      if (output_start_filename) {
386
0
        php_error_docref(NULL, E_WARNING, "Cannot set response code - headers already sent "
387
0
          "(output started at %s:%d)", output_start_filename, output_start_lineno);
388
0
      } else {
389
0
        php_error_docref(NULL, E_WARNING, "Cannot set response code - headers already sent");
390
0
      }
391
0
      RETURN_FALSE;
392
0
    }
393
394
0
    if (SG(sapi_headers).http_status_line) {
395
0
      php_error_docref(NULL, E_WARNING, "Calling http_response_code() after header('HTTP/...') has no effect");
396
      // If it is decided that this should have effect in the future, replace warning with
397
      // efree(SG(sapi_headers).http_status_line);
398
      // SG(sapi_headers).http_status_line = NULL;
399
0
    }
400
401
0
    zend_long old_response_code;
402
403
0
    old_response_code = SG(sapi_headers).http_response_code;
404
0
    SG(sapi_headers).http_response_code = (int)response_code;
405
406
0
    if (old_response_code) {
407
0
      RETURN_LONG(old_response_code);
408
0
    }
409
410
0
    RETURN_TRUE;
411
0
  }
412
413
0
  if (!SG(sapi_headers).http_response_code) {
414
0
    RETURN_FALSE;
415
0
  }
416
417
0
  RETURN_LONG(SG(sapi_headers).http_response_code);
418
0
}
419
/* }}} */