From 98ed3be52fa15521ef405fc8029176279d80778e Mon Sep 17 00:00:00 2001 From: Julien Voisin Date: Thu, 24 Dec 2020 10:32:28 +0000 Subject: Add PHP8 support --- src/php_snuffleupagus.h | 2 +- src/sp_pcre_compat.c | 24 ++++- src/sp_pcre_compat.h | 16 ++-- src/tests/xxe/disable_xxe_dom.phpt | 75 ---------------- src/tests/xxe/disable_xxe_dom_disabled_php8.phpt | 60 ------------- src/tests/xxe/disable_xxe_xml_parse_php8.phpt | 106 ----------------------- 6 files changed, 30 insertions(+), 253 deletions(-) delete mode 100644 src/tests/xxe/disable_xxe_dom.phpt delete mode 100644 src/tests/xxe/disable_xxe_dom_disabled_php8.phpt delete mode 100644 src/tests/xxe/disable_xxe_xml_parse_php8.phpt (limited to 'src') diff --git a/src/php_snuffleupagus.h b/src/php_snuffleupagus.h index 14efadb..02b464e 100644 --- a/src/php_snuffleupagus.h +++ b/src/php_snuffleupagus.h @@ -14,7 +14,6 @@ #include #include #include -#include "sp_pcre_compat.h" #include #include #include @@ -29,6 +28,7 @@ #include #include "SAPI.h" +#include "ext/pcre/php_pcre.h" #include "ext/standard/head.h" #include "ext/standard/info.h" #include "ext/standard/url.h" diff --git a/src/sp_pcre_compat.c b/src/sp_pcre_compat.c index c575a79..d2efc71 100644 --- a/src/sp_pcre_compat.c +++ b/src/sp_pcre_compat.c @@ -3,14 +3,21 @@ sp_pcre* sp_pcre_compile(const char* const pattern) { assert(NULL != pattern); + sp_pcre* ret = NULL; +#ifdef SP_HAS_PCRE2 unsigned char pcre_error[128] = {0}; int errornumber; PCRE2_SIZE erroroffset; - sp_pcre* ret = pcre2_compile((PCRE2_SPTR)pattern, PCRE2_ZERO_TERMINATED, + ret = pcre2_compile((PCRE2_SPTR)pattern, PCRE2_ZERO_TERMINATED, PCRE2_CASELESS, &errornumber, &erroroffset, NULL); + pcre2_get_error_message(errornumber, pcre_error, sizeof(pcre_error)); +#else + const char* pcre_error = NULL; + int erroroffset; + ret = php_pcre_compile(pattern, PCRE_CASELESS, &pcre_error, &erroroffset, NULL); +#endif if (NULL == ret) { - pcre2_get_error_message(errornumber, pcre_error, sizeof(pcre_error)); sp_log_err("config", "Failed to compile '%s': %s on line %zu.", pattern, pcre_error, sp_line_no); } @@ -19,15 +26,26 @@ sp_pcre* sp_pcre_compile(const char* const pattern) { bool ZEND_HOT sp_is_regexp_matching_len(const sp_pcre* regexp, const char* str, size_t len) { + int ret = 0; + assert(NULL != regexp); assert(NULL != str); +#ifdef SP_HAS_PCRE2 pcre2_match_data* match_data = pcre2_match_data_create_from_pattern(regexp, NULL); - int ret = pcre2_match(regexp, (PCRE2_SPTR)str, len, 0, 0, match_data, NULL); + ret = pcre2_match(regexp, (PCRE2_SPTR)str, len, 0, 0, match_data, NULL); +#else + int vec[30]; + ret = php_pcre_exec(regexp, NULL, str, len, 0, 0, vec, sizeof(vec) / sizeof(int)); +#endif if (ret < 0) { +#ifdef SP_HAS_PCRE2 if (ret != PCRE2_ERROR_NOMATCH) { +#else + if (ret != PCRE_ERROR_NOMATCH) { +#endif // LCOV_EXCL_START sp_log_err("regexp", "Something went wrong with a regexp (%d).", ret); // LCOV_EXCL_STOP diff --git a/src/sp_pcre_compat.h b/src/sp_pcre_compat.h index 6fcb383..b70630d 100644 --- a/src/sp_pcre_compat.h +++ b/src/sp_pcre_compat.h @@ -7,18 +7,18 @@ #undef pcre_exec #undef pcre_compile -#if HAVE_BUNDLED_PCRE -#if PHP_VERSION_ID >= 70300 -#include "ext/pcre/php_pcre.h" -#else -#include "ext/pcre/pcrelib/pcre.h" -#endif -#else + #define PCRE2_CODE_UNIT_WIDTH 8 -#include "pcre2.h" +#if PHP_VERSION_ID >= 70300 +#define SP_HAS_PCRE2 #endif +#include "ext/pcre/php_pcre.h" // PCRE1 +#ifdef SP_HAS_PCRE2 #define sp_pcre pcre2_code +#else +#define sp_pcre pcre +#endif sp_pcre* sp_pcre_compile(const char* str); #define sp_is_regexp_matching_zend(regexp, zstr) \ diff --git a/src/tests/xxe/disable_xxe_dom.phpt b/src/tests/xxe/disable_xxe_dom.phpt deleted file mode 100644 index 99ed572..0000000 --- a/src/tests/xxe/disable_xxe_dom.phpt +++ /dev/null @@ -1,75 +0,0 @@ ---TEST-- -Disable XXE, in php8 ---SKIPIF-- - - ---INI-- -sp.configuration_file={PWD}/config/disable_xxe.ini ---EXTENSIONS-- -dom ---FILE-- - - -]> -&foo; -EOD; - -file_put_contents('content.xml', $xml); - -libxml_disable_entity_loader(true); -$dom = new DOMDocument('1.0'); -$dom->loadXML($xml, LIBXML_DTDATTR|LIBXML_DTDLOAD|LIBXML_NOENT); -printf("libxml_disable_entity to true: %s\n", $dom->getElementsByTagName('testing')->item(0)->nodeValue); - -libxml_disable_entity_loader(false); -$dom = new DOMDocument('1.0'); -$dom->loadXML($xml, LIBXML_DTDATTR|LIBXML_DTDLOAD|LIBXML_NOENT); -printf("libxml_disable_entity to false: %s\n", $dom->getElementsByTagName('testing')->item(0)->nodeValue); - -$xml = "foo"; -file_put_contents('content.xml', $xml); - -libxml_disable_entity_loader(false); -$dom = new DOMDocument('1.0'); -$dom->loadXML($xml, LIBXML_DTDATTR|LIBXML_DTDLOAD|LIBXML_NOENT); -printf("without xxe: %s", $dom->getElementsByTagName('testing')->item(0)->nodeValue); - -?> ---CLEAN-- - ---EXPECTF-- -Deprecated: Function libxml_disable_entity_loader() is deprecated in %s/tests/xxe/disable_xxe_dom.php on line %d - -Warning: DOMDocument::loadXML(): I/O warning : failed to load external entity "file://%s/tests/xxe/content.txt" in /var/www/html/snuffleupagus/src/tests/xxe/disable_xxe_dom.php on line %d - -Warning: DOMDocument::loadXML(): Failure to process entity foo in Entity, line: 6 in %s/tests/xxe/disable_xxe_dom.php on line %d - -Warning: DOMDocument::loadXML(): Entity 'foo' not defined in Entity, line: 6 in %s/tests/xxe/disable_xxe_dom.php on line %d - -Warning: Attempt to read property "nodeValue" on null in %s/tests/xxe/disable_xxe_dom.php on line %d -libxml_disable_entity to true: - -Deprecated: Function libxml_disable_entity_loader() is deprecated in %s/tests/xxe/disable_xxe_dom.php on line %d - -Warning: DOMDocument::loadXML(): I/O warning : failed to load external entity "file://%s/tests/xxe/content.txt" in /var/www/html/snuffleupagus/src/tests/xxe/disable_xxe_dom.php on line %d - -Warning: DOMDocument::loadXML(): Failure to process entity foo in Entity, line: 6 in %s/tests/xxe/disable_xxe_dom.php on line %d - -Warning: DOMDocument::loadXML(): Entity 'foo' not defined in Entity, line: 6 in %s/tests/xxe/disable_xxe_dom.php on line %d - -Warning: Attempt to read property "nodeValue" on null in %s/tests/xxe/disable_xxe_dom.php on line %d -libxml_disable_entity to false: - -Deprecated: Function libxml_disable_entity_loader() is deprecated in %s/tests/xxe/disable_xxe_dom.php on line %d diff --git a/src/tests/xxe/disable_xxe_dom_disabled_php8.phpt b/src/tests/xxe/disable_xxe_dom_disabled_php8.phpt deleted file mode 100644 index c0db7fc..0000000 --- a/src/tests/xxe/disable_xxe_dom_disabled_php8.phpt +++ /dev/null @@ -1,60 +0,0 @@ ---TEST-- -Disable XXE in php8 ---SKIPIF-- - - ---INI-- -sp.configuration_file={PWD}/config/disable_xxe_disable.ini ---EXTENSIONS-- -dom ---FILE-- -WARNING, external entity loaded!'; -file_put_contents($dir . '/content.txt', $content); - -$xml = << - -]> -&foo; -EOD; - -file_put_contents($dir . '/content.xml', $xml); - -libxml_disable_entity_loader(true); -$dom = new DOMDocument('1.0'); -$dom->loadXML($xml, LIBXML_DTDATTR|LIBXML_DTDLOAD|LIBXML_NOENT); -printf("libxml_disable_entity to true: %s\n", $dom->getElementsByTagName('testing')->item(0)->nodeValue); - -libxml_disable_entity_loader(false); -$dom = new DOMDocument('1.0'); -$dom->loadXML($xml, LIBXML_DTDATTR|LIBXML_DTDLOAD|LIBXML_NOENT); -printf("libxml_disable_entity to false: %s\n", $dom->getElementsByTagName('testing')->item(0)->nodeValue); - -$xml = "foo"; -file_put_contents('content.xml', $xml); - -libxml_disable_entity_loader(false); -$dom = new DOMDocument('1.0'); -$dom->loadXML($xml, LIBXML_DTDATTR|LIBXML_DTDLOAD|LIBXML_NOENT); -printf("without xxe: %s", $dom->getElementsByTagName('testing')->item(0)->nodeValue); - -?> ---CLEAN-- - ---EXPECTF-- -Deprecated: Function libxml_disable_entity_loader() is deprecated in %s/tests/xxe/disable_xxe_dom_disabled.php on line %d -libxml_disable_entity to true: WARNING, external entity loaded! - -Deprecated: Function libxml_disable_entity_loader() is deprecated in %s/tests/xxe/disable_xxe_dom_disabled.php on line %d -libxml_disable_entity to false: WARNING, external entity loaded! - -Deprecated: Function libxml_disable_entity_loader() is deprecated in %s/tests/xxe/disable_xxe_dom_disabled.php on line %d - diff --git a/src/tests/xxe/disable_xxe_xml_parse_php8.phpt b/src/tests/xxe/disable_xxe_xml_parse_php8.phpt deleted file mode 100644 index 4a8622a..0000000 --- a/src/tests/xxe/disable_xxe_xml_parse_php8.phpt +++ /dev/null @@ -1,106 +0,0 @@ ---TEST-- -Disable XXE in xml_parse, in php8 ---SKIPIF-- - - ---EXTENSIONS-- -xml ---INI-- -sp.configuration_file={PWD}/config/disable_xxe.ini ---FILE-- - - -]> -&foo; -EOD; - -file_put_contents('content.xml', $xml); - -function create_parser() { - $parser = xml_parser_create(); - xml_set_element_handler( - $parser, - function($parser, $name, array $attributes) { - var_dump($name); - echo "\n"; - var_dump($attributes); - }, - function($parser, $name) { - var_dump($name); - } - ); - - xml_set_character_data_handler( - $parser, - function ($parser, $text){ - echo 'text' . $text; - } - ); - - return $parser; -} - -libxml_disable_entity_loader(true); -$parser = create_parser(); -$doc = xml_parse($parser, $xml, true); -xml_parser_free($parser); - -libxml_disable_entity_loader(false); -$parser = create_parser(); -$doc = xml_parse($parser, $xml, true); -xml_parser_free($parser); - -$xml = "foo"; -file_put_contents('content.xml', $xml); -$parser = create_parser(); -$doc = xml_parse($parser, $xml, true); -xml_parser_free($parser); - ---EXPECTF-- - Deprecated: Function libxml_disable_entity_loader() is deprecated in %s/tests/xxe/disable_xxe_xml_parse.php on line 41 -string(4) "TEST" - -array(0) { -} -string(7) "TESTING" - -array(0) { -} -string(7) "TESTING" -string(4) "TEST" - -Deprecated: Function libxml_disable_entity_loader() is deprecated in %s/tests/xxe/disable_xxe_xml_parse.php on line 46 -string(4) "TEST" - -array(0) { -} -string(7) "TESTING" - -array(0) { -} -string(7) "TESTING" -string(4) "TEST" -string(4) "TEST" - -array(0) { -} -string(7) "TESTING" - -array(0) { -} -textfoostring(7) "TESTING" - -- cgit v1.3