Skip to content

Commit 1f9ffb3

Browse files
committed
feat: ✨ Runtime directive mapping and schema
Added the ability to get all available options for any directive and for those that are dynamic, provides a schema that lets you scaffold a compliant string.
1 parent d4cc7f5 commit 1f9ffb3

File tree

4 files changed

+257
-98
lines changed

4 files changed

+257
-98
lines changed

README.md

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
[![Libraries.io dependency status for latest release](https://img.shields.io/librariesio/release/npm/csp-typed-directives?label=Deps&style=flat-square)](https://libraries.io/npm/csp-typed-directives)
99
[![Rate on Openbase](https://badges.openbase.com/js/rating/csp-typed-directives.svg)](https://openbase.com/js/csp-typed-directives?utm_source=embedded&utm_medium=badge&utm_campaign=rate-badge)
1010
[![Test](https://github.com/josh-hemphill/csp-typed-directives/actions/workflows/test.yml/badge.svg)](https://github.com/josh-hemphill/csp-typed-directives/actions/workflows/test.yml)
11-
[![Release](https://github.com/josh-hemphill/csp-typed-directives/actions/workflows/release.yml/badge.svg)](https://github.com/josh-hemphill/csp-typed-directives/actions/workflows/release.yml)
11+
[![Build Release](https://github.com/josh-hemphill/csp-typed-directives/actions/workflows/release.yml/badge.svg)](https://github.com/josh-hemphill/csp-typed-directives/actions/workflows/release.yml)
1212

1313
Provides type information for all CSP directives and related headers' directives; as well as a basic utility funtion that helps convert the typed properties to key/values of each header content's policy string.
1414

@@ -31,7 +31,7 @@ Either pass your CSP directives in at instatiation, or after.
3131
```javascript
3232
const { CspDirectives } = require('csp-typed-directives')
3333
// or ESM
34-
import { CspDirectives } from "module";
34+
import { CspDirectives } from 'csp-typed-directives';
3535

3636
const cspD = new CspDirectives({
3737
'child-src': 'none',
@@ -57,7 +57,7 @@ The default configuration produces a referrer policy of `strict-origin-when-cros
5757
```typescript
5858
const { CspDirectives } = require('csp-typed-directives')
5959
// or ESM
60-
import { CspDirectives } from "module";
60+
import { CspDirectives } from 'csp-typed-directives';
6161

6262
const reportTo: ReportTo[] = [
6363
{
@@ -95,6 +95,46 @@ csp.headers === {
9595

9696
For reading up on the descriptions and implications of all directives see [Mozilla's CSP documentation](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy)
9797

98+
### Iterate over all available directives
99+
100+
This also provides a map of constants of every available directive name and the category(s) of souces/directives it can be assigned.
101+
102+
```ts
103+
import { directiveNamesList } from 'csp-typed-directives';
104+
105+
const myDirectives = directiveNamesList
106+
.reduce((acc,v) => {
107+
// ! Warning: not all directives allow the full set of directive parameters
108+
// Though as of 5/6/2021 they all support the 'none' directive, though would be kind of pointless to do this.
109+
acc[v] = 'none'
110+
},{})
111+
```
112+
113+
```ts
114+
import { DirectiveMap } from 'csp-typed-directives';
115+
116+
let myDirectives = DirectiveMap.get('report-to')
117+
myDirectives === [
118+
{
119+
displayName: 'Any String',
120+
consumes: {
121+
'String': 'string',
122+
},
123+
compose: (args: {String:string}) => args.String,
124+
},
125+
]
126+
127+
myDirectives = DirectiveMap.get('require-sri-for')
128+
myDirectives === [
129+
'script', 'style', 'script style'
130+
]
131+
132+
myDirectives = DirectiveMap.get('upgrade-insecure-requests')
133+
myDirectives === [
134+
true, false,
135+
]
136+
```
137+
98138
## Changelog
99139

100140
Take a look at the [CHANGELOG.md](https://github.com/josh-hemphill/csp-typed-directives/tree/latest/CHANGELOG.md).

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,15 @@
3535
"url": "git+https://github.com/josh-hemphill/csp-typed-directives.git"
3636
},
3737
"keywords": [
38-
"vite-plugin",
3938
"CSP",
4039
"CSPs",
40+
"content-security-policy",
4141
"header",
4242
"security",
43-
"generate"
43+
"generate",
44+
"directive",
45+
"types",
46+
"typescript"
4447
],
4548
"maintainers": [
4649
{

0 commit comments

Comments
 (0)