FindBugs : real threat behind EI_EXPOSE_REP

我的未来我决定 提交于 2019-11-29 11:46:34

The point is that any time you open up an implementation you run the risk of code doing damage, intentionally or otherwise. Obviously a malicious library user, for example, could just disassemble your jar and learn details that way–the point is to minimize the risk of exposure: it cannot be eliminated.

A trivial example of external code accessing your library:

Consider something simple like an object that holds an access level. If it was mutable, it's conceivable a library user could set their own access level. Something this trivial would rarely be exposed by any reasonable library, but it's a clear example of when an internal representation might be abused.

The bottom line is that exposed mutable state makes code difficult to reason about, and difficult to protect. Your code or others may accidentally, or deliberately, modify something your own code/library uses. If your library then changes its behavior without taking that into account, you may introduce a subtle (or not so subtle) bug.

The ability to have an object to hide state and provide a strong, static interface is core to Java mobile code security. There are a number of ways that a programmer messing this up can cause a vulnerability.

For value objects, consider String. We trust any instance of String not to change. We don't want to validate [check] a particular file name, for example, we don't want it to change when we actually use it (note, java.io.File doesn't work well in this sense). Also a mutable internals may have malicious equals method (say), that is called by the enclosing class' equals, but maliciously acquires references to internal objects of other enclosing class instances compared to.

Reference types are often object capabilities. Typically they will attenuate a capability of an object they were endowed with (passed through the constructor). Say, you can only write a file to a particular directory through the instance you have access to, but it has a field with a capability to write to an entire filesystem.

Then there's the Java 2 Security Model, and that's never a good thing. The inner object may have methods called in a privileged context, which isn't usually a problem. Now a malicious party places in object (of a trusted type) that does something that it shouldn't in that context.

Of course, watch out for randomly subclassing classes. They may acquire a get method in future versions.

Having said that, in my opinion this warning from Findbugs is not helpful. The code probably wasn't intented to hide the object.

(For a verbose description of the problem of exposing internal objects see Chapter 13 (and 14) of Michael Feathers Working Effectively with Legacy Code.)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!