Skip to content

Commit

Permalink
Fixed #15560 - PrimeNGConfig | Add csp
Browse files Browse the repository at this point in the history
  • Loading branch information
cetincakiroglu committed May 14, 2024
1 parent a99dd1a commit 91695fc
Show file tree
Hide file tree
Showing 14 changed files with 48 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/app/components/api/primengconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export class PrimeNGConfig {

overlayOptions: OverlayOptions = {};

csp = signal<{ nonce: string | undefined }>({ nonce: undefined });

filterMatchModeOptions = {
text: [FilterMatchMode.STARTS_WITH, FilterMatchMode.CONTAINS, FilterMatchMode.NOT_CONTAINS, FilterMatchMode.ENDS_WITH, FilterMatchMode.EQUALS, FilterMatchMode.NOT_EQUALS],
numeric: [FilterMatchMode.EQUALS, FilterMatchMode.NOT_EQUALS, FilterMatchMode.LESS_THAN, FilterMatchMode.LESS_THAN_OR_EQUAL_TO, FilterMatchMode.GREATER_THAN, FilterMatchMode.GREATER_THAN_OR_EQUAL_TO],
Expand Down
1 change: 1 addition & 0 deletions src/app/components/calendar/calendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3558,6 +3558,7 @@ export class Calendar implements OnInit, OnDestroy, ControlValueAccessor {
}

(<HTMLStyleElement>this.responsiveStyleElement).innerHTML = innerHTML;
DomHandler.setAttribute(this.responsiveStyleElement, 'nonce', this.config?.csp()?.nonce);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/app/components/carousel/carousel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,7 @@ export class Carousel implements AfterContentInit {
if (!this.carouselStyle) {
this.carouselStyle = this.renderer.createElement('style');
this.carouselStyle.type = 'text/css';
DomHandler.setAttribute(this.carouselStyle, 'nonce', this.config?.csp()?.nonce);
this.renderer.appendChild(this.document.head, this.carouselStyle);
}

Expand Down
1 change: 1 addition & 0 deletions src/app/components/confirmdialog/confirmdialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,7 @@ export class ConfirmDialog implements AfterContentInit, OnInit, OnDestroy {
}

this.styleElement.innerHTML = innerHTML;
DomHandler.setAttribute(this.styleElement, 'nonce', this.config?.csp()?.nonce);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/app/components/dialog/dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,7 @@ export class Dialog implements AfterContentInit, OnInit, OnDestroy {
}

this.renderer.setProperty(this.styleElement, 'innerHTML', innerHTML);
DomHandler.setAttribute(this.styleElement, 'nonce', this.config?.csp()?.nonce);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/app/components/dynamicdialog/dynamicdialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ export class DynamicDialogComponent implements AfterViewInit, OnDestroy {
}

this.renderer.setProperty(this.styleElement, 'innerHTML', innerHTML);
DomHandler.setAttribute(this.styleElement, 'nonce', this.primeNGConfig?.csp()?.nonce);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/app/components/galleria/galleria.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1074,6 +1074,7 @@ export class GalleriaThumbnails implements OnInit, AfterContentChecked, AfterVie
}

this.thumbnailsStyle.innerHTML = innerHTML;
DomHandler.setAttribute(this.thumbnailsStyle, 'nonce', this.galleria.config?.csp()?.nonce);
}

calculatePosition() {
Expand Down
1 change: 1 addition & 0 deletions src/app/components/orderlist/orderlist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,7 @@ export class OrderList implements AfterViewChecked, AfterContentInit {
}
`;
this.renderer.setProperty(this.styleElement, 'innerHTML', innerHTML);
DomHandler.setAttribute(this.styleElement, 'nonce', this.config?.csp()?.nonce);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/app/components/picklist/picklist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1651,6 +1651,7 @@ export class PickList implements AfterViewChecked, AfterContentInit {
}`;

this.renderer.setProperty(this.styleElement, 'innerHTML', innerHTML);
DomHandler.setAttribute(this.styleElement, 'nonce', this.config?.csp()?.nonce);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/app/components/table/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2975,6 +2975,7 @@ export class Table implements OnInit, AfterViewInit, AfterContentInit, Blockable
this.styleElement = this.renderer.createElement('style');
this.styleElement.type = 'text/css';
this.renderer.appendChild(this.document.head, this.styleElement);
DomHandler.setAttribute(this.styleElement, 'nonce', this.config?.csp()?.nonce);
}

getGroupRowsMeta() {
Expand Down
2 changes: 2 additions & 0 deletions src/app/components/toast/toast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { RippleModule } from 'primeng/ripple';
import { ObjectUtils, UniqueComponentId, ZIndexUtils } from 'primeng/utils';
import { Subscription } from 'rxjs';
import { ToastCloseEvent, ToastItemCloseEvent, ToastPositionType } from './toast.interface';
import { DomHandler } from 'primeng/dom';

@Component({
selector: 'p-toastItem',
Expand Down Expand Up @@ -480,6 +481,7 @@ export class Toast implements OnInit, AfterContentInit, OnDestroy {
}

this.renderer.setProperty(this.styleElement, 'innerHTML', innerHTML);
DomHandler.setAttribute(this.styleElement, 'nonce', this.config?.csp()?.nonce);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ import { RepositoryDoc } from './locale/repositorydoc';
import { SetLocaleDoc } from './locale/setlocaledoc';
import { RippleDoc } from './rippledoc';
import { ZIndexDoc } from './zindexdoc';
import { CspDoc } from './cspdoc';

@NgModule({
imports: [CommonModule, AppCodeModule, AppDocModule, RouterModule],
exports: [AppDocModule],
declarations: [FilterModeDoc, ImportDoc, RippleDoc, ZIndexDoc, NgxTranslateDoc, ApiDoc, RepositoryDoc, SetLocaleDoc]
declarations: [FilterModeDoc, ImportDoc, CspDoc, RippleDoc, ZIndexDoc, NgxTranslateDoc, ApiDoc, RepositoryDoc, SetLocaleDoc]
})
export class ConfigurationDocModule {}
21 changes: 21 additions & 0 deletions src/app/showcase/doc/configuration/cspdoc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Component, inject } from '@angular/core';
import { Code } from '@domain/code';
import { PrimeNGConfig } from 'primeng/api';

@Component({
selector: 'csp-doc',
template: `
<app-docsectiontext>
<p>The <i>nonce</i> value to use on dynamically generated style elements in core.</p>
</app-docsectiontext>
<app-code [code]="code" [hideToggleCode]="true"></app-code>
`
})
export class CspDoc {
code: Code = {
basic: `constructor(private primengConfig: PrimeNGConfig) {
this.config.csp.set({nonce: '...'});
}
`
};
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Component } from '@angular/core';
import { CspDoc } from '@doc/configuration/cspdoc';
import { FilterModeDoc } from '@doc/configuration/filtermodedoc';
import { ImportDoc } from '@doc/configuration/importdoc';
import { ApiDoc } from '@doc/configuration/locale/apidoc';
Expand Down Expand Up @@ -29,6 +30,17 @@ export class ConfigurationDemoComponent {
label: 'ZIndex',
component: ZIndexDoc
},
{
id: 'csp',
label: 'CSP',
children: [
{
id: 'csp-nonce',
label: 'Nonce',
component: CspDoc
}
]
},
{
id: 'filter-mode',
label: 'Filter Mode',
Expand Down

0 comments on commit 91695fc

Please sign in to comment.