@@ -221,6 +221,44 @@ void php_mail_log_to_file(char *filename, char *message, size_t message_size TSR
221221}
222222
223223
224+ static int php_mail_detect_multiple_crlf (char * hdr ) {
225+ /* This function detects multiple/malformed multiple newlines. */
226+ size_t len ;
227+
228+ if (!hdr ) {
229+ return 0 ;
230+ }
231+
232+ /* Should not have any newlines at the beginning. */
233+ /* RFC 2822 2.2. Header Fields */
234+ if (* hdr < 33 || * hdr > 126 || * hdr == ':' ) {
235+ return 1 ;
236+ }
237+
238+ while (* hdr ) {
239+ if (* hdr == '\r' ) {
240+ if (* (hdr + 1 ) == '\0' || * (hdr + 1 ) == '\r' || (* (hdr + 1 ) == '\n' && (* (hdr + 2 ) == '\0' || * (hdr + 2 ) == '\n' || * (hdr + 2 ) == '\r' ))) {
241+ /* Malformed or multiple newlines. */
242+ return 1 ;
243+ } else {
244+ hdr += 2 ;
245+ }
246+ } else if (* hdr == '\n' ) {
247+ if (* (hdr + 1 ) == '\0' || * (hdr + 1 ) == '\r' || * (hdr + 1 ) == '\n' ) {
248+ /* Malformed or multiple newlines. */
249+ return 1 ;
250+ } else {
251+ hdr += 2 ;
252+ }
253+ } else {
254+ hdr ++ ;
255+ }
256+ }
257+
258+ return 0 ;
259+ }
260+
261+
224262/* {{{ php_mail
225263 */
226264PHPAPI int php_mail (char * to , char * subject , char * message , char * headers , char * extra_cmd TSRMLS_DC )
@@ -266,6 +304,7 @@ PHPAPI int php_mail(char *to, char *subject, char *message, char *headers, char
266304
267305 efree (tmp );
268306 }
307+
269308 if (PG (mail_x_header )) {
270309 const char * tmp = zend_get_executed_filename (TSRMLS_C );
271310 char * f ;
@@ -281,6 +320,11 @@ PHPAPI int php_mail(char *to, char *subject, char *message, char *headers, char
281320 efree (f );
282321 }
283322
323+ if (hdr && php_mail_detect_multiple_crlf (hdr )) {
324+ php_error_docref (NULL TSRMLS_CC , E_WARNING , "Multiple or malformed newlines found in additional_header" );
325+ MAIL_RET (0 );
326+ }
327+
284328 if (!sendmail_path ) {
285329#if (defined PHP_WIN32 || defined NETWARE )
286330 /* handle old style win smtp sending */
0 commit comments