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
@@ -148,17 +149,15 @@ typedef struct _php_stream_http_response_header_info {
148
149
size_t file_size ;
149
150
bool error ;
150
151
bool follow_location ;
151
- char location [HTTP_HEADER_BLOCK_SIZE ];
152
+ char * location ;
153
+ size_t location_len ;
152
154
} php_stream_http_response_header_info ;
153
155
154
156
static void php_stream_http_response_header_info_init (
155
157
php_stream_http_response_header_info * header_info )
156
158
{
157
- header_info -> transfer_encoding = NULL ;
158
- header_info -> file_size = 0 ;
159
- header_info -> error = false;
159
+ memset (header_info , 0 , sizeof (php_stream_http_response_header_info ));
160
160
header_info -> follow_location = 1 ;
161
- header_info -> location [0 ] = '\0' ;
162
161
}
163
162
164
163
/* Trim white spaces from response header line and update its length */
@@ -284,7 +283,22 @@ static zend_string *php_stream_http_response_headers_parse(php_stream_wrapper *w
284
283
* RFC 7238 defines 308: http://tools.ietf.org/html/rfc7238 */
285
284
header_info -> follow_location = 0 ;
286
285
}
287
- strlcpy (header_info -> location , last_header_value , sizeof (header_info -> location ));
286
+ size_t last_header_value_len = strlen (last_header_value );
287
+ if (last_header_value_len > HTTP_HEADER_MAX_LOCATION_SIZE ) {
288
+ header_info -> error = true;
289
+ php_stream_wrapper_log_error (wrapper , options ,
290
+ "HTTP Location header size is over the limit of %d bytes" ,
291
+ HTTP_HEADER_MAX_LOCATION_SIZE );
292
+ zend_string_efree (last_header_line_str );
293
+ return NULL ;
294
+ }
295
+ if (header_info -> location_len == 0 ) {
296
+ header_info -> location = emalloc (last_header_value_len + 1 );
297
+ } else if (header_info -> location_len <= last_header_value_len ) {
298
+ header_info -> location = erealloc (header_info -> location , last_header_value_len + 1 );
299
+ }
300
+ header_info -> location_len = last_header_value_len ;
301
+ memcpy (header_info -> location , last_header_value , last_header_value_len + 1 );
288
302
} else if (!strncasecmp (last_header_line , "Content-Type:" , sizeof ("Content-Type:" )- 1 )) {
289
303
php_stream_notify_info (context , PHP_STREAM_NOTIFY_MIME_TYPE_IS , last_header_value , 0 );
290
304
} else if (!strncasecmp (last_header_line , "Content-Length:" , sizeof ("Content-Length:" )- 1 )) {
@@ -554,6 +568,8 @@ static php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper,
554
568
}
555
569
}
556
570
571
+ php_stream_http_response_header_info_init (& header_info );
572
+
557
573
if (stream == NULL )
558
574
goto out ;
559
575
@@ -935,8 +951,6 @@ static php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper,
935
951
}
936
952
}
937
953
938
- php_stream_http_response_header_info_init (& header_info );
939
-
940
954
/* read past HTTP headers */
941
955
while (!php_stream_eof (stream )) {
942
956
size_t http_header_line_length ;
@@ -1006,12 +1020,12 @@ static php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper,
1006
1020
last_header_line_str , NULL , NULL , response_code , response_header , & header_info );
1007
1021
}
1008
1022
1009
- if (!reqok || (header_info .location [ 0 ] != '\0' && header_info .follow_location )) {
1023
+ if (!reqok || (header_info .location != NULL && header_info .follow_location )) {
1010
1024
if (!header_info .follow_location || (((options & STREAM_ONLY_GET_HEADERS ) || ignore_errors ) && redirect_max <= 1 )) {
1011
1025
goto out ;
1012
1026
}
1013
1027
1014
- if (header_info .location [ 0 ] != '\0' )
1028
+ if (header_info .location != NULL )
1015
1029
php_stream_notify_info (context , PHP_STREAM_NOTIFY_REDIRECTED , header_info .location , 0 );
1016
1030
1017
1031
php_stream_close (stream );
@@ -1022,18 +1036,17 @@ static php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper,
1022
1036
header_info .transfer_encoding = NULL ;
1023
1037
}
1024
1038
1025
- if (header_info .location [ 0 ] != '\0' ) {
1039
+ if (header_info .location != NULL ) {
1026
1040
1027
- char new_path [HTTP_HEADER_BLOCK_SIZE ];
1028
- char loc_path [HTTP_HEADER_BLOCK_SIZE ];
1041
+ char * new_path = NULL ;
1029
1042
1030
- * new_path = '\0' ;
1031
1043
if (strlen (header_info .location ) < 8 ||
1032
1044
(strncasecmp (header_info .location , "http://" , sizeof ("http://" )- 1 ) &&
1033
1045
strncasecmp (header_info .location , "https://" , sizeof ("https://" )- 1 ) &&
1034
1046
strncasecmp (header_info .location , "ftp://" , sizeof ("ftp://" )- 1 ) &&
1035
1047
strncasecmp (header_info .location , "ftps://" , sizeof ("ftps://" )- 1 )))
1036
1048
{
1049
+ char * loc_path = NULL ;
1037
1050
if (* header_info .location != '/' ) {
1038
1051
if (* (header_info .location + 1 ) != '\0' && resource -> path ) {
1039
1052
char * s = strrchr (ZSTR_VAL (resource -> path ), '/' );
@@ -1051,31 +1064,35 @@ static php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper,
1051
1064
if (resource -> path &&
1052
1065
ZSTR_VAL (resource -> path )[0 ] == '/' &&
1053
1066
ZSTR_VAL (resource -> path )[1 ] == '\0' ) {
1054
- snprintf (loc_path , sizeof (loc_path ) - 1 , "%s%s" ,
1055
- ZSTR_VAL (resource -> path ), header_info .location );
1067
+ spprintf (& loc_path , 0 , "%s%s" , ZSTR_VAL (resource -> path ), header_info .location );
1056
1068
} else {
1057
- snprintf (loc_path , sizeof (loc_path ) - 1 , "%s/%s" ,
1058
- ZSTR_VAL (resource -> path ), header_info .location );
1069
+ spprintf (& loc_path , 0 , "%s/%s" , ZSTR_VAL (resource -> path ), header_info .location );
1059
1070
}
1060
1071
} else {
1061
- snprintf ( loc_path , sizeof ( loc_path ) - 1 , "/%s" , header_info .location );
1072
+ spprintf ( & loc_path , 0 , "/%s" , header_info .location );
1062
1073
}
1063
1074
} else {
1064
- strlcpy (loc_path , header_info .location , sizeof (loc_path ));
1075
+ loc_path = header_info .location ;
1076
+ header_info .location = NULL ;
1065
1077
}
1066
1078
if ((use_ssl && resource -> port != 443 ) || (!use_ssl && resource -> port != 80 )) {
1067
- snprintf (new_path , sizeof (new_path ) - 1 , "%s://%s:%d%s" , ZSTR_VAL (resource -> scheme ), ZSTR_VAL (resource -> host ), resource -> port , loc_path );
1079
+ spprintf (& new_path , 0 , "%s://%s:%d%s" , ZSTR_VAL (resource -> scheme ),
1080
+ ZSTR_VAL (resource -> host ), resource -> port , loc_path );
1068
1081
} else {
1069
- snprintf (new_path , sizeof (new_path ) - 1 , "%s://%s%s" , ZSTR_VAL (resource -> scheme ), ZSTR_VAL (resource -> host ), loc_path );
1082
+ spprintf (& new_path , 0 , "%s://%s%s" , ZSTR_VAL (resource -> scheme ),
1083
+ ZSTR_VAL (resource -> host ), loc_path );
1070
1084
}
1085
+ efree (loc_path );
1071
1086
} else {
1072
- strlcpy (new_path , header_info .location , sizeof (new_path ));
1087
+ new_path = header_info .location ;
1088
+ header_info .location = NULL ;
1073
1089
}
1074
1090
1075
1091
php_url_free (resource );
1076
1092
/* check for invalid redirection URLs */
1077
1093
if ((resource = php_url_parse (new_path )) == NULL ) {
1078
1094
php_stream_wrapper_log_error (wrapper , options , "Invalid redirect URL! %s" , new_path );
1095
+ efree (new_path );
1079
1096
goto out ;
1080
1097
}
1081
1098
@@ -1087,6 +1104,7 @@ static php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper,
1087
1104
while (s < e) { \
1088
1105
if (iscntrl(*s)) { \
1089
1106
php_stream_wrapper_log_error(wrapper, options, "Invalid redirect URL! %s", new_path); \
1107
+ efree(new_path); \
1090
1108
goto out; \
1091
1109
} \
1092
1110
s++; \
@@ -1109,6 +1127,7 @@ static php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper,
1109
1127
stream = php_stream_url_wrap_http_ex (
1110
1128
wrapper , new_path , mode , options , opened_path , context ,
1111
1129
-- redirect_max , new_flags , response_header STREAMS_CC );
1130
+ efree (new_path );
1112
1131
} else {
1113
1132
php_stream_wrapper_log_error (wrapper , options , "HTTP request failed! %s" , tmp_line );
1114
1133
}
@@ -1121,6 +1140,10 @@ static php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper,
1121
1140
efree (http_header_line );
1122
1141
}
1123
1142
1143
+ if (header_info .location != NULL ) {
1144
+ efree (header_info .location );
1145
+ }
1146
+
1124
1147
if (resource ) {
1125
1148
php_url_free (resource );
1126
1149
}
0 commit comments