class Sanitize::URISanitizer

Overview

A URISanitizer is used to validate and transform a URI based on specified rules.

Defined in:

Constructors

Instance Method Summary

Constructor Detail

def self.new(accepted_schemes : Set(String)? = Set {"http", "https", "mailto", "tel"}) #

Instance Method Detail

def accept_scheme(scheme : String) #

Adds scheme to #accepted_schemes.


def accepted_hosts : Set(String)? #

Specifies a whitelist of hosts this sanitizer accepts.

If empty, no hosts are accepted (i.e. only relative URIs are valid). If nil, all hosts are accepted (default).

The blacklist #rejected_hosts has precedence over this whitelist.


def accepted_hosts=(accepted_hosts : Set(String)?) #

Specifies a whitelist of hosts this sanitizer accepts.

If empty, no hosts are accepted (i.e. only relative URIs are valid). If nil, all hosts are accepted (default).

The blacklist #rejected_hosts has precedence over this whitelist.


def accepted_schemes : Set(String)? #

Specifies a whitelist of URI schemes this sanitizer accepts.

If empty, no schemes are accepted (i.e. only relative URIs are valid). If nil, all schemes are accepted (this setting is potentially dangerous).

Relative URIs are not affected by this setting.


def accepted_schemes=(accepted_schemes : Set(String)?) #

Specifies a whitelist of URI schemes this sanitizer accepts.

If empty, no schemes are accepted (i.e. only relative URIs are valid). If nil, all schemes are accepted (this setting is potentially dangerous).

Relative URIs are not affected by this setting.


def accepts_host?(host) #

def accepts_scheme?(scheme) #

def base_url : URI? #

Specifies a base URL all relative URLs are resolved against.

If nil, relative URLs are not resolved.


def base_url=(base_url : URI?) #

Specifies a base URL all relative URLs are resolved against.

If nil, relative URLs are not resolved.


def rejected_hosts : Set(String) #

Specifies a blacklist of hosts this sanitizer rejects.

If empty, no hosts are rejected.

This blacklist has precedence over the whitelist #accepted_hosts.


def rejected_hosts=(rejected_hosts : Set(String)) #

Specifies a blacklist of hosts this sanitizer rejects.

If empty, no hosts are rejected.

This blacklist has precedence over the whitelist #accepted_hosts.


def resolve_base_url(uri) #

def resolve_fragment_urls : Bool #

Configures whether fragment-only URIs are resolved on #base_url.

sanitizer = Sanitize::URISanitizer.new
sanitizer.base_url = URI.parse("https://example.com/base/")
sanitizer.sanitize(URI.parse("#foo")) # => "#foo"

sanitizer.resolve_fragment_urls = true
sanitizer.sanitize(URI.parse("#foo")) # => "https://example.com/base/#foo"

def resolve_fragment_urls=(resolve_fragment_urls) #

Configures whether fragment-only URIs are resolved on #base_url.

sanitizer = Sanitize::URISanitizer.new
sanitizer.base_url = URI.parse("https://example.com/base/")
sanitizer.sanitize(URI.parse("#foo")) # => "#foo"

sanitizer.resolve_fragment_urls = true
sanitizer.sanitize(URI.parse("#foo")) # => "https://example.com/base/#foo"

def sanitize(uri : URI) : URI? #