forked from Meet-Coder-Study/book-effective-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c3f3759
commit 4700945
Showing
1 changed file
with
36 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,42 @@ | ||
# [Effective Java] item 85. 자바 직렬화의 대안을 찾으라 | ||
|
||
### 직렬화란? | ||
- `java.io.Serializable` 인터페이스를 상속받은 객체를 외부의 자바 시스템에서도 사용할 수 있도록 byte 형태로 데이터를 변환하는 기술 | ||
- 역직렬화는 반대로 byte로 변환된 data를 Object로 변환하는 기술 | ||
```java | ||
public class Member implements Serializable { | ||
private String name; | ||
private String email; | ||
private int age; | ||
|
||
public Member(String name, String email, int age) { | ||
this.name = name; | ||
this.email = email; | ||
this.age = age; | ||
} | ||
@Override | ||
public String toString() { | ||
return String.format("Member{name='%s', email='%s', age='%s'}", name, email, age); | ||
} | ||
} | ||
``` | ||
|
||
```java | ||
public static void main(String[] args){ | ||
Member member = new Member("홍길동", "[email protected]", 25); | ||
byte[] serializedMember; | ||
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { | ||
try (ObjectOutputStream oos = new ObjectOutputStream(baos)) { | ||
oos.writeObject(member); | ||
serializedMember = baos.toByteArray(); | ||
} | ||
} | ||
System.out.println(Base64.getEncoder().encodeToString(serializedMember)); | ||
} | ||
``` | ||
### 역직렬화의 위험성 | ||
- 1997년, 자바에 처음으로 직렬화가 도입되었다. 대중적 언어에 적용된건 처음이었기에 그때는 다소 위험하지않겠냐는 이야기가 나왔다. | ||
- 프로그래머가 어렵지 않게 분산 객체를 만들 수 있다는 구호는 매력적이었지만, `보이지 않는 생성자, API와 구현 사이의 모호해진 경계, 잠재적인 정확성 문제, 성능, 보안, 유지보수성 등 그 대가가 컸다.` | ||
- 1997년, 자바에 처음으로 직렬화가 도입 | ||
- 직렬화는 프로그래머가 어렵지 않게 분산 객체를 만들 수 있다는 구호는 매력적이었지만, `보이지 않는 생성자, API와 구현 사이의 모호해진 경계, 잠재적인 정확성 문제, 성능, 보안, 유지보수성 등 그 대가가 크다` | ||
- 직렬화의 근본적인 문제는 공격 범위가 너무 넓고 지속적으로 더 넓어져 방어하기 어렵다는 점이다. | ||
- `ObjectInputStream의 readObject 메서드를 호출하면서 객체 그래프가 역직렬화`되기 때문이다. `readObject 메서드는 (serializable 인터페이스를 구현했다면) 클래스패스 안의 거의 모든 타입의 객체를 만들어 낼 수 있는, 사실상 마법 같은 생성자`다. | ||
- 바이트 스트림을 역직렬화하는 과정에서 이 메서드는 그 타입들 안의 모든 코드를 수행할 수 있다. 이 말인즉슨, 그 타입들의 코드 전체가 공격 범위에 들어간다는 뜻이다. | ||
|