Skip to content

Commit cfceccd

Browse files
docs(security): content security policies (#2975)
Co-authored-by: Liam DeBeasi <[email protected]>
1 parent b52a8c3 commit cfceccd

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

docs/techniques/security.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,53 @@ export const ToastExample: React.FC = () => {
160160
</TabItem>
161161
</Tabs>
162162
````
163+
164+
## Content Security Policies (CSP)
165+
166+
A [Content Security Policy (CSP)](https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP) is a security mechanism that helps protect web applications against certain types of attacks, such as cross-site scripting (XSS) and data injection. It is implemented through an HTTP header that instructs the browser on which sources of content, such as scripts, stylesheets, and images, are allowed to be loaded and executed on a web page.
167+
168+
The main purpose of a CSP is to mitigate the risks associated with code injection attacks. By defining a policy, web developers can specify from which domains or sources the browser should allow the loading and execution of various types of content. This effectively limits the potential damage that can be caused by malicious scripts or unauthorized content.
169+
170+
### Enabling CSPs
171+
172+
Developers can assign a CSP to their application by setting a meta tag with the policy details and the expected nonce value on script and style tags.
173+
174+
```html
175+
<meta
176+
http-equiv="Content-Security-Policy"
177+
content="default-src 'self'; script-src 'self' 'nonce-randomNonceGoesHere'; style-src 'self' 'nonce-randomNonceGoesHere';"
178+
/>
179+
```
180+
181+
### Ionic and CSP
182+
183+
Ionic Framework provides a function to help developers set the nonce value used when constructing the web component stylesheets. This function should be called before any Ionic components are loaded. This is required to pass the nonce value to the web components so that they can be used in a CSP environment.
184+
185+
```ts
186+
import { setNonce } from '@ionic/core/loader';
187+
188+
setNonce('randomNonceGoesHere');
189+
```
190+
191+
:::tip
192+
193+
In Angular this can be called in the `main.ts` file, before the application is bootstrapped.
194+
195+
:::
196+
197+
For more information on how to use CSPs with Stencil web components, see the [Stencil documentation](https://stenciljs.com/docs/csp-nonce).
198+
199+
### Angular
200+
201+
Starting in Angular 16, Angular provides two options for setting the nonce value.
202+
203+
1. Set the `ngCspNonce` attribute on the root application element as `<app ngCspNonce="randomNonceGoesHere"></app>`. Use this approach if you have access to server-side templating that can add the nonce both to the header and the `index.html` when constructing the response.
204+
2. Provide the nonce using the [`CSP_NONCE`](https://angular.io/api/core/CSP_NONCE) injection token. Use this approach if you have access to the nonce at runtime and you want to be able to cache the `index.html`.
205+
206+
:::tip
207+
208+
If providing the `CSP_NONCE` injection token, set the provider in your `AppModule` for module projects or within the `bootstrapApplication` for standalone projects.
209+
210+
:::
211+
212+
For more information on how to use CSPs with Angular, see the [Angular documentation](https://angular.io/guide/security#content-security-policy).

0 commit comments

Comments
 (0)