Skip to content

Commit

Permalink
Fix require-trusted-types-for
Browse files Browse the repository at this point in the history
  • Loading branch information
dragonbear-os committed Sep 9, 2022
1 parent c407570 commit fa7c4b0
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/yellow-apricots-tan.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@strict-csp/builder": patch
---

Make require-trusted-types-for produce a valid header
6 changes: 6 additions & 0 deletions packages/builder/src/builder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,10 @@ describe("CSP Builder", () => {
const expectBuilder = new CspBuilder().withStrictDynamic(hashes);
expect(expectBuilder.csp()).toEqual(fixtureBuilder.csp());
});

it("can quotes script in require-trusted-types-for correctly", () => {
const fixtureBuilder = new CspBuilder(`require-trusted-types-for 'script';`);
const expectBuilder = new CspBuilder().withDirectives({"require-trusted-types-for": ["script"]});
expect(expectBuilder.toString()).toEqual(fixtureBuilder.toString());
});
});
6 changes: 3 additions & 3 deletions packages/builder/src/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class CspBuilder {
const isCspHeader = param[0] === CSP_HEADER;
const isCspReportOnlyHeader = param[0] === CSP_HEADER_REPORT_ONLY;
if (!(isCspHeader || isCspReportOnlyHeader)) {
this._csp = empty;
this._csp = {...empty};
} else {
this._csp = {
directives: fromCspContent(param[1]),
Expand All @@ -58,7 +58,7 @@ export class CspBuilder {
};
}
} else {
this._csp = empty;
this._csp = {...empty};
}
}

Expand Down Expand Up @@ -212,7 +212,7 @@ export class CspBuilder {
}

public reset() {
this._csp = empty;
this._csp = {...empty};
}

public isEmpty() {
Expand Down
1 change: 1 addition & 0 deletions packages/builder/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const singleQuotify = (directiveValue: string) => `'${directiveValue}'`;

const isLiteralDirectiveValue = (directiveValue: string) => {
const c1 = [
"script",
"strict-dynamic",
"report-sample",
"self",
Expand Down

0 comments on commit fa7c4b0

Please # to comment.