동일 출처 정책(Same-Origin Policy, SOP)은 한 출처에서 로드된 문서나 스크립트가 다른 출처의 리소스와 상호작용하는 것을 제한하는 브라우저의 핵심 보안 메커니즘이다.
출처(Origin) 정의
출처 = 프로토콜 + 호스트 + 포트
https://example.com:443/path?q=1#hash
└── 출처: https://example.com:443
비교 예시 (기준: https://example.com):
https://example.com/other → 동일 출처 (경로 무관)
http://example.com → 다른 출처 (프로토콜 다름)
https://sub.example.com → 다른 출처 (호스트 다름)
https://example.com:8080 → 다른 출처 (포트 다름)
https://other.com → 다른 출처
SOP가 제한하는 것
javascript
// === 제한됨 (다른 출처) ===
// 1. XHR/fetch 응답 읽기 (CORS 없으면)
fetch('https://other.com/api/data') // 요청은 가지만 응답 못 읽음
// 2. iframe 내 다른 출처 DOM 접근
const iframe = document.querySelector('iframe');
iframe.contentDocument.body; // DOMException: 블로킹
// 3. 다른 출처 localStorage/IndexedDB 접근 불가
// === 허용됨 ===
// 이미지, CSS, 스크립트 로드 (읽기는 불가)
// <img src="https://other.com/img.png"> → 렌더링은 됨
// <link rel="stylesheet" href="..."> → 적용은 됨
// <script src="..."> → 실행은 됨
// postMessage: 명시적 크로스 오리진 통신
window.parent.postMessage({ type: 'ready' }, 'https://parent.com');
window.addEventListener('message', (e) => {
if (e.origin !== 'https://trusted.com') return; // 검증 필수!
console.log(e.data);
});
CORS (Cross-Origin Resource Sharing)
http
# 서버에서 크로스 오리진 허용
# 단순 요청 응답
Access-Control-Allow-Origin: https://frontend.com
Access-Control-Allow-Credentials: true # 쿠키 포함 시
# Preflight (OPTIONS) 응답
Access-Control-Allow-Methods: GET, POST, PUT, DELETE
Access-Control-Allow-Headers: Content-Type, Authorization
Access-Control-Max-Age: 86400 # Preflight 캐시 24시간
관련 문서
- •[[browser-security-model|브라우저 보안 모델]]
- •[[cookie-security|쿠키 보안]]
- •[[subresource-integrity|SRI]]