Skip to content

Commit 5bc90e5

Browse files
committed
CSP compliance for CSS is added
1 parent f918fe3 commit 5bc90e5

File tree

10 files changed

+524
-327
lines changed

10 files changed

+524
-327
lines changed

docs-website/docs/api/component-inputs.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,46 @@ pageSpacing = {
387387
toolbarColor = '#f5f5f5'; // Light gray toolbar
388388
```
389389

390+
#### `borderRadius`
391+
- **Type**: `string`
392+
- **Default**: `undefined`
393+
- **Description**: Border radius for UI elements
394+
395+
```typescript
396+
borderRadius = '8px'; // Rounded corners
397+
```
398+
399+
#### `customCSS`
400+
- **Type**: `string`
401+
- **Default**: `undefined`
402+
- **Description**: Custom CSS styles to apply to the viewer
403+
404+
```typescript
405+
customCSS = '.page { box-shadow: 0 4px 8px rgba(0,0,0,0.2); }';
406+
```
407+
408+
#### `cspNonce`
409+
- **Type**: `string`
410+
- **Default**: `undefined`
411+
- **Description**: CSP nonce for customCSS when using strict Content Security Policy
412+
413+
```typescript
414+
// Only needed with strict CSP and customCSS
415+
cspNonce = 'random-nonce-value';
416+
```
417+
418+
```html
419+
<!-- With strict CSP -->
420+
<ng2-pdfjs-viewer
421+
[customCSS]="customStyles"
422+
[cspNonce]="cspNonce">
423+
</ng2-pdfjs-viewer>
424+
```
425+
426+
:::info CSP Compliance
427+
The viewer is CSP-compliant by default. The `cspNonce` input is only needed when using `customCSS` with strict Content Security Policy that requires nonces for inline styles.
428+
:::
429+
390430
### Advanced Theming
391431

392432
#### `themeConfig`
@@ -402,6 +442,7 @@ interface ThemeConfig {
402442
textColor?: string;
403443
borderRadius?: string;
404444
customCSS?: string;
445+
cspNonce?: string; // Optional CSP nonce
405446
}
406447

407448
themeConfig: ThemeConfig = {

docs-website/docs/features/security.md

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,68 @@ interface SecurityWarning {
248248
### Additional Security Measures
249249

250250
1. **Server-Side Validation** - Always validate file access permissions
251-
2. **Content Security Policy** - Implement CSP headers
251+
2. **Content Security Policy** - Implement CSP headers (viewer is CSP-compliant)
252252
3. **HTTPS Only** - Use HTTPS in production
253253
4. **File Type Validation** - Validate PDF files on upload
254254
5. **Access Control** - Implement proper user authentication and authorization
255+
256+
## 🔐 Content Security Policy (CSP) Compliance
257+
258+
### Overview
259+
260+
ng2-pdfjs-viewer is fully compliant with strict Content Security Policy (`style-src 'self'`). All styling is implemented using CSP-safe methods.
261+
262+
### CSP Compliance Features
263+
264+
-**External Stylesheets** - All static styles loaded from external CSS files
265+
-**CSS Custom Properties** - Dynamic styling via `element.style.setProperty()`
266+
-**CSS Classes** - Visibility and layout via class toggles
267+
-**Optional Nonce Support** - For `customCSS` with strict CSP
268+
269+
### Using with Strict CSP
270+
271+
The viewer works out-of-the-box with strict CSP policies:
272+
273+
```html
274+
<!-- Your index.html -->
275+
<meta http-equiv="Content-Security-Policy"
276+
content="default-src 'self';
277+
style-src 'self';
278+
script-src 'self';">
279+
280+
<!-- Viewer works without modifications -->
281+
<ng2-pdfjs-viewer pdfSrc="document.pdf"></ng2-pdfjs-viewer>
282+
```
283+
284+
### Custom CSS with CSP
285+
286+
When using `customCSS` with strict CSP, provide a nonce:
287+
288+
```typescript
289+
// Component
290+
customCSS = '.page { box-shadow: 0 4px 8px rgba(0,0,0,0.2); }';
291+
cspNonce = 'your-random-nonce'; // Must match CSP policy
292+
```
293+
294+
```html
295+
<!-- index.html -->
296+
<meta http-equiv="Content-Security-Policy"
297+
content="style-src 'self' 'nonce-your-random-nonce';">
298+
299+
<!-- Template -->
300+
<ng2-pdfjs-viewer
301+
[customCSS]="customCSS"
302+
[cspNonce]="cspNonce">
303+
</ng2-pdfjs-viewer>
304+
```
305+
306+
### CSP Best Practices
307+
308+
1. **Default Usage** - No nonce needed for standard theming
309+
2. **Custom CSS** - Only provide nonce when using `customCSS`
310+
3. **Server-Generated Nonce** - Generate unique nonce per request
311+
4. **Test Thoroughly** - Verify zero CSP violations in console
312+
313+
:::tip
314+
The viewer is CSP-compliant by default. You only need the `cspNonce` input when using `customCSS` with strict CSP policies.
315+
:::

docs-website/docs/features/theming.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,32 @@ export class MyComponent {
9292
</ng2-pdfjs-viewer>
9393
```
9494

95+
## CSP Compliance
96+
97+
:::info
98+
All theming features are **Content Security Policy (CSP) compliant**. The viewer uses only CSP-safe methods:
99+
- External CSS files
100+
- CSS custom properties via `element.style.setProperty()`
101+
- CSS class toggles
102+
103+
No inline style injection that would violate strict CSP policies.
104+
:::
105+
106+
### Using customCSS with Strict CSP
107+
108+
When using `customCSS` with strict CSP, provide a nonce:
109+
110+
```typescript
111+
themeConfig: ThemeConfig = {
112+
theme: 'dark',
113+
primaryColor: '#ff6b6b',
114+
customCSS: '.page { box-shadow: 0 4px 8px rgba(0,0,0,0.2); }',
115+
cspNonce: 'your-random-nonce' // Match your CSP policy
116+
};
117+
```
118+
119+
See [Security Features](./security#content-security-policy-csp-compliance) for details.
120+
95121
## CSS Custom Properties
96122

97123
### Available CSS Variables

lib/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ Add PDF.js assets to your `angular.json`:
200200

201201
### 🔒 Security Features
202202

203+
- **CSP Compliant** - Works with strict Content Security Policy (`style-src 'self'`)
203204
- **URL Validation** - Prevents unauthorized file parameter manipulation in external viewer
204205
- **Custom Security Templates** - Angular template support for security warnings
205206
- **Console Warnings** - Developer-friendly security notifications
@@ -496,6 +497,7 @@ export class MyComponent {
496497
| `textColor` | `string` | - | Text color |
497498
| `borderRadius` | `string` | - | Border radius |
498499
| `customCSS` | `string` | - | Custom CSS styles |
500+
| `cspNonce` | `string` | - | CSP nonce for customCSS (optional) |
499501
| `showSpinner` | `boolean` | `true` | Show loading spinner |
500502
| `customSpinnerTpl` | `TemplateRef` | - | Custom spinner template |
501503
| `spinnerClass` | `string` | - | Custom spinner CSS class |

0 commit comments

Comments
 (0)