ac-sanitizer 4.1.4 → 4.2.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/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
1
+
2
+ # [4.2.0](https://github.com/mmpro/ac-sanitizer/compare/v4.1.4..v4.2.0) (2025-05-09 07:40:18)
3
+
4
+
5
+ ### Feature
6
+
7
+ * **App:** Option to anonymize IPs | MP | [6f7a63787467221db3e61a0c7ba27089518ee30f](https://github.com/mmpro/ac-sanitizer/commit/6f7a63787467221db3e61a0c7ba27089518ee30f)
8
+ Added option to anonymize IPs (v4 and v6), set number of octets/segments and optional replacement character
9
+ Related issues:
10
+ ### Chores
11
+
12
+ * **App:** Updated packages | MP | [aba6b031d7b07cda75720e86a65c6fbe7606c5e9](https://github.com/mmpro/ac-sanitizer/commit/aba6b031d7b07cda75720e86a65c6fbe7606c5e9)
13
+ Updated packages
14
+ Related issues:
1
15
 
2
16
  ## [4.1.4](https://github.com/mmpro/ac-sanitizer/compare/v4.1.3..v4.1.4) (2025-04-19 18:34:41)
3
17
 
package/README.md CHANGED
@@ -103,7 +103,7 @@ integer | | 0 - 2^31
103
103
  integer \| string | | Value can be an integer OR a string
104
104
  iso-639-1 | convert | With convert = nativeName you can retrieve the native name of the given ISO string
105
105
  iso-639-2 | convert | With convert = nativeName you can retrieve the native name of the given ISO string
106
- ip | version | version can be "4" or "6", defaults to "4"
106
+ ip | version, anonymize, replacement | version can be "4" or "6", defaults to "4", anonymize can be the number of octets/segments to anonymize, replacement (optional) defines the replacement character for IPv4 (defaults to 0)
107
107
  long | | 0 - 2^63
108
108
  object | properties | Use properties to define object structure, properties is equal to fields array
109
109
  number | | Should no be used - use integer, long, short, floag
@@ -147,6 +147,24 @@ let test = sanitizer.checkAndSanitizeValues(fieldsToCheck)
147
147
 
148
148
  ```
149
149
 
150
+ ## IP
151
+ ```
152
+ // CIDR Example
153
+
154
+ const sanitizer = require('ac-sanitizer')
155
+
156
+ let fieldsToCheck = {
157
+ params: {
158
+ ip: '8.8.8.8/32
159
+ },
160
+ fields: [
161
+ { field: 'ip, type: 'ip', anonymize: 4 }
162
+ ]
163
+ }
164
+ let test = sanitizer.checkAndSanitizeValues(fieldsToCheck)
165
+ // ip -> 8.8.0.0
166
+ ```
167
+
150
168
  ## Objects
151
169
  By default properties which are not defined will be ignored and removed from payload.
152
170
 
package/index.js CHANGED
@@ -390,9 +390,38 @@ const sanitizer = function() {
390
390
  }
391
391
  }
392
392
  else if (field.type === 'ip') {
393
- const version = _.get(field, 'version')
393
+ let version = _.get(field, 'version')
394
394
  if (version && !validator.isIP(value, version)) error = { message: fieldName + '_' + getTypeMapping(field.type, 'errorMessage'), additionalInfo: { version } }
395
395
  if (!validator.isIP(value)) error = { message: fieldName + '_' + getTypeMapping(field.type, 'errorMessage') }
396
+ if (field.anonymize) {
397
+ version = version || validator.isIP(value, '6') ? 6 : 4
398
+ const replacement = field.replacement || '0'
399
+ if (version === 4 && (field.anonymize >= 1 && field.anonymize <= 4)) {
400
+ const octets = value.split('.')
401
+ for (let i = 4; i > 4 - field.anonymize; i--) {
402
+ octets[i-1] = replacement
403
+ }
404
+ value = octets.join('.')
405
+ }
406
+ else if (version === 6) {
407
+ const segmentsToKeep = Math.max(0, 8 - (field.anonymize * 2))
408
+ if (segmentsToKeep === 0) {
409
+ value = "::"
410
+ }
411
+ if (value.includes("::")) {
412
+ const parts = value.split("::")
413
+ const frontPart = parts[0].split(":")
414
+ const keptSegments = frontPart.slice(0, segmentsToKeep)
415
+ value = keptSegments.length > 0 ? keptSegments.join(":") + "::" : "::"
416
+ }
417
+ else {
418
+ const segments = value.split(":")
419
+ const keptSegments = segments.slice(0, segmentsToKeep)
420
+ value = keptSegments.join(":") + "::"
421
+ }
422
+ }
423
+ _.set(paramsToCheck, fieldName, value)
424
+ }
396
425
  }
397
426
  else if (field.type === 'cidr') {
398
427
  // cidr can be a plain value of an array of objects with properties cidr and optional type
package/package.json CHANGED
@@ -4,10 +4,10 @@
4
4
  "author": "Mark Poepping (https://www.admiralcloud.com)",
5
5
  "license": "MIT",
6
6
  "repository": "admiralcloud/ac-sanitizer",
7
- "version": "4.1.4",
7
+ "version": "4.2.0",
8
8
  "homepage": "https://www.admiralcloud.com",
9
9
  "dependencies": {
10
- "ac-countrylist": "^1.0.12",
10
+ "ac-countrylist": "^1.0.13",
11
11
  "ac-file-extensions": "^2.0.10",
12
12
  "ac-ip": "^4.1.3",
13
13
  "chai": "^4.5.0",
@@ -17,10 +17,10 @@
17
17
  "validator": "^13.15.0"
18
18
  },
19
19
  "devDependencies": {
20
- "ac-semantic-release": "^0.4.5",
20
+ "ac-semantic-release": "^0.4.6",
21
21
  "c8": "^10.1.3",
22
- "eslint": "^9.25.0",
23
- "mocha": "^11.1.0"
22
+ "eslint": "^9.26.0",
23
+ "mocha": "^11.2.2"
24
24
  },
25
25
  "scripts": {
26
26
  "test": "mocha --reporter spec --bail",
package/test/tests/ip.js CHANGED
@@ -9,7 +9,13 @@ module.exports = {
9
9
  { name: 'Valid IPv6', type: 'ip', value: '1200:0000:AB00:1234:0000:2552:7777:1313', expected: '1200:0000:AB00:1234:0000:2552:7777:1313' },
10
10
  { name: 'Valid IPv6 - use version 6', type: 'ip', version: '6', value: '1200:0000:AB00:1234:0000:2552:7777:1313', expected: '1200:0000:AB00:1234:0000:2552:7777:1313' },
11
11
  { name: 'Valid local IPv6', type: 'ip', value: '::ffff:127.0.0.1', expected: '::ffff:127.0.0.1' },
12
- { name: 'Invalid IPv4 - should fail', type: 'ip', value: 'A', error: 'ip_notAnIP' },
12
+ { name: 'Invalid IPv4 - should fail', type: 'ip', value: 'A', error: 'ip_notAnIP' },
13
+ // anonymize
14
+ { name: 'Valid IPv4 - anonymize 2 octets with 0', type: 'ip', value: '8.8.8.8', expected: '8.8.0.0', anonymize: 2 },
15
+ { name: 'Valid IPv4 - anonymize 2 octets with x', type: 'ip', value: '8.8.8.8', expected: '8.8.x.x', anonymize: 2, replacement: 'x' },
16
+ { name: 'Valid IPv6 - anonymize 2 segements', type: 'ip', version: '6', value: '2001:db8:85a3:1:2:3:4:5', expected: '2001:db8:85a3:1::', anonymize: 2 },
17
+ { name: 'Valid IPv6 - anonymize 2 segements', type: 'ip', version: '6', value: '2001:db8:85a3::8a2e:370:7334', expected: '2001:db8:85a3::', anonymize: 2 },
18
+ { name: 'Valid IPv6 - anonymize 4 segements', type: 'ip', version: '6', value: '2001:0db8:85a3:0000:0000:8a2e:0370:7334', expected: '::', anonymize: 4 },
13
19
  ]
14
20
 
15
21
  runValidationTests(baseTests, 'ip')