item 88) readObject 메서드는 방어적으로 작성하라
가변 공격
public class MutablePeriod {
//Period 인스턴스
public final Period period;
public final Date start;
public final Date end;
public MutablePeriod() {
try {
ByteArrayOutputStream bos =new ByteArrayOutputStream();
ObjectOutputStream out =new ObjectOutputStream(bos);
// 1) Period 인스턴스 직렬화
out.writeObject(new Period(new Date(),new Date()));
// 2) start, end 필드에 대한 참조 추가
byte[] ref = {0x71, 0, 0x7e, 0, 5};
bos.write(ref);
ref[4] = 4;
bos.write(ref);
// 3) Period 역직렬화 후 Date 참조를 훔친다.
ObjectInputStream in =new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()));
period = (Period)in.readObject();
start = (Date)in.readObject(); //Period 내부의 start 데이터 탈취
end = (Date)in.readObject(); //Period 내부의 end 데이터 탈취
} catch (IOException | ClassNotFoundException e) {
throw new AssertionError(e);
}
}기본 readObject 메서드
Last updated