...
Once a serializable class has been exported, future refactoring of its code often becomes problematic. Specifically, existing serialized forms (encoded representations) become part of the object's published API and must be supported for an indefinite period. This can be troublesome from a security perspective; not only does it promote dead code, it can also commits commit the provider to potentially eternally maintain a compatible codebasecode base for the life of their products.
Classes that implement Serializable
and fail to override its functionality are said to be using the default serialized form. In the event of future changes to the class, byte streams produced by users of old versions of the class will be incompatible with the new implementation. Programs must maintain serialization compatibility during class evolution. An acceptable approach is the use of a custom serialized form, which relieves the implementer of the necessity to maintain the original serialized form and the corresponding version of the class in addition to the newly evolved version.
...
Code Block | ||
---|---|---|
| ||
class WeaponStore implements Serializable { int noOfWeapons = 10; // Total number of weapons } public class GameWeapon implements Serializable { WeaponStore ws = new WeaponStore(); private static final ObjectStreamField[] serialPersistentFields = {new ObjectStreamField("ws", WeaponStore.class)}; private void readObject(ObjectInputStream ois) throws IOException { try { ObjectInputStream.GetField gf = ois.readFields(); this.ws = (WeaponStore) gf.get("ws", ws); } catch (ClassNotFoundException e) { /* Forward to handler */ } } private void writeObject(ObjectOutputStream oos) throws IOException { ObjectOutputStream.PutField pf = oos.putFields(); pf.put("ws", ws); oos.writeFields(); } public String toString() { return String.valueOf(ws); } } |
Exceptions
...
Inner classes can only contain
final static
fields that are initialized to constants or expressions built up from constants. Consequently, it is not possible to setserialPersistentFields
for an inner class (though it is possible to set it forstatic
member classes).
Finally, serialization is easy to get wrong and must consequently be carefully implemented.
Risk Assessment
Failure to provide a consistent serialization mechanism across releases can limit the extensibility of classes. If classes are extended, compatibility issues may result.
...
Automated detection of classes that use the default serialized form is straightforward.
Related
...
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
Related Guidelines
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="c9c0b31d5b238dc9-a79b4f81-4e014c64-aff9a7d2-071815d7d3504dca99173322"><ac:plain-text-body><![CDATA[ | [[MITRE 2009 | AA. Bibliography#MITRE 09]] | [CWE ID 589 | http://cwe.mitre.org/data/definitions/589.html] "Call to Non-ubiquitous API" | ]]></ac:plain-text-body></ac:structured-macro> |
...
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="f012a54d71cca975-c87abad8-4cb54b2c-93978dbb-dd5c5a52d65fdf6846a7a55a"><ac:plain-text-body><![CDATA[ | [[API 2006 | AA. Bibliography#API 06]] |
| ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="fae3e964f9632f71-59b0aa78-4ef246dd-8a2ebaff-c56eea6123c24bee8217fc07"><ac:plain-text-body><![CDATA[ | [[Sun 2006 | AA. Bibliography#Sun 06]] | "Serialization specification", "1.5 Defining Serializable Fields for a Class" and "1.7 Accessing Serializable Fields of a Class" | ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="4c6d7e6e3a86a7f0-689c243d-4aca42b4-8c80918a-cf852ccb5bdfdf97776e8bc2"><ac:plain-text-body><![CDATA[ | [[Bloch 2008 | AA. Bibliography#Bloch 08]] | Item 74: "Implement serialization judiciously" | ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="98f84a1ae2090bc8-a2d077c1-47124079-bd4baf54-bde603f426b8c4ac2126cb08"><ac:plain-text-body><![CDATA[ | [[Harold 2006 | AA. Bibliography#Harold 06]] | 13.7.5. serialPersistentFields | ]]></ac:plain-text-body></ac:structured-macro> |
...