4.0.9 • Published 3 months ago

ac-sanitizer v4.0.9

Weekly downloads
47
License
MIT
Repository
github
Last release
3 months ago

AC Sanitizer

Sanitizes payloads with given field definitions

Node.js CI

Version 4 - Breaking changes

Version 4 requires Node 16.

Usage

const sanitizer = require('ac-sanitizer')

let fieldsToCheck = {
  params: {
    stringVar: 'This is a string'
  },
  fields: [
    { field: 'stringVar', type: 'string', required: true },
    { field: 'forceJob', type: 'boolean', adminLevel: 64 }
  ],
  adminLevel: 32 // optional adminLevel of the user - must be at least the one defined in fields
}
let test = sanitizer.checkAndSanitizeValues(fieldsToCheck)
// COMPLEX EXAMPLE WITH NESTED PROPERTIES AND CONDITIONAL REQUIREMENTS

const sanitizer = require('ac-sanitizer')

let fieldsToCheck = {
  params: {
    obj: {
      f1: true
  },
  fields: [
    { field: 'obj', type: 'object', properties: [
      { field: 'f1', type: 'boolean' },
      { field: 'f2', type: 'boolean', required: 'f1' } // if f1 is true, then f2 is required
    ]}
  ]
}
let test = sanitizer.checkAndSanitizeValues(fieldsToCheck)

Field definition

ParameterTypeRemarks
fieldstringName of the field
typestringType of the field to sanitize, see below for available values
requiredboolean OR stringSet to true if required or set a path^1 to a param (if that param is set, this value is required)
enumarray OR stringOptional list of allowed values. You can a string placeholder for certain standard lists (see below)
adminLevelintegerOptional adminLevel required for this field
omitFieldsbooleanIf adminLevel is set and you do not have the proper adminLevel the sanitizer will just omit the field (and not return an error) if omitFields is true
convertboolean OR stringSome types can be automatically converted (e.g. base64 to string)
valueTypestringUse it to sanitize values of an array by defining the allowed type here
strictbooleanFor objects only - if true and payload contains a property not defined, an error will be returned.
nullAllowedbooleanIf true, sending NULL is allowed.

^1: The path must be set with the parent propery as root, e.g. the actual field is settings.video.width, in property video the condition is then just "width" not the full path.

ENUM lists

The following enum lists are available using a string placeholder

PlaceholderItemsRemarks
iso-639-1ISO 639-1 entriese.g. de, en, fr, es...
iso-639-2ISO 639-2 entriese.g. deu, eng, fra ...
countrylistlist of country namese.g. Laos, Brazil, Norway...

Convert

Some types allow automatic conversion: Type | Example | Remarks --- | --- | --- | integer | 60.1 -> 60 | Convert incoming number to integer - this way you can make your check more lenient string | Hello Developer -> Hello (with maxLength = 5) | Reduce string to max length base64 | SGVsbG8= -> Hello | Convert base64 encoded string to UTF-8 string iso-639 | { iso-639-2: 'tlh', translations: [] } -> tlh (with convert=iso-639-2) | Returns only the select property for the ISO-639 object

Available types

TypeOptionsRemarks
anyAny can be string, integer, boolean, object or array - it will be automatically detected.
base64Checks if a string is base64 encoded, optional with field option "convert" (to string)
boolean
cidrCheck CIDR, see example
integer | booleanValue can be an integer OR a boolean
datedateFormatDate or date time, support various date formats (e.g. DD.MM.YYYY, DD/MM/YYYY, YYYY-MM-DD, etc) as well as a custom format defined in dateFormat.
fqdnwildcardAllowed (bool)Fully qualified domain names, optional with wildcard subdomain (e.g. *.admiralcloud.com)
emaila@b.c
float0 - 2^31
fileExtension
gpsCan be a string of "LAT,LNG" or one including a distance as 3rd parameter like "LAT,LNG,DISTANCE".
hashidsHashIds - https://hashids.org
hexColorCheck valid hexadecimal color like #ff99cc
integer0 - 2^31
integer | stringValue can be an integer OR a string
iso-639-1convertWith convert = nativeName you can retrieve the native name of the given ISO string
iso-639-2convertWith convert = nativeName you can retrieve the native name of the given ISO string
ipversionversion can be "4" or "6", defaults to "4"
long0 - 2^63
objectpropertiesUse properties to define object structure, properties is equal to fields array
numberShould no be used - use integer, long, short, floag
ratiox:y
rgbCheck for valid RGB value (r,g,b) or (r%,g%, b%)
short0 - 2^15
stringminLength (int), maxLength (int)
urlprotocols, require_tld, require_protocolDefault values: protocols 'http', 'https', required_tld true, require_protocol true

Examples

CIDR

// CIDR Example

const sanitizer = require('ac-sanitizer')

let fieldsToCheck = {
  params: {
    cidr: [{ cidr: '8.8.8.8/32' }]
  },
  fields: [
    { field: 'cidr', type: 'cidr' }
  ]
}
let test = sanitizer.checkAndSanitizeValues(fieldsToCheck)


// multiple mixed CIDR
let fieldsToCheck = {
  params: {
    cidr: [
      { cidr: '8.8.8.8/32' },
      { cidr: '::ffff:127.0.0.1', type: 'ipv6' }
    ]
  },
  fields: [
    { field: 'cidr', type: 'cidr' }
  ]
}
let test = sanitizer.checkAndSanitizeValues(fieldsToCheck)

Objects

By default properties which are not defined will be ignored and removed from payload.

Objects with strict mode activate

In strict mode, only defined properties are allowed.

let fieldsToCheck = {
  params: {
    settings: {
      allowed: true,
      notAllowed: true
    }
  },
  fields: [
    { field: 'settings', type: 'object', strict: true, properties: [
      { field: 'allowed', type: 'boolean' }
    ] }
  ]
}
let test = sanitizer.checkAndSanitizeValues(fieldsToCheck)
// error: object_settings_containsInvalidProperties

Links

License

MIT License Copyright © 2009-present, AdmiralCloud AG, Mark Poepping

4.0.9

3 months ago

4.0.7

4 months ago

4.0.6

4 months ago

4.0.8

4 months ago

4.0.5

5 months ago

4.0.4

8 months ago

4.0.3

10 months ago

4.0.2

11 months ago

4.0.1

1 year ago

4.0.0

1 year ago

3.9.17

2 years ago

3.9.18

2 years ago

3.9.15

2 years ago

3.9.16

2 years ago

3.9.13

2 years ago

3.9.14

2 years ago

3.10.1

2 years ago

3.10.0

2 years ago

3.10.3

2 years ago

3.10.2

2 years ago

3.10.5

2 years ago

3.10.4

2 years ago

3.10.7

1 year ago

3.10.6

2 years ago

3.9.11

2 years ago

3.9.9

2 years ago

3.9.8

2 years ago

3.9.10

2 years ago

3.9.7

3 years ago

3.9.6

3 years ago

3.9.5

3 years ago

3.9.4

3 years ago

3.9.3

3 years ago

3.9.2

3 years ago

3.7.4

3 years ago

3.9.1

3 years ago

3.7.3

3 years ago

3.9.0

3 years ago

3.8.1

3 years ago

3.8.0

3 years ago

3.7.2

3 years ago

3.7.1

3 years ago

3.7.0

3 years ago

3.6.0

3 years ago

3.5.1

3 years ago

3.5.0

3 years ago

3.4.3

3 years ago

3.4.2

3 years ago

3.4.1

3 years ago

3.4.0

4 years ago

3.3.1

4 years ago

3.3.0

4 years ago

3.2.0

4 years ago

3.1.0

4 years ago

3.0.0

4 years ago

2.3.4

4 years ago

2.3.3

4 years ago

2.3.5

4 years ago

2.3.2

4 years ago

2.3.0

4 years ago

2.3.1

4 years ago

2.2.2

4 years ago

2.2.1

4 years ago

2.2.0

4 years ago

2.1.3

4 years ago

2.1.1

4 years ago

2.1.0

4 years ago

2.0.0

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago