@localnerve/csp-hashes 1.1.0 → 2.0.1
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/{dist/index.mjs → index.js} +0 -0
- package/{dist/lib → lib}/index.js +39 -29
- package/{dist/lib → lib}/removeCspMeta.js +10 -13
- package/package.json +8 -14
- package/readme.md +3 -3
- package/dist/index.js +0 -24
|
File without changes
|
|
@@ -1,20 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.default = exports.hashstream = hashstream;
|
|
7
|
-
Object.defineProperty(exports, "removeCspMeta", {
|
|
8
|
-
enumerable: true,
|
|
9
|
-
get: function () {
|
|
10
|
-
return _removeCspMeta.removeCspMeta;
|
|
11
|
-
}
|
|
12
|
-
});
|
|
13
|
-
var _stream = require("stream");
|
|
14
|
-
var _crypto = _interopRequireDefault(require("crypto"));
|
|
15
|
-
var _cheerio = _interopRequireDefault(require("cheerio"));
|
|
16
|
-
var _removeCspMeta = require("./removeCspMeta.js");
|
|
17
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
18
1
|
/**
|
|
19
2
|
* CSP Hashes.
|
|
20
3
|
*
|
|
@@ -24,6 +7,10 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
|
|
|
24
7
|
* Copyright (c) 2022 Alex Grant (@localnerve), LocalNerve LLC
|
|
25
8
|
* Licensed under the MIT license.
|
|
26
9
|
*/
|
|
10
|
+
import { Transform } from 'stream';
|
|
11
|
+
import crypto from 'crypto';
|
|
12
|
+
import cheerio from 'cheerio';
|
|
13
|
+
export { removeCspMeta } from './removeCspMeta.js';
|
|
27
14
|
|
|
28
15
|
/**
|
|
29
16
|
* Collect all CSP Hashes and fill the given `hashes` structure.
|
|
@@ -32,21 +19,33 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
|
|
|
32
19
|
* @param {Buffer} html - The html content
|
|
33
20
|
* @param {Object} hashes - The hash structure to fill
|
|
34
21
|
*/
|
|
35
|
-
function collectHashes(hashFn, html, hashes) {
|
|
36
|
-
const $ =
|
|
22
|
+
function collectHashes (hashFn, html, hashes) {
|
|
23
|
+
const $ = cheerio.load(html);
|
|
24
|
+
|
|
37
25
|
Object.keys(hashes).forEach(what => {
|
|
38
|
-
hashes[what].elements = $(`${what}:not([src])`).map(
|
|
26
|
+
hashes[what].elements = $(`${what}:not([src])`).map(
|
|
27
|
+
(i, el) => hashFn($(el).html())
|
|
28
|
+
).toArray();
|
|
39
29
|
});
|
|
40
|
-
|
|
30
|
+
|
|
31
|
+
hashes.style.attributes.push(
|
|
32
|
+
...$('[style]').map((i, el) => hashFn($(el).attr('style'))).toArray()
|
|
33
|
+
);
|
|
34
|
+
|
|
41
35
|
const eventHandlerRe = /^on/i;
|
|
42
36
|
const jsUrlRe = /^javascript:/i;
|
|
37
|
+
|
|
43
38
|
$('*').each(function (i, el) {
|
|
44
39
|
for (const attrName in el.attribs) {
|
|
45
40
|
if (eventHandlerRe.test(attrName)) {
|
|
46
|
-
hashes.script.attributes.push(
|
|
41
|
+
hashes.script.attributes.push(
|
|
42
|
+
hashFn(el.attribs[attrName])
|
|
43
|
+
);
|
|
47
44
|
}
|
|
48
45
|
if (jsUrlRe.test(el.attribs[attrName])) {
|
|
49
|
-
hashes.script.attributes.push(
|
|
46
|
+
hashes.script.attributes.push(
|
|
47
|
+
hashFn(el.attribs[attrName].split(jsUrlRe)[1])
|
|
48
|
+
);
|
|
50
49
|
}
|
|
51
50
|
}
|
|
52
51
|
});
|
|
@@ -62,50 +61,61 @@ function collectHashes(hashFn, html, hashes) {
|
|
|
62
61
|
* @param {Boolean} [options.replace] - True if callback is used for meta html replacements, defaults to false.
|
|
63
62
|
* @returns Transform object stream to process Vinyl objects.
|
|
64
63
|
*/
|
|
65
|
-
function hashstream({
|
|
64
|
+
export function hashstream ({
|
|
66
65
|
algo = 'sha256',
|
|
67
66
|
replace = false,
|
|
68
67
|
callback = null
|
|
69
68
|
} = {}) {
|
|
69
|
+
|
|
70
70
|
if (!/^sha(256|384|512)$/.test(algo)) {
|
|
71
71
|
throw new Error('algo option must be one of "sha256", "sha384", or "sha512" only.');
|
|
72
72
|
}
|
|
73
|
+
|
|
73
74
|
if (typeof callback !== 'function') {
|
|
74
75
|
throw new Error('callback option must be a valid function.');
|
|
75
76
|
}
|
|
76
|
-
|
|
77
|
+
|
|
78
|
+
const createHash = r => crypto.createHash(algo).update(r).digest('base64');
|
|
77
79
|
const formatHash = h => `'${algo}-${h}'`;
|
|
78
80
|
const makeCSPHash = s => formatHash(createHash(s));
|
|
79
|
-
|
|
81
|
+
|
|
82
|
+
const transformObjectStream = new Transform({
|
|
80
83
|
objectMode: true,
|
|
81
84
|
transform: (vinyl, enc, done) => {
|
|
82
85
|
const path = vinyl.path;
|
|
83
86
|
const content = vinyl.contents;
|
|
87
|
+
|
|
84
88
|
const hashes = {
|
|
85
89
|
script: {
|
|
86
90
|
elements: [],
|
|
87
91
|
attributes: [],
|
|
88
|
-
get all() {
|
|
92
|
+
get all () {
|
|
89
93
|
return this.elements.concat(this.attributes);
|
|
90
94
|
}
|
|
91
95
|
},
|
|
92
96
|
style: {
|
|
93
97
|
elements: [],
|
|
94
98
|
attributes: [],
|
|
95
|
-
get all() {
|
|
99
|
+
get all () {
|
|
96
100
|
return this.elements.concat(this.attributes);
|
|
97
101
|
}
|
|
98
102
|
}
|
|
99
103
|
};
|
|
104
|
+
|
|
100
105
|
collectHashes(makeCSPHash, content, hashes);
|
|
106
|
+
|
|
101
107
|
if (replace) {
|
|
102
108
|
const s = callback(path, hashes, content.toString());
|
|
103
109
|
vinyl.contents = Buffer.from(s, enc);
|
|
104
110
|
} else {
|
|
105
111
|
callback(path, hashes);
|
|
106
112
|
}
|
|
113
|
+
|
|
107
114
|
done(null, vinyl);
|
|
108
115
|
}
|
|
109
116
|
});
|
|
117
|
+
|
|
110
118
|
return transformObjectStream;
|
|
111
|
-
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export { hashstream as default }
|
|
@@ -1,10 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.default = exports.removeCspMeta = removeCspMeta;
|
|
7
|
-
var _stream = require("stream");
|
|
8
1
|
/**
|
|
9
2
|
* removeCspMeta.js
|
|
10
3
|
*
|
|
@@ -14,16 +7,18 @@ var _stream = require("stream");
|
|
|
14
7
|
* Copyright (c) 2022 Alex Grant (@localnerve), LocalNerve LLC
|
|
15
8
|
* Licensed under the MIT license.
|
|
16
9
|
*/
|
|
10
|
+
import { Transform } from 'stream';
|
|
17
11
|
|
|
18
|
-
function removeCspMeta() {
|
|
19
|
-
return new
|
|
12
|
+
export function removeCspMeta () {
|
|
13
|
+
return new Transform({
|
|
20
14
|
objectMode: true,
|
|
21
15
|
transform: (vinyl, enc, done) => {
|
|
22
|
-
var _vinyl$contents;
|
|
23
16
|
let e = null;
|
|
24
|
-
const input = vinyl
|
|
17
|
+
const input = vinyl?.contents?.toString();
|
|
25
18
|
if (input) {
|
|
26
|
-
const output = input.replace(
|
|
19
|
+
const output = input.replace(
|
|
20
|
+
/("?Content-Security-Policy"?)(\s+)(content=")([^"]+)"/i, '$1$2$3$2"'
|
|
21
|
+
);
|
|
27
22
|
vinyl.contents = Buffer.from(output, enc);
|
|
28
23
|
} else {
|
|
29
24
|
e = new Error('removeCspMeta could not get Vinyl object file contents');
|
|
@@ -32,4 +27,6 @@ function removeCspMeta() {
|
|
|
32
27
|
done(e, vinyl);
|
|
33
28
|
}
|
|
34
29
|
});
|
|
35
|
-
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export { removeCspMeta as default }
|
package/package.json
CHANGED
|
@@ -1,29 +1,23 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@localnerve/csp-hashes",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.1",
|
|
4
4
|
"description": "Flexible library to generate CSP hashes",
|
|
5
|
-
"main": "
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"type": "module",
|
|
6
7
|
"exports": {
|
|
7
|
-
"import": "./
|
|
8
|
-
"
|
|
9
|
-
"default": "./dist/index.js"
|
|
8
|
+
"import": "./index.js",
|
|
9
|
+
"default": "./index.js"
|
|
10
10
|
},
|
|
11
11
|
"scripts": {
|
|
12
12
|
"lint": "eslint .",
|
|
13
|
-
"transpile": "rimraf ./dist && babel --out-dir ./dist index.js && babel --out-dir ./dist/lib ./lib",
|
|
14
|
-
"prepublishBuild": "node -e 'try{require(\"fs\").copyFileSync(\"./index.js\", \"./dist/index.mjs\");}catch(e){}'",
|
|
15
|
-
"prepublishOnly": "npm run transpile && npm run prepublishBuild",
|
|
16
13
|
"pretest": "node -e 'try{require(\"fs\").symlinkSync(\"../lib\", \"./__tests__/lib\");}catch(e){}'",
|
|
17
14
|
"test": "jest",
|
|
18
15
|
"test:debug": "node --inspect-brk ./node_modules/.bin/jest"
|
|
19
16
|
},
|
|
20
17
|
"devDependencies": {
|
|
21
|
-
"@babel/
|
|
22
|
-
"
|
|
23
|
-
"
|
|
24
|
-
"eslint": "^8.26.0",
|
|
25
|
-
"jest": "^29.2.2",
|
|
26
|
-
"rimraf": "^3.0.2",
|
|
18
|
+
"@babel/preset-env": "^7.20.2",
|
|
19
|
+
"eslint": "^8.30.0",
|
|
20
|
+
"jest": "^29.3.1",
|
|
27
21
|
"vinyl": "^2.2.1"
|
|
28
22
|
},
|
|
29
23
|
"dependencies": {
|
package/readme.md
CHANGED
|
@@ -37,13 +37,13 @@ Stream hashstream ({
|
|
|
37
37
|
```
|
|
38
38
|
|
|
39
39
|
### removeCspMeta
|
|
40
|
-
This library also exports a convenience helper method, `removeCspMeta` that is useful for some types of development builds. This method returns a stream that operates on [Vinyl](https://github.com/gulpjs/vinyl) objects and removes any `Content-Security-Policy` content found in the files.
|
|
40
|
+
This library also exports a convenience helper method, `removeCspMeta` that is useful for some types of development builds. This method takes no options and returns a stream that operates on [Vinyl](https://github.com/gulpjs/vinyl) objects and removes any `Content-Security-Policy` content found in the files.
|
|
41
41
|
|
|
42
42
|
```
|
|
43
43
|
Stream removeCspMeta ()
|
|
44
44
|
```
|
|
45
45
|
|
|
46
|
-
### Options
|
|
46
|
+
### Hashstream Options
|
|
47
47
|
|
|
48
48
|
+ {Function} **callback** - Required - A [function](#callback-function) to process the hashes. Receives file contents and must return new file contents if `replace` option is true.
|
|
49
49
|
+ {Boolean} **\[replace\]** - Optional - Defaults to `false`, set to true to indicate your `callback` function returns new file contents to replace the original.
|
|
@@ -138,7 +138,7 @@ export function cspMetaTags (settings) {
|
|
|
138
138
|
}
|
|
139
139
|
```
|
|
140
140
|
|
|
141
|
-
### Build
|
|
141
|
+
### Build Step to Remove CSP Meta Tag Content
|
|
142
142
|
In this example, a build step removes any content from a `Content-Security-Policy` in a development build that wishes to ignore it.
|
|
143
143
|
|
|
144
144
|
```javascript
|
package/dist/index.js
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
Object.defineProperty(exports, "default", {
|
|
7
|
-
enumerable: true,
|
|
8
|
-
get: function () {
|
|
9
|
-
return _index.hashstream;
|
|
10
|
-
}
|
|
11
|
-
});
|
|
12
|
-
Object.defineProperty(exports, "hashstream", {
|
|
13
|
-
enumerable: true,
|
|
14
|
-
get: function () {
|
|
15
|
-
return _index.hashstream;
|
|
16
|
-
}
|
|
17
|
-
});
|
|
18
|
-
Object.defineProperty(exports, "removeCspMeta", {
|
|
19
|
-
enumerable: true,
|
|
20
|
-
get: function () {
|
|
21
|
-
return _index.removeCspMeta;
|
|
22
|
-
}
|
|
23
|
-
});
|
|
24
|
-
var _index = require("./lib/index.js");
|