Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for Content-Security-Policy through Nonce injection. #184

Merged
merged 2 commits into from
May 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,9 @@ this.printService.printStyle = styleSheet;
this.printService.styleSheetFile = fileLocation;
```

## Content-Security-Policy (CSP) Support
If Angular is configured to use a [CSP Nonce](https://angular.io/api/core/CSP_NONCE), ngx-print will automatically inject the `[printStyle]` CSS rules with this Nonce authorization.

## Contributors :1st_place_medal:

Huge thanks to: [deeplotia](https://github.com/deeplotia) , [Ben L](https://github.com/broem) , [Gavyn McKenzie](https://github.com/gavmck) , [silenceway](https://github.com/silenceway), [Muhammad Ahsan Ayaz](https://github.com/AhsanAyaz), [Core121](https://github.com/Core121) and to all `ngx-print` users
Expand Down
9 changes: 6 additions & 3 deletions src/lib/ngx-print.base.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Injectable } from "@angular/core";
import { CSP_NONCE, Inject, Injectable, Optional } from "@angular/core";
import { PrintOptions } from "./print-options";

@Injectable({
Expand All @@ -9,6 +9,8 @@ export class PrintBase {
private _printStyle: string[] = [];
private _styleSheetFile: string = '';

constructor(@Inject(CSP_NONCE) @Optional() private nonce?: string | null) {}

//#region Getters and Setters
/**
* Sets the print styles based on the provided values.
Expand All @@ -34,7 +36,8 @@ export class PrintBase {
* -join/replace to transform an array objects to css-styled string
*/
public returnStyleValues() {
return `<style> ${this._printStyle.join(' ').replace(/,/g, ';')} </style>`;
const styleNonce = this.nonce ? ` nonce="${this.nonce}"` : '';
return `<style${styleNonce}> ${this._printStyle.join(' ').replace(/,/g, ';')} </style>`;
}

/**
Expand Down Expand Up @@ -196,7 +199,7 @@ export class PrintBase {
links = this.getElementTag('link');
}

// If the openNewTab option is set to true, then set the popOut option to an empty string.
// If the openNewTab option is set to true, then set the popOut option to an empty string.
// This will cause the print dialog to open in a new tab.
if (printOptions.openNewTab) {
popOut = '';
Expand Down
9 changes: 6 additions & 3 deletions src/lib/ngx-print.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';

import { NgxPrintService } from './ngx-print.service';
import { Component } from '@angular/core';
import { Component, CSP_NONCE } from '@angular/core';
import { PrintOptions } from './print-options';

const testNonce = 'dummy-nonce-value';

@Component({
template: `
<div id="print-section">
Expand Down Expand Up @@ -57,7 +60,7 @@ describe('NgxPrintService', () => {
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [TestNgxPrintServiceComponent],
providers: [NgxPrintService]
providers: [{ provide: CSP_NONCE, useValue: testNonce }, NgxPrintService]
});
service = TestBed.inject(NgxPrintService);
// Create a fixture object (that is going to allows us to create an instance of that component)
Expand Down Expand Up @@ -178,6 +181,6 @@ describe('NgxPrintService', () => {
service.printStyle = styleSheet;

// Ensure the print styles are correctly formatted in the document
expect(service.returnStyleValues()).toEqual('<style> h2{border:solid 1px} h1{color:red;border:1px solid} </style>');
expect(service.returnStyleValues()).toEqual('<style nonce="' + testNonce + '"> h2{border:solid 1px} h1{color:red;border:1px solid} </style>');
});
});