exframe-middleware 1.4.1 → 1.5.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/index.mjs ADDED
@@ -0,0 +1,9 @@
1
+ 'use strict';
2
+
3
+ import middleware from './index.js';
4
+
5
+ const { handleErrors, standardRouteResponseBuilder, authorizeUser, buildCsp, buildExpressRequestContext } = middleware;
6
+
7
+ export { handleErrors, standardRouteResponseBuilder, authorizeUser, buildCsp, buildExpressRequestContext };
8
+
9
+ export default middleware;
@@ -4,7 +4,7 @@ const errors = require('exframe-errors');
4
4
 
5
5
  module.exports = (req, { cspIsRequired = true }) => {
6
6
  const csp = req.headers['harmony-csp'] || '';
7
- if (cspIsRequired && csp && !/^[A-Z]+:[A-Z]{2}:[A-Z\d]+$/.test(csp)) {
7
+ if (cspIsRequired && csp && !/^[A-Z\d]+:[A-Z]{2}:[A-Z\d]+$/.test(csp)) {
8
8
  throw new errors.Forbidden('CSP is required');
9
9
  }
10
10
 
package/package.json CHANGED
@@ -1,8 +1,15 @@
1
1
  {
2
2
  "name": "exframe-middleware",
3
- "version": "1.4.1",
3
+ "version": "1.5.1",
4
4
  "description": "",
5
5
  "main": "index.js",
6
+ "module": "index.mjs",
7
+ "exports": {
8
+ ".": {
9
+ "import": "./index.mjs",
10
+ "require": "./index.js"
11
+ }
12
+ },
6
13
  "config": {
7
14
  "reporter": "mocha-exzeo-reporter"
8
15
  },
@@ -36,5 +43,5 @@
36
43
  "engines": {
37
44
  "node": ">=14.0.0"
38
45
  },
39
- "gitHead": "ef87c842aa08c54cbcb03ee075db8463ebc8fd43"
46
+ "gitHead": "5a5a106c3d0f7b491daed81955df1ed8fd2c906e"
40
47
  }
@@ -6,7 +6,7 @@ const errors = require('exframe-errors');
6
6
  const { buildCsp } = require('../index');
7
7
 
8
8
  context('buildCsp', () => {
9
- it('works', () => {
9
+ it('applies csp object with companyCode TTIC, state FL, and product HO3', () => {
10
10
  // mq -- super easy ya
11
11
  const obj = {};
12
12
  buildCsp().mq(obj);
@@ -34,6 +34,63 @@ context('buildCsp', () => {
34
34
  });
35
35
  });
36
36
 
37
+ it('applies csp object with companyCode ABC123, state GA, and product DP4', () => {
38
+ const obj = {};
39
+ buildCsp().mq(obj);
40
+ expect(obj.csp).to.deep.equal({});
41
+
42
+ const req = {
43
+ headers: {
44
+ 'harmony-csp': 'ABC123:GA:DP4'
45
+ },
46
+ someOtherStuff: 'yippie'
47
+ };
48
+
49
+ let called = 0;
50
+ const next = () => ++called;
51
+ buildCsp().express(req, {}, next);
52
+
53
+ expect(called).to.equal(1);
54
+ expect(req).to.deep.equal({
55
+ ...req,
56
+ csp: {
57
+ companyCode: 'ABC123',
58
+ state: 'GA',
59
+ product: 'DP4'
60
+ }
61
+ });
62
+ });
63
+
64
+ it('throws forbidden Forbidden: CSP is required when state is greater than 2 characters', () => {
65
+ const req = {
66
+ headers: {
67
+ 'harmony-csp': 'TTIC:FLA:HO3'
68
+ }
69
+ };
70
+
71
+ try { buildCsp().express(req); } catch (e) {
72
+ expect(e).to.be.an.instanceOf(errors.Forbidden);
73
+ return;
74
+ }
75
+
76
+ expect.fail('should have thrown an error');
77
+ });
78
+
79
+ it('throws forbidden Forbidden: CSP is required when state contains a number', () => {
80
+ const req = {
81
+ headers: {
82
+ 'harmony-csp': 'TTIC:F2:HO3'
83
+ }
84
+ };
85
+
86
+ try { buildCsp().express(req); } catch (e) {
87
+ expect(e).to.be.an.instanceOf(errors.Forbidden);
88
+ return;
89
+ }
90
+
91
+ expect.fail('should have thrown an error');
92
+ });
93
+
37
94
  it('throws forbidden when no csp exists or is malformed', () => {
38
95
  const req = {
39
96
  headers: {
@@ -41,8 +98,7 @@ context('buildCsp', () => {
41
98
  }
42
99
  };
43
100
 
44
- try { buildCsp().express(req); }
45
- catch (e) {
101
+ try { buildCsp().express(req); } catch (e) {
46
102
  expect(e).to.be.an.instanceOf(errors.Forbidden);
47
103
  return;
48
104
  }