sanitize

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 26, 2026 License: BSD-3-Clause Imports: 7 Imported by: 34 modules, 35 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

go-sanitize

A Go port of the Flamework lib_sanitize library.

What does it do?

Text that comes from the outside world (web forms, uploaded files, copy-and-paste) can contain characters you can't see but that cause real trouble: hidden control codes, invisible spaces, markers that flip the direction of text, and bytes that aren't valid text at all. These can garble your data, break other programs, or be used to disguise what a piece of text really says.

go-sanitize cleans that up. Give it a piece of text and it hands back a tidy version:

  • Invalid text is rejected. If the input isn't valid UTF-8, you get an error instead of guesswork.
  • Hidden and troublemaking characters are removed. This covers control codes, the byte order mark, text-direction overrides, zero-width spaces and similar invisible characters.
  • Tabs and line breaks are made consistent. By default they become plain spaces. You can choose to keep line breaks.
  • Placeholder "unknown" characters become ?.
  • Optionally, strip even more. Turn on StripReserved to also remove private-use and other unassigned characters.

It also has small helpers, SanitizeInt32, SanitizeInt64 and SanitizeFloat64, that turn text into numbers and return an error if the text isn't a clean number. Not-a-number and infinity values are rejected.

What it does not do

This package cleans up characters. It does not make text safe to put into a web page, database query or command line. For that you still need the right escaping for where the text is going, such as HTML escaping or parameterized SQL queries.

Install

You will need to have both Go (specifically version 1.12 or higher because we're using Go modules) and the make programs installed on your computer.

All of this package's dependencies are bundled with the code in the vendor directory.

Example

package main

import (
       "fmt"
       "github.com/whosonfirst/go-sanitize"
       )

func main() {

     input := "foo bar\nbaz ok:'\u2318' bad:'\u0007' bad:'\u206E' woop woop"
     
     opts := sanitize.DefaultOptions()
     output, _ := sanitize.SanitizeString(input, opts)

     fmt.Println(output)

}

In this example the bell character (\u0007) and the deprecated format character (\u206E) are removed, the newline becomes a space, and the ⌘ symbol is kept.

See also

Documentation

Overview

Package sanitize removes or replaces problematic Unicode characters from strings and provides strict parsing helpers for numeric input.

It is a Go port of the sanitizing routines in Flamework's lib_sanitize.php. Sanitizing a string involves, in order:

  1. rejecting input that is not valid UTF-8,
  2. removing "evil" code points (most control characters, byte order marks, bidirectional controls, zero width spaces, Unicode "tag" characters),
  3. removing either all "other" (\p{C}) characters or only invalid non-characters, depending on Options.StripReserved,
  4. normalizing tabs, line breaks, form feeds and replacement characters.

Basic usage:

opts := sanitize.DefaultOptions()
clean, err := sanitize.SanitizeString(userInput, opts)

Useful references:

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SanitizeFloat64

func SanitizeFloat64(input string) (float64, error)

SanitizeFloat64 parses input as a 64-bit floating point number. It returns the strconv error (syntax or range) if parsing fails, and an error for NaN and infinite values, which strconv otherwise accepts.

func SanitizeInt32

func SanitizeInt32(input string) (int32, error)

SanitizeInt32 parses input as a base-10 integer that fits in an int32. It returns the strconv error (syntax or range) if parsing fails.

func SanitizeInt64

func SanitizeInt64(input string) (int64, error)

SanitizeInt64 parses input as a base-10 integer that fits in an int64. It returns the strconv error (syntax or range) if parsing fails.

func SanitizeString

func SanitizeString(input string, options *Options) (string, error)

SanitizeString returns input with unsafe or invisible Unicode characters removed or replaced according to options.

It returns an error, and an empty string, if input is not valid UTF-8. If options is nil, DefaultOptions is used.

Types

type Options

type Options struct {
	// StripReserved removes every character in the Unicode "Other" category
	// (\p{C}: controls, format, private use, unassigned, surrogates). When
	// false, only invalid non-characters are removed.
	StripReserved bool
	// AllowNewlines preserves line breaks as "\n" (and form feeds / paragraph
	// separators as "\n\n") instead of replacing them.
	AllowNewlines bool
	// contains filtered or unexported fields
}

Options controls how SanitizeString behaves.

The replacement fields are unexported; obtain a configured value from DefaultOptions or DebugOptions and then adjust the exported fields.

func DebugOptions

func DebugOptions() *Options

DebugOptions returns Options that replace every sanitized character with the visible marker " { SANITIZED } ", making it easy to see what was altered.

func DefaultOptions

func DefaultOptions() *Options

DefaultOptions returns Options that delete unsafe characters and turn tabs and line breaks into single spaces. Unknown/replacement characters become "?".

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL