@react-foundry/fastify-harden 0.1.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/LICENSE +22 -0
- package/README.md +62 -0
- package/dist/common.d.ts +3 -0
- package/dist/common.js +7 -0
- package/dist/common.mjs +2 -0
- package/dist/content-security-policy.d.ts +19 -0
- package/dist/content-security-policy.js +61 -0
- package/dist/content-security-policy.mjs +57 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +56 -0
- package/dist/index.mjs +50 -0
- package/dist/permissions-policy.d.ts +4 -0
- package/dist/permissions-policy.js +83 -0
- package/dist/permissions-policy.mjs +79 -0
- package/package.json +41 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (C) 2019-2025 Crown Copyright
|
|
4
|
+
Copyright (C) 2019-2026 Daniel A.C. Martin
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
7
|
+
this software and associated documentation files (the "Software"), to deal in
|
|
8
|
+
the Software without restriction, including without limitation the rights to
|
|
9
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
|
10
|
+
of the Software, and to permit persons to whom the Software is furnished to do
|
|
11
|
+
so, subject to the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
React Foundry - Fastify Harden
|
|
2
|
+
==============================
|
|
3
|
+
|
|
4
|
+
Fastify plugin for extra cyber-security hardening.
|
|
5
|
+
|
|
6
|
+
This adds extra HTTP headers to tell the browser to run in a stricter fashion in
|
|
7
|
+
order to prevent a variety of exploits. It also censors error messages on any
|
|
8
|
+
_Internal Server Errors_ that occur in production (they can still be found in
|
|
9
|
+
the logs.
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
Using this package
|
|
13
|
+
------------------
|
|
14
|
+
|
|
15
|
+
First install the package into your project:
|
|
16
|
+
|
|
17
|
+
```shell
|
|
18
|
+
npm install -S @react-foundry/fastify-harden
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Then use it in your code as follows:
|
|
22
|
+
|
|
23
|
+
```js
|
|
24
|
+
import Fastify from 'fastify';
|
|
25
|
+
import fastifyHarden from '@react-foundry/fastify-harden';
|
|
26
|
+
|
|
27
|
+
const httpd = Fastify({});
|
|
28
|
+
|
|
29
|
+
await httpd.register(fastifyHarden, {
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
await httpd.listen({ port: 8080 });
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
Working on this package
|
|
37
|
+
-----------------------
|
|
38
|
+
|
|
39
|
+
Before working on this package you must install its dependencies using
|
|
40
|
+
the following command:
|
|
41
|
+
|
|
42
|
+
```shell
|
|
43
|
+
pnpm install
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
### Building
|
|
48
|
+
|
|
49
|
+
Build the package by compiling the source code.
|
|
50
|
+
|
|
51
|
+
```shell
|
|
52
|
+
npm run build
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
### Clean-up
|
|
57
|
+
|
|
58
|
+
Remove any previously built files.
|
|
59
|
+
|
|
60
|
+
```shell
|
|
61
|
+
npm run clean
|
|
62
|
+
```
|
package/dist/common.d.ts
ADDED
package/dist/common.js
ADDED
package/dist/common.mjs
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export type Source = '\'none\'' | '\'self\'' | string;
|
|
2
|
+
export type Sources = Source | Source[];
|
|
3
|
+
export type CSPOptions = {
|
|
4
|
+
dev?: boolean;
|
|
5
|
+
formAction?: Sources;
|
|
6
|
+
frameAncestors?: Sources;
|
|
7
|
+
nonce?: string;
|
|
8
|
+
};
|
|
9
|
+
export type CSPResult = {
|
|
10
|
+
policy: string;
|
|
11
|
+
frameOptions?: string;
|
|
12
|
+
};
|
|
13
|
+
type CSP = (x: CSPOptions) => CSPResult;
|
|
14
|
+
export declare const none: Source;
|
|
15
|
+
export declare const self: Source;
|
|
16
|
+
export declare const unsafeEval: Source;
|
|
17
|
+
export declare const unsafeInline: Source;
|
|
18
|
+
export declare const contentSecurityPolicy: CSP;
|
|
19
|
+
export {};
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.contentSecurityPolicy = exports.unsafeInline = exports.unsafeEval = exports.self = exports.none = void 0;
|
|
4
|
+
const common_1 = require("./common");
|
|
5
|
+
exports.none = "'none'";
|
|
6
|
+
exports.self = "'self'";
|
|
7
|
+
exports.unsafeEval = "'unsafe-eval'";
|
|
8
|
+
exports.unsafeInline = "'unsafe-inline'";
|
|
9
|
+
const contentSecurityPolicy = ({ dev = process.env.NODE_ENV === 'development', formAction: _formAction, frameAncestors: _frameAncestors, nonce: _nonce }) => {
|
|
10
|
+
const formAction = (Array.isArray(_formAction)
|
|
11
|
+
? _formAction
|
|
12
|
+
: [_formAction]).filter(common_1.id);
|
|
13
|
+
const frameAncestors = (Array.isArray(_frameAncestors)
|
|
14
|
+
? _frameAncestors
|
|
15
|
+
: [_frameAncestors]).filter(common_1.id);
|
|
16
|
+
const frameAncestor = (frameAncestors.length > 1
|
|
17
|
+
? 'multiple'
|
|
18
|
+
: frameAncestors[0]) || exports.none;
|
|
19
|
+
const frameOptions = (frameAncestor === 'multiple'
|
|
20
|
+
? undefined
|
|
21
|
+
: (frameAncestor === exports.self
|
|
22
|
+
? 'SAMEORIGIN'
|
|
23
|
+
: 'DENY'));
|
|
24
|
+
const nonce = _nonce && `'nonce-${_nonce}'`;
|
|
25
|
+
const cspObject = {
|
|
26
|
+
'default-src': exports.none,
|
|
27
|
+
'connect-src': (dev
|
|
28
|
+
? [exports.self, 'ws://localhost:*']
|
|
29
|
+
: exports.self),
|
|
30
|
+
'font-src': exports.self,
|
|
31
|
+
'frame-src': exports.self,
|
|
32
|
+
'img-src': exports.self,
|
|
33
|
+
'manifest-src': exports.self,
|
|
34
|
+
'media-src': exports.self,
|
|
35
|
+
'script-src': [exports.self, nonce],
|
|
36
|
+
'style-src': [exports.self, exports.unsafeInline],
|
|
37
|
+
'form-action': formAction.length && formAction || exports.self,
|
|
38
|
+
'frame-ancestors': frameAncestors.length && frameAncestors || exports.none
|
|
39
|
+
};
|
|
40
|
+
const cspString = (Object.keys(cspObject)
|
|
41
|
+
.map(directive => {
|
|
42
|
+
const _valueArr = cspObject[directive];
|
|
43
|
+
const valueArr = (Array.isArray(_valueArr)
|
|
44
|
+
? _valueArr
|
|
45
|
+
: [_valueArr]);
|
|
46
|
+
const values = (valueArr
|
|
47
|
+
.filter(common_1.isDefined)
|
|
48
|
+
.map(v => `${v}`)
|
|
49
|
+
.join(' '));
|
|
50
|
+
return (values === ''
|
|
51
|
+
? undefined
|
|
52
|
+
: `${directive} ${values}`);
|
|
53
|
+
})
|
|
54
|
+
.filter(common_1.isDefined)
|
|
55
|
+
.join('; '));
|
|
56
|
+
return {
|
|
57
|
+
policy: cspString,
|
|
58
|
+
frameOptions
|
|
59
|
+
};
|
|
60
|
+
};
|
|
61
|
+
exports.contentSecurityPolicy = contentSecurityPolicy;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { id, isDefined } from './common';
|
|
2
|
+
export const none = "'none'";
|
|
3
|
+
export const self = "'self'";
|
|
4
|
+
export const unsafeEval = "'unsafe-eval'";
|
|
5
|
+
export const unsafeInline = "'unsafe-inline'";
|
|
6
|
+
export const contentSecurityPolicy = ({ dev = process.env.NODE_ENV === 'development', formAction: _formAction, frameAncestors: _frameAncestors, nonce: _nonce }) => {
|
|
7
|
+
const formAction = (Array.isArray(_formAction)
|
|
8
|
+
? _formAction
|
|
9
|
+
: [_formAction]).filter(id);
|
|
10
|
+
const frameAncestors = (Array.isArray(_frameAncestors)
|
|
11
|
+
? _frameAncestors
|
|
12
|
+
: [_frameAncestors]).filter(id);
|
|
13
|
+
const frameAncestor = (frameAncestors.length > 1
|
|
14
|
+
? 'multiple'
|
|
15
|
+
: frameAncestors[0]) || none;
|
|
16
|
+
const frameOptions = (frameAncestor === 'multiple'
|
|
17
|
+
? undefined
|
|
18
|
+
: (frameAncestor === self
|
|
19
|
+
? 'SAMEORIGIN'
|
|
20
|
+
: 'DENY'));
|
|
21
|
+
const nonce = _nonce && `'nonce-${_nonce}'`;
|
|
22
|
+
const cspObject = {
|
|
23
|
+
'default-src': none,
|
|
24
|
+
'connect-src': (dev
|
|
25
|
+
? [self, 'ws://localhost:*']
|
|
26
|
+
: self),
|
|
27
|
+
'font-src': self,
|
|
28
|
+
'frame-src': self,
|
|
29
|
+
'img-src': self,
|
|
30
|
+
'manifest-src': self,
|
|
31
|
+
'media-src': self,
|
|
32
|
+
'script-src': [self, nonce],
|
|
33
|
+
'style-src': [self, unsafeInline],
|
|
34
|
+
'form-action': formAction.length && formAction || self,
|
|
35
|
+
'frame-ancestors': frameAncestors.length && frameAncestors || none
|
|
36
|
+
};
|
|
37
|
+
const cspString = (Object.keys(cspObject)
|
|
38
|
+
.map(directive => {
|
|
39
|
+
const _valueArr = cspObject[directive];
|
|
40
|
+
const valueArr = (Array.isArray(_valueArr)
|
|
41
|
+
? _valueArr
|
|
42
|
+
: [_valueArr]);
|
|
43
|
+
const values = (valueArr
|
|
44
|
+
.filter(isDefined)
|
|
45
|
+
.map(v => `${v}`)
|
|
46
|
+
.join(' '));
|
|
47
|
+
return (values === ''
|
|
48
|
+
? undefined
|
|
49
|
+
: `${directive} ${values}`);
|
|
50
|
+
})
|
|
51
|
+
.filter(isDefined)
|
|
52
|
+
.join('; '));
|
|
53
|
+
return {
|
|
54
|
+
policy: cspString,
|
|
55
|
+
frameOptions
|
|
56
|
+
};
|
|
57
|
+
};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { FastifyPluginCallback, FastifyReply as _FastifyReply } from 'fastify';
|
|
2
|
+
import type { CSPOptions } from './content-security-policy';
|
|
3
|
+
import type { PPOptions } from './permissions-policy';
|
|
4
|
+
type FastifyHardenPluginOptions = {
|
|
5
|
+
contentSecurityPolicy?: Omit<CSPOptions, 'dev' | 'nonce'>;
|
|
6
|
+
dev?: boolean;
|
|
7
|
+
permissionsPolicy?: PPOptions;
|
|
8
|
+
};
|
|
9
|
+
export type FastifyReply = _FastifyReply & {
|
|
10
|
+
cspNonce?: string;
|
|
11
|
+
};
|
|
12
|
+
export declare const fastifyHarden: FastifyPluginCallback<FastifyHardenPluginOptions>;
|
|
13
|
+
export default fastifyHarden;
|
|
14
|
+
export type { FastifyHardenPluginOptions as FastifyHardenOptions };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.fastifyHarden = void 0;
|
|
7
|
+
const fastify_plugin_1 = __importDefault(require("fastify-plugin"));
|
|
8
|
+
const permissions_policy_1 = require("./permissions-policy");
|
|
9
|
+
const content_security_policy_1 = require("./content-security-policy");
|
|
10
|
+
const fastifyHardenPlugin = async (fastify, { contentSecurityPolicy: cspOptions = {}, dev = process.env.NODE_ENV === 'development', permissionsPolicy: ppOptions = {} }) => {
|
|
11
|
+
if (!dev) {
|
|
12
|
+
fastify.setErrorHandler((error, req, reply) => {
|
|
13
|
+
const statusCode = error && error.statusCode;
|
|
14
|
+
if (!statusCode || statusCode === 500) {
|
|
15
|
+
error.message = 'An unexpected error occurred.';
|
|
16
|
+
}
|
|
17
|
+
reply.send(error);
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
fastify.addHook('preHandler', async (_req, reply) => {
|
|
21
|
+
const nonce = (Math.random()
|
|
22
|
+
.toString(36)
|
|
23
|
+
.slice(2));
|
|
24
|
+
reply.cspNonce = nonce;
|
|
25
|
+
});
|
|
26
|
+
fastify.addHook('onSend', async (_req, reply, payload) => {
|
|
27
|
+
const nonce = reply.cspNonce;
|
|
28
|
+
const headers = reply.getHeaders();
|
|
29
|
+
const { policy: pp } = (0, permissions_policy_1.permissionsPolicy)(ppOptions);
|
|
30
|
+
const { policy: csp, frameOptions } = (0, content_security_policy_1.contentSecurityPolicy)({
|
|
31
|
+
...cspOptions,
|
|
32
|
+
dev,
|
|
33
|
+
nonce
|
|
34
|
+
});
|
|
35
|
+
if (!headers['cache-control']) {
|
|
36
|
+
reply.header('Cache-Control', 'no-cache, no-store, must-revalidate, private');
|
|
37
|
+
reply.header('Pragma', 'no-cache');
|
|
38
|
+
reply.header('Expires', '0');
|
|
39
|
+
reply.header('Cross-Origin-Embedder-Policy', 'require-corp');
|
|
40
|
+
reply.header('Cross-Origin-Resource-Policy', 'same-origin');
|
|
41
|
+
reply.header('Cross-Origin-Opener-Policy', 'same-origin');
|
|
42
|
+
}
|
|
43
|
+
reply.header('X-Content-Type-Options', 'nosniff');
|
|
44
|
+
if (frameOptions) {
|
|
45
|
+
reply.header('X-Frame-Options', frameOptions);
|
|
46
|
+
}
|
|
47
|
+
reply.header('Content-Security-Policy', csp);
|
|
48
|
+
reply.header('Permissions-Policy', pp);
|
|
49
|
+
return payload;
|
|
50
|
+
});
|
|
51
|
+
};
|
|
52
|
+
exports.fastifyHarden = (0, fastify_plugin_1.default)(fastifyHardenPlugin, {
|
|
53
|
+
fastify: '5.x',
|
|
54
|
+
name: 'harden',
|
|
55
|
+
});
|
|
56
|
+
exports.default = exports.fastifyHarden;
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import fp from 'fastify-plugin';
|
|
2
|
+
import { permissionsPolicy } from './permissions-policy';
|
|
3
|
+
import { contentSecurityPolicy } from './content-security-policy';
|
|
4
|
+
const fastifyHardenPlugin = async (fastify, { contentSecurityPolicy: cspOptions = {}, dev = process.env.NODE_ENV === 'development', permissionsPolicy: ppOptions = {} }) => {
|
|
5
|
+
if (!dev) {
|
|
6
|
+
fastify.setErrorHandler((error, req, reply) => {
|
|
7
|
+
const statusCode = error && error.statusCode;
|
|
8
|
+
if (!statusCode || statusCode === 500) {
|
|
9
|
+
error.message = 'An unexpected error occurred.';
|
|
10
|
+
}
|
|
11
|
+
reply.send(error);
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
fastify.addHook('preHandler', async (_req, reply) => {
|
|
15
|
+
const nonce = (Math.random()
|
|
16
|
+
.toString(36)
|
|
17
|
+
.slice(2));
|
|
18
|
+
reply.cspNonce = nonce;
|
|
19
|
+
});
|
|
20
|
+
fastify.addHook('onSend', async (_req, reply, payload) => {
|
|
21
|
+
const nonce = reply.cspNonce;
|
|
22
|
+
const headers = reply.getHeaders();
|
|
23
|
+
const { policy: pp } = permissionsPolicy(ppOptions);
|
|
24
|
+
const { policy: csp, frameOptions } = contentSecurityPolicy({
|
|
25
|
+
...cspOptions,
|
|
26
|
+
dev,
|
|
27
|
+
nonce
|
|
28
|
+
});
|
|
29
|
+
if (!headers['cache-control']) {
|
|
30
|
+
reply.header('Cache-Control', 'no-cache, no-store, must-revalidate, private');
|
|
31
|
+
reply.header('Pragma', 'no-cache');
|
|
32
|
+
reply.header('Expires', '0');
|
|
33
|
+
reply.header('Cross-Origin-Embedder-Policy', 'require-corp');
|
|
34
|
+
reply.header('Cross-Origin-Resource-Policy', 'same-origin');
|
|
35
|
+
reply.header('Cross-Origin-Opener-Policy', 'same-origin');
|
|
36
|
+
}
|
|
37
|
+
reply.header('X-Content-Type-Options', 'nosniff');
|
|
38
|
+
if (frameOptions) {
|
|
39
|
+
reply.header('X-Frame-Options', frameOptions);
|
|
40
|
+
}
|
|
41
|
+
reply.header('Content-Security-Policy', csp);
|
|
42
|
+
reply.header('Permissions-Policy', pp);
|
|
43
|
+
return payload;
|
|
44
|
+
});
|
|
45
|
+
};
|
|
46
|
+
export const fastifyHarden = fp(fastifyHardenPlugin, {
|
|
47
|
+
fastify: '5.x',
|
|
48
|
+
name: 'harden',
|
|
49
|
+
});
|
|
50
|
+
export default fastifyHarden;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.permissionsPolicy = void 0;
|
|
4
|
+
const common_1 = require("./common");
|
|
5
|
+
const keywords = [
|
|
6
|
+
'self',
|
|
7
|
+
'src'
|
|
8
|
+
];
|
|
9
|
+
const ppObj = {
|
|
10
|
+
'accelerometer': 'self',
|
|
11
|
+
'ambient-light-sensor': 'self',
|
|
12
|
+
'attribution-reporting': 'self',
|
|
13
|
+
'autoplay': 'self',
|
|
14
|
+
'battery': 'self',
|
|
15
|
+
'bluetooth': 'self',
|
|
16
|
+
'browsing-topics': 'self',
|
|
17
|
+
'camera': 'self',
|
|
18
|
+
'clipboard-read': 'self',
|
|
19
|
+
'clipboard-write': 'self',
|
|
20
|
+
'compute-pressure': 'self',
|
|
21
|
+
'conversion-measurement': 'self',
|
|
22
|
+
'cross-origin-isolated': 'self',
|
|
23
|
+
'display-capture': 'self',
|
|
24
|
+
'document-domain': 'self',
|
|
25
|
+
'encrypted-media': 'self',
|
|
26
|
+
'execution-while-not-rendered': 'self',
|
|
27
|
+
'execution-while-out-of-viewport': 'self',
|
|
28
|
+
'focus-without-user-activation': 'self',
|
|
29
|
+
'fullscreen': 'self',
|
|
30
|
+
'gamepad': 'self',
|
|
31
|
+
'geolocation': 'self',
|
|
32
|
+
'gyroscope': 'self',
|
|
33
|
+
'keyboard-map': 'self',
|
|
34
|
+
'hid': 'self',
|
|
35
|
+
'identity-credentials-get': 'self',
|
|
36
|
+
'idle-detection': 'self',
|
|
37
|
+
'interest-cohort': 'self',
|
|
38
|
+
'local-fonts': 'self',
|
|
39
|
+
'magnetometer': 'self',
|
|
40
|
+
'microphone': 'self',
|
|
41
|
+
'midi': 'self',
|
|
42
|
+
'navigation-override': 'self',
|
|
43
|
+
'otp-credentials': 'self',
|
|
44
|
+
'payment': 'self',
|
|
45
|
+
'picture-in-picture': 'self',
|
|
46
|
+
'publickey-credentials-create': 'self',
|
|
47
|
+
'publickey-credentials-get': 'self',
|
|
48
|
+
'screen-wake-lock': 'self',
|
|
49
|
+
'serial': 'self',
|
|
50
|
+
'speaker-selection': 'self',
|
|
51
|
+
'storage-access': 'self',
|
|
52
|
+
'sync-script': 'self',
|
|
53
|
+
'sync-xhr': 'self',
|
|
54
|
+
'trust-token-redemption': 'self',
|
|
55
|
+
'unload': 'self',
|
|
56
|
+
'usb': 'self',
|
|
57
|
+
'web-share': 'self',
|
|
58
|
+
'window-management': 'self',
|
|
59
|
+
'window-placement': 'self',
|
|
60
|
+
'vertical-scroll': 'self',
|
|
61
|
+
'xr-spatial-tracking': 'self'
|
|
62
|
+
};
|
|
63
|
+
const permissionsPolicy = (_options) => ({
|
|
64
|
+
policy: Object.keys(ppObj)
|
|
65
|
+
.map(directive => {
|
|
66
|
+
const _valueArr = ppObj[directive];
|
|
67
|
+
const valueArr = (Array.isArray(_valueArr)
|
|
68
|
+
? _valueArr
|
|
69
|
+
: [_valueArr]);
|
|
70
|
+
const values = (valueArr
|
|
71
|
+
.filter(common_1.isDefined)
|
|
72
|
+
.map(v => (keywords.includes(v)
|
|
73
|
+
? v
|
|
74
|
+
: `"${v}"`))
|
|
75
|
+
.join(' '));
|
|
76
|
+
return (values === ''
|
|
77
|
+
? undefined
|
|
78
|
+
: `${directive}=(${values})`);
|
|
79
|
+
})
|
|
80
|
+
.filter(common_1.isDefined)
|
|
81
|
+
.join(', ')
|
|
82
|
+
});
|
|
83
|
+
exports.permissionsPolicy = permissionsPolicy;
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { isDefined } from './common';
|
|
2
|
+
const keywords = [
|
|
3
|
+
'self',
|
|
4
|
+
'src'
|
|
5
|
+
];
|
|
6
|
+
const ppObj = {
|
|
7
|
+
'accelerometer': 'self',
|
|
8
|
+
'ambient-light-sensor': 'self',
|
|
9
|
+
'attribution-reporting': 'self',
|
|
10
|
+
'autoplay': 'self',
|
|
11
|
+
'battery': 'self',
|
|
12
|
+
'bluetooth': 'self',
|
|
13
|
+
'browsing-topics': 'self',
|
|
14
|
+
'camera': 'self',
|
|
15
|
+
'clipboard-read': 'self',
|
|
16
|
+
'clipboard-write': 'self',
|
|
17
|
+
'compute-pressure': 'self',
|
|
18
|
+
'conversion-measurement': 'self',
|
|
19
|
+
'cross-origin-isolated': 'self',
|
|
20
|
+
'display-capture': 'self',
|
|
21
|
+
'document-domain': 'self',
|
|
22
|
+
'encrypted-media': 'self',
|
|
23
|
+
'execution-while-not-rendered': 'self',
|
|
24
|
+
'execution-while-out-of-viewport': 'self',
|
|
25
|
+
'focus-without-user-activation': 'self',
|
|
26
|
+
'fullscreen': 'self',
|
|
27
|
+
'gamepad': 'self',
|
|
28
|
+
'geolocation': 'self',
|
|
29
|
+
'gyroscope': 'self',
|
|
30
|
+
'keyboard-map': 'self',
|
|
31
|
+
'hid': 'self',
|
|
32
|
+
'identity-credentials-get': 'self',
|
|
33
|
+
'idle-detection': 'self',
|
|
34
|
+
'interest-cohort': 'self',
|
|
35
|
+
'local-fonts': 'self',
|
|
36
|
+
'magnetometer': 'self',
|
|
37
|
+
'microphone': 'self',
|
|
38
|
+
'midi': 'self',
|
|
39
|
+
'navigation-override': 'self',
|
|
40
|
+
'otp-credentials': 'self',
|
|
41
|
+
'payment': 'self',
|
|
42
|
+
'picture-in-picture': 'self',
|
|
43
|
+
'publickey-credentials-create': 'self',
|
|
44
|
+
'publickey-credentials-get': 'self',
|
|
45
|
+
'screen-wake-lock': 'self',
|
|
46
|
+
'serial': 'self',
|
|
47
|
+
'speaker-selection': 'self',
|
|
48
|
+
'storage-access': 'self',
|
|
49
|
+
'sync-script': 'self',
|
|
50
|
+
'sync-xhr': 'self',
|
|
51
|
+
'trust-token-redemption': 'self',
|
|
52
|
+
'unload': 'self',
|
|
53
|
+
'usb': 'self',
|
|
54
|
+
'web-share': 'self',
|
|
55
|
+
'window-management': 'self',
|
|
56
|
+
'window-placement': 'self',
|
|
57
|
+
'vertical-scroll': 'self',
|
|
58
|
+
'xr-spatial-tracking': 'self'
|
|
59
|
+
};
|
|
60
|
+
export const permissionsPolicy = (_options) => ({
|
|
61
|
+
policy: Object.keys(ppObj)
|
|
62
|
+
.map(directive => {
|
|
63
|
+
const _valueArr = ppObj[directive];
|
|
64
|
+
const valueArr = (Array.isArray(_valueArr)
|
|
65
|
+
? _valueArr
|
|
66
|
+
: [_valueArr]);
|
|
67
|
+
const values = (valueArr
|
|
68
|
+
.filter(isDefined)
|
|
69
|
+
.map(v => (keywords.includes(v)
|
|
70
|
+
? v
|
|
71
|
+
: `"${v}"`))
|
|
72
|
+
.join(' '));
|
|
73
|
+
return (values === ''
|
|
74
|
+
? undefined
|
|
75
|
+
: `${directive}=(${values})`);
|
|
76
|
+
})
|
|
77
|
+
.filter(isDefined)
|
|
78
|
+
.join(', ')
|
|
79
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@react-foundry/fastify-harden",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Fastify plugin for extra cyber-security hardening.",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"import": "./dist/index.mjs",
|
|
10
|
+
"require": "./dist/index.js",
|
|
11
|
+
"default": "./dist/index.mjs"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"/dist"
|
|
16
|
+
],
|
|
17
|
+
"author": "Daniel A.C. Martin <npm@daniel-martin.co.uk> (http://daniel-martin.co.uk/)",
|
|
18
|
+
"license": "MIT",
|
|
19
|
+
"engines": {
|
|
20
|
+
"node": ">=18.0.0"
|
|
21
|
+
},
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"fastify-plugin": "^5.1.0"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"fastify": "5.7.1",
|
|
27
|
+
"jest": "30.2.0",
|
|
28
|
+
"jest-environment-jsdom": "30.2.0",
|
|
29
|
+
"ts-jest": "29.4.6",
|
|
30
|
+
"typescript": "5.9.3"
|
|
31
|
+
},
|
|
32
|
+
"scripts": {
|
|
33
|
+
"test": "NODE_OPTIONS=--experimental-vm-modules jest",
|
|
34
|
+
"build": "npm run build:esm && npm run build:cjs",
|
|
35
|
+
"build:esm": "tsc -m es2022 && find dist -name '*.js' -exec sh -c 'mv \"$0\" \"${0%.js}.mjs\"' {} \\;",
|
|
36
|
+
"build:cjs": "tsc",
|
|
37
|
+
"clean": "rm -rf dist tsconfig.tsbuildinfo"
|
|
38
|
+
},
|
|
39
|
+
"module": "dist/index.mjs",
|
|
40
|
+
"typings": "dist/index.d.ts"
|
|
41
|
+
}
|