反射机制是否破坏了java中的安全性?请解释一下
我听说反射机制破坏了Java中的安全性。请问谁能解释一下吗?
I have heard that the reflection mechanism breaks the security in Java. Please can anyone explain it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
显然,如果反射 API 明显破坏了小程序的安全性,那么现在就有人会注意到了。
首先我们需要一些背景信息。不同寻常的是,Java 平台/可以/提供一个安全的环境来运行不受信任的代码(出现或出现奇怪的错误 - 如果发现错误,请务必告知 Oracle 安全响应团队)。然而,大多数 Java 的使用都忽略了这一点。
当安全运行时,反射 API 将不受信任的代码可以执行的操作大致限制为在没有反射的情况下可以执行的操作(除了一些标准 Java 安全检查之外,它还检查直接调用者 - 请参阅 Java 编程语言的安全编码指南)。例如,不受信任的代码可以访问同一包中的“包私有”类,但不能访问其他包中的类。
为什么要使用反射?通常是因为它允许您代表某些客户端代码以相同的方式对一组开放式类型进行操作。客户端代码通常位于其他包中,但反射 API 会根据使用反射的代码来限制访问。因此,除非使用反射的代码编写得当,否则客户端代码不应允许访问它。这是漏洞的丰富来源。
当不安全运行时,您可以读/写文件、运行程序等,所以谁在乎呢?
Clearly if the reflection API obviously broke applet security, someone would have noticed by now.
Firstly we need some context. Unusually, the Java platform /can/ provide a secure environment for running untrusted code (give or take the odd bug - if you find one, do let the Oracle security response team know). However, most uses of Java ignore this.
When running securely, the reflection API limits what untrusted code can do to roughly what it can do without reflection (it checks the immediate caller in addition to some standard Java security checking - see the Secure Coding Guidelines for the Java Programming Language). For instance, untrusted code can access "package private" classes in the same package, but not those from other packages.
Why would you use reflection? Generally because it allows you do operate in same way upon an open-ended set of types on behalf of some client code. The client code will generally be in some other package, but the reflection API will limit access based on the reflection-using code. Therefore, unless the reflection-using code is competently written, the client code has access it should not be allowed. This is a rich source of vulnerabilities.
When not running securely, you can read/write files, run programs, etc., so who cares?
您可以访问私有字段和使用反射的对象方法
例如
you can access private fields & method of Object using reflection
For example
使用 reflection 你几乎可以做任何事情:通过 将它们设置为可访问,并修改不可变对象
一个常见的示例是更改 String:
在此示例中 - 实际的字符串对象正在更改!
请注意,使用反射来更改不可变对象有其自身的问题,正如您在这篇文章中看到的那样
Using reflection you can do almost anything: access private variables by setting them as accessable , and modify immutable objects
A common example, is changing a String:
In this example - the actual string object is being changed!
Note that using reflection to change immutable objects has its own problems, as you can see in this post