Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

It is possible to add access-specifier to the readResolve() and writeReplace() methods. However, if they are declared private, extending classes cannot invoke or override them. Similarly, if either of these methods is declared static, extending classes cannot override the method, they can only hide it.

Deviating from these method signatures produces a method that is not invoked during object serialization or deserialization. Such methods, especially if declared public, might be accessible to untrusted code.

Unlike most interfaces, Serializable does not define the method signatures it requires because readObject() and writeObject() are private. Consequently, the Java compiler will not identify an incorrect method signature.

...

This noncompliant code example shows a class Ser with a private constructor, indicating that code external to the class should not be able to create instances of it. The class implements java.io.Serializable and defines public readObject() and writeObject() methods. Consequently, untrusted code can obtain the reconstituted objects by using readObject(), and can write to the stream by using writeObject().

...

This compliant solution declares the readObject() and writeObject() methods private and non-static to limit their accessibility.

...

This noncompliant code example declares the readResolve() and writeReplace() methods as private.

Code Block
bgColor#FFCCCC
class Extendable implements Serializable {
  private Object readResolve() {
    // ...
  }

  private Object writeReplace() {
    // ...
  }
}

...

This noncompliant code example declares the readResolve() and writeReplace() methods as static.

Code Block
bgColor#FFCCCC
class Extendable implements Serializable {
  protected static Object readResolve() {
    // ...
  }

  protected static Object writeReplace() {
    // ...
  }
}

...

This compliant solution declares the two methods protected while eliminating the static keyword, so that subclasses can inherit them.

...

Related Guidelines

MITRE CWE

CWE ID -502, "Deserialization of Untrusted Data"

...

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="3adc57925968e75d-3eaf1ca3-48074513-bc20a5f5-6619c7ad22da76e6274f3236"><ac:plain-text-body><![CDATA[

[[API 2006

AA. Bibliography#API 06]]

Serializable

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="9c60e169c363f885-7e499214-4fb3497f-98348ab3-42f0dead977b03c4ff362b5e"><ac:plain-text-body><![CDATA[

[[Sun 2006

AA. Bibliography#Sun 06]]

"Serialization specification"

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="3364293601d2fceb-359fe1a2-4aa14991-8bfea7b5-7039440652d03c5082629ecb"><ac:plain-text-body><![CDATA[

[[Ware 2008

AA. Bibliography#Ware 08]]

 

]]></ac:plain-text-body></ac:structured-macro>

...