summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJulien Voisin2020-12-24 10:32:28 +0000
committerGitHub2020-12-24 10:32:28 +0000
commit98ed3be52fa15521ef405fc8029176279d80778e (patch)
treebafe6541bb3d3863f7edb9196a3e1db40c88bf7f /src
parenta9e240ef0655175f930810cf78ac7df593a6dde6 (diff)
Add PHP8 support
Diffstat (limited to 'src')
-rw-r--r--src/php_snuffleupagus.h2
-rw-r--r--src/sp_pcre_compat.c24
-rw-r--r--src/sp_pcre_compat.h16
-rw-r--r--src/tests/xxe/disable_xxe_dom.phpt75
-rw-r--r--src/tests/xxe/disable_xxe_dom_disabled_php8.phpt60
-rw-r--r--src/tests/xxe/disable_xxe_xml_parse_php8.phpt106
6 files changed, 30 insertions, 253 deletions
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 @@
14#include <errno.h> 14#include <errno.h>
15#include <fcntl.h> 15#include <fcntl.h>
16#include <inttypes.h> 16#include <inttypes.h>
17#include "sp_pcre_compat.h"
18#include <stdbool.h> 17#include <stdbool.h>
19#include <stdio.h> 18#include <stdio.h>
20#include <stdlib.h> 19#include <stdlib.h>
@@ -29,6 +28,7 @@
29#include <sys/syslog.h> 28#include <sys/syslog.h>
30 29
31#include "SAPI.h" 30#include "SAPI.h"
31#include "ext/pcre/php_pcre.h"
32#include "ext/standard/head.h" 32#include "ext/standard/head.h"
33#include "ext/standard/info.h" 33#include "ext/standard/info.h"
34#include "ext/standard/url.h" 34#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 @@
3sp_pcre* sp_pcre_compile(const char* const pattern) { 3sp_pcre* sp_pcre_compile(const char* const pattern) {
4 assert(NULL != pattern); 4 assert(NULL != pattern);
5 5
6 sp_pcre* ret = NULL;
7#ifdef SP_HAS_PCRE2
6 unsigned char pcre_error[128] = {0}; 8 unsigned char pcre_error[128] = {0};
7 int errornumber; 9 int errornumber;
8 PCRE2_SIZE erroroffset; 10 PCRE2_SIZE erroroffset;
9 sp_pcre* ret = pcre2_compile((PCRE2_SPTR)pattern, PCRE2_ZERO_TERMINATED, 11 ret = pcre2_compile((PCRE2_SPTR)pattern, PCRE2_ZERO_TERMINATED,
10 PCRE2_CASELESS, &errornumber, &erroroffset, NULL); 12 PCRE2_CASELESS, &errornumber, &erroroffset, NULL);
13 pcre2_get_error_message(errornumber, pcre_error, sizeof(pcre_error));
14#else
15 const char* pcre_error = NULL;
16 int erroroffset;
17 ret = php_pcre_compile(pattern, PCRE_CASELESS, &pcre_error, &erroroffset, NULL);
18#endif
11 19
12 if (NULL == ret) { 20 if (NULL == ret) {
13 pcre2_get_error_message(errornumber, pcre_error, sizeof(pcre_error));
14 sp_log_err("config", "Failed to compile '%s': %s on line %zu.", pattern, 21 sp_log_err("config", "Failed to compile '%s': %s on line %zu.", pattern,
15 pcre_error, sp_line_no); 22 pcre_error, sp_line_no);
16 } 23 }
@@ -19,15 +26,26 @@ sp_pcre* sp_pcre_compile(const char* const pattern) {
19 26
20bool ZEND_HOT sp_is_regexp_matching_len(const sp_pcre* regexp, const char* str, 27bool ZEND_HOT sp_is_regexp_matching_len(const sp_pcre* regexp, const char* str,
21 size_t len) { 28 size_t len) {
29 int ret = 0;
30
22 assert(NULL != regexp); 31 assert(NULL != regexp);
23 assert(NULL != str); 32 assert(NULL != str);
24 33
34#ifdef SP_HAS_PCRE2
25 pcre2_match_data* match_data = 35 pcre2_match_data* match_data =
26 pcre2_match_data_create_from_pattern(regexp, NULL); 36 pcre2_match_data_create_from_pattern(regexp, NULL);
27 int ret = pcre2_match(regexp, (PCRE2_SPTR)str, len, 0, 0, match_data, NULL); 37 ret = pcre2_match(regexp, (PCRE2_SPTR)str, len, 0, 0, match_data, NULL);
38#else
39 int vec[30];
40 ret = php_pcre_exec(regexp, NULL, str, len, 0, 0, vec, sizeof(vec) / sizeof(int));
41#endif
28 42
29 if (ret < 0) { 43 if (ret < 0) {
44#ifdef SP_HAS_PCRE2
30 if (ret != PCRE2_ERROR_NOMATCH) { 45 if (ret != PCRE2_ERROR_NOMATCH) {
46#else
47 if (ret != PCRE_ERROR_NOMATCH) {
48#endif
31 // LCOV_EXCL_START 49 // LCOV_EXCL_START
32 sp_log_err("regexp", "Something went wrong with a regexp (%d).", ret); 50 sp_log_err("regexp", "Something went wrong with a regexp (%d).", ret);
33 // LCOV_EXCL_STOP 51 // 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 @@
7#undef pcre_exec 7#undef pcre_exec
8#undef pcre_compile 8#undef pcre_compile
9 9
10#if HAVE_BUNDLED_PCRE 10
11#if PHP_VERSION_ID >= 70300
12#include "ext/pcre/php_pcre.h"
13#else
14#include "ext/pcre/pcrelib/pcre.h"
15#endif
16#else
17#define PCRE2_CODE_UNIT_WIDTH 8 11#define PCRE2_CODE_UNIT_WIDTH 8
18#include "pcre2.h" 12#if PHP_VERSION_ID >= 70300
13#define SP_HAS_PCRE2
19#endif 14#endif
15#include "ext/pcre/php_pcre.h" // PCRE1
20 16
17#ifdef SP_HAS_PCRE2
21#define sp_pcre pcre2_code 18#define sp_pcre pcre2_code
19#else
20#define sp_pcre pcre
21#endif
22 22
23sp_pcre* sp_pcre_compile(const char* str); 23sp_pcre* sp_pcre_compile(const char* str);
24#define sp_is_regexp_matching_zend(regexp, zstr) \ 24#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 @@
1--TEST--
2Disable XXE, in php8
3--SKIPIF--
4<?php if (!extension_loaded("snuffleupagus") || !extension_loaded("dom")) print("skip"); ?>
5<?php if (PHP_VERSION_ID < 80000) print "skip"; ?>
6--INI--
7sp.configuration_file={PWD}/config/disable_xxe.ini
8--EXTENSIONS--
9dom
10--FILE--
11<?php
12$dir = __DIR__;
13$content = 'WARNING, external entity loaded!';
14file_put_contents('content.txt', $content);
15
16$xml = <<<EOD
17<?xml version="1.0"?>
18<!DOCTYPE root
19[
20<!ENTITY foo SYSTEM "file://$dir/content.txt">
21]>
22<test><testing>&foo;</testing></test>
23EOD;
24
25file_put_contents('content.xml', $xml);
26
27libxml_disable_entity_loader(true);
28$dom = new DOMDocument('1.0');
29$dom->loadXML($xml, LIBXML_DTDATTR|LIBXML_DTDLOAD|LIBXML_NOENT);
30printf("libxml_disable_entity to true: %s\n", $dom->getElementsByTagName('testing')->item(0)->nodeValue);
31
32libxml_disable_entity_loader(false);
33$dom = new DOMDocument('1.0');
34$dom->loadXML($xml, LIBXML_DTDATTR|LIBXML_DTDLOAD|LIBXML_NOENT);
35printf("libxml_disable_entity to false: %s\n", $dom->getElementsByTagName('testing')->item(0)->nodeValue);
36
37$xml = "<test><testing>foo</testing></test>";
38file_put_contents('content.xml', $xml);
39
40libxml_disable_entity_loader(false);
41$dom = new DOMDocument('1.0');
42$dom->loadXML($xml, LIBXML_DTDATTR|LIBXML_DTDLOAD|LIBXML_NOENT);
43printf("without xxe: %s", $dom->getElementsByTagName('testing')->item(0)->nodeValue);
44
45?>
46--CLEAN--
47<?php
48$dir = __DIR__;
49unlink($dir . "content.xml");
50unlink($dir . "content.txt");
51?>
52--EXPECTF--
53Deprecated: Function libxml_disable_entity_loader() is deprecated in %s/tests/xxe/disable_xxe_dom.php on line %d
54
55Warning: 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
56
57Warning: DOMDocument::loadXML(): Failure to process entity foo in Entity, line: 6 in %s/tests/xxe/disable_xxe_dom.php on line %d
58
59Warning: DOMDocument::loadXML(): Entity 'foo' not defined in Entity, line: 6 in %s/tests/xxe/disable_xxe_dom.php on line %d
60
61Warning: Attempt to read property "nodeValue" on null in %s/tests/xxe/disable_xxe_dom.php on line %d
62libxml_disable_entity to true:
63
64Deprecated: Function libxml_disable_entity_loader() is deprecated in %s/tests/xxe/disable_xxe_dom.php on line %d
65
66Warning: 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
67
68Warning: DOMDocument::loadXML(): Failure to process entity foo in Entity, line: 6 in %s/tests/xxe/disable_xxe_dom.php on line %d
69
70Warning: DOMDocument::loadXML(): Entity 'foo' not defined in Entity, line: 6 in %s/tests/xxe/disable_xxe_dom.php on line %d
71
72Warning: Attempt to read property "nodeValue" on null in %s/tests/xxe/disable_xxe_dom.php on line %d
73libxml_disable_entity to false:
74
75Deprecated: 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 @@
1--TEST--
2Disable XXE in php8
3--SKIPIF--
4<?php if (!extension_loaded("snuffleupagus") || !extension_loaded("dom")) print("skip"); ?>
5<?php if (PHP_VERSION_ID < 80000) print "skip"; ?>
6--INI--
7sp.configuration_file={PWD}/config/disable_xxe_disable.ini
8--EXTENSIONS--
9dom
10--FILE--
11<?php
12$dir = __DIR__;
13$content = '<content>WARNING, external entity loaded!</content>';
14file_put_contents($dir . '/content.txt', $content);
15
16$xml = <<<EOD
17<?xml version="1.0"?>
18<!DOCTYPE root
19[
20<!ENTITY foo SYSTEM "file://$dir/content.txt">
21]>
22<test><testing>&foo;</testing></test>
23EOD;
24
25file_put_contents($dir . '/content.xml', $xml);
26
27libxml_disable_entity_loader(true);
28$dom = new DOMDocument('1.0');
29$dom->loadXML($xml, LIBXML_DTDATTR|LIBXML_DTDLOAD|LIBXML_NOENT);
30printf("libxml_disable_entity to true: %s\n", $dom->getElementsByTagName('testing')->item(0)->nodeValue);
31
32libxml_disable_entity_loader(false);
33$dom = new DOMDocument('1.0');
34$dom->loadXML($xml, LIBXML_DTDATTR|LIBXML_DTDLOAD|LIBXML_NOENT);
35printf("libxml_disable_entity to false: %s\n", $dom->getElementsByTagName('testing')->item(0)->nodeValue);
36
37$xml = "<test><testing>foo</testing></test>";
38file_put_contents('content.xml', $xml);
39
40libxml_disable_entity_loader(false);
41$dom = new DOMDocument('1.0');
42$dom->loadXML($xml, LIBXML_DTDATTR|LIBXML_DTDLOAD|LIBXML_NOENT);
43printf("without xxe: %s", $dom->getElementsByTagName('testing')->item(0)->nodeValue);
44
45?>
46--CLEAN--
47<?php
48$dir = __DIR__;
49unlink($dir . "/content.xml");
50unlink($dir . "/content.txt");
51?>
52--EXPECTF--
53Deprecated: Function libxml_disable_entity_loader() is deprecated in %s/tests/xxe/disable_xxe_dom_disabled.php on line %d
54libxml_disable_entity to true: WARNING, external entity loaded!
55
56Deprecated: Function libxml_disable_entity_loader() is deprecated in %s/tests/xxe/disable_xxe_dom_disabled.php on line %d
57libxml_disable_entity to false: WARNING, external entity loaded!
58
59Deprecated: Function libxml_disable_entity_loader() is deprecated in %s/tests/xxe/disable_xxe_dom_disabled.php on line %d
60
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 @@
1--TEST--
2Disable XXE in xml_parse, in php8
3--SKIPIF--
4<?php
5 if (!extension_loaded("snuffleupagus")) {
6 echo "skip because snuffleupagus isn't loaded";
7} elseif (!extension_loaded("xml")) {
8 echo "skip because the `xml` extension isn't loaded";
9}
10?>
11<?php if (PHP_VERSION_ID < 80000) print "skip"; ?>
12--EXTENSIONS--
13xml
14--INI--
15sp.configuration_file={PWD}/config/disable_xxe.ini
16--FILE--
17<?php
18$dir = __DIR__;
19$content = 'WARNING, external entity loaded!';
20file_put_contents('content.txt', $content);
21
22$xml = <<<EOD
23<?xml version="1.0"?>
24<!DOCTYPE root
25[
26<!ENTITY foo SYSTEM "file://$dir/content.txt">
27]>
28<test><testing>&foo;</testing></test>
29EOD;
30
31file_put_contents('content.xml', $xml);
32
33function create_parser() {
34 $parser = xml_parser_create();
35 xml_set_element_handler(
36 $parser,
37 function($parser, $name, array $attributes) {
38 var_dump($name);
39 echo "\n";
40 var_dump($attributes);
41 },
42 function($parser, $name) {
43 var_dump($name);
44 }
45 );
46
47 xml_set_character_data_handler(
48 $parser,
49 function ($parser, $text){
50 echo 'text' . $text;
51 }
52 );
53
54 return $parser;
55}
56
57libxml_disable_entity_loader(true);
58$parser = create_parser();
59$doc = xml_parse($parser, $xml, true);
60xml_parser_free($parser);
61
62libxml_disable_entity_loader(false);
63$parser = create_parser();
64$doc = xml_parse($parser, $xml, true);
65xml_parser_free($parser);
66
67$xml = "<test><testing>foo</testing></test>";
68file_put_contents('content.xml', $xml);
69$parser = create_parser();
70$doc = xml_parse($parser, $xml, true);
71xml_parser_free($parser);
72
73--EXPECTF--
74 Deprecated: Function libxml_disable_entity_loader() is deprecated in %s/tests/xxe/disable_xxe_xml_parse.php on line 41
75string(4) "TEST"
76
77array(0) {
78}
79string(7) "TESTING"
80
81array(0) {
82}
83string(7) "TESTING"
84string(4) "TEST"
85
86Deprecated: Function libxml_disable_entity_loader() is deprecated in %s/tests/xxe/disable_xxe_xml_parse.php on line 46
87string(4) "TEST"
88
89array(0) {
90}
91string(7) "TESTING"
92
93array(0) {
94}
95string(7) "TESTING"
96string(4) "TEST"
97string(4) "TEST"
98
99array(0) {
100}
101string(7) "TESTING"
102
103array(0) {
104}
105textfoostring(7) "TESTING"
106