콘텐츠 보안 정책(CSP)
주입 공격을 완화하고 리소스 로딩을 제어할 수 있도록 강력한 콘텐츠 보안 정책을 구성하고 적용합니다.
콘텐츠 보안 정책(CSP)은(는) CoddyKit의 무료 Browser Extensions Development (Chrome & Edge) 강의입니다. 이것은 4개 중 3번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Browser Extensions Development (Chrome & Edge) 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Browser Extensions Development (Chrome & Edge) 강의에는 총 4개의 강의가 포함되어 있습니다.
이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.
What is CSP?
Welcome to Content Security Policy (CSP)! This powerful security feature helps protect your browser extensions and web pages from dangerous attacks like Cross-Site Scripting (XSS).
Think of CSP as a bouncer for your extension. It tells the browser exactly which sources are allowed to load scripts, styles, images, and other resources.
How CSP Works
CSP operates by defining a set of "directives" in your extension's manifest.json file. Each directive specifies valid sources for a particular type of resource.
- Scripts: Where JavaScript can load from.
- Styles: Where CSS can come from.
- Images: Allowed sources for images.
- Frames: Which URLs can be embedded in iframes.
If a resource tries to load from an unapproved source, the browser blocks it!
MV3's Default CSP
For Manifest V3 extensions, Chrome and Edge automatically apply a very strict default Content Security Policy. This helps ensure a baseline level of security.
You can customize or override this default CSP using the content_security_policy key within your manifest.json file. This is crucial when your extension needs to load resources from specific external domains.
`default-src`: The Catch-All
The default-src directive is your CSP's fallback. If you don't specify a directive for a particular resource type (like script-src or img-src), the browser will use the rules defined in default-src.
Common values:
'self': Allows resources only from the extension's own origin.https://example.com: Allows resources from a specific HTTPS domain.*: Allows resources from any origin (use with extreme caution!).
Restricting JavaScript (`script-src`)
The script-src directive is vital for preventing Cross-Site Scripting (XSS). It dictates where your extension can load JavaScript code from.
For extensions, always avoid using 'unsafe-inline' or 'unsafe-eval'. These directives allow inline scripts and eval(), which are major security risks. Load scripts from your extension's package or explicitly whitelisted safe domains.
Manifest with CSP
Here's how you'd define a basic CSP in your manifest.json to allow scripts only from your extension's package and images from any source:
{
"name": "My Secure Extension",
"version": "1.0",
"manifest_version": 3,
"action": {
"default_popup": "popup.html"
},
"content_security_policy": {
"extension_pages": "script-src 'self'; object-src 'self'; img-src *"
}
}Styles, Images & More
Just like with scripts, you can control other resource types:
style-src: Defines valid sources for CSS stylesheets. Use'self'or specific HTTPS URLs.img-src: Specifies allowed sources for images. You might use'self',data:(for base64 images), or specific image CDNs.
Always be as restrictive as possible to enhance security!
Beyond Basic Directives
CSP offers many more directives for fine-grained control:
object-src: Restricts sources for plugins like<object>,<embed>.frame-src: Controls which URLs can be loaded into<frame>,<iframe>, etc.connect-src: Limits where your extension can make network requests (e.g.,fetch(),XMLHttpRequest).
Principle of Least Privilege
When defining your CSP, always follow the "Principle of Least Privilege". This means only allowing exactly what your extension needs and nothing more.
- Start with the strictest possible CSP.
- Gradually add directives and sources as required.
- Avoid wildcards (
*) unless absolutely necessary and justified. - Regularly review your CSP as your extension evolves.
CSP Rule Check
You are building an extension and want to allow scripts only from your extension's own files, and images from any external HTTPS source. Which of the following CSP configurations would achieve this for extension_pages?
Recap: Secure with CSP
Great job! You've learned how Content Security Policy (CSP) is a critical defense mechanism for your browser extensions.
- CSP helps prevent XSS and other injection attacks.
- It works by whitelisting trusted sources for resources.
- Manifest V3 enforces a strict default CSP.
- Use directives like
default-src,script-src,style-src, andimg-srcto control content. - Always apply the Principle of Least Privilege when defining your CSP.
A well-configured CSP is key to building secure and robust extensions!
자주 묻는 질문
“콘텐츠 보안 정책(CSP)” 강의는 무료인가요?
네 — “콘텐츠 보안 정책(CSP)” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Browser Extensions Development (Chrome & Edge) 강의 전체를 잠금 해제할 수 있습니다. Browser Extensions Development (Chrome & Edge) 강의에는 총 4개의 강의가 포함되어 있습니다.
“콘텐츠 보안 정책(CSP)”에서 뭘 배우나요?
주입 공격을 완화하고 리소스 로딩을 제어할 수 있도록 강력한 콘텐츠 보안 정책을 구성하고 적용합니다. 브라우저에서 직접 실행하는 실습 코드로 Browser Extensions Development (Chrome & Edge)을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
Browser Extensions Development (Chrome & Edge)을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 Browser Extensions Development (Chrome & Edge)은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 3번째 강의입니다.
“콘텐츠 보안 정책(CSP)” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 Browser Extensions Development (Chrome & Edge) 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 Browser Extensions Development (Chrome & Edge) 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- 고급 권한 이해하기
- 안전한 코딩 관행
- 콘텐츠 보안 정책(CSP)
- 선택적 권한과 실행 중 요청