Skip to content

Commit b13a525

Browse files
committed
Parse into a Map instead of an object, develop with Deno
1 parent 55266ef commit b13a525

File tree

11 files changed

+222
-3108
lines changed

11 files changed

+222
-3108
lines changed

CHANGELOG.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
### Changed
6+
7+
- **Breaking:** Parse into a `Map` instead of an object
8+
39
## 0.5.0 - 2024-01-26
410

511
### Fixed
612

7-
- Fix prototype pollution bug when parsing `__proto__`. See [#11](https://github.com/helmetjs/content-security-policy-parser/issues/11)
13+
- Fix prototype pollution bug when parsing `__proto__`. See
14+
[#11](https://github.com/helmetjs/content-security-policy-parser/issues/11)
815

916
### Removed
1017

@@ -32,7 +39,8 @@
3239

3340
### Added
3441

35-
- Added TypeScript type definitions. See [#3](https://github.com/helmetjs/content-security-policy-parser/pull/3)
42+
- Added TypeScript type definitions. See
43+
[#3](https://github.com/helmetjs/content-security-policy-parser/pull/3)
3644

3745
### Changed
3846

README.md

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,15 @@ Usage:
88
const parse = require("content-security-policy-parser");
99

1010
parse(
11-
"default-src 'self'; script-src 'unsafe-eval' scripts.com; object-src; style-src styles.biz",
11+
"default-src 'self'; script-src 'unsafe-eval' scripts.example; object-src; style-src styles.example",
1212
);
13-
/*
14-
{
15-
'default-src': ["'self'"],
16-
'script-src': ["'unsafe-eval'", 'scripts.com'],
17-
'object-src': [],
18-
'style-src': ['styles.biz']
19-
}
20-
*/
13+
// => Map(X) {
14+
// "default-src" => ["'self'"],
15+
// "script-src" => ["'unsafe-eval'", "scripts.example"],
16+
// "object-src" => [],
17+
// "style-src" => ["styles.example"],
18+
// }
2119
```
2220

23-
This module is considered "complete". I expect to continue maintenance if needed, but I don't plan to add features or make breaking changes.
21+
This module is considered "complete". I expect to continue maintenance if
22+
needed, but I don't plan to add features or make breaking changes.

build-npm.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { build, emptyDir } from "https://deno.land/x/[email protected]/mod.ts";
2+
3+
await emptyDir("./dist/npm");
4+
5+
await build({
6+
entryPoints: ["./mod.ts"],
7+
outDir: "./dist/npm",
8+
shims: { deno: "dev" },
9+
package: {
10+
name: "content-security-policy-parser",
11+
author: "Evan Hahn <[email protected]> (https://evanhahn.com)",
12+
description: "Parse Content Security Policy directives.",
13+
version: "0.5.0",
14+
license: "MIT",
15+
keywords: ["security", "content", "security", "policy", "csp", "parser"],
16+
homepage: "https://github.com/helmetjs/content-security-policy-parser",
17+
repository: {
18+
type: "git",
19+
url: "git://github.com/helmetjs/content-security-policy-parser.git",
20+
},
21+
bugs: {
22+
url: "https://github.com/helmetjs/content-security-policy-parser/issues",
23+
24+
},
25+
engines: {
26+
node: ">=18.0.0",
27+
},
28+
},
29+
postBuild() {
30+
Deno.copyFileSync("LICENSE", "./dist/npm/LICENSE");
31+
Deno.copyFileSync("README.md", "./dist/npm/README.md");
32+
Deno.copyFileSync("CHANGELOG.md", "./dist/npm/CHANGELOG.md");
33+
},
34+
});

deno.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"tasks": {
3+
"npm": "deno run -A build-npm.ts",
4+
"clean": "rm -rf dist",
5+
"test": "deno task clean && deno task test:fmt && deno task test:lint && deno test",
6+
"test:fmt": "deno fmt --check",
7+
"test:lint": "deno lint"
8+
},
9+
"compilerOptions": {
10+
"noFallthroughCasesInSwitch": true,
11+
"noImplicitReturns": true,
12+
"noUnusedLocals": true,
13+
"noUnusedParameters": true,
14+
"noUncheckedIndexedAccess": true,
15+
"useUnknownInCatchVariables": true
16+
},
17+
"fmt": {
18+
"exclude": ["dist"]
19+
},
20+
"lint": {
21+
"exclude": ["dist"]
22+
}
23+
}

deno.lock

Lines changed: 117 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.ts

Lines changed: 0 additions & 26 deletions
This file was deleted.

mod.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
type ParsedContentSecurityPolicy = Map<string, string[]>;
2+
3+
export default function parseContentSecurityPolicy(
4+
policy: string,
5+
): ParsedContentSecurityPolicy {
6+
const result: ParsedContentSecurityPolicy = new Map();
7+
policy.split(";").forEach((directive) => {
8+
const [directiveKey, ...directiveValue] = directive.trim().split(/\s+/g);
9+
if (
10+
directiveKey &&
11+
!result.has(directiveKey)
12+
) {
13+
result.set(directiveKey, directiveValue);
14+
}
15+
});
16+
return result;
17+
}

0 commit comments

Comments
 (0)