@jamesbrightman/pretty-csp 0.0.0-stage → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
@@ -0,0 +1,61 @@
1
+ # pretty-csp
2
+
3
+ ## 0.2.0
4
+
5
+ Create readable policies in TypeScript, extend them with reusable rule sets, and test the requests they allow. This release simplifies the API around those workflows and includes breaking changes from 0.1.0.
6
+
7
+ ### Added and improved
8
+
9
+ - Add several rule sets in one call: `policy.withRuleSet([imageCdn, appApi])`. Single rule sets, readonly arrays, and array spreads are supported. The entire update is validated together, and invalid or duplicate rule sets leave the original policy unchanged.
10
+ - Use TypeScript throughout CLI starters and integration examples. The playground accepts type annotations, type-only imports, assertions, and `satisfies` expressions in policy declarations.
11
+ - Export `CspAssertions` from the testing entry point and generate `csp/vitest.d.ts` so starter tests type-check with custom matchers.
12
+ - Try policy creation, extension, and request tests in the interactive playground, with editable policy code and a formatted response-header preview.
13
+ - Expand the documentation with API examples, optional file layouts, and Node, Express, Next.js, and vanilla React integrations. Policies can live anywhere in an application and be kept in one file or split into rule-set modules.
14
+
15
+ ### Fixed
16
+
17
+ - Make `pretty-csp --help` print usage and exit successfully, matching the documented command.
18
+
19
+ ### Breaking changes
20
+
21
+ - Replace `sourceExpressions` and `NONCE_PLACEHOLDER` with named uppercase keyword exports.
22
+ - Remove delivery mode: the `mode` option, `init --mode`, and `policy.headerName`. Set the `Content-Security-Policy` response header explicitly with `policy.toHeader()`.
23
+ - Change policy snapshots to schema version 2 without a delivery-mode field.
24
+ - Generate TypeScript starter files only and remove `init --language`. The default CLI configuration is `csp.config.ts`.
25
+ - Remove header import: `pretty-csp import`, `importHeader`, `serializeImportedPolicy`, and `importedPolicyToDraft`. Create policies with `definePolicy()` and `defineRuleSet()`.
26
+ - Remove policy comparison: `pretty-csp diff`, `diffPolicies`, `PolicyDiff`, and `PolicyChange`. Review policy changes with snapshots and version control.
27
+ - Remove violation-report replay: `pretty-csp check-reports`, `policy.check()`, `parseViolationReports`, `replayReports`, `ViolationReport`, and `ReplayResult`. Test explicit requests with `policy.allows()` or the testing matchers.
28
+ - Remove `policy.toMetaContent()`. Deliver the policy through the response header.
29
+
30
+ ### Upgrade from 0.1.0
31
+
32
+ Replace source-expression imports using this mapping:
33
+
34
+ | Previous expression | Named export |
35
+ | ---------------------------------------------- | ------------------ |
36
+ | `sourceExpressions.self` | `SELF` |
37
+ | `sourceExpressions.none` | `NONE` |
38
+ | `sourceExpressions.nonce`, `NONCE_PLACEHOLDER` | `NONCE` |
39
+ | `sourceExpressions.strictDynamic` | `STRICT_DYNAMIC` |
40
+ | `sourceExpressions.reportSample` | `REPORT_SAMPLE` |
41
+ | `sourceExpressions.wasmUnsafeEval` | `WASM_UNSAFE_EVAL` |
42
+
43
+ Remove `mode` from existing configurations; it is rejected as an unknown property. Set the header directly:
44
+
45
+ ```ts
46
+ response.setHeader('Content-Security-Policy', policy.toHeader());
47
+ ```
48
+
49
+ If the policy contains `NONCE`, generate a fresh nonce per response and pass it to both `policy.toHeader({ nonce })` and the intended script elements. Reporting endpoint configuration remains supported for enforced policies.
50
+
51
+ For existing `.mjs` starters, rename policy and config files to `.ts` and use extensionless local imports, or select an existing config explicitly with `--config path/to/policy.ts`. Review and regenerate committed snapshots with `pretty-csp snapshot --update` or your test runner's snapshot update command.
52
+
53
+ ## 0.1.0
54
+
55
+ First public release as `@jamesbrightman/pretty-csp`. The installed command is `pretty-csp`.
56
+
57
+ ### Minor Changes
58
+
59
+ - 375b504: Expose `sourceExpressions` and named helpers for risky keywords and hashes in the first public API.
60
+ - Generate reviewable policy and rule-set files from supported CSP headers. Group hosts by registrable domain, preview with `--dry-run`, and use report-only mode by default.
61
+ - e97e9cb: Add an experimental CSP policy core, matching, audit, migration helpers, testing utilities, and CLI.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 James Brightman
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,113 @@
1
- # Temporary Holding Version
1
+ # pretty-csp
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ ![pretty-csp: CSP as code](website/static/img/pretty-csp-logo.svg)
4
+
5
+ `pretty-csp` is CSP as code for TypeScript applications. Keep your policy alongside application code, compose permissions from named rule sets, and test the requests you expect to allow or block. Generate the response header from that definition.
6
+
7
+ - **Readable.** Give each rule set a name, purpose, and owner. Use `policy.explain()` to trace a source to every rule set that contributed it.
8
+ - **Extendable.** Add reusable rule sets one at a time or together with `policy.withRuleSet([imageCdn, appApi])`. Remove them as features change, and write custom audit rules for your team's conventions.
9
+ - **Testable.** Assert which requests should pass, check advisory audit findings, and review policy snapshots in CI.
10
+
11
+ ## Define and compose a policy
12
+
13
+ Save this as `src/security/policy.ts`, or choose a location that suits your application. Commit it to version control with the feature code that needs these permissions.
14
+
15
+ ```ts
16
+ import {
17
+ definePolicy,
18
+ defineRuleSet,
19
+ SELF,
20
+ NONE,
21
+ } from '@jamesbrightman/pretty-csp';
22
+
23
+ export const imageCdn = defineRuleSet({
24
+ name: 'Image CDN',
25
+ purpose: 'Serve product images',
26
+ owner: 'web',
27
+ rules: { images: ['https://cdn.example.com'] },
28
+ });
29
+
30
+ const basePolicy = definePolicy({
31
+ base: {
32
+ default: [SELF],
33
+ images: [SELF],
34
+ objects: [NONE],
35
+ baseUri: [SELF],
36
+ frameAncestors: [NONE],
37
+ },
38
+ });
39
+
40
+ export const policy = basePolicy.withRuleSet(imageCdn);
41
+ ```
42
+
43
+ Use `basePolicy.withRuleSet([imageCdn, appApi])` to compose several rule sets in one call. Each rule set records the feature's permissions and purpose; `policy.explain()` shows which rule set contributed each source.
44
+
45
+ ## Test the permissions you intend
46
+
47
+ Import the same policy definition into your tests. Save this beside it as `src/security/policy.test.ts` and run it with `npx tsx --test src/security/policy.test.ts` after adding `tsx` as a dev dependency.
48
+
49
+ ```ts
50
+ import assert from 'node:assert/strict';
51
+ import { test } from 'node:test';
52
+ import { policy } from './policy';
53
+
54
+ const context = { origin: 'https://shop.example.com', redirectCount: 0 };
55
+
56
+ test('permits catalog images and blocks an unrelated origin', () => {
57
+ assert.equal(
58
+ policy.allows('img-src', 'https://cdn.example.com/product.jpg', context)
59
+ .status,
60
+ 'allowed',
61
+ );
62
+ assert.equal(
63
+ policy.allows('img-src', 'https://other.example.com/product.jpg', context)
64
+ .status,
65
+ 'blocked',
66
+ );
67
+ });
68
+ ```
69
+
70
+ Run policy tests with your application tests in CI. Review a feature's rule-set changes, request assertions, and policy snapshots together in the same pull request.
71
+
72
+ ## Generate the response header
73
+
74
+ Your server imports the policy and generates the header value when building a response:
75
+
76
+ ```ts
77
+ response.setHeader('Content-Security-Policy', policy.toHeader());
78
+ ```
79
+
80
+ The TypeScript definition is the source of truth. The header string is its generated output. See the [integration examples](https://jamesbrightman.github.io/pretty-csp/docs/integrations) for server and static-host setups.
81
+
82
+ Your server sets the header, renders per-response nonces when needed, and configures reporting. Request checks can return `indeterminate` when context is missing. Verify your rollout in the browsers you support.
83
+
84
+ ## Documentation
85
+
86
+ Install the package with `npm install @jamesbrightman/pretty-csp`, then follow the [Getting started guide](https://jamesbrightman.github.io/pretty-csp/docs/getting-started) to create your first policy.
87
+
88
+ - [Concepts](https://jamesbrightman.github.io/pretty-csp/docs/concepts) explains how rule sets merge and how to review a contribution.
89
+ - [Files and project structure](https://jamesbrightman.github.io/pretty-csp/docs/files) explains the one-file setup, optional rule-set modules, and every file created by `init`.
90
+ - [API reference](https://jamesbrightman.github.io/pretty-csp/docs/api) covers policy creation, extension, header generation, request checks, and audits.
91
+ - [Testing policies](https://jamesbrightman.github.io/pretty-csp/docs/testing) includes Node and Vitest examples.
92
+ - [Integrations](https://jamesbrightman.github.io/pretty-csp/docs/integrations) shows how to set the header and render a nonce.
93
+ - [CLI](https://jamesbrightman.github.io/pretty-csp/docs/cli) covers starter policies, header output, explanations, audits, and snapshots.
94
+
95
+ Use `pnpm docs:dev` for the local docs site or `pnpm docs:build` for the static output in `website/build`.
96
+
97
+ ## Development
98
+
99
+ The package and repository toolchain support Node.js 20 or newer. Use pnpm 10.17.1.
100
+
101
+ All new commits and PR titles use Conventional Commits. See [CONTRIBUTING.md](CONTRIBUTING.md) for the format and local hook setup.
102
+
103
+ ```sh
104
+ pnpm install --frozen-lockfile
105
+ pnpm format:check
106
+ pnpm lint
107
+ pnpm typecheck
108
+ pnpm test
109
+ pnpm test:coverage
110
+ pnpm build
111
+ ```
112
+
113
+ The [maintainer release guide](https://github.com/JamesBrightman/pretty-csp/blob/main/RELEASE.md) covers package verification and publishing.