Skip to content

Commit a7670fb

Browse files
committed
Java: Enhance IncorrectSerializableMethods.ql
1 parent 12936ff commit a7670fb

File tree

4 files changed

+31
-12
lines changed

4 files changed

+31
-12
lines changed

java/ql/src/Likely Bugs/Serialization/IncorrectSerializableMethods.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ void readObject(ObjectInputStream in) {
55
//...
66
}
77

8+
// BAD: Does not match the exact signature required for a custom
9+
// deserialization protocol. Will not be called during deserialization.
10+
void readObjectNoData() {
11+
//...
12+
}
13+
814
// BAD: Does not match the exact signature required for a custom
915
// serialization protocol. Will not be called during serialization.
1016
protected void writeObject(ObjectOutputStream out) {
@@ -18,6 +24,11 @@ private void readObject(ObjectInputStream in) {
1824
//...
1925
}
2026

27+
// GOOD: Signature for a custom deserialization implementation.
28+
private void readObjectNoData() {
29+
//...
30+
}
31+
2132
// GOOD: Signature for a custom serialization implementation.
2233
private void writeObject(ObjectOutputStream out) {
2334
//...

java/ql/src/Likely Bugs/Serialization/IncorrectSerializableMethods.qhelp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,26 @@
77
<overview>
88
<p>
99
A serializable object that defines its own serialization protocol using the methods
10-
<code>readObject</code> and <code>writeObject</code> must use the signature that is expected by the
11-
Java serialization framework. Otherwise, the default serialization mechanism is used.
10+
<code>readObject</code>, <code>readObjectNoData</code> or <code>writeObject</code> must use
11+
the signature that is expected by the Java serialization framework. Otherwise, the default
12+
serialization mechanism is used.
1213
</p>
1314

1415
</overview>
1516
<recommendation>
1617
<p>
17-
Make sure that the signatures of <code>readObject</code> and <code>writeObject</code> on
18-
serializable classes use these exact signatures:
18+
Make sure that the signatures of <code>readObject</code>, <code>readObjectNoData</code> and
19+
<code>writeObject</code> on serializable classes match these expected signatures:
1920
</p>
2021

2122
<sample src="IncorrectSerializableMethodsSig.java" />
2223

2324
</recommendation>
2425
<example>
2526

26-
<p>In the following example, <code>WrongNetRequest</code> defines <code>readObject</code> and
27-
<code>writeObject</code> using the wrong signatures. However, <code>NetRequest</code> defines them
28-
correctly.</p>
27+
<p>In the following example, <code>WrongNetRequest</code> defines <code>readObject</code>,
28+
<code>readObjectNoData</code> and <code>writeObject</code> using the wrong signatures. However,
29+
<code>NetRequest</code> defines them correctly.</p>
2930

3031
<sample src="IncorrectSerializableMethods.java" />
3132

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @name Serialization methods do not match required signature
3-
* @description A serialized class that implements 'readObject' or 'writeObject' but does not use
4-
* the correct signatures causes the default serialization mechanism to be used.
3+
* @description A serialized class that implements 'readObject', 'readObjectNoData' or 'writeObject' but
4+
* does not use the correct signatures causes the default serialization mechanism to be used.
55
* @kind problem
66
* @problem.severity warning
77
* @precision medium
@@ -13,12 +13,17 @@
1313

1414
import java
1515

16-
from Method m, TypeSerializable serializable
16+
from Method m, TypeSerializable serializable, string reason
1717
where
1818
m.getDeclaringType().hasSupertype+(serializable) and
1919
(
2020
m.hasStringSignature("readObject(ObjectInputStream)") or
21+
m.hasStringSignature("readObjectNoData()") or
2122
m.hasName("writeObject(ObjectOutputStream)")
2223
) and
23-
not m.isPrivate()
24-
select m, "readObject and writeObject should be private methods."
24+
(
25+
not m.isPrivate() and reason = "Method must be private"
26+
or m.isStatic() and reason = "Method must not be static"
27+
or not m.getReturnType() instanceof VoidType and reason = "Return type must be void"
28+
)
29+
select m, reason
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
private void readObject(java.io.ObjectInputStream in)
22
throws IOException, ClassNotFoundException;
3+
private void readObjectNoData()
4+
throws ObjectStreamException;
35
private void writeObject(java.io.ObjectOutputStream out)
46
throws IOException;

0 commit comments

Comments
 (0)