xml - 尝试在 ANDROID 资源中使用 <!ENTITY,错误为 : "The entity was referenced, but not declared."

标签 xml android-studio android-gradle-plugin android-resources dtd

我正在按照此解决方案在我的字符串资源文件中使用 enetities:

Is it possible to do string substitution in Android resource XML files directly?

我正在资源树中使用外部文件:/res/raw/entities.dtd ,其内容:

<!ENTITY ent_devicename "MyDeviceName">

string.xml资源文件:

<!DOCTYPE resources [
    <!ENTITY % ent_devicename SYSTEM "../raw/entities.dtd">
    %ent_devicename;
]>

<resources>
    <string name="name">The name is &ent_devicename;</string>
</resources>

但是我得到这个错误:

The entity "ent_devicename" was referenced, but not declared.

如您所见,Android Studio 可以识别外部实体文件:

enter image description here

和实体:

enter image description here

有人可以提供完整的正确示例来使事情正常进行吗? 我的意思是一个完整的可编译 Android Studio 项目,在单独的文件中包含实体声明。

更新 好的,如果你更关注这个w3schools链接:

https://www.w3schools.com/xml/xml_dtd_entities.asp

你看到了解决方案:

外部entities.dtd文件包含

<!ENTITY ent_devicename "MyDeviceName">

然后是新的字符串资源资源:

<?xml version="1.0" encoding="utf-8"?>

    <!DOCTYPE resources [
        <!ENTITY ent_devicename SYSTEM "../raw/entities.dtd">
        ]>

<resources xmlns:tools="http://schemas.android.com/tools" tools:ignore="Typos">
    <string name="ent_devicenamxxe2">&ent_devicename;</string>

我改变了<!ENTITY % ent_devicename<!ENTITY ent_devicename (不再 %) 我删除了%ent_devicename;

现在它可以编译,但生成的 APK 似乎忽略了实体值(使用空字符串)。所以问题没有解决!

告诉我!

最佳答案

TL;DR 此功能已从 Android Studio 中删除。请参阅错误报告 here .

看起来 XML 外部实体曾在 Android Studio 中受支持。在我看来,Android Studio 目前应该支持外部实体,因为编辑器没有提示。 XML 的底层处理实际上并没有像预期的那样执行包含,给出了“引用但未声明”的错误。

虽然我没有找到 Android Studio 放弃外部实体的明确引用,但由于发现了一个漏洞,它是有道理的。参见 Android Developers Susceptible to Data Exposure from XXE Attacksecurity write-up .

也有可能现在以某种方式限制了对外部实体的访问,但我认为如果是这种情况,Android Studio 会更有帮助。更有可能的是,由于存在漏洞,该功能刚刚被删除。

顺便说一句,我已经尝试过其他答案,但我没有运气 - 只是同样的错误。

编辑:

我刚遇到一个 Medium post讨论了 JetBrains 解决 XXE 漏洞的问题。

第二次编辑

我找到了一个 reference用于禁用错误跟踪器。

this may have been disabled for security reason, it requires complete analysis before resolution, punting to 3.2

This was indeed disabled for security reasons (preventing XXE attacks) in Change-Id: I2f1978bc5458ba2b2b2d6ffbc9df5710c487a4e4.

它的状态是“不会修复预期的行为”。如果将 Studio 更改为发出一条错误消息,指出该设施出于安全原因被禁用,那就太好了。

关于xml - 尝试在 ANDROID 资源中使用 &lt;!ENTITY,错误为 : "The entity was referenced, but not declared.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50933985/

相关文章:

java - 如何使用 SAX 解析器解析 namespace ?

java - JAXB XJC 代码生成 - Marshaller 生成的 xml 中缺少 "schemaLocation"

java - 抽屉式导航 - 错误 : failed linking file resources

android - Android N 预览中的 Java 8

java - 将 Android 应用程序上传到市场错误

php - 在发送之前预览 CURL 请求

java - 文本更改后从 sqlite 数据库中获取特定行

android-studio - 我可以设置 Android Studio 显示包含我的 TODO 列表的弹出窗口吗?

android - 更新到android gradle插件3.5后出现proguard问题

android - 没有可用于离线模式的缓存版本