@localnerve/csp-hashes 3.0.3 → 3.1.2
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/index.js +1 -1
- package/lib/index.js +24 -5
- package/package.json +3 -3
- package/readme.md +14 -0
package/index.js
CHANGED
package/lib/index.js
CHANGED
|
@@ -51,6 +51,29 @@ function collectHashes (hashFn, html, hashes) {
|
|
|
51
51
|
});
|
|
52
52
|
}
|
|
53
53
|
|
|
54
|
+
/**
|
|
55
|
+
* Makes a CSP hash string for the given input and algorithm.
|
|
56
|
+
*
|
|
57
|
+
* @param {String} algo - The hash algorithm to use.
|
|
58
|
+
* @param {String} input - A string of text to generate a hash for.
|
|
59
|
+
* @returns {String} The hash value of the given input.
|
|
60
|
+
*/
|
|
61
|
+
function makeCspHash (algo, input) {
|
|
62
|
+
const createHash = r => crypto.createHash(algo).update(r).digest('base64');
|
|
63
|
+
const formatHash = h => `'${algo}-${h}'`;
|
|
64
|
+
return formatHash(createHash(input));
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Makes a CSP hash string for the given input and algorithm.
|
|
68
|
+
*
|
|
69
|
+
* @param {String} input - A string of text to generate a hash for.
|
|
70
|
+
* @param {String} [algo] - hash algorithm, default sha256. Can be sha384, sha512.
|
|
71
|
+
* @returns
|
|
72
|
+
*/
|
|
73
|
+
export function createCspHash(input, algo = 'sha256') {
|
|
74
|
+
return makeCspHash(algo, input);
|
|
75
|
+
}
|
|
76
|
+
|
|
54
77
|
/**
|
|
55
78
|
* hashstream
|
|
56
79
|
* Accepts the processing options and returns the Vinyl transform object stream.
|
|
@@ -75,10 +98,6 @@ export function hashstream ({
|
|
|
75
98
|
throw new Error('callback option must be a valid function.');
|
|
76
99
|
}
|
|
77
100
|
|
|
78
|
-
const createHash = r => crypto.createHash(algo).update(r).digest('base64');
|
|
79
|
-
const formatHash = h => `'${algo}-${h}'`;
|
|
80
|
-
const makeCSPHash = s => formatHash(createHash(s));
|
|
81
|
-
|
|
82
101
|
const transformObjectStream = new Transform({
|
|
83
102
|
objectMode: true,
|
|
84
103
|
transform: (vinyl, enc, done) => {
|
|
@@ -102,7 +121,7 @@ export function hashstream ({
|
|
|
102
121
|
}
|
|
103
122
|
};
|
|
104
123
|
|
|
105
|
-
collectHashes(
|
|
124
|
+
collectHashes(makeCspHash.bind(null, algo), content, hashes);
|
|
106
125
|
|
|
107
126
|
if (replace) {
|
|
108
127
|
const s = callback(path, hashes, content.toString());
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@localnerve/csp-hashes",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.1.2",
|
|
4
4
|
"description": "Flexible library to generate CSP hashes",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -16,8 +16,8 @@
|
|
|
16
16
|
},
|
|
17
17
|
"devDependencies": {
|
|
18
18
|
"@babel/preset-env": "^7.22.9",
|
|
19
|
-
"eslint": "^8.
|
|
20
|
-
"jest": "^29.6.
|
|
19
|
+
"eslint": "^8.46.0",
|
|
20
|
+
"jest": "^29.6.2",
|
|
21
21
|
"vinyl": "^3.0.0"
|
|
22
22
|
},
|
|
23
23
|
"dependencies": {
|
package/readme.md
CHANGED
|
@@ -41,6 +41,20 @@ Stream hashstream ({
|
|
|
41
41
|
})
|
|
42
42
|
```
|
|
43
43
|
|
|
44
|
+
See [`hashstream options`](#hashstream-options) for a detailed explanation of the input options.
|
|
45
|
+
|
|
46
|
+
### createCspHash
|
|
47
|
+
This library exports the helper method it uses to make CSP formatted hashes. This is useful if you have a picece of code you need to hash and place into your hash list outside the scope of the page as rendered.
|
|
48
|
+
|
|
49
|
+
```
|
|
50
|
+
String createCSPHash(inputString, algo = 'sha256')
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
+ {String} **inputString** - Required - The input content to hash.
|
|
54
|
+
+ {String} **\[algo\]** - Optional - Defaults to `'sha256'`, can be one of 'sha256', 'sha384' or 'sha512'.
|
|
55
|
+
|
|
56
|
+
Returns a ready to use csp hash string (with quotes) in the form of `'sha256-d3ii1Pel57UO62xosCMNgTaZJhJa87Gd/X6e7UdlEU8='`.
|
|
57
|
+
|
|
44
58
|
### removeCspMeta
|
|
45
59
|
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.
|
|
46
60
|
|