You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
二是将敏感变量加上transient关键字,在变量声明前加上该关键字,可以阻止该变量被序列化到文件中,在被反序列化后,transient 变量的值被设为初始值,如 int 型的是 0,对象型的是 null。
修复代码1
将敏感变量加上transient关键字
package testcases.CWE499_Sensitive_Data_Serializable;
import java.sql.*;
import java.io.*;
import java.util.logging.Level;
import testcasesupport.*;
/* We would never expect to see a real class like this, but our implementation tries to ensure the fact
* that one of the fields is sensitive */
public class CWE499_Sensitive_Data_Serializable__serializable_01_good2 extends CWE499_Sensitive_Data_Serializable__serializable_Helper
{
/* FIX: Use the transient keyword to deny serialization */
/* Sensitive field */
private transient String passwordHash;
protected void setPassword(String password)
{
passwordHash = password;
}
protected String getPassword()
{
return passwordHash;
}
}
修复代码2
将父类的序列化相关操作重写,如果子类执行序列化操作就抛异常。
package testcases.CWE499_Sensitive_Data_Serializable;
import java.sql.*;
import java.io.*;
import java.util.logging.Level;
import testcasesupport.*;
public class CWE499_Sensitive_Data_Serializable__serializable_01_good1 extends CWE499_Sensitive_Data_Serializable__serializable_Helper
{
/* Sensitive field */
private String passwordHash;
protected void setPassword(String password)
{
passwordHash = password;
}
protected String getPassword()
{
return passwordHash;
}
/* FIX: Override writeObject(), readObject(), and readObjectNoData() methods to deny serialization attempts */
private final void writeObject(ObjectOutputStream out) throws IOException
{
throw new NotSerializableException();
}
private final void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException
{
throw new NotSerializableException();
}
private final void readObjectNoData() throws ObjectStreamException
{
throw new NotSerializableException();
}
}