|
| 1 | +# iframe Security |
| 2 | + |
| 3 | +ng2-pdfjs-viewer includes built-in iframe security features to protect your application from potential security vulnerabilities. |
| 4 | + |
| 5 | +## π Security Overview |
| 6 | + |
| 7 | +The library uses static iframe sandbox attributes to provide a secure environment for PDF viewing while maintaining full functionality. The sandbox configuration is fixed for security and Angular compliance. |
| 8 | + |
| 9 | +### Built-in Security Configuration |
| 10 | + |
| 11 | +```html |
| 12 | +<ng2-pdfjs-viewer pdfSrc="document.pdf"></ng2-pdfjs-viewer> |
| 13 | +``` |
| 14 | + |
| 15 | +**Static sandbox attributes (always enabled):** |
| 16 | +- `allow-forms` - Enables PDF form functionality |
| 17 | +- `allow-scripts` - Enables PDF.js JavaScript execution |
| 18 | +- `allow-same-origin` - Enables loading PDF files and assets |
| 19 | +- `allow-modals` - Enables PDF.js dialogs (print, download) |
| 20 | + |
| 21 | +## π‘οΈ Security Benefits |
| 22 | + |
| 23 | +### XSS Prevention |
| 24 | +- Prevents malicious scripts in PDFs from affecting the parent page |
| 25 | +- Isolates PDF.js execution environment |
| 26 | +- Blocks unauthorized access to parent window context |
| 27 | + |
| 28 | +### CSP Compliance |
| 29 | +- Meets Content Security Policy requirements |
| 30 | +- Compatible with strict CSP headers |
| 31 | +- Reduces security audit findings |
| 32 | + |
| 33 | +### Data Protection |
| 34 | +- Limits iframe access to parent window |
| 35 | +- Prevents data exfiltration through PDFs |
| 36 | +- Protects sensitive application data |
| 37 | + |
| 38 | +### Enterprise Ready |
| 39 | +- Suitable for corporate security environments |
| 40 | +- Meets regulatory compliance requirements |
| 41 | +- Integrates with security monitoring tools |
| 42 | + |
| 43 | +## βοΈ Configuration Options |
| 44 | + |
| 45 | +### Basic Usage |
| 46 | + |
| 47 | +```html |
| 48 | +<!-- Built-in security (always enabled) --> |
| 49 | +<ng2-pdfjs-viewer |
| 50 | + pdfSrc="document.pdf"> |
| 51 | +</ng2-pdfjs-viewer> |
| 52 | +``` |
| 53 | + |
| 54 | +### iframe Border Customization |
| 55 | + |
| 56 | +```html |
| 57 | +<!-- Custom border styling --> |
| 58 | +<ng2-pdfjs-viewer |
| 59 | + pdfSrc="document.pdf" |
| 60 | + iframeBorder="2px solid #ccc"> |
| 61 | +</ng2-pdfjs-viewer> |
| 62 | +``` |
| 63 | + |
| 64 | +> **Note**: Sandbox attributes are fixed for security and Angular compliance. Only border styling can be customized. |
| 65 | +
|
| 66 | +## π Sandbox Attributes Reference |
| 67 | + |
| 68 | +### Required Attributes |
| 69 | + |
| 70 | +| Attribute | Purpose | Required For | |
| 71 | +|-----------|---------|--------------| |
| 72 | +| `allow-forms` | PDF form functionality | PDF form filling and submission | |
| 73 | +| `allow-scripts` | JavaScript execution | PDF.js viewer functionality | |
| 74 | +| `allow-same-origin` | Same-origin requests | Loading PDF files and assets | |
| 75 | +| `allow-modals` | Dialog boxes | Print dialog, download confirmations | |
| 76 | + |
| 77 | +### Optional Attributes |
| 78 | + |
| 79 | +| Attribute | Purpose | Use Case | |
| 80 | +|-----------|---------|----------| |
| 81 | +| `allow-popups` | Open new windows | External window functionality | |
| 82 | +| `allow-downloads` | Download files | PDF download functionality | |
| 83 | +| `allow-top-navigation` | Navigate parent window | PDF navigation features | |
| 84 | +| `allow-pointer-lock` | Pointer lock API | Advanced interaction features | |
| 85 | + |
| 86 | +### Security Attributes |
| 87 | + |
| 88 | +| Attribute | Purpose | Security Impact | |
| 89 | +|-----------|---------|-----------------| |
| 90 | +| `allow-same-origin` | Same-origin requests | Required for functionality | |
| 91 | +| `allow-forms` | Form submission | Required for PDF forms | |
| 92 | +| `allow-scripts` | JavaScript execution | Required for PDF.js | |
| 93 | +| `allow-modals` | Dialog boxes | Required for user interactions | |
| 94 | + |
| 95 | +## π§ Advanced Configuration |
| 96 | + |
| 97 | +### Border Customization |
| 98 | + |
| 99 | +```typescript |
| 100 | +export class MyComponent { |
| 101 | + // Dynamic border configuration |
| 102 | + borderConfig = "0"; |
| 103 | + |
| 104 | + // Update border based on theme |
| 105 | + updateBorder(theme: string) { |
| 106 | + switch (theme) { |
| 107 | + case 'dark': |
| 108 | + this.borderConfig = "1px solid #333"; |
| 109 | + break; |
| 110 | + case 'light': |
| 111 | + this.borderConfig = "1px solid #ccc"; |
| 112 | + break; |
| 113 | + case 'none': |
| 114 | + this.borderConfig = "0"; |
| 115 | + break; |
| 116 | + } |
| 117 | + } |
| 118 | +} |
| 119 | +``` |
| 120 | + |
| 121 | +```html |
| 122 | +<ng2-pdfjs-viewer |
| 123 | + pdfSrc="document.pdf" |
| 124 | + [iframeBorder]="borderConfig"> |
| 125 | +</ng2-pdfjs-viewer> |
| 126 | +``` |
| 127 | + |
| 128 | +## π§ͺ Testing Security Configuration |
| 129 | + |
| 130 | +### Basic Functionality Test |
| 131 | + |
| 132 | +```typescript |
| 133 | +describe('iframe Security', () => { |
| 134 | + it('should load PDF with static sandbox', () => { |
| 135 | + const fixture = TestBed.createComponent(PdfViewerComponent); |
| 136 | + fixture.componentInstance.pdfSrc = 'test.pdf'; |
| 137 | + fixture.detectChanges(); |
| 138 | + |
| 139 | + const iframe = fixture.debugElement.query(By.css('iframe')); |
| 140 | + expect(iframe.nativeElement.sandbox).toBe('allow-forms allow-scripts allow-same-origin allow-modals'); |
| 141 | + }); |
| 142 | + |
| 143 | + it('should allow custom border configuration', () => { |
| 144 | + const fixture = TestBed.createComponent(PdfViewerComponent); |
| 145 | + fixture.componentInstance.pdfSrc = 'test.pdf'; |
| 146 | + fixture.componentInstance.iframeBorder = '2px solid #ccc'; |
| 147 | + fixture.detectChanges(); |
| 148 | + |
| 149 | + const iframe = fixture.debugElement.query(By.css('iframe')); |
| 150 | + expect(iframe.nativeElement.style.border).toBe('2px solid #ccc'); |
| 151 | + }); |
| 152 | +}); |
| 153 | +``` |
| 154 | + |
| 155 | +### Security Validation Test |
| 156 | + |
| 157 | +```typescript |
| 158 | +it('should prevent XSS attacks', () => { |
| 159 | + // Test that malicious scripts in PDFs cannot access parent window |
| 160 | + const maliciousPdf = createMaliciousPdf(); |
| 161 | + // ... test implementation |
| 162 | +}); |
| 163 | +``` |
| 164 | + |
| 165 | +## π¨ Common Issues and Solutions |
| 166 | + |
| 167 | +### Issue: PDF Forms Not Working |
| 168 | + |
| 169 | +**Problem:** PDF forms are not interactive or submit buttons don't work. |
| 170 | + |
| 171 | +**Solution:** Ensure `allow-forms` is included in sandbox attributes. |
| 172 | + |
| 173 | +```html |
| 174 | +<ng2-pdfjs-viewer |
| 175 | + pdfSrc="document.pdf" |
| 176 | + iframeSandbox="allow-forms allow-scripts allow-same-origin allow-modals"> |
| 177 | +</ng2-pdfjs-viewer> |
| 178 | +``` |
| 179 | + |
| 180 | +### Issue: Download/Print Buttons Not Working |
| 181 | + |
| 182 | +**Problem:** Download and print functionality is disabled. |
| 183 | + |
| 184 | +**Solution:** Add `allow-modals` to sandbox attributes. |
| 185 | + |
| 186 | +```html |
| 187 | +<ng2-pdfjs-viewer |
| 188 | + pdfSrc="document.pdf" |
| 189 | + iframeSandbox="allow-forms allow-scripts allow-same-origin allow-modals"> |
| 190 | +</ng2-pdfjs-viewer> |
| 191 | +``` |
| 192 | + |
| 193 | +### Issue: External Window Not Opening |
| 194 | + |
| 195 | +**Problem:** "Open in new tab" button doesn't work. |
| 196 | + |
| 197 | +**Solution:** Add `allow-popups` to sandbox attributes. |
| 198 | + |
| 199 | +```html |
| 200 | +<ng2-pdfjs-viewer |
| 201 | + pdfSrc="document.pdf" |
| 202 | + iframeSandbox="allow-forms allow-scripts allow-same-origin allow-modals allow-popups"> |
| 203 | +</ng2-pdfjs-viewer> |
| 204 | +``` |
| 205 | + |
| 206 | +### Issue: PDF Not Loading |
| 207 | + |
| 208 | +**Problem:** PDF fails to load with sandbox enabled. |
| 209 | + |
| 210 | +**Solution:** Ensure `allow-same-origin` is included for same-domain PDFs. |
| 211 | + |
| 212 | +```html |
| 213 | +<ng2-pdfjs-viewer |
| 214 | + pdfSrc="document.pdf" |
| 215 | + iframeSandbox="allow-forms allow-scripts allow-same-origin allow-modals"> |
| 216 | +</ng2-pdfjs-viewer> |
| 217 | +``` |
| 218 | + |
| 219 | +## π Security Best Practices |
| 220 | + |
| 221 | +### 1. Use Default Configuration |
| 222 | +- Start with the default sandbox attributes |
| 223 | +- Only add additional permissions when needed |
| 224 | +- Test thoroughly after any changes |
| 225 | + |
| 226 | +### 2. Regular Security Audits |
| 227 | +- Review sandbox configuration regularly |
| 228 | +- Test with security scanning tools |
| 229 | +- Monitor for security advisories |
| 230 | + |
| 231 | +### 3. Principle of Least Privilege |
| 232 | +- Only enable permissions that are actually needed |
| 233 | +- Document why each permission is required |
| 234 | +- Remove unused permissions |
| 235 | + |
| 236 | +### 4. Environment-Specific Configuration |
| 237 | +- Use stricter settings in production |
| 238 | +- Relax settings only in development when needed |
| 239 | +- Implement security policies consistently |
| 240 | + |
| 241 | +### 5. Monitoring and Logging |
| 242 | +- Monitor for security violations |
| 243 | +- Log sandbox configuration changes |
| 244 | +- Alert on unexpected behavior |
| 245 | + |
| 246 | +## π Related Documentation |
| 247 | + |
| 248 | +- [Security Features Overview](./security) - Complete security guide |
| 249 | +- [Component Inputs](../api/component-inputs) - All configuration options |
| 250 | +- [Getting Started](../getting-started) - Quick setup guide |
| 251 | +- [Examples](../examples/basic-usage) - Practical examples |
| 252 | + |
| 253 | +## π Support |
| 254 | + |
| 255 | +If you encounter security-related issues: |
| 256 | + |
| 257 | +1. Check the [Common Issues](#-common-issues-and-solutions) section |
| 258 | +2. Review your sandbox configuration |
| 259 | +3. Test with minimal sandbox attributes |
| 260 | +4. Open an issue on GitHub with details |
| 261 | + |
| 262 | +Remember: Security is a shared responsibility. Always test your configuration and stay updated with security best practices. |
0 commit comments