@nitra/cf-security 1.0.6 → 3.0.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/README.md CHANGED
@@ -1,2 +1,19 @@
1
1
  # cf-security
2
- Check security header in CF
2
+
3
+ [![GitHub Super-Linter](https://github.com/nitra/cf-security/workflows/npm-publish/badge.svg)](https://github.com/marketplace/actions/super-linter)
4
+
5
+ Check security header in Cloud Functions
6
+
7
+ ```HTTP
8
+ X_NITRA_CF_KEY: secret
9
+ ```
10
+
11
+ ```JavaScript
12
+ const { cfSecurity } = require('@nitra/cf-security')
13
+
14
+ exports.function = async (req, res) => {
15
+ if (!cfSecurity(req)) {
16
+ res.send(`Nitra security not passed`)
17
+ return
18
+ }
19
+ ```
package/package.json CHANGED
@@ -1,21 +1,12 @@
1
1
  {
2
2
  "name": "@nitra/cf-security",
3
- "version": "1.0.6",
3
+ "version": "3.0.0",
4
4
  "description": "check header in cloud functions",
5
5
  "main": "src/index.js",
6
- "@pika/pack": {
7
- "pipeline": [
8
- [
9
- "@pika/plugin-standard-pkg"
10
- ],
11
- [
12
- "@pika/plugin-build-node"
13
- ]
14
- ]
15
- },
6
+ "type": "module",
16
7
  "scripts": {
17
- "fix": "standard --fix",
18
- "version": "npx @pika/pack build"
8
+ "fix": "npx standard --fix && npx prettier --write .",
9
+ "test": "env $(cat ./test/.env) npx coverage-node test/index.js"
19
10
  },
20
11
  "repository": {
21
12
  "type": "git",
@@ -27,16 +18,13 @@
27
18
  "url": "https://github.com/nitra/cf-security/issues"
28
19
  },
29
20
  "homepage": "https://github.com/nitra/cf-security#readme",
21
+ "prettier": "prettier-config-standard",
30
22
  "devDependencies": {
31
- "@pika/cli": "^0.2.0",
32
- "@pika/pack": "^0.5.0",
33
- "@pika/plugin-build-node": "^0.6.0",
34
- "@pika/plugin-standard-pkg": "^0.6.0",
35
- "@types/node": "^12.7.4",
36
- "standard": "^14.1.0"
23
+ "prettier-config-standard": "^1.0.1",
24
+ "test-director": "^7.0.0"
37
25
  },
38
26
  "dependencies": {
39
- "@47ng/check-env": "^1.3.1",
40
- "loglevel-colored-level-prefix": "^1.0.0"
27
+ "@nitra/check-env": "^2.0.1",
28
+ "@nitra/consola": "^1.3.1"
41
29
  }
42
30
  }
package/src/index.js CHANGED
@@ -1,42 +1,31 @@
1
- /**
2
- * CF Security module
3
- * @module @nitra/cf-security
4
- */
1
+ import consola from '@nitra/consola'
2
+ import checkEnv from '@nitra/check-env'
3
+ checkEnv(['X_NITRA_CF_KEY'])
5
4
 
6
5
  /**
7
- * @const {Function}
6
+ * Check request for Nitra security rules
7
+ *
8
+ * @param {object} req - ApolloServer or Express Request for check
9
+ * @return {boolean} if check passed
8
10
  */
9
- const checkEnv = require('@47ng/check-env').default
10
- checkEnv({ required: ['X_NITRA_CF_KEY'] })
11
-
12
- const log = require('loglevel-colored-level-prefix')()
13
- log.debug('cfSecurity in DEBUG MODE')
14
-
15
- /**
16
- * Check request for Nitra security rules
17
- *
18
- * @param {object} req - ApolloServer or Express Request for check
19
- * @return {boolean} if check passed
20
- */
21
-
22
- exports.cfSecurity = function (req) {
11
+ export const cfSecurity = req => {
23
12
  if (typeof req.headers === 'undefined') {
24
- log.debug('Request without headers')
13
+ consola.debug('Request without headers')
25
14
  return false
26
15
  }
27
16
 
28
- if (typeof req.headers.x_nitra_cf_key === 'undefined') {
29
- log.debug('Nitra key not exist in request')
17
+ if (typeof req.headers['x-nitra-cf-key'] === 'undefined') {
18
+ consola.debug('Nitra key not exist in request')
30
19
  return false
31
20
  }
32
21
 
33
- if (req.headers.x_nitra_cf_key.length === 0) {
34
- log.debug('Empty Nitra key in headers request')
22
+ if (req.headers['x-nitra-cf-key'] === 0) {
23
+ consola.debug('Empty Nitra key in headers request')
35
24
  return false
36
25
  }
37
26
 
38
- if (req.headers.x_nitra_cf_key !== process.env.X_NITRA_CF_KEY) {
39
- log.debug('Not equal Nitra key')
27
+ if (req.headers['x-nitra-cf-key'] !== process.env.X_NITRA_CF_KEY) {
28
+ consola.debug('Not equal Nitra key')
40
29
  return false
41
30
  }
42
31