67
67
68
68
#include "php_fopen_wrappers.h"
69
69
70
- #define HTTP_HEADER_BLOCK_SIZE 1024
71
- #define PHP_URL_REDIRECT_MAX 20
72
- #define HTTP_HEADER_USER_AGENT 1
73
- #define HTTP_HEADER_HOST 2
74
- #define HTTP_HEADER_AUTH 4
75
- #define HTTP_HEADER_FROM 8
76
- #define HTTP_HEADER_CONTENT_LENGTH 16
77
- #define HTTP_HEADER_TYPE 32
78
- #define HTTP_HEADER_CONNECTION 64
70
+ #define HTTP_HEADER_BLOCK_SIZE 1024
71
+ #define HTTP_HEADER_MAX_LOCATION_SIZE 8182 /* 8192 - 10 (size of "Location: ") */
72
+ #define PHP_URL_REDIRECT_MAX 20
73
+ #define HTTP_HEADER_USER_AGENT 1
74
+ #define HTTP_HEADER_HOST 2
75
+ #define HTTP_HEADER_AUTH 4
76
+ #define HTTP_HEADER_FROM 8
77
+ #define HTTP_HEADER_CONTENT_LENGTH 16
78
+ #define HTTP_HEADER_TYPE 32
79
+ #define HTTP_HEADER_CONNECTION 64
79
80
80
81
#define HTTP_WRAPPER_HEADER_INIT 1
81
82
#define HTTP_WRAPPER_REDIRECTED 2
@@ -120,17 +121,15 @@ typedef struct _php_stream_http_response_header_info {
120
121
size_t file_size ;
121
122
bool error ;
122
123
bool follow_location ;
123
- char location [HTTP_HEADER_BLOCK_SIZE ];
124
+ char * location ;
125
+ size_t location_len ;
124
126
} php_stream_http_response_header_info ;
125
127
126
128
static void php_stream_http_response_header_info_init (
127
129
php_stream_http_response_header_info * header_info )
128
130
{
129
- header_info -> transfer_encoding = NULL ;
130
- header_info -> file_size = 0 ;
131
- header_info -> error = false;
131
+ memset (header_info , 0 , sizeof (php_stream_http_response_header_info ));
132
132
header_info -> follow_location = 1 ;
133
- header_info -> location [0 ] = '\0' ;
134
133
}
135
134
136
135
/* Trim white spaces from response header line and update its length */
@@ -256,7 +255,22 @@ static zend_string *php_stream_http_response_headers_parse(php_stream_wrapper *w
256
255
* RFC 7238 defines 308: http://tools.ietf.org/html/rfc7238 */
257
256
header_info -> follow_location = 0 ;
258
257
}
259
- strlcpy (header_info -> location , last_header_value , sizeof (header_info -> location ));
258
+ size_t last_header_value_len = strlen (last_header_value );
259
+ if (last_header_value_len > HTTP_HEADER_MAX_LOCATION_SIZE ) {
260
+ header_info -> error = true;
261
+ php_stream_wrapper_log_error (wrapper , options ,
262
+ "HTTP Location header size is over the limit of %d bytes" ,
263
+ HTTP_HEADER_MAX_LOCATION_SIZE );
264
+ zend_string_efree (last_header_line_str );
265
+ return NULL ;
266
+ }
267
+ if (header_info -> location_len == 0 ) {
268
+ header_info -> location = emalloc (last_header_value_len + 1 );
269
+ } else if (header_info -> location_len <= last_header_value_len ) {
270
+ header_info -> location = erealloc (header_info -> location , last_header_value_len + 1 );
271
+ }
272
+ header_info -> location_len = last_header_value_len ;
273
+ memcpy (header_info -> location , last_header_value , last_header_value_len + 1 );
260
274
} else if (!strncasecmp (last_header_line , "Content-Type:" , sizeof ("Content-Type:" )- 1 )) {
261
275
php_stream_notify_info (context , PHP_STREAM_NOTIFY_MIME_TYPE_IS , last_header_value , 0 );
262
276
} else if (!strncasecmp (last_header_line , "Content-Length:" , sizeof ("Content-Length:" )- 1 )) {
@@ -536,6 +550,8 @@ static php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper,
536
550
}
537
551
}
538
552
553
+ php_stream_http_response_header_info_init (& header_info );
554
+
539
555
if (stream == NULL )
540
556
goto out ;
541
557
@@ -917,8 +933,6 @@ static php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper,
917
933
}
918
934
}
919
935
920
- php_stream_http_response_header_info_init (& header_info );
921
-
922
936
/* read past HTTP headers */
923
937
while (!php_stream_eof (stream )) {
924
938
size_t http_header_line_length ;
@@ -988,12 +1002,12 @@ static php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper,
988
1002
last_header_line_str , NULL , NULL , response_code , response_header , & header_info );
989
1003
}
990
1004
991
- if (!reqok || (header_info .location [ 0 ] != '\0' && header_info .follow_location )) {
1005
+ if (!reqok || (header_info .location != NULL && header_info .follow_location )) {
992
1006
if (!header_info .follow_location || (((options & STREAM_ONLY_GET_HEADERS ) || ignore_errors ) && redirect_max <= 1 )) {
993
1007
goto out ;
994
1008
}
995
1009
996
- if (header_info .location [ 0 ] != '\0' )
1010
+ if (header_info .location != NULL )
997
1011
php_stream_notify_info (context , PHP_STREAM_NOTIFY_REDIRECTED , header_info .location , 0 );
998
1012
999
1013
php_stream_close (stream );
@@ -1004,18 +1018,17 @@ static php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper,
1004
1018
header_info .transfer_encoding = NULL ;
1005
1019
}
1006
1020
1007
- if (header_info .location [ 0 ] != '\0' ) {
1021
+ if (header_info .location != NULL ) {
1008
1022
1009
- char new_path [HTTP_HEADER_BLOCK_SIZE ];
1010
- char loc_path [HTTP_HEADER_BLOCK_SIZE ];
1023
+ char * new_path = NULL ;
1011
1024
1012
- * new_path = '\0' ;
1013
1025
if (strlen (header_info .location ) < 8 ||
1014
1026
(strncasecmp (header_info .location , "http://" , sizeof ("http://" )- 1 ) &&
1015
1027
strncasecmp (header_info .location , "https://" , sizeof ("https://" )- 1 ) &&
1016
1028
strncasecmp (header_info .location , "ftp://" , sizeof ("ftp://" )- 1 ) &&
1017
1029
strncasecmp (header_info .location , "ftps://" , sizeof ("ftps://" )- 1 )))
1018
1030
{
1031
+ char * loc_path = NULL ;
1019
1032
if (* header_info .location != '/' ) {
1020
1033
if (* (header_info .location + 1 ) != '\0' && resource -> path ) {
1021
1034
char * s = strrchr (ZSTR_VAL (resource -> path ), '/' );
@@ -1033,31 +1046,35 @@ static php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper,
1033
1046
if (resource -> path &&
1034
1047
ZSTR_VAL (resource -> path )[0 ] == '/' &&
1035
1048
ZSTR_VAL (resource -> path )[1 ] == '\0' ) {
1036
- snprintf (loc_path , sizeof (loc_path ) - 1 , "%s%s" ,
1037
- ZSTR_VAL (resource -> path ), header_info .location );
1049
+ spprintf (& loc_path , 0 , "%s%s" , ZSTR_VAL (resource -> path ), header_info .location );
1038
1050
} else {
1039
- snprintf (loc_path , sizeof (loc_path ) - 1 , "%s/%s" ,
1040
- ZSTR_VAL (resource -> path ), header_info .location );
1051
+ spprintf (& loc_path , 0 , "%s/%s" , ZSTR_VAL (resource -> path ), header_info .location );
1041
1052
}
1042
1053
} else {
1043
- snprintf ( loc_path , sizeof ( loc_path ) - 1 , "/%s" , header_info .location );
1054
+ spprintf ( & loc_path , 0 , "/%s" , header_info .location );
1044
1055
}
1045
1056
} else {
1046
- strlcpy (loc_path , header_info .location , sizeof (loc_path ));
1057
+ loc_path = header_info .location ;
1058
+ header_info .location = NULL ;
1047
1059
}
1048
1060
if ((use_ssl && resource -> port != 443 ) || (!use_ssl && resource -> port != 80 )) {
1049
- snprintf (new_path , sizeof (new_path ) - 1 , "%s://%s:%d%s" , ZSTR_VAL (resource -> scheme ), ZSTR_VAL (resource -> host ), resource -> port , loc_path );
1061
+ spprintf (& new_path , 0 , "%s://%s:%d%s" , ZSTR_VAL (resource -> scheme ),
1062
+ ZSTR_VAL (resource -> host ), resource -> port , loc_path );
1050
1063
} else {
1051
- snprintf (new_path , sizeof (new_path ) - 1 , "%s://%s%s" , ZSTR_VAL (resource -> scheme ), ZSTR_VAL (resource -> host ), loc_path );
1064
+ spprintf (& new_path , 0 , "%s://%s%s" , ZSTR_VAL (resource -> scheme ),
1065
+ ZSTR_VAL (resource -> host ), loc_path );
1052
1066
}
1067
+ efree (loc_path );
1053
1068
} else {
1054
- strlcpy (new_path , header_info .location , sizeof (new_path ));
1069
+ new_path = header_info .location ;
1070
+ header_info .location = NULL ;
1055
1071
}
1056
1072
1057
1073
php_url_free (resource );
1058
1074
/* check for invalid redirection URLs */
1059
1075
if ((resource = php_url_parse (new_path )) == NULL ) {
1060
1076
php_stream_wrapper_log_error (wrapper , options , "Invalid redirect URL! %s" , new_path );
1077
+ efree (new_path );
1061
1078
goto out ;
1062
1079
}
1063
1080
@@ -1069,6 +1086,7 @@ static php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper,
1069
1086
while (s < e) { \
1070
1087
if (iscntrl(*s)) { \
1071
1088
php_stream_wrapper_log_error(wrapper, options, "Invalid redirect URL! %s", new_path); \
1089
+ efree(new_path); \
1072
1090
goto out; \
1073
1091
} \
1074
1092
s++; \
@@ -1091,6 +1109,7 @@ static php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper,
1091
1109
stream = php_stream_url_wrap_http_ex (
1092
1110
wrapper , new_path , mode , options , opened_path , context ,
1093
1111
-- redirect_max , new_flags , response_header STREAMS_CC );
1112
+ efree (new_path );
1094
1113
} else {
1095
1114
php_stream_wrapper_log_error (wrapper , options , "HTTP request failed! %s" , tmp_line );
1096
1115
}
@@ -1103,6 +1122,10 @@ static php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper,
1103
1122
efree (http_header_line );
1104
1123
}
1105
1124
1125
+ if (header_info .location != NULL ) {
1126
+ efree (header_info .location );
1127
+ }
1128
+
1106
1129
if (resource ) {
1107
1130
php_url_free (resource );
1108
1131
}
0 commit comments