|
| 1 | +/*! |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +import {Component, CSP_NONCE, destroyPlatform, ElementRef, inject, ViewEncapsulation} from '@angular/core'; |
| 10 | +import {bootstrapApplication} from '@angular/platform-browser'; |
| 11 | +import {withBody} from '@angular/private/testing'; |
| 12 | + |
| 13 | +describe('CSP integration', () => { |
| 14 | + beforeEach(destroyPlatform); |
| 15 | + afterEach(destroyPlatform); |
| 16 | + |
| 17 | + const testStyles = '.a { color: var(--csp-test-var, hotpink); }'; |
| 18 | + |
| 19 | + function findTestNonces(rootNode: ParentNode): string[] { |
| 20 | + const styles = rootNode.querySelectorAll('style'); |
| 21 | + const nonces: string[] = []; |
| 22 | + |
| 23 | + for (let i = 0; i < styles.length; i++) { |
| 24 | + const style = styles[i]; |
| 25 | + if (style.textContent?.includes('--csp-test-var') && style.nonce) { |
| 26 | + nonces.push(style.nonce); |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + return nonces; |
| 31 | + } |
| 32 | + |
| 33 | + it('should use the predefined ngCspNonce when inserting styles with emulated encapsulation', |
| 34 | + withBody('<app ngCspNonce="emulated-nonce"></app>', async () => { |
| 35 | + @Component({ |
| 36 | + selector: 'uses-styles', |
| 37 | + template: '', |
| 38 | + styles: [testStyles], |
| 39 | + standalone: true, |
| 40 | + encapsulation: ViewEncapsulation.Emulated |
| 41 | + }) |
| 42 | + class UsesStyles { |
| 43 | + } |
| 44 | + |
| 45 | + @Component({ |
| 46 | + selector: 'app', |
| 47 | + standalone: true, |
| 48 | + template: '<uses-styles></uses-styles>', |
| 49 | + imports: [UsesStyles] |
| 50 | + }) |
| 51 | + class App { |
| 52 | + } |
| 53 | + |
| 54 | + const appRef = await bootstrapApplication(App); |
| 55 | + |
| 56 | + expect(findTestNonces(document)).toEqual(['emulated-nonce']); |
| 57 | + |
| 58 | + appRef.destroy(); |
| 59 | + })); |
| 60 | + |
| 61 | + it('should use the predefined ngCspNonce when inserting styles with no encapsulation', |
| 62 | + withBody('<app ngCspNonce="disabled-nonce"></app>', async () => { |
| 63 | + @Component({ |
| 64 | + selector: 'uses-styles', |
| 65 | + template: '', |
| 66 | + styles: [testStyles], |
| 67 | + standalone: true, |
| 68 | + encapsulation: ViewEncapsulation.None |
| 69 | + }) |
| 70 | + class UsesStyles { |
| 71 | + } |
| 72 | + |
| 73 | + @Component({ |
| 74 | + selector: 'app', |
| 75 | + standalone: true, |
| 76 | + template: '<uses-styles></uses-styles>', |
| 77 | + imports: [UsesStyles] |
| 78 | + }) |
| 79 | + class App { |
| 80 | + } |
| 81 | + |
| 82 | + const appRef = await bootstrapApplication(App); |
| 83 | + |
| 84 | + expect(findTestNonces(document)).toEqual(['disabled-nonce']); |
| 85 | + |
| 86 | + appRef.destroy(); |
| 87 | + })); |
| 88 | + |
| 89 | + |
| 90 | + it('should use the predefined ngCspNonce when inserting styles with shadow DOM encapsulation', |
| 91 | + withBody('<app ngCspNonce="shadow-nonce"></app>', async () => { |
| 92 | + if (!document.body.attachShadow) { |
| 93 | + return; |
| 94 | + } |
| 95 | + |
| 96 | + let usesStylesRootNode!: HTMLElement; |
| 97 | + |
| 98 | + @Component({ |
| 99 | + selector: 'uses-styles', |
| 100 | + template: '', |
| 101 | + styles: [testStyles], |
| 102 | + standalone: true, |
| 103 | + encapsulation: ViewEncapsulation.ShadowDom |
| 104 | + }) |
| 105 | + class UsesStyles { |
| 106 | + constructor() { |
| 107 | + usesStylesRootNode = inject(ElementRef).nativeElement; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + @Component({ |
| 112 | + selector: 'app', |
| 113 | + standalone: true, |
| 114 | + template: '<uses-styles></uses-styles>', |
| 115 | + imports: [UsesStyles] |
| 116 | + }) |
| 117 | + class App { |
| 118 | + } |
| 119 | + |
| 120 | + const appRef = await bootstrapApplication(App); |
| 121 | + |
| 122 | + expect(findTestNonces(usesStylesRootNode.shadowRoot!)).toEqual(['shadow-nonce']); |
| 123 | + |
| 124 | + appRef.destroy(); |
| 125 | + })); |
| 126 | + |
| 127 | + it('should prefer nonce provided through DI over one provided in the DOM', |
| 128 | + withBody('<app ngCspNonce="dom-nonce"></app>', async () => { |
| 129 | + @Component({selector: 'uses-styles', template: '', styles: [testStyles], standalone: true}) |
| 130 | + class UsesStyles { |
| 131 | + } |
| 132 | + |
| 133 | + @Component({ |
| 134 | + selector: 'app', |
| 135 | + standalone: true, |
| 136 | + template: '<uses-styles></uses-styles>', |
| 137 | + imports: [UsesStyles] |
| 138 | + }) |
| 139 | + class App { |
| 140 | + } |
| 141 | + |
| 142 | + const appRef = await bootstrapApplication(App, { |
| 143 | + providers: [{provide: CSP_NONCE, useValue: 'di-nonce'}], |
| 144 | + }); |
| 145 | + |
| 146 | + expect(findTestNonces(document)).toEqual(['di-nonce']); |
| 147 | + |
| 148 | + appRef.destroy(); |
| 149 | + })); |
| 150 | +}); |
0 commit comments