@vrobots/fastify 0.1.3 → 0.1.5

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.
@@ -62,16 +62,30 @@ class FastifyInit {
62
62
  }
63
63
  initMiddleware(app) {
64
64
  app.register(formbody_1.default);
65
- function checkOriginAgainstWhitelist() {
66
- const wildcardOrigins = conf_1.default.secure.allowOrigin.filter(origin => origin.includes('*'));
67
- const origins = wildcardOrigins?.length > 0 ? wildcardOrigins.map(origin => new RegExp(origin.replace('*', ''))) : [...conf_1.default.secure.allowOrigin];
68
- return origins;
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
+ reply.header('Access-Control-Allow-Origin', '*');
78
+ if (request.method === 'OPTIONS') {
79
+ reply.header('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS');
80
+ reply.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
81
+ reply.send();
82
+ }
83
+ });
70
84
  app.register(cors_1.default, {
71
- origin: checkOriginAgainstWhitelist,
85
+ origin: conf_1.default.secure.allowOrigin,
72
86
  exposedHeaders: ['Access-Control-Allow-Origin'],
73
87
  credentials: true,
74
- methods: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH'],
88
+ methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
75
89
  });
76
90
  }
77
91
  initModulesConfiguration(app) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vrobots/fastify",
3
- "version": "0.1.3",
3
+ "version": "0.1.5",
4
4
  "description": "",
5
5
  "license": "ISC",
6
6
  "author": "Bryan Ricci",