summaryrefslogtreecommitdiff
path: root/src/sp_disable_xxe.c
diff options
context:
space:
mode:
authorjvoisin2021-04-27 22:22:34 +0200
committerjvoisin2021-04-27 22:26:24 +0200
commitd9cccbbe417d305bb56911cd07a7feac6b89e9a6 (patch)
tree98b0898cc287d714169318b698a6756741929b5f /src/sp_disable_xxe.c
parenta3feae2fb319899d13ab5013f510b51ce20b4db4 (diff)
Protect against XXE in php8
PHP8 disables external entities by default, but they can still be explicitly used (cf. https://blog.sonarsource.com/wordpress-xxe-security-vulnerability/), which is badâ„¢. The right way to defend against XXE is now to set libxml_set_external_entity_loader to null.
Diffstat (limited to 'src/sp_disable_xxe.c')
-rw-r--r--src/sp_disable_xxe.c16
1 files changed, 9 insertions, 7 deletions
diff --git a/src/sp_disable_xxe.c b/src/sp_disable_xxe.c
index 113d84b..3ef1a5d 100644
--- a/src/sp_disable_xxe.c
+++ b/src/sp_disable_xxe.c
@@ -5,20 +5,22 @@ PHP_FUNCTION(sp_libxml_disable_entity_loader) { RETURN_TRUE; }
5int hook_libxml_disable_entity_loader() { 5int hook_libxml_disable_entity_loader() {
6 TSRMLS_FETCH(); 6 TSRMLS_FETCH();
7 7
8// External entities are disabled by default in PHP8+
9#if PHP_VERSION_ID < 80000
10 /* Call the php function here instead of re-implementing it is a bit
11 * ugly, but we do not want to introduce compile-time dependencies against
12 * libxml. */
13 zval func_name; 8 zval func_name;
14 zval hmac; 9 zval retval;
15 zval params[1]; 10 zval params[1];
16 11
12#if PHP_VERSION_ID < 80000
13 // This function is deprecated in PHP8, but better safe than sorry for php7.
17 ZVAL_STRING(&func_name, "libxml_disable_entity_loader"); 14 ZVAL_STRING(&func_name, "libxml_disable_entity_loader");
18 ZVAL_STRING(&params[0], "true"); 15 ZVAL_STRING(&params[0], "true");
19 call_user_function(CG(function_table), NULL, &func_name, &hmac, 1, params); 16 call_user_function(CG(function_table), NULL, &func_name, &retval, 1, params);
20#endif 17#endif
21 18
19 // This is now the recommended way to disable external entities
20 ZVAL_STRING(&func_name, "libxml_set_external_entity_loader");
21 ZVAL_NULL(&params[0]);
22 call_user_function(CG(function_table), NULL, &func_name, &retval, 1, params);
23
22 HOOK_FUNCTION("libxml_disable_entity_loader", sp_internal_functions_hook, 24 HOOK_FUNCTION("libxml_disable_entity_loader", sp_internal_functions_hook,
23 PHP_FN(sp_libxml_disable_entity_loader)); 25 PHP_FN(sp_libxml_disable_entity_loader));
24 26