Modules@robinpath/sanitize
sanitize

@robinpath/sanitize

0.1.1Node.jsPublic

Input sanitization utilities for security: HTML escaping, XSS prevention, SQL escaping, filename and path sanitization, URL cleaning, and more

Sanitize

Input sanitization utilities for security: HTML escaping, XSS prevention, SQL escaping, filename and path sanitization, URL cleaning, and more

Package: @robinpath/sanitize | Category: Utility | Type: Utility

Authentication

No authentication required. All functions are available immediately.

Use Cases

Use the sanitize module when you need to:

  • Strip or escape HTML tags from input -- Use sanitize.html to perform this operation
  • Remove XSS attack vectors from input -- Use sanitize.xss to perform this operation
  • Escape SQL special characters to prevent injection -- Use sanitize.sql to perform this operation
  • Escape special regex characters in a string -- Use sanitize.regex to perform this operation
  • Sanitize a string for safe use as a filename -- Use sanitize.filename to perform this operation

Quick Reference

FunctionDescriptionReturns
htmlStrip or escape HTML tags from inputobject
xssRemove XSS attack vectors from inputobject
sqlEscape SQL special characters to prevent injectionobject
regexEscape special regex characters in a stringobject
filenameSanitize a string for safe use as a filenameobject
pathPrevent path traversal attacks by sanitizing a file pathobject
urlSanitize a URL, stripping dangerous protocols like javascript:object
emailNormalize an email address (lowercase, remove dots/plus aliases for Gmail)object
stripTagsRemove all HTML tags from a string, optionally allowing specific tagsobject
escapeHtmlEscape HTML special characters: & < > " 'object
unescapeHtmlUnescape HTML entities back to their original charactersobject
trimDeep trim all string values within an object, array, or stringobject
truncateTruncate a string to a maximum length with a suffixobject
alphanumericStrip all non-alphanumeric characters from a stringobject
slugSanitize a string into a URL-safe slugobject

Functions

html

Strip or escape HTML tags from input

Module: sanitize | Returns: object -- API response.

sanitize.html
ParameterTypeRequiredDescription
inputstringYesThe string to sanitize
modestringNoMode: 'escape' (default) or 'strip'

xss

Remove XSS attack vectors from input

Module: sanitize | Returns: object -- API response.

sanitize.xss
ParameterTypeRequiredDescription
inputstringYesThe string to sanitize

sql

Escape SQL special characters to prevent injection

Module: sanitize | Returns: object -- API response.

sanitize.sql
ParameterTypeRequiredDescription
inputstringYesThe string to escape

regex

Escape special regex characters in a string

Module: sanitize | Returns: object -- API response.

sanitize.regex
ParameterTypeRequiredDescription
inputstringYesThe string to escape

filename

Sanitize a string for safe use as a filename

Module: sanitize | Returns: object -- API response.

sanitize.filename
ParameterTypeRequiredDescription
inputstringYesThe filename to sanitize
replacementstringNoReplacement character for invalid chars (default: '_')

path

Prevent path traversal attacks by sanitizing a file path

Module: sanitize | Returns: object -- API response.

sanitize.path
ParameterTypeRequiredDescription
inputstringYesThe path to sanitize

url

Sanitize a URL, stripping dangerous protocols like javascript:

Module: sanitize | Returns: object -- API response.

sanitize.url
ParameterTypeRequiredDescription
inputstringYesThe URL to sanitize

email

Normalize an email address (lowercase, remove dots/plus aliases for Gmail)

Module: sanitize | Returns: object -- API response.

sanitize.email
ParameterTypeRequiredDescription
inputstringYesThe email to normalize

stripTags

Remove all HTML tags from a string, optionally allowing specific tags

Module: sanitize | Returns: object -- API response.

sanitize.stripTags
ParameterTypeRequiredDescription
inputstringYesThe string to strip tags from
allowedstringNoAllowed tags, e.g. '<b><i><a>'

escapeHtml

Escape HTML special characters: & < > " '

Module: sanitize | Returns: object -- API response.

sanitize.escapeHtml
ParameterTypeRequiredDescription
inputstringYesThe string to escape

unescapeHtml

Unescape HTML entities back to their original characters

Module: sanitize | Returns: object -- API response.

sanitize.unescapeHtml
ParameterTypeRequiredDescription
inputstringYesThe string to unescape

trim

Deep trim all string values within an object, array, or string

Module: sanitize | Returns: object -- API response.

sanitize.trim
ParameterTypeRequiredDescription
valueanyYesThe value to deep-trim

truncate

Truncate a string to a maximum length with a suffix

Module: sanitize | Returns: object -- API response.

sanitize.truncate
ParameterTypeRequiredDescription
inputstringYesThe string to truncate
maxLengthnumberNoMaximum length (default: 100)
suffixstringNoSuffix to append when truncated (default: '...')

alphanumeric

Strip all non-alphanumeric characters from a string

Module: sanitize | Returns: object -- API response.

sanitize.alphanumeric
ParameterTypeRequiredDescription
inputstringYesThe string to sanitize
allowSpacesbooleanNoWhether to allow spaces (default: false)

slug

Sanitize a string into a URL-safe slug

Module: sanitize | Returns: object -- API response.

sanitize.slug
ParameterTypeRequiredDescription
inputstringYesThe string to slugify
separatorstringNoSeparator character (default: '-')

Error Handling

All functions throw on failure. Common errors:

ErrorCause
(standard errors)Check function parameters and authentication
@desc "Html and validate result"
do
  set $result as sanitize.html
  if $result != null
    print "Success"
  else
    print "No result"
  end
enddo

Recipes

1. Multi-step Sanitize workflow

Chain multiple sanitize operations together.

@desc "Html, xss, and more"
do
  set $r_html as sanitize.html
  set $r_xss as sanitize.xss
  set $r_sql as sanitize.sql
  print "All operations complete"
enddo

2. Safe html with validation

Check results before proceeding.

@desc "Html and validate result"
do
  set $result as sanitize.html
  if $result != null
    print "Success: " + $result
  else
    print "Operation returned no data"
  end
enddo

Related Modules

  • json -- JSON module for complementary functionality

Versions (1)

VersionTagPublished
0.1.1latest1 months ago
Install
$ robinpath add @robinpath/sanitize

Collaborators

Dumitru Balaban
Dumitru Balaban
@dumitru
View all @robinpath modules
Version0.1.1
LicenseMIT
Unpacked Size5.7 KB
Versions1
Weekly Downloads21
Total Downloads21
Stars0
Last Publish1 months ago
Created1 months ago

Category

utilities