CakeFest 2024: The Official CakePHP Conference

libxml_disable_entity_loader

(PHP 5 >= 5.2.11, PHP 7, PHP 8)

libxml_disable_entity_loader外部エンティティの読み込み機能を無効にする

警告

この関数は PHP 8.0.0 で 非推奨になります。この関数に頼らないことを強く推奨します。

説明

libxml_disable_entity_loader(bool $disable = true): bool

外部エンティティ読み込み機能の有効/無効を切り替えます。 外部エンティティの読み込みを無効にすると、 XML文書を読み込む際に問題が起こる可能性があることに注意して下さい。 しかし、libxml 2.9.0 以降では、エンティティの置換はデフォルトで無効になっているため、 LIBXML_NOENT を使って内部エンティティの参照を解決する必要がない限り、 外部エンティティの読み込みを無効にする必要はありません。 一般的には、外部エンティティの読み込みを抑制するのであれば、 libxml_set_external_entity_loader() を使うことが望ましいです。

パラメータ

disable

libxml を使用する拡張モジュール (DOMXMLWriter および XMLReader) で、外部エンティティの読み込み機能を 無効 (true) あるいは有効 (false) にします。

戻り値

変更前の値を返します。

参考

add a note

User Contributed Notes 7 notes

up
7
suconghou at gmail dot com
3 years ago
In PHP 8.0 and later, PHP uses libxml versions from 2.9.0, libxml_disable_entity_loader is deprecated.
so it is now safe to remove all `libxml_disable_entity_loader` calls on php8

if you want Backwards Compatibility

use this snippet

if (\PHP_VERSION_ID < 80000) {
libxml_disable_entity_loader(true);
}
up
8
vavra at 602 dot cz
6 years ago
If is called
libxml_disable_entity_loader(true);

, it causes that new SoapClient(.) fails with

SOAP-ERROR: Parsing WSDL: Couldn't load from 'D:\path/dm_operations.wsdl' : failed to load external entity "D:\path/dm_operations.wsdl

because this wsdl imports a xsd as an another external file.
Tested on php 7.1.12, win x64.
up
0
editor at geekkb.com
11 months ago
There is an extra \ should be deleted before PHP_VERSION_ID in the code which suconghou posted 2 years ago.

if (PHP_VERSION_ID < 80000) {
libxml_disable_entity_loader(true);
}
up
0
phofstetter at sensational dot ch
10 years ago
Be mindful that this also disables url loading in simplexml_load_file() and likely other libxml based functions that deal with URLs
up
0
simonsimcity
12 years ago
Using this function you can prevent a vulnerable to Local and Remote File Inclusion attacks.

You'll see it in an example where I load and validate the following string:

<!DOCTYPE scan [<!ENTITY test SYSTEM "php://filter/read=convert.base64-encode/resource=/etc/passwd">]>
<scan>&test;</scan>

One way to prevent that the file in given back is to set this value to 0.
Please take a closer look at the release of symfony 2.0.11
up
-3
brendan at bloodbone dot ws
10 years ago
This also seems to have an impact on <xsl:import /> statements if this is applied when loading XSLT for the XSLTProcessor class.
up
-7
daschtour at me dot com
10 years ago
This function was reported to be not thread safe. So this might affect php-scripts on the same server.
To Top