@vrobots/fastify 0.1.3 → 0.1.4
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/src/lib/fastify.js +27 -6
- package/package.json +1 -1
package/dist/src/lib/fastify.js
CHANGED
|
@@ -62,16 +62,37 @@ class FastifyInit {
|
|
|
62
62
|
}
|
|
63
63
|
initMiddleware(app) {
|
|
64
64
|
app.register(formbody_1.default);
|
|
65
|
-
function checkOriginAgainstWhitelist() {
|
|
66
|
-
const
|
|
67
|
-
const
|
|
68
|
-
|
|
65
|
+
function checkOriginAgainstWhitelist(ctx) {
|
|
66
|
+
const requestOrigin = ctx.accept.headers.origin;
|
|
67
|
+
const requestOriginParts = requestOrigin.split('.');
|
|
68
|
+
const wildcardOriginExists = !!conf_1.default.secure.allowOrigin.find(origin => origin.includes('*'));
|
|
69
|
+
const directOriginMatch = conf_1.default.secure.allowOrigin.includes(requestOrigin);
|
|
70
|
+
const wildcardMatchOrigin = conf_1.default.secure.allowOrigin.includes(`*.${requestOriginParts.slice(-2).join('.')}`) || conf_1.default.secure.allowOrigin.includes(`*.${requestOrigin.replace('https://', '').replace('http://', '')}`);
|
|
71
|
+
if ((wildcardOriginExists && !wildcardMatchOrigin) || (!wildcardOriginExists && !directOriginMatch)) {
|
|
72
|
+
return ctx.throw(`${requestOrigin} is not a valid origin`);
|
|
73
|
+
}
|
|
74
|
+
return requestOrigin;
|
|
69
75
|
}
|
|
76
|
+
app.addHook('onRequest', async (request, reply) => {
|
|
77
|
+
const requestOrigin = request.headers.origin || '';
|
|
78
|
+
const requestOriginParts = requestOrigin.split('.');
|
|
79
|
+
const wildcardOriginExists = !!conf_1.default.secure.allowOrigin.find(origin => origin.includes('*'));
|
|
80
|
+
const directOriginMatch = conf_1.default.secure.allowOrigin.includes(requestOrigin);
|
|
81
|
+
const wildcardMatchOrigin = conf_1.default.secure.allowOrigin.includes(`*.${requestOriginParts.slice(-2).join('.')}`) || conf_1.default.secure.allowOrigin.includes(`*.${requestOrigin.replace('https://', '').replace('http://', '')}`);
|
|
82
|
+
if ((wildcardOriginExists && !wildcardMatchOrigin) || (!wildcardOriginExists && !directOriginMatch)) {
|
|
83
|
+
reply.status(403).send(`${requestOrigin} is not a valid origin`);
|
|
84
|
+
}
|
|
85
|
+
reply.header('Access-Control-Allow-Origin', '*');
|
|
86
|
+
if (request.method === 'OPTIONS') {
|
|
87
|
+
reply.header('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS');
|
|
88
|
+
reply.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
|
|
89
|
+
reply.send();
|
|
90
|
+
}
|
|
91
|
+
});
|
|
70
92
|
app.register(cors_1.default, {
|
|
71
|
-
origin: checkOriginAgainstWhitelist,
|
|
72
93
|
exposedHeaders: ['Access-Control-Allow-Origin'],
|
|
73
94
|
credentials: true,
|
|
74
|
-
methods: ['GET', 'POST', 'PUT', 'DELETE', '
|
|
95
|
+
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
|
|
75
96
|
});
|
|
76
97
|
}
|
|
77
98
|
initModulesConfiguration(app) {
|