Coverage Report

Created: 2026-10-28 06:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/php-src/main/php_variables.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
   | Authors: Rasmus Lerdorf <rasmus@lerdorf.on.ca>                       |
12
   |          Zeev Suraski <zeev@php.net>                                 |
13
   +----------------------------------------------------------------------+
14
 */
15
16
#include <stdio.h>
17
#include "php.h"
18
#include "ext/standard/php_standard.h"
19
#include "ext/standard/credits.h"
20
#include "zend_smart_str.h"
21
#include "php_variables.h"
22
#include "php_globals.h"
23
#include "php_content_types.h"
24
#include "SAPI.h"
25
#include "zend_globals.h"
26
#include "zend_exceptions.h"
27
28
/* for systems that need to override reading of environment variables */
29
static void _php_import_environment_variables(zval *array_ptr);
30
static void _php_load_environment_variables(zval *array_ptr);
31
PHPAPI void (*php_import_environment_variables)(zval *array_ptr) = _php_import_environment_variables;
32
PHPAPI void (*php_load_environment_variables)(zval *array_ptr) = _php_load_environment_variables;
33
34
PHPAPI void php_register_variable(const char *var, const char *strval, zval *track_vars_array)
35
162k
{
36
162k
  php_register_variable_safe(var, strval, strlen(strval), track_vars_array);
37
162k
}
38
39
/* binary-safe version */
40
PHPAPI void php_register_variable_safe(const char *var, const char *strval, size_t str_len, zval *track_vars_array)
41
162k
{
42
162k
  zval new_entry;
43
162k
  assert(strval != NULL);
44
45
162k
  ZVAL_STRINGL_FAST(&new_entry, strval, str_len);
46
47
162k
  php_register_variable_ex(var, &new_entry, track_vars_array);
48
162k
}
49
50
static zend_always_inline void php_register_variable_quick(const char *name, size_t name_len, zval *val, HashTable *ht)
51
1.51k
{
52
1.51k
  zend_string *key = zend_string_init_interned(name, name_len, 0);
53
54
1.51k
  zend_hash_update_ind(ht, key, val);
55
1.51k
  zend_string_release_ex(key, 0);
56
1.51k
}
57
58
PHPAPI void php_register_known_variable(const char *var_name, size_t var_name_len, zval *value, zval *track_vars_array)
59
0
{
60
0
  HashTable *symbol_table = NULL;
61
62
0
  ZEND_ASSERT(var_name != NULL);
63
0
  ZEND_ASSERT(var_name_len != 0);
64
0
  ZEND_ASSERT(track_vars_array != NULL && Z_TYPE_P(track_vars_array) == IS_ARRAY);
65
66
0
  symbol_table = Z_ARRVAL_P(track_vars_array);
67
68
0
#if ZEND_DEBUG
69
  /* Verify the name is valid for a PHP variable */
70
0
  ZEND_ASSERT(!(var_name_len == strlen("GLOBALS") && !memcmp(var_name, "GLOBALS", strlen("GLOBALS"))));
71
0
  ZEND_ASSERT(!(var_name_len == strlen("this") && !memcmp(var_name, "this", strlen("this"))));
72
73
  /* Assert that the variable name is not numeric */
74
0
  zend_ulong idx;
75
0
  ZEND_ASSERT(!ZEND_HANDLE_NUMERIC_STR(var_name, var_name_len, idx));
76
  /* ensure that we don't have null bytes, spaces, dots, or array bracket in the variable name (not binary safe) */
77
0
  const char *p = var_name;
78
0
  for (size_t l = 0; l < var_name_len; l++) {
79
0
    ZEND_ASSERT(*p != '\0' && *p != ' ' && *p != '.' && *p != '[');
80
0
    p++;
81
0
  }
82
83
  /* Do not allow to register cookies this way */
84
0
  ZEND_ASSERT(Z_TYPE(PG(http_globals)[TRACK_VARS_COOKIE]) == IS_UNDEF ||
85
0
    Z_ARRVAL(PG(http_globals)[TRACK_VARS_COOKIE]) != symbol_table);
86
0
#endif
87
88
0
  php_register_variable_quick(var_name, var_name_len, value, symbol_table);
89
0
}
90
91
/* Discard variable if mangling made it start with __Host-, where pre-mangling it did not start with __Host-
92
 * Discard variable if mangling made it start with __Secure-, where pre-mangling it did not start with __Secure- */
93
static bool php_is_forbidden_variable_name(const char *mangled_name, size_t mangled_name_len, const char *pre_mangled_name)
94
0
{
95
0
  if (mangled_name_len >= sizeof("__Host-")-1 && strncmp(mangled_name, "__Host-", sizeof("__Host-")-1) == 0 && strncmp(pre_mangled_name, "__Host-", sizeof("__Host-")-1) != 0) {
96
0
    return true;
97
0
  }
98
99
0
  if (mangled_name_len >= sizeof("__Secure-")-1 && strncmp(mangled_name, "__Secure-", sizeof("__Secure-")-1) == 0 && strncmp(pre_mangled_name, "__Secure-", sizeof("__Secure-")-1) != 0) {
100
0
    return true;
101
0
  }
102
103
0
  return false;
104
0
}
105
106
PHPAPI void php_register_variable_ex(const char *var_name, zval *val, zval *track_vars_array)
107
162k
{
108
162k
  char *p = NULL;
109
162k
  char *ip = NULL;   /* index pointer */
110
162k
  char *index;
111
162k
  char *var, *var_orig;
112
162k
  size_t var_len, index_len;
113
162k
  zval gpc_element, *gpc_element_p;
114
162k
  bool is_array = 0;
115
162k
  HashTable *symtable1 = NULL;
116
162k
  ALLOCA_FLAG(use_heap)
117
118
162k
  assert(var_name != NULL);
119
120
162k
  if (track_vars_array && Z_TYPE_P(track_vars_array) == IS_ARRAY) {
121
0
    symtable1 = Z_ARRVAL_P(track_vars_array);
122
0
  }
123
124
162k
  if (!symtable1) {
125
    /* Nothing to do */
126
162k
    zval_ptr_dtor_nogc(val);
127
162k
    return;
128
162k
  }
129
130
131
  /* ignore leading spaces in the variable name */
132
0
  while (*var_name==' ') {
133
0
    var_name++;
134
0
  }
135
136
  /*
137
   * Prepare variable name
138
   */
139
0
  var_len = strlen(var_name);
140
0
  var = var_orig = do_alloca(var_len + 1, use_heap);
141
0
  memcpy(var_orig, var_name, var_len + 1);
142
143
  /* ensure that we don't have spaces or dots in the variable name (not binary safe) */
144
0
  for (p = var; *p; p++) {
145
0
    if (*p == ' ' || *p == '.') {
146
0
      *p='_';
147
0
    } else if (*p == '[') {
148
0
      is_array = 1;
149
0
      ip = p;
150
0
      *p = 0;
151
0
      break;
152
0
    }
153
0
  }
154
0
  var_len = p - var;
155
156
0
  if (var_len==0) { /* empty variable name, or variable name with a space in it */
157
0
    zval_ptr_dtor_nogc(val);
158
0
    free_alloca(var_orig, use_heap);
159
0
    return;
160
0
  }
161
162
0
  if (var_len == sizeof("this")-1 && EG(current_execute_data)) {
163
0
    zend_execute_data *ex = EG(current_execute_data);
164
165
0
    while (ex) {
166
0
      if (ex->func && ZEND_USER_CODE(ex->func->common.type)) {
167
0
        if ((ZEND_CALL_INFO(ex) & ZEND_CALL_HAS_SYMBOL_TABLE)
168
0
            && ex->symbol_table == symtable1) {
169
0
          if (memcmp(var, "this", sizeof("this")-1) == 0) {
170
0
            zend_throw_error(NULL, "Cannot re-assign $this");
171
0
            zval_ptr_dtor_nogc(val);
172
0
            free_alloca(var_orig, use_heap);
173
0
            return;
174
0
          }
175
0
        }
176
0
        break;
177
0
      }
178
0
      ex = ex->prev_execute_data;
179
0
    }
180
0
  }
181
182
  /* GLOBALS hijack attempt, reject parameter */
183
0
  if (symtable1 == &EG(symbol_table) &&
184
0
    var_len == sizeof("GLOBALS")-1 &&
185
0
    !memcmp(var, "GLOBALS", sizeof("GLOBALS")-1)) {
186
0
    zval_ptr_dtor_nogc(val);
187
0
    free_alloca(var_orig, use_heap);
188
0
    return;
189
0
  }
190
191
0
  index = var;
192
0
  index_len = var_len;
193
194
0
  if (is_array) {
195
0
    int nest_level = 0;
196
0
    while (1) {
197
0
      char *index_s;
198
0
      size_t new_idx_len = 0;
199
200
0
      if(++nest_level > PG(max_input_nesting_level)) {
201
0
        HashTable *ht;
202
        /* too many levels of nesting */
203
204
0
        if (track_vars_array) {
205
0
          ht = Z_ARRVAL_P(track_vars_array);
206
0
          zend_symtable_str_del(ht, var, var_len);
207
0
        }
208
209
0
        zval_ptr_dtor_nogc(val);
210
211
        /* do not output the error message to the screen,
212
         this helps us to avoid "information disclosure" */
213
0
        if (!PG(display_errors)) {
214
0
          php_error_docref(NULL, E_WARNING, "Input variable nesting level exceeded " ZEND_LONG_FMT ". To increase the limit change max_input_nesting_level in php.ini.", PG(max_input_nesting_level));
215
0
        }
216
0
        free_alloca(var_orig, use_heap);
217
0
        return;
218
0
      }
219
220
      /* Each level retains an array, so stop once over the limit. */
221
0
      if (ZEND_HAS_OOM()) {
222
0
        zval_ptr_dtor_nogc(val);
223
0
        free_alloca(var_orig, use_heap);
224
0
        return;
225
0
      }
226
227
0
      ip++;
228
0
      index_s = ip;
229
0
      if (isspace((unsigned char)*ip)) {
230
0
        ip++;
231
0
      }
232
0
      if (*ip==']') {
233
0
        index_s = NULL;
234
0
      } else {
235
0
        ip = strchr(ip, ']');
236
0
        if (!ip) {
237
          /* not an index; un-terminate the var name */
238
0
          *(index_s - 1) = '_';
239
          /* PHP variables cannot contain ' ', '.', '[' in their names, so we replace the characters with a '_' */
240
0
          for (p = index_s; *p; p++) {
241
0
            if (*p == ' ' || *p == '.' || *p == '[') {
242
0
              *p = '_';
243
0
            }
244
0
          }
245
246
0
          index_len = 0;
247
0
          if (index) {
248
0
            index_len = strlen(index);
249
0
          }
250
0
          goto plain_var;
251
0
          return;
252
0
        }
253
0
        *ip = 0;
254
0
        new_idx_len = strlen(index_s);
255
0
      }
256
257
0
      if (!index) {
258
0
        array_init_singleton(&gpc_element);
259
0
        if ((gpc_element_p = zend_hash_next_index_insert(symtable1, &gpc_element)) == NULL) {
260
0
          zend_array_destroy(Z_ARR(gpc_element));
261
0
          zval_ptr_dtor_nogc(val);
262
0
          free_alloca(var_orig, use_heap);
263
0
          return;
264
0
        }
265
0
      } else {
266
0
        if (php_is_forbidden_variable_name(index, index_len, var_name)) {
267
0
          zval_ptr_dtor_nogc(val);
268
0
          free_alloca(var_orig, use_heap);
269
0
          return;
270
0
        }
271
272
0
        gpc_element_p = zend_symtable_str_find(symtable1, index, index_len);
273
0
        if (!gpc_element_p) {
274
0
          zval tmp;
275
0
          array_init_singleton(&tmp);
276
0
          gpc_element_p = zend_symtable_str_update_ind(symtable1, index, index_len, &tmp);
277
0
        } else {
278
0
          if (Z_TYPE_P(gpc_element_p) == IS_INDIRECT) {
279
0
            gpc_element_p = Z_INDIRECT_P(gpc_element_p);
280
0
          }
281
0
          if (Z_TYPE_P(gpc_element_p) != IS_ARRAY) {
282
0
            zval_ptr_dtor_nogc(gpc_element_p);
283
0
            array_init_singleton(gpc_element_p);
284
0
          } else {
285
0
            SEPARATE_ARRAY(gpc_element_p);
286
0
          }
287
0
        }
288
0
      }
289
0
      symtable1 = Z_ARRVAL_P(gpc_element_p);
290
      /* ip pointed to the '[' character, now obtain the key */
291
0
      index = index_s;
292
0
      index_len = new_idx_len;
293
294
0
      ip++;
295
0
      if (*ip == '[') {
296
0
        is_array = 1;
297
0
        *ip = 0;
298
0
      } else {
299
0
        goto plain_var;
300
0
      }
301
0
    }
302
0
  } else {
303
0
plain_var:
304
0
    if (!index) {
305
0
      if (zend_hash_next_index_insert(symtable1, val) == NULL) {
306
0
        zval_ptr_dtor_nogc(val);
307
0
      }
308
0
    } else {
309
0
      if (php_is_forbidden_variable_name(index, index_len, var_name)) {
310
0
        zval_ptr_dtor_nogc(val);
311
0
        free_alloca(var_orig, use_heap);
312
0
        return;
313
0
      }
314
315
0
      zend_ulong idx;
316
317
      /*
318
       * According to rfc2965, more specific paths are listed above the less specific ones.
319
       * If we encounter a duplicate cookie name, we should skip it, since it is not possible
320
       * to have the same (plain text) cookie name for the same path and we should not overwrite
321
       * more specific cookies with the less specific ones.
322
       */
323
0
      if (Z_TYPE(PG(http_globals)[TRACK_VARS_COOKIE]) != IS_UNDEF &&
324
0
        symtable1 == Z_ARRVAL(PG(http_globals)[TRACK_VARS_COOKIE]) &&
325
0
        zend_symtable_str_exists(symtable1, index, index_len)) {
326
0
        zval_ptr_dtor_nogc(val);
327
0
      } else if (ZEND_HANDLE_NUMERIC_STR(index, index_len, idx)) {
328
0
        zend_hash_index_update(symtable1, idx, val);
329
0
      } else {
330
0
        php_register_variable_quick(index, index_len, val, symtable1);
331
0
      }
332
0
    }
333
0
  }
334
0
  free_alloca(var_orig, use_heap);
335
0
}
336
337
typedef struct post_var_data {
338
  smart_str str;
339
  char *ptr;
340
  char *end;
341
  uint64_t cnt;
342
343
  /* Bytes in ptr that have already been scanned for '&' */
344
  size_t already_scanned;
345
} post_var_data_t;
346
347
static bool add_post_var(zval *arr, post_var_data_t *var, bool eof)
348
0
{
349
0
  char *start, *ksep, *vsep, *val;
350
0
  size_t klen, vlen;
351
0
  size_t new_vlen;
352
353
0
  if (var->ptr >= var->end) {
354
0
    return false;
355
0
  }
356
357
0
  start = var->ptr + var->already_scanned;
358
0
  vsep = memchr(start, '&', var->end - start);
359
0
  if (!vsep) {
360
0
    if (!eof) {
361
0
      var->already_scanned = var->end - var->ptr;
362
0
      return false;
363
0
    } else {
364
0
      vsep = var->end;
365
0
    }
366
0
  }
367
368
0
  ksep = memchr(var->ptr, '=', vsep - var->ptr);
369
0
  if (ksep) {
370
0
    *ksep = '\0';
371
    /* "foo=bar&" or "foo=&" */
372
0
    klen = ksep - var->ptr;
373
0
    vlen = vsep - ++ksep;
374
0
  } else {
375
0
    ksep = "";
376
    /* "foo&" */
377
0
    klen = vsep - var->ptr;
378
0
    vlen = 0;
379
0
  }
380
381
0
  php_url_decode(var->ptr, klen);
382
383
0
  val = estrndup(ksep, vlen);
384
0
  if (ZEND_HAS_OOM()) {
385
0
    efree(val);
386
0
    return 0;
387
0
  }
388
0
  if (vlen) {
389
0
    vlen = php_url_decode(val, vlen);
390
0
  }
391
392
0
  if (sapi_module.input_filter(PARSE_POST, var->ptr, &val, vlen, &new_vlen)) {
393
0
    php_register_variable_safe(var->ptr, val, new_vlen, arr);
394
0
  }
395
0
  if (ZEND_HAS_OOM()) {
396
0
    efree(val);
397
0
    return 0;
398
0
  }
399
0
  efree(val);
400
401
0
  var->ptr = vsep + (vsep != var->end);
402
0
  var->already_scanned = 0;
403
0
  return true;
404
0
}
405
406
static inline zend_result add_post_vars(zval *arr, post_var_data_t *vars, bool eof)
407
0
{
408
0
  uint64_t max_vars = REQUEST_PARSE_BODY_OPTION_GET(max_input_vars, PG(max_input_vars));
409
410
0
  vars->ptr = ZSTR_VAL(vars->str.s);
411
0
  vars->end = ZSTR_VAL(vars->str.s) + ZSTR_LEN(vars->str.s);
412
0
  while (add_post_var(arr, vars, eof)) {
413
0
    if (++vars->cnt > max_vars) {
414
0
      php_error_docref(NULL, E_WARNING,
415
0
          "Input variables exceeded %" PRIu64 ". "
416
0
          "To increase the limit change max_input_vars in php.ini.",
417
0
          max_vars);
418
0
      return FAILURE;
419
0
    }
420
0
    if (ZEND_HAS_OOM()) {
421
0
      return FAILURE;
422
0
    }
423
0
  }
424
0
  if (ZEND_HAS_OOM()) {
425
0
    return FAILURE;
426
0
  }
427
428
0
  if (!eof && ZSTR_VAL(vars->str.s) != vars->ptr) {
429
0
    memmove(ZSTR_VAL(vars->str.s), vars->ptr, ZSTR_LEN(vars->str.s) = vars->end - vars->ptr);
430
0
  }
431
0
  return SUCCESS;
432
0
}
433
434
#ifdef PHP_WIN32
435
#define SAPI_POST_HANDLER_BUFSIZ 16384
436
#else
437
0
# define SAPI_POST_HANDLER_BUFSIZ BUFSIZ
438
#endif
439
SAPI_API SAPI_POST_HANDLER_FUNC(php_std_post_handler)
440
0
{
441
0
  zval *arr = (zval *) arg;
442
0
  php_stream *s = SG(request_info).request_body;
443
0
  post_var_data_t post_data;
444
445
0
  if (s && SUCCESS == php_stream_rewind(s)) {
446
0
    memset(&post_data, 0, sizeof(post_data));
447
448
0
    while (!php_stream_eof(s)) {
449
0
      char buf[SAPI_POST_HANDLER_BUFSIZ] = {0};
450
0
      ssize_t len = php_stream_read(s, buf, SAPI_POST_HANDLER_BUFSIZ);
451
452
0
      if (len > 0) {
453
0
        smart_str_appendl(&post_data.str, buf, len);
454
455
0
        if (ZEND_HAS_OOM()) {
456
0
          smart_str_free(&post_data.str);
457
0
          return;
458
0
        }
459
460
0
        if (SUCCESS != add_post_vars(arr, &post_data, 0)) {
461
0
          smart_str_free(&post_data.str);
462
0
          return;
463
0
        }
464
0
      }
465
466
0
      if (len != SAPI_POST_HANDLER_BUFSIZ){
467
0
        break;
468
0
      }
469
0
    }
470
471
0
    if (post_data.str.s) {
472
0
      add_post_vars(arr, &post_data, 1);
473
0
      smart_str_free(&post_data.str);
474
0
    }
475
0
  }
476
0
}
477
#undef SAPI_POST_HANDLER_BUFSIZ
478
479
SAPI_API SAPI_INPUT_FILTER_FUNC(php_default_input_filter)
480
0
{
481
  /* TODO: check .ini setting here and apply user-defined input filter */
482
0
  if(new_val_len) *new_val_len = val_len;
483
0
  return 1;
484
0
}
485
486
SAPI_API SAPI_TREAT_DATA_FUNC(php_default_treat_data)
487
613k
{
488
613k
  char *res = NULL, *var, *val, *separator = NULL;
489
613k
  const char *c_var;
490
613k
  zval array;
491
613k
  int free_buffer = 0;
492
613k
  char *strtok_buf = NULL;
493
613k
  zend_long count = 0;
494
495
613k
  ZVAL_UNDEF(&array);
496
613k
  switch (arg) {
497
0
    case PARSE_POST:
498
306k
    case PARSE_GET:
499
613k
    case PARSE_COOKIE:
500
613k
      array_init_singleton(&array);
501
613k
      switch (arg) {
502
0
        case PARSE_POST:
503
0
          zval_ptr_dtor_nogc(&PG(http_globals)[TRACK_VARS_POST]);
504
0
          ZVAL_COPY_VALUE(&PG(http_globals)[TRACK_VARS_POST], &array);
505
0
          break;
506
306k
        case PARSE_GET:
507
306k
          zval_ptr_dtor_nogc(&PG(http_globals)[TRACK_VARS_GET]);
508
306k
          ZVAL_COPY_VALUE(&PG(http_globals)[TRACK_VARS_GET], &array);
509
306k
          break;
510
306k
        case PARSE_COOKIE:
511
306k
          zval_ptr_dtor_nogc(&PG(http_globals)[TRACK_VARS_COOKIE]);
512
306k
          ZVAL_COPY_VALUE(&PG(http_globals)[TRACK_VARS_COOKIE], &array);
513
306k
          break;
514
613k
      }
515
613k
      break;
516
613k
    default:
517
0
      ZVAL_COPY_VALUE(&array, destArray);
518
0
      break;
519
613k
  }
520
521
613k
  if (arg == PARSE_POST) {
522
0
    sapi_handle_post(&array);
523
0
    return;
524
0
  }
525
526
613k
  if (arg == PARSE_GET) {   /* GET data */
527
306k
    c_var = SG(request_info).query_string;
528
306k
    if (c_var && *c_var) {
529
0
      res = (char *) estrdup(c_var);
530
0
      free_buffer = 1;
531
306k
    } else {
532
306k
      free_buffer = 0;
533
306k
    }
534
306k
  } else if (arg == PARSE_COOKIE) {   /* Cookie data */
535
306k
    c_var = SG(request_info).cookie_data;
536
306k
    if (c_var && *c_var) {
537
0
      res = (char *) estrdup(c_var);
538
0
      free_buffer = 1;
539
306k
    } else {
540
306k
      free_buffer = 0;
541
306k
    }
542
306k
  } else if (arg == PARSE_STRING) {   /* String data */
543
0
    res = str;
544
0
    free_buffer = 1;
545
0
  }
546
547
613k
  if (!res) {
548
613k
    return;
549
613k
  }
550
551
0
  switch (arg) {
552
0
    case PARSE_GET:
553
0
    case PARSE_STRING:
554
0
      separator = ZSTR_VAL(PG(arg_separator).input);
555
0
      break;
556
0
    case PARSE_COOKIE:
557
0
      separator = ";\0";
558
0
      break;
559
0
  }
560
561
0
  var = php_strtok_r(res, separator, &strtok_buf);
562
563
0
  while (var) {
564
0
    size_t val_len;
565
0
    size_t new_val_len;
566
567
0
    val = strchr(var, '=');
568
569
0
    if (arg == PARSE_COOKIE) {
570
      /* Remove leading spaces from cookie names, needed for multi-cookie header where ; can be followed by a space */
571
0
      while (isspace((unsigned char)*var)) {
572
0
        var++;
573
0
      }
574
0
      if (var == val || *var == '\0') {
575
0
        goto next_cookie;
576
0
      }
577
0
    }
578
579
0
    zend_long max_input_vars = REQUEST_PARSE_BODY_OPTION_GET(max_input_vars, PG(max_input_vars));
580
0
    if (++count > max_input_vars) {
581
0
      php_error_docref(NULL, E_WARNING, "Input variables exceeded " ZEND_LONG_FMT ". To increase the limit change max_input_vars in php.ini.", max_input_vars);
582
0
      break;
583
0
    }
584
585
0
    if (val) { /* have a value */
586
587
0
      *val++ = '\0';
588
589
0
      if (arg == PARSE_COOKIE) {
590
0
        val_len = php_raw_url_decode(val, strlen(val));
591
0
      } else {
592
0
        val_len = php_url_decode(val, strlen(val));
593
0
      }
594
0
    } else {
595
0
      val     = "";
596
0
      val_len =  0;
597
0
    }
598
599
0
    val = estrndup(val, val_len);
600
0
    if (arg != PARSE_COOKIE) {
601
0
      php_url_decode(var, strlen(var));
602
0
    }
603
0
    if (sapi_module.input_filter(arg, var, &val, val_len, &new_val_len)) {
604
0
      php_register_variable_safe(var, val, new_val_len, &array);
605
0
    }
606
0
    efree(val);
607
0
next_cookie:
608
0
    var = php_strtok_r(NULL, separator, &strtok_buf);
609
0
  }
610
611
0
  if (free_buffer) {
612
0
    efree(res);
613
0
  }
614
0
}
615
616
static zend_always_inline int valid_environment_name(const char *name, const char *end)
617
1.50k
{
618
1.50k
  const char *s;
619
620
17.8k
  for (s = name; s < end; s++) {
621
16.3k
    if (*s == ' ' || *s == '.' || *s == '[') {
622
0
      return 0;
623
0
    }
624
16.3k
  }
625
1.50k
  return 1;
626
1.50k
}
627
628
static zend_always_inline void import_environment_variable(HashTable *ht, char *env)
629
1.50k
{
630
1.50k
  char *p;
631
1.50k
  size_t name_len, len;
632
1.50k
  zval val;
633
1.50k
  zend_ulong idx;
634
635
1.50k
  p = strchr(env, '=');
636
1.50k
  if (!p
637
1.50k
    || p == env
638
1.50k
    || !valid_environment_name(env, p)) {
639
    /* malformed entry? */
640
0
    return;
641
0
  }
642
1.50k
  name_len = p - env;
643
1.50k
  p++;
644
1.50k
  len = strlen(p);
645
1.50k
  ZVAL_STRINGL_FAST(&val, p, len);
646
1.50k
  if (ZEND_HANDLE_NUMERIC_STR(env, name_len, idx)) {
647
0
    zend_hash_index_update(ht, idx, &val);
648
1.50k
  } else {
649
1.50k
    php_register_variable_quick(env, name_len, &val, ht);
650
1.50k
  }
651
1.50k
}
652
653
static void _php_import_environment_variables(zval *array_ptr)
654
43
{
655
43
  tsrm_env_lock();
656
657
43
#ifndef PHP_WIN32
658
1.54k
  for (char **env = environ; env != NULL && *env != NULL; env++) {
659
1.50k
    import_environment_variable(Z_ARRVAL_P(array_ptr), *env);
660
1.50k
  }
661
#else
662
  wchar_t *environmentw = GetEnvironmentStringsW();
663
  for (wchar_t *envw = environmentw; envw != NULL && *envw; envw += wcslen(envw) + 1) {
664
    char *env = php_win32_cp_w_to_any(envw);
665
    if (env != NULL) {
666
      import_environment_variable(Z_ARRVAL_P(array_ptr), env);
667
      free(env);
668
    }
669
  }
670
  FreeEnvironmentStringsW(environmentw);
671
#endif
672
673
43
  tsrm_env_unlock();
674
43
}
675
676
static void _php_load_environment_variables(zval *array_ptr)
677
0
{
678
0
  php_import_environment_variables(array_ptr);
679
0
}
680
681
/* {{{ php_build_argv */
682
PHPAPI void php_build_argv(const char *s, zval *track_vars_array)
683
306k
{
684
306k
  zval arr, argc, tmp;
685
306k
  int count = 0;
686
687
306k
  if (!(SG(request_info).argc || track_vars_array)) {
688
306k
    return;
689
306k
  }
690
691
0
  array_init(&arr);
692
693
  /* Prepare argv */
694
0
  if (SG(request_info).argc) { /* are we in cli sapi? */
695
0
    int i;
696
0
    for (i = 0; i < SG(request_info).argc; i++) {
697
0
      ZVAL_STRING(&tmp, SG(request_info).argv[i]);
698
0
      if (zend_hash_next_index_insert(Z_ARRVAL(arr), &tmp) == NULL) {
699
0
        zend_string_efree(Z_STR(tmp));
700
0
      }
701
0
    }
702
0
  } else  if (s && *s) {
703
0
    while (1) {
704
0
      const char *space = strchr(s, '+');
705
      /* auto-type */
706
0
      ZVAL_STRINGL(&tmp, s, space ? space - s : strlen(s));
707
0
      count++;
708
0
      if (zend_hash_next_index_insert(Z_ARRVAL(arr), &tmp) == NULL) {
709
0
        zend_string_efree(Z_STR(tmp));
710
0
      }
711
0
      if (!space) {
712
0
        break;
713
0
      }
714
0
      s = space + 1;
715
0
    }
716
0
  }
717
718
  /* prepare argc */
719
0
  if (SG(request_info).argc) {
720
0
    ZVAL_LONG(&argc, SG(request_info).argc);
721
0
  } else {
722
0
    ZVAL_LONG(&argc, count);
723
0
  }
724
725
0
  if (SG(request_info).argc) {
726
0
    Z_ADDREF(arr);
727
0
    zend_hash_update(&EG(symbol_table), ZSTR_KNOWN(ZEND_STR_ARGV), &arr);
728
0
    zend_hash_update(&EG(symbol_table), ZSTR_KNOWN(ZEND_STR_ARGC), &argc);
729
0
  }
730
0
  if (track_vars_array && Z_TYPE_P(track_vars_array) == IS_ARRAY) {
731
0
    Z_ADDREF(arr);
732
0
    zend_hash_update(Z_ARRVAL_P(track_vars_array), ZSTR_KNOWN(ZEND_STR_ARGV), &arr);
733
0
    zend_hash_update(Z_ARRVAL_P(track_vars_array), ZSTR_KNOWN(ZEND_STR_ARGC), &argc);
734
0
  }
735
0
  zval_ptr_dtor_nogc(&arr);
736
0
}
737
/* }}} */
738
739
/* {{{ php_register_server_variables */
740
static inline void php_register_server_variables(void)
741
4
{
742
4
  zval tmp;
743
4
  zval *arr = &PG(http_globals)[TRACK_VARS_SERVER];
744
4
  HashTable *ht;
745
746
4
  zval_ptr_dtor_nogc(arr);
747
4
  array_init(arr);
748
749
  /* Server variables */
750
4
  if (sapi_module.register_server_variables) {
751
4
    sapi_module.register_server_variables(arr);
752
4
  }
753
4
  ht = Z_ARRVAL_P(arr);
754
755
  /* PHP Authentication support */
756
4
  if (SG(request_info).auth_user) {
757
0
    ZVAL_STRING(&tmp, SG(request_info).auth_user);
758
0
    php_register_variable_quick("PHP_AUTH_USER", sizeof("PHP_AUTH_USER")-1, &tmp, ht);
759
0
  }
760
4
  if (SG(request_info).auth_password) {
761
0
    ZVAL_STRING(&tmp, SG(request_info).auth_password);
762
0
    php_register_variable_quick("PHP_AUTH_PW", sizeof("PHP_AUTH_PW")-1, &tmp, ht);
763
0
  }
764
4
  if (SG(request_info).auth_digest) {
765
0
    ZVAL_STRING(&tmp, SG(request_info).auth_digest);
766
0
    php_register_variable_quick("PHP_AUTH_DIGEST", sizeof("PHP_AUTH_DIGEST")-1, &tmp, ht);
767
0
  }
768
769
  /* store request init time */
770
4
  ZVAL_DOUBLE(&tmp, sapi_get_request_time());
771
4
  php_register_variable_quick("REQUEST_TIME_FLOAT", sizeof("REQUEST_TIME_FLOAT")-1, &tmp, ht);
772
4
  ZVAL_LONG(&tmp, zend_dval_to_lval_silent(Z_DVAL(tmp)));
773
4
  php_register_variable_quick("REQUEST_TIME", sizeof("REQUEST_TIME")-1, &tmp, ht);
774
4
}
775
/* }}} */
776
777
/* {{{ php_autoglobal_merge */
778
static void php_autoglobal_merge(HashTable *dest, HashTable *src)
779
0
{
780
0
  zval *src_entry, *dest_entry;
781
0
  zend_string *string_key;
782
0
  zend_ulong num_key;
783
0
  int globals_check = (dest == (&EG(symbol_table)));
784
785
0
  ZEND_HASH_FOREACH_KEY_VAL(src, num_key, string_key, src_entry) {
786
0
    if (Z_TYPE_P(src_entry) != IS_ARRAY
787
0
      || (string_key && (dest_entry = zend_hash_find(dest, string_key)) == NULL)
788
0
      || (string_key == NULL && (dest_entry = zend_hash_index_find(dest, num_key)) == NULL)
789
0
      || Z_TYPE_P(dest_entry) != IS_ARRAY) {
790
0
      Z_TRY_ADDREF_P(src_entry);
791
0
      if (string_key) {
792
0
        if (!globals_check || !zend_string_equals_literal(string_key, "GLOBALS")) {
793
0
          zend_hash_update(dest, string_key, src_entry);
794
0
        } else {
795
0
          Z_TRY_DELREF_P(src_entry);
796
0
        }
797
0
      } else {
798
0
        zend_hash_index_update(dest, num_key, src_entry);
799
0
      }
800
0
    } else {
801
0
      SEPARATE_ARRAY(dest_entry);
802
0
      php_autoglobal_merge(Z_ARRVAL_P(dest_entry), Z_ARRVAL_P(src_entry));
803
0
    }
804
0
  } ZEND_HASH_FOREACH_END();
805
0
}
806
/* }}} */
807
808
/* {{{ php_hash_environment */
809
PHPAPI zend_result php_hash_environment(void)
810
306k
{
811
306k
  memset(PG(http_globals), 0, sizeof(PG(http_globals)));
812
  /* Register $argc and $argv for CLI SAPIs. $_SERVER['argc'] and $_SERVER['argv']
813
   * will be registered in php_auto_globals_create_server() which clears
814
   * PG(http_globals)[TRACK_VARS_SERVER] anyways, making registration at this point
815
   * useless.
816
   */
817
306k
  php_build_argv(NULL, NULL);
818
306k
  zend_activate_auto_globals();
819
306k
  return SUCCESS;
820
306k
}
821
/* }}} */
822
823
static bool php_auto_globals_create_get(zend_string *name)
824
306k
{
825
306k
  if (PG(variables_order) && (strchr(PG(variables_order),'G') || strchr(PG(variables_order),'g'))) {
826
306k
    sapi_module.treat_data(PARSE_GET, NULL, NULL);
827
306k
  } else {
828
0
    zval_ptr_dtor_nogc(&PG(http_globals)[TRACK_VARS_GET]);
829
0
    array_init(&PG(http_globals)[TRACK_VARS_GET]);
830
0
  }
831
832
306k
  zend_hash_update(&EG(symbol_table), name, &PG(http_globals)[TRACK_VARS_GET]);
833
306k
  Z_ADDREF(PG(http_globals)[TRACK_VARS_GET]);
834
835
306k
  return false; /* don't rearm */
836
306k
}
837
838
static bool php_auto_globals_create_post(zend_string *name)
839
306k
{
840
306k
  if (PG(variables_order) &&
841
306k
      (strchr(PG(variables_order),'P') || strchr(PG(variables_order),'p')) &&
842
306k
    !SG(headers_sent) &&
843
306k
    SG(request_info).request_method &&
844
0
    !strcasecmp(SG(request_info).request_method, "POST")) {
845
0
    sapi_module.treat_data(PARSE_POST, NULL, NULL);
846
306k
  } else {
847
306k
    zval_ptr_dtor_nogc(&PG(http_globals)[TRACK_VARS_POST]);
848
306k
    array_init(&PG(http_globals)[TRACK_VARS_POST]);
849
306k
  }
850
851
306k
  zend_hash_update(&EG(symbol_table), name, &PG(http_globals)[TRACK_VARS_POST]);
852
306k
  Z_ADDREF(PG(http_globals)[TRACK_VARS_POST]);
853
854
306k
  return false; /* don't rearm */
855
306k
}
856
857
static bool php_auto_globals_create_cookie(zend_string *name)
858
306k
{
859
306k
  if (PG(variables_order) && (strchr(PG(variables_order),'C') || strchr(PG(variables_order),'c'))) {
860
306k
    sapi_module.treat_data(PARSE_COOKIE, NULL, NULL);
861
306k
  } else {
862
0
    zval_ptr_dtor_nogc(&PG(http_globals)[TRACK_VARS_COOKIE]);
863
0
    array_init(&PG(http_globals)[TRACK_VARS_COOKIE]);
864
0
  }
865
866
306k
  zend_hash_update(&EG(symbol_table), name, &PG(http_globals)[TRACK_VARS_COOKIE]);
867
306k
  Z_ADDREF(PG(http_globals)[TRACK_VARS_COOKIE]);
868
869
306k
  return false; /* don't rearm */
870
306k
}
871
872
static bool php_auto_globals_create_files(zend_string *name)
873
306k
{
874
306k
  if (Z_TYPE(PG(http_globals)[TRACK_VARS_FILES]) == IS_UNDEF) {
875
306k
    array_init(&PG(http_globals)[TRACK_VARS_FILES]);
876
306k
  }
877
878
306k
  zend_hash_update(&EG(symbol_table), name, &PG(http_globals)[TRACK_VARS_FILES]);
879
306k
  Z_ADDREF(PG(http_globals)[TRACK_VARS_FILES]);
880
881
306k
  return false; /* don't rearm */
882
306k
}
883
884
/* Ugly hack to fix HTTP_PROXY issue, see bug #72573 */
885
static void check_http_proxy(HashTable *var_table)
886
43
{
887
43
  if (zend_hash_str_exists(var_table, "HTTP_PROXY", sizeof("HTTP_PROXY")-1)) {
888
0
    char *local_proxy = getenv("HTTP_PROXY");
889
890
0
    if (!local_proxy) {
891
0
      zend_hash_str_del(var_table, "HTTP_PROXY", sizeof("HTTP_PROXY")-1);
892
0
    } else {
893
0
      zval local_zval;
894
0
      ZVAL_STRING(&local_zval, local_proxy);
895
0
      zend_hash_str_update(var_table, "HTTP_PROXY", sizeof("HTTP_PROXY")-1, &local_zval);
896
0
    }
897
0
  }
898
43
}
899
900
static bool php_auto_globals_create_server(zend_string *name)
901
4
{
902
4
  if (PG(variables_order) && (strchr(PG(variables_order),'S') || strchr(PG(variables_order),'s'))) {
903
4
    php_register_server_variables();
904
905
4
    if (SG(request_info).argc) {
906
0
      zval *argc, *argv;
907
908
0
      if ((argc = zend_hash_find_ex_ind(&EG(symbol_table), ZSTR_KNOWN(ZEND_STR_ARGC), 1)) != NULL &&
909
0
        (argv = zend_hash_find_ex_ind(&EG(symbol_table), ZSTR_KNOWN(ZEND_STR_ARGV), 1)) != NULL) {
910
0
        Z_ADDREF_P(argv);
911
0
        zend_hash_update(Z_ARRVAL(PG(http_globals)[TRACK_VARS_SERVER]), ZSTR_KNOWN(ZEND_STR_ARGV), argv);
912
0
        zend_hash_update(Z_ARRVAL(PG(http_globals)[TRACK_VARS_SERVER]), ZSTR_KNOWN(ZEND_STR_ARGC), argc);
913
0
      }
914
4
    } else if (PG(register_argc_argv)) {
915
0
      zend_error(E_DEPRECATED, "Deriving $_SERVER['argv'] from the query string is deprecated. Configure register_argc_argv=0 to turn this message off");
916
0
      php_build_argv(SG(request_info).query_string, &PG(http_globals)[TRACK_VARS_SERVER]);
917
0
    }
918
919
4
  } else {
920
0
    zval_ptr_dtor_nogc(&PG(http_globals)[TRACK_VARS_SERVER]);
921
0
    array_init(&PG(http_globals)[TRACK_VARS_SERVER]);
922
0
    zend_hash_real_init_mixed(Z_ARRVAL(PG(http_globals)[TRACK_VARS_SERVER]));
923
0
  }
924
925
4
  check_http_proxy(Z_ARRVAL(PG(http_globals)[TRACK_VARS_SERVER]));
926
4
  zend_hash_update(&EG(symbol_table), name, &PG(http_globals)[TRACK_VARS_SERVER]);
927
4
  Z_ADDREF(PG(http_globals)[TRACK_VARS_SERVER]);
928
929
  /* TODO: TRACK_VARS_SERVER is modified in a number of places (e.g. phar) past this point,
930
   * where rc>1 due to the $_SERVER global. Ideally this shouldn't happen, but for now we
931
   * ignore this issue, as it would probably require larger changes. */
932
4
  HT_ALLOW_COW_VIOLATION(Z_ARRVAL(PG(http_globals)[TRACK_VARS_SERVER]));
933
934
4
  return false; /* don't rearm */
935
4
}
936
937
static bool php_auto_globals_create_env(zend_string *name)
938
39
{
939
39
  zval_ptr_dtor_nogc(&PG(http_globals)[TRACK_VARS_ENV]);
940
39
  array_init(&PG(http_globals)[TRACK_VARS_ENV]);
941
942
39
  if (PG(variables_order) && (strchr(PG(variables_order),'E') || strchr(PG(variables_order),'e'))) {
943
39
    php_import_environment_variables(&PG(http_globals)[TRACK_VARS_ENV]);
944
39
  }
945
946
39
  check_http_proxy(Z_ARRVAL(PG(http_globals)[TRACK_VARS_ENV]));
947
39
  zend_hash_update(&EG(symbol_table), name, &PG(http_globals)[TRACK_VARS_ENV]);
948
39
  Z_ADDREF(PG(http_globals)[TRACK_VARS_ENV]);
949
950
39
  return false; /* don't rearm */
951
39
}
952
953
static bool php_auto_globals_create_request(zend_string *name)
954
0
{
955
0
  zval form_variables;
956
0
  unsigned char _gpc_flags[3] = {0, 0, 0};
957
0
  char *p;
958
959
0
  array_init(&form_variables);
960
961
0
  if (PG(request_order) != NULL) {
962
0
    p = PG(request_order);
963
0
  } else {
964
0
    p = PG(variables_order);
965
0
  }
966
967
0
  for (; p && *p; p++) {
968
0
    switch (*p) {
969
0
      case 'g':
970
0
      case 'G':
971
0
        if (!_gpc_flags[0]) {
972
0
          php_autoglobal_merge(Z_ARRVAL(form_variables), Z_ARRVAL(PG(http_globals)[TRACK_VARS_GET]));
973
0
          _gpc_flags[0] = 1;
974
0
        }
975
0
        break;
976
0
      case 'p':
977
0
      case 'P':
978
0
        if (!_gpc_flags[1]) {
979
0
          php_autoglobal_merge(Z_ARRVAL(form_variables), Z_ARRVAL(PG(http_globals)[TRACK_VARS_POST]));
980
0
          _gpc_flags[1] = 1;
981
0
        }
982
0
        break;
983
0
      case 'c':
984
0
      case 'C':
985
0
        if (!_gpc_flags[2]) {
986
0
          php_autoglobal_merge(Z_ARRVAL(form_variables), Z_ARRVAL(PG(http_globals)[TRACK_VARS_COOKIE]));
987
0
          _gpc_flags[2] = 1;
988
0
        }
989
0
        break;
990
0
    }
991
0
  }
992
993
0
  zend_hash_update(&EG(symbol_table), name, &form_variables);
994
0
  return false;
995
0
}
996
997
void php_startup_auto_globals(void)
998
16
{
999
16
  zend_register_auto_global(zend_string_init_interned("_GET", sizeof("_GET")-1, 1), 0, php_auto_globals_create_get);
1000
16
  zend_register_auto_global(zend_string_init_interned("_POST", sizeof("_POST")-1, 1), 0, php_auto_globals_create_post);
1001
16
  zend_register_auto_global(zend_string_init_interned("_COOKIE", sizeof("_COOKIE")-1, 1), 0, php_auto_globals_create_cookie);
1002
16
  zend_register_auto_global(ZSTR_KNOWN(ZEND_STR_AUTOGLOBAL_SERVER), PG(auto_globals_jit) && (SG(request_info).argc || !PG(register_argc_argv)), php_auto_globals_create_server);
1003
16
  zend_register_auto_global(ZSTR_KNOWN(ZEND_STR_AUTOGLOBAL_ENV), PG(auto_globals_jit), php_auto_globals_create_env);
1004
16
  zend_register_auto_global(ZSTR_KNOWN(ZEND_STR_AUTOGLOBAL_REQUEST), PG(auto_globals_jit), php_auto_globals_create_request);
1005
16
  zend_register_auto_global(zend_string_init_interned("_FILES", sizeof("_FILES")-1, 1), 0, php_auto_globals_create_files);
1006
16
}