forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/golang.org/x/net/idna/idna9.0.0.go

1286 lines
20 KiB
Go
Raw Normal View History

2024-02-18 10:42:21 +00:00
// Code generated by running "go generate" in golang.org/x/text. DO NOT EDIT.
// Copyright 2016 The Go Authors. All rights reserved.
2024-02-18 10:42:21 +00:00
// Use of this source code is governed by a BSD-style
2024-02-18 10:42:21 +00:00
// license that can be found in the LICENSE file.
//go:build !go1.10
// Package idna implements IDNA2008 using the compatibility processing
2024-02-18 10:42:21 +00:00
// defined by UTS (Unicode Technical Standard) #46, which defines a standard to
2024-02-18 10:42:21 +00:00
// deal with the transition from IDNA2003.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// IDNA2008 (Internationalized Domain Names for Applications), is defined in RFC
2024-02-18 10:42:21 +00:00
// 5890, RFC 5891, RFC 5892, RFC 5893 and RFC 5894.
2024-02-18 10:42:21 +00:00
// UTS #46 is defined in https://www.unicode.org/reports/tr46.
2024-02-18 10:42:21 +00:00
// See https://unicode.org/cldr/utility/idna.jsp for a visualization of the
2024-02-18 10:42:21 +00:00
// differences between these two standards.
2024-02-18 10:42:21 +00:00
package idna // import "golang.org/x/net/idna"
import (
"fmt"
"strings"
"unicode/utf8"
"golang.org/x/text/secure/bidirule"
"golang.org/x/text/unicode/norm"
)
// NOTE: Unlike common practice in Go APIs, the functions will return a
2024-02-18 10:42:21 +00:00
// sanitized domain name in case of errors. Browsers sometimes use a partially
2024-02-18 10:42:21 +00:00
// evaluated string as lookup.
2024-02-18 10:42:21 +00:00
// TODO: the current error handling is, in my opinion, the least opinionated.
2024-02-18 10:42:21 +00:00
// Other strategies are also viable, though:
2024-02-18 10:42:21 +00:00
// Option 1) Return an empty string in case of error, but allow the user to
2024-02-18 10:42:21 +00:00
// specify explicitly which errors to ignore.
2024-02-18 10:42:21 +00:00
// Option 2) Return the partially evaluated string if it is itself a valid
2024-02-18 10:42:21 +00:00
// string, otherwise return the empty string in case of error.
2024-02-18 10:42:21 +00:00
// Option 3) Option 1 and 2.
2024-02-18 10:42:21 +00:00
// Option 4) Always return an empty string for now and implement Option 1 as
2024-02-18 10:42:21 +00:00
// needed, and document that the return string may not be empty in case of
2024-02-18 10:42:21 +00:00
// error in the future.
2024-02-18 10:42:21 +00:00
// I think Option 1 is best, but it is quite opinionated.
// ToASCII is a wrapper for Punycode.ToASCII.
2024-02-18 10:42:21 +00:00
func ToASCII(s string) (string, error) {
2024-02-18 10:42:21 +00:00
return Punycode.process(s, true)
2024-02-18 10:42:21 +00:00
}
// ToUnicode is a wrapper for Punycode.ToUnicode.
2024-02-18 10:42:21 +00:00
func ToUnicode(s string) (string, error) {
2024-02-18 10:42:21 +00:00
return Punycode.process(s, false)
2024-02-18 10:42:21 +00:00
}
// An Option configures a Profile at creation time.
2024-02-18 10:42:21 +00:00
type Option func(*options)
// Transitional sets a Profile to use the Transitional mapping as defined in UTS
2024-02-18 10:42:21 +00:00
// #46. This will cause, for example, "ß" to be mapped to "ss". Using the
2024-02-18 10:42:21 +00:00
// transitional mapping provides a compromise between IDNA2003 and IDNA2008
2024-02-18 10:42:21 +00:00
// compatibility. It is used by some browsers when resolving domain names. This
2024-02-18 10:42:21 +00:00
// option is only meaningful if combined with MapForLookup.
2024-02-18 10:42:21 +00:00
func Transitional(transitional bool) Option {
2024-02-18 10:42:21 +00:00
return func(o *options) { o.transitional = transitional }
2024-02-18 10:42:21 +00:00
}
// VerifyDNSLength sets whether a Profile should fail if any of the IDN parts
2024-02-18 10:42:21 +00:00
// are longer than allowed by the RFC.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// This option corresponds to the VerifyDnsLength flag in UTS #46.
2024-02-18 10:42:21 +00:00
func VerifyDNSLength(verify bool) Option {
2024-02-18 10:42:21 +00:00
return func(o *options) { o.verifyDNSLength = verify }
2024-02-18 10:42:21 +00:00
}
// RemoveLeadingDots removes leading label separators. Leading runes that map to
2024-02-18 10:42:21 +00:00
// dots, such as U+3002 IDEOGRAPHIC FULL STOP, are removed as well.
2024-02-18 10:42:21 +00:00
func RemoveLeadingDots(remove bool) Option {
2024-02-18 10:42:21 +00:00
return func(o *options) { o.removeLeadingDots = remove }
2024-02-18 10:42:21 +00:00
}
// ValidateLabels sets whether to check the mandatory label validation criteria
2024-02-18 10:42:21 +00:00
// as defined in Section 5.4 of RFC 5891. This includes testing for correct use
2024-02-18 10:42:21 +00:00
// of hyphens ('-'), normalization, validity of runes, and the context rules.
2024-02-18 10:42:21 +00:00
// In particular, ValidateLabels also sets the CheckHyphens and CheckJoiners flags
2024-02-18 10:42:21 +00:00
// in UTS #46.
2024-02-18 10:42:21 +00:00
func ValidateLabels(enable bool) Option {
2024-02-18 10:42:21 +00:00
return func(o *options) {
2024-02-18 10:42:21 +00:00
// Don't override existing mappings, but set one that at least checks
2024-02-18 10:42:21 +00:00
// normalization if it is not set.
2024-02-18 10:42:21 +00:00
if o.mapping == nil && enable {
2024-02-18 10:42:21 +00:00
o.mapping = normalize
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
o.trie = trie
2024-02-18 10:42:21 +00:00
o.checkJoiners = enable
2024-02-18 10:42:21 +00:00
o.checkHyphens = enable
2024-02-18 10:42:21 +00:00
if enable {
2024-02-18 10:42:21 +00:00
o.fromPuny = validateFromPunycode
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
o.fromPuny = nil
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// CheckHyphens sets whether to check for correct use of hyphens ('-') in
2024-02-18 10:42:21 +00:00
// labels. Most web browsers do not have this option set, since labels such as
2024-02-18 10:42:21 +00:00
// "r3---sn-apo3qvuoxuxbt-j5pe" are in common use.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// This option corresponds to the CheckHyphens flag in UTS #46.
2024-02-18 10:42:21 +00:00
func CheckHyphens(enable bool) Option {
2024-02-18 10:42:21 +00:00
return func(o *options) { o.checkHyphens = enable }
2024-02-18 10:42:21 +00:00
}
// CheckJoiners sets whether to check the ContextJ rules as defined in Appendix
2024-02-18 10:42:21 +00:00
// A of RFC 5892, concerning the use of joiner runes.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// This option corresponds to the CheckJoiners flag in UTS #46.
2024-02-18 10:42:21 +00:00
func CheckJoiners(enable bool) Option {
2024-02-18 10:42:21 +00:00
return func(o *options) {
2024-02-18 10:42:21 +00:00
o.trie = trie
2024-02-18 10:42:21 +00:00
o.checkJoiners = enable
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// StrictDomainName limits the set of permissible ASCII characters to those
2024-02-18 10:42:21 +00:00
// allowed in domain names as defined in RFC 1034 (A-Z, a-z, 0-9 and the
2024-02-18 10:42:21 +00:00
// hyphen). This is set by default for MapForLookup and ValidateForRegistration,
2024-02-18 10:42:21 +00:00
// but is only useful if ValidateLabels is set.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// This option is useful, for instance, for browsers that allow characters
2024-02-18 10:42:21 +00:00
// outside this range, for example a '_' (U+005F LOW LINE). See
2024-02-18 10:42:21 +00:00
// http://www.rfc-editor.org/std/std3.txt for more details.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// This option corresponds to the UseSTD3ASCIIRules flag in UTS #46.
2024-02-18 10:42:21 +00:00
func StrictDomainName(use bool) Option {
2024-02-18 10:42:21 +00:00
return func(o *options) { o.useSTD3Rules = use }
2024-02-18 10:42:21 +00:00
}
// NOTE: the following options pull in tables. The tables should not be linked
2024-02-18 10:42:21 +00:00
// in as long as the options are not used.
// BidiRule enables the Bidi rule as defined in RFC 5893. Any application
2024-02-18 10:42:21 +00:00
// that relies on proper validation of labels should include this rule.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// This option corresponds to the CheckBidi flag in UTS #46.
2024-02-18 10:42:21 +00:00
func BidiRule() Option {
2024-02-18 10:42:21 +00:00
return func(o *options) { o.bidirule = bidirule.ValidString }
2024-02-18 10:42:21 +00:00
}
// ValidateForRegistration sets validation options to verify that a given IDN is
2024-02-18 10:42:21 +00:00
// properly formatted for registration as defined by Section 4 of RFC 5891.
2024-02-18 10:42:21 +00:00
func ValidateForRegistration() Option {
2024-02-18 10:42:21 +00:00
return func(o *options) {
2024-02-18 10:42:21 +00:00
o.mapping = validateRegistration
2024-02-18 10:42:21 +00:00
StrictDomainName(true)(o)
2024-02-18 10:42:21 +00:00
ValidateLabels(true)(o)
2024-02-18 10:42:21 +00:00
VerifyDNSLength(true)(o)
2024-02-18 10:42:21 +00:00
BidiRule()(o)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// MapForLookup sets validation and mapping options such that a given IDN is
2024-02-18 10:42:21 +00:00
// transformed for domain name lookup according to the requirements set out in
2024-02-18 10:42:21 +00:00
// Section 5 of RFC 5891. The mappings follow the recommendations of RFC 5894,
2024-02-18 10:42:21 +00:00
// RFC 5895 and UTS 46. It does not add the Bidi Rule. Use the BidiRule option
2024-02-18 10:42:21 +00:00
// to add this check.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// The mappings include normalization and mapping case, width and other
2024-02-18 10:42:21 +00:00
// compatibility mappings.
2024-02-18 10:42:21 +00:00
func MapForLookup() Option {
2024-02-18 10:42:21 +00:00
return func(o *options) {
2024-02-18 10:42:21 +00:00
o.mapping = validateAndMap
2024-02-18 10:42:21 +00:00
StrictDomainName(true)(o)
2024-02-18 10:42:21 +00:00
ValidateLabels(true)(o)
2024-02-18 10:42:21 +00:00
RemoveLeadingDots(true)(o)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
type options struct {
transitional bool
useSTD3Rules bool
checkHyphens bool
checkJoiners bool
verifyDNSLength bool
2024-02-18 10:42:21 +00:00
removeLeadingDots bool
trie *idnaTrie
// fromPuny calls validation rules when converting A-labels to U-labels.
2024-02-18 10:42:21 +00:00
fromPuny func(p *Profile, s string) error
// mapping implements a validation and mapping step as defined in RFC 5895
2024-02-18 10:42:21 +00:00
// or UTS 46, tailored to, for example, domain registration or lookup.
2024-02-18 10:42:21 +00:00
mapping func(p *Profile, s string) (string, error)
// bidirule, if specified, checks whether s conforms to the Bidi Rule
2024-02-18 10:42:21 +00:00
// defined in RFC 5893.
2024-02-18 10:42:21 +00:00
bidirule func(s string) bool
}
// A Profile defines the configuration of a IDNA mapper.
2024-02-18 10:42:21 +00:00
type Profile struct {
options
}
func apply(o *options, opts []Option) {
2024-02-18 10:42:21 +00:00
for _, f := range opts {
2024-02-18 10:42:21 +00:00
f(o)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// New creates a new Profile.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// With no options, the returned Profile is the most permissive and equals the
2024-02-18 10:42:21 +00:00
// Punycode Profile. Options can be passed to further restrict the Profile. The
2024-02-18 10:42:21 +00:00
// MapForLookup and ValidateForRegistration options set a collection of options,
2024-02-18 10:42:21 +00:00
// for lookup and registration purposes respectively, which can be tailored by
2024-02-18 10:42:21 +00:00
// adding more fine-grained options, where later options override earlier
2024-02-18 10:42:21 +00:00
// options.
2024-02-18 10:42:21 +00:00
func New(o ...Option) *Profile {
2024-02-18 10:42:21 +00:00
p := &Profile{}
2024-02-18 10:42:21 +00:00
apply(&p.options, o)
2024-02-18 10:42:21 +00:00
return p
2024-02-18 10:42:21 +00:00
}
// ToASCII converts a domain or domain label to its ASCII form. For example,
2024-02-18 10:42:21 +00:00
// ToASCII("bücher.example.com") is "xn--bcher-kva.example.com", and
2024-02-18 10:42:21 +00:00
// ToASCII("golang") is "golang". If an error is encountered it will return
2024-02-18 10:42:21 +00:00
// an error and a (partially) processed result.
2024-02-18 10:42:21 +00:00
func (p *Profile) ToASCII(s string) (string, error) {
2024-02-18 10:42:21 +00:00
return p.process(s, true)
2024-02-18 10:42:21 +00:00
}
// ToUnicode converts a domain or domain label to its Unicode form. For example,
2024-02-18 10:42:21 +00:00
// ToUnicode("xn--bcher-kva.example.com") is "bücher.example.com", and
2024-02-18 10:42:21 +00:00
// ToUnicode("golang") is "golang". If an error is encountered it will return
2024-02-18 10:42:21 +00:00
// an error and a (partially) processed result.
2024-02-18 10:42:21 +00:00
func (p *Profile) ToUnicode(s string) (string, error) {
2024-02-18 10:42:21 +00:00
pp := *p
2024-02-18 10:42:21 +00:00
pp.transitional = false
2024-02-18 10:42:21 +00:00
return pp.process(s, false)
2024-02-18 10:42:21 +00:00
}
// String reports a string with a description of the profile for debugging
2024-02-18 10:42:21 +00:00
// purposes. The string format may change with different versions.
2024-02-18 10:42:21 +00:00
func (p *Profile) String() string {
2024-02-18 10:42:21 +00:00
s := ""
2024-02-18 10:42:21 +00:00
if p.transitional {
2024-02-18 10:42:21 +00:00
s = "Transitional"
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
s = "NonTransitional"
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if p.useSTD3Rules {
2024-02-18 10:42:21 +00:00
s += ":UseSTD3Rules"
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if p.checkHyphens {
2024-02-18 10:42:21 +00:00
s += ":CheckHyphens"
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if p.checkJoiners {
2024-02-18 10:42:21 +00:00
s += ":CheckJoiners"
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if p.verifyDNSLength {
2024-02-18 10:42:21 +00:00
s += ":VerifyDNSLength"
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return s
2024-02-18 10:42:21 +00:00
}
var (
2024-02-18 10:42:21 +00:00
// Punycode is a Profile that does raw punycode processing with a minimum
2024-02-18 10:42:21 +00:00
// of validation.
2024-02-18 10:42:21 +00:00
Punycode *Profile = punycode
// Lookup is the recommended profile for looking up domain names, according
2024-02-18 10:42:21 +00:00
// to Section 5 of RFC 5891. The exact configuration of this profile may
2024-02-18 10:42:21 +00:00
// change over time.
2024-02-18 10:42:21 +00:00
Lookup *Profile = lookup
// Display is the recommended profile for displaying domain names.
2024-02-18 10:42:21 +00:00
// The configuration of this profile may change over time.
2024-02-18 10:42:21 +00:00
Display *Profile = display
// Registration is the recommended profile for checking whether a given
2024-02-18 10:42:21 +00:00
// IDN is valid for registration, according to Section 4 of RFC 5891.
2024-02-18 10:42:21 +00:00
Registration *Profile = registration
punycode = &Profile{}
lookup = &Profile{options{
transitional: true,
2024-02-18 10:42:21 +00:00
removeLeadingDots: true,
useSTD3Rules: true,
checkHyphens: true,
checkJoiners: true,
trie: trie,
fromPuny: validateFromPunycode,
mapping: validateAndMap,
bidirule: bidirule.ValidString,
2024-02-18 10:42:21 +00:00
}}
2024-02-18 10:42:21 +00:00
display = &Profile{options{
useSTD3Rules: true,
2024-02-18 10:42:21 +00:00
removeLeadingDots: true,
checkHyphens: true,
checkJoiners: true,
trie: trie,
fromPuny: validateFromPunycode,
mapping: validateAndMap,
bidirule: bidirule.ValidString,
2024-02-18 10:42:21 +00:00
}}
2024-02-18 10:42:21 +00:00
registration = &Profile{options{
useSTD3Rules: true,
2024-02-18 10:42:21 +00:00
verifyDNSLength: true,
checkHyphens: true,
checkJoiners: true,
trie: trie,
fromPuny: validateFromPunycode,
mapping: validateRegistration,
bidirule: bidirule.ValidString,
2024-02-18 10:42:21 +00:00
}}
// TODO: profiles
2024-02-18 10:42:21 +00:00
// Register: recommended for approving domain names: don't do any mappings
2024-02-18 10:42:21 +00:00
// but rather reject on invalid input. Bundle or block deviation characters.
2024-02-18 10:42:21 +00:00
)
type labelError struct{ label, code_ string }
func (e labelError) code() string { return e.code_ }
2024-02-18 10:42:21 +00:00
func (e labelError) Error() string {
2024-02-18 10:42:21 +00:00
return fmt.Sprintf("idna: invalid label %q", e.label)
2024-02-18 10:42:21 +00:00
}
type runeError rune
func (e runeError) code() string { return "P1" }
2024-02-18 10:42:21 +00:00
func (e runeError) Error() string {
2024-02-18 10:42:21 +00:00
return fmt.Sprintf("idna: disallowed rune %U", e)
2024-02-18 10:42:21 +00:00
}
// process implements the algorithm described in section 4 of UTS #46,
2024-02-18 10:42:21 +00:00
// see https://www.unicode.org/reports/tr46.
2024-02-18 10:42:21 +00:00
func (p *Profile) process(s string, toASCII bool) (string, error) {
2024-02-18 10:42:21 +00:00
var err error
2024-02-18 10:42:21 +00:00
if p.mapping != nil {
2024-02-18 10:42:21 +00:00
s, err = p.mapping(p, s)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
// Remove leading empty labels.
2024-02-18 10:42:21 +00:00
if p.removeLeadingDots {
2024-02-18 10:42:21 +00:00
for ; len(s) > 0 && s[0] == '.'; s = s[1:] {
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
// It seems like we should only create this error on ToASCII, but the
2024-02-18 10:42:21 +00:00
// UTS 46 conformance tests suggests we should always check this.
2024-02-18 10:42:21 +00:00
if err == nil && p.verifyDNSLength && s == "" {
2024-02-18 10:42:21 +00:00
err = &labelError{s, "A4"}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
labels := labelIter{orig: s}
2024-02-18 10:42:21 +00:00
for ; !labels.done(); labels.next() {
2024-02-18 10:42:21 +00:00
label := labels.label()
2024-02-18 10:42:21 +00:00
if label == "" {
2024-02-18 10:42:21 +00:00
// Empty labels are not okay. The label iterator skips the last
2024-02-18 10:42:21 +00:00
// label if it is empty.
2024-02-18 10:42:21 +00:00
if err == nil && p.verifyDNSLength {
2024-02-18 10:42:21 +00:00
err = &labelError{s, "A4"}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
continue
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if strings.HasPrefix(label, acePrefix) {
2024-02-18 10:42:21 +00:00
u, err2 := decode(label[len(acePrefix):])
2024-02-18 10:42:21 +00:00
if err2 != nil {
2024-02-18 10:42:21 +00:00
if err == nil {
2024-02-18 10:42:21 +00:00
err = err2
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
// Spec says keep the old label.
2024-02-18 10:42:21 +00:00
continue
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
labels.set(u)
2024-02-18 10:42:21 +00:00
if err == nil && p.fromPuny != nil {
2024-02-18 10:42:21 +00:00
err = p.fromPuny(p, u)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if err == nil {
2024-02-18 10:42:21 +00:00
// This should be called on NonTransitional, according to the
2024-02-18 10:42:21 +00:00
// spec, but that currently does not have any effect. Use the
2024-02-18 10:42:21 +00:00
// original profile to preserve options.
2024-02-18 10:42:21 +00:00
err = p.validateLabel(u)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
} else if err == nil {
2024-02-18 10:42:21 +00:00
err = p.validateLabel(label)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if toASCII {
2024-02-18 10:42:21 +00:00
for labels.reset(); !labels.done(); labels.next() {
2024-02-18 10:42:21 +00:00
label := labels.label()
2024-02-18 10:42:21 +00:00
if !ascii(label) {
2024-02-18 10:42:21 +00:00
a, err2 := encode(acePrefix, label)
2024-02-18 10:42:21 +00:00
if err == nil {
2024-02-18 10:42:21 +00:00
err = err2
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
label = a
2024-02-18 10:42:21 +00:00
labels.set(a)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
n := len(label)
2024-02-18 10:42:21 +00:00
if p.verifyDNSLength && err == nil && (n == 0 || n > 63) {
2024-02-18 10:42:21 +00:00
err = &labelError{label, "A4"}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
s = labels.result()
2024-02-18 10:42:21 +00:00
if toASCII && p.verifyDNSLength && err == nil {
2024-02-18 10:42:21 +00:00
// Compute the length of the domain name minus the root label and its dot.
2024-02-18 10:42:21 +00:00
n := len(s)
2024-02-18 10:42:21 +00:00
if n > 0 && s[n-1] == '.' {
2024-02-18 10:42:21 +00:00
n--
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if len(s) < 1 || n > 253 {
2024-02-18 10:42:21 +00:00
err = &labelError{s, "A4"}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return s, err
2024-02-18 10:42:21 +00:00
}
func normalize(p *Profile, s string) (string, error) {
2024-02-18 10:42:21 +00:00
return norm.NFC.String(s), nil
2024-02-18 10:42:21 +00:00
}
func validateRegistration(p *Profile, s string) (string, error) {
2024-02-18 10:42:21 +00:00
if !norm.NFC.IsNormalString(s) {
2024-02-18 10:42:21 +00:00
return s, &labelError{s, "V1"}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
for i := 0; i < len(s); {
2024-02-18 10:42:21 +00:00
v, sz := trie.lookupString(s[i:])
2024-02-18 10:42:21 +00:00
// Copy bytes not copied so far.
2024-02-18 10:42:21 +00:00
switch p.simplify(info(v).category()) {
2024-02-18 10:42:21 +00:00
// TODO: handle the NV8 defined in the Unicode idna data set to allow
2024-02-18 10:42:21 +00:00
// for strict conformance to IDNA2008.
2024-02-18 10:42:21 +00:00
case valid, deviation:
2024-02-18 10:42:21 +00:00
case disallowed, mapped, unknown, ignored:
2024-02-18 10:42:21 +00:00
r, _ := utf8.DecodeRuneInString(s[i:])
2024-02-18 10:42:21 +00:00
return s, runeError(r)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
i += sz
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return s, nil
2024-02-18 10:42:21 +00:00
}
func validateAndMap(p *Profile, s string) (string, error) {
2024-02-18 10:42:21 +00:00
var (
err error
b []byte
k int
2024-02-18 10:42:21 +00:00
)
2024-02-18 10:42:21 +00:00
for i := 0; i < len(s); {
2024-02-18 10:42:21 +00:00
v, sz := trie.lookupString(s[i:])
2024-02-18 10:42:21 +00:00
start := i
2024-02-18 10:42:21 +00:00
i += sz
2024-02-18 10:42:21 +00:00
// Copy bytes not copied so far.
2024-02-18 10:42:21 +00:00
switch p.simplify(info(v).category()) {
2024-02-18 10:42:21 +00:00
case valid:
2024-02-18 10:42:21 +00:00
continue
2024-02-18 10:42:21 +00:00
case disallowed:
2024-02-18 10:42:21 +00:00
if err == nil {
2024-02-18 10:42:21 +00:00
r, _ := utf8.DecodeRuneInString(s[start:])
2024-02-18 10:42:21 +00:00
err = runeError(r)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
continue
2024-02-18 10:42:21 +00:00
case mapped, deviation:
2024-02-18 10:42:21 +00:00
b = append(b, s[k:start]...)
2024-02-18 10:42:21 +00:00
b = info(v).appendMapping(b, s[start:i])
2024-02-18 10:42:21 +00:00
case ignored:
2024-02-18 10:42:21 +00:00
b = append(b, s[k:start]...)
2024-02-18 10:42:21 +00:00
// drop the rune
2024-02-18 10:42:21 +00:00
case unknown:
2024-02-18 10:42:21 +00:00
b = append(b, s[k:start]...)
2024-02-18 10:42:21 +00:00
b = append(b, "\ufffd"...)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
k = i
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if k == 0 {
2024-02-18 10:42:21 +00:00
// No changes so far.
2024-02-18 10:42:21 +00:00
s = norm.NFC.String(s)
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
b = append(b, s[k:]...)
2024-02-18 10:42:21 +00:00
if norm.NFC.QuickSpan(b) != len(b) {
2024-02-18 10:42:21 +00:00
b = norm.NFC.Bytes(b)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
// TODO: the punycode converters require strings as input.
2024-02-18 10:42:21 +00:00
s = string(b)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return s, err
2024-02-18 10:42:21 +00:00
}
// A labelIter allows iterating over domain name labels.
2024-02-18 10:42:21 +00:00
type labelIter struct {
orig string
slice []string
2024-02-18 10:42:21 +00:00
curStart int
curEnd int
i int
2024-02-18 10:42:21 +00:00
}
func (l *labelIter) reset() {
2024-02-18 10:42:21 +00:00
l.curStart = 0
2024-02-18 10:42:21 +00:00
l.curEnd = 0
2024-02-18 10:42:21 +00:00
l.i = 0
2024-02-18 10:42:21 +00:00
}
func (l *labelIter) done() bool {
2024-02-18 10:42:21 +00:00
return l.curStart >= len(l.orig)
2024-02-18 10:42:21 +00:00
}
func (l *labelIter) result() string {
2024-02-18 10:42:21 +00:00
if l.slice != nil {
2024-02-18 10:42:21 +00:00
return strings.Join(l.slice, ".")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return l.orig
2024-02-18 10:42:21 +00:00
}
func (l *labelIter) label() string {
2024-02-18 10:42:21 +00:00
if l.slice != nil {
2024-02-18 10:42:21 +00:00
return l.slice[l.i]
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
p := strings.IndexByte(l.orig[l.curStart:], '.')
2024-02-18 10:42:21 +00:00
l.curEnd = l.curStart + p
2024-02-18 10:42:21 +00:00
if p == -1 {
2024-02-18 10:42:21 +00:00
l.curEnd = len(l.orig)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return l.orig[l.curStart:l.curEnd]
2024-02-18 10:42:21 +00:00
}
// next sets the value to the next label. It skips the last label if it is empty.
2024-02-18 10:42:21 +00:00
func (l *labelIter) next() {
2024-02-18 10:42:21 +00:00
l.i++
2024-02-18 10:42:21 +00:00
if l.slice != nil {
2024-02-18 10:42:21 +00:00
if l.i >= len(l.slice) || l.i == len(l.slice)-1 && l.slice[l.i] == "" {
2024-02-18 10:42:21 +00:00
l.curStart = len(l.orig)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
l.curStart = l.curEnd + 1
2024-02-18 10:42:21 +00:00
if l.curStart == len(l.orig)-1 && l.orig[l.curStart] == '.' {
2024-02-18 10:42:21 +00:00
l.curStart = len(l.orig)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
func (l *labelIter) set(s string) {
2024-02-18 10:42:21 +00:00
if l.slice == nil {
2024-02-18 10:42:21 +00:00
l.slice = strings.Split(l.orig, ".")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
l.slice[l.i] = s
2024-02-18 10:42:21 +00:00
}
// acePrefix is the ASCII Compatible Encoding prefix.
2024-02-18 10:42:21 +00:00
const acePrefix = "xn--"
func (p *Profile) simplify(cat category) category {
2024-02-18 10:42:21 +00:00
switch cat {
2024-02-18 10:42:21 +00:00
case disallowedSTD3Mapped:
2024-02-18 10:42:21 +00:00
if p.useSTD3Rules {
2024-02-18 10:42:21 +00:00
cat = disallowed
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
cat = mapped
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
case disallowedSTD3Valid:
2024-02-18 10:42:21 +00:00
if p.useSTD3Rules {
2024-02-18 10:42:21 +00:00
cat = disallowed
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
cat = valid
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
case deviation:
2024-02-18 10:42:21 +00:00
if !p.transitional {
2024-02-18 10:42:21 +00:00
cat = valid
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
case validNV8, validXV8:
2024-02-18 10:42:21 +00:00
// TODO: handle V2008
2024-02-18 10:42:21 +00:00
cat = valid
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return cat
2024-02-18 10:42:21 +00:00
}
func validateFromPunycode(p *Profile, s string) error {
2024-02-18 10:42:21 +00:00
if !norm.NFC.IsNormalString(s) {
2024-02-18 10:42:21 +00:00
return &labelError{s, "V1"}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
for i := 0; i < len(s); {
2024-02-18 10:42:21 +00:00
v, sz := trie.lookupString(s[i:])
2024-02-18 10:42:21 +00:00
if c := p.simplify(info(v).category()); c != valid && c != deviation {
2024-02-18 10:42:21 +00:00
return &labelError{s, "V6"}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
i += sz
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}
const (
zwnj = "\u200c"
zwj = "\u200d"
2024-02-18 10:42:21 +00:00
)
type joinState int8
const (
stateStart joinState = iota
2024-02-18 10:42:21 +00:00
stateVirama
2024-02-18 10:42:21 +00:00
stateBefore
2024-02-18 10:42:21 +00:00
stateBeforeVirama
2024-02-18 10:42:21 +00:00
stateAfter
2024-02-18 10:42:21 +00:00
stateFAIL
)
var joinStates = [][numJoinTypes]joinState{
2024-02-18 10:42:21 +00:00
stateStart: {
joiningL: stateBefore,
joiningD: stateBefore,
joinZWNJ: stateFAIL,
joinZWJ: stateFAIL,
2024-02-18 10:42:21 +00:00
joinVirama: stateVirama,
},
2024-02-18 10:42:21 +00:00
stateVirama: {
2024-02-18 10:42:21 +00:00
joiningL: stateBefore,
2024-02-18 10:42:21 +00:00
joiningD: stateBefore,
},
2024-02-18 10:42:21 +00:00
stateBefore: {
joiningL: stateBefore,
joiningD: stateBefore,
joiningT: stateBefore,
joinZWNJ: stateAfter,
joinZWJ: stateFAIL,
2024-02-18 10:42:21 +00:00
joinVirama: stateBeforeVirama,
},
2024-02-18 10:42:21 +00:00
stateBeforeVirama: {
2024-02-18 10:42:21 +00:00
joiningL: stateBefore,
2024-02-18 10:42:21 +00:00
joiningD: stateBefore,
2024-02-18 10:42:21 +00:00
joiningT: stateBefore,
},
2024-02-18 10:42:21 +00:00
stateAfter: {
joiningL: stateFAIL,
joiningD: stateBefore,
joiningT: stateAfter,
joiningR: stateStart,
joinZWNJ: stateFAIL,
joinZWJ: stateFAIL,
2024-02-18 10:42:21 +00:00
joinVirama: stateAfter, // no-op as we can't accept joiners here
2024-02-18 10:42:21 +00:00
},
2024-02-18 10:42:21 +00:00
stateFAIL: {
0: stateFAIL,
joiningL: stateFAIL,
joiningD: stateFAIL,
joiningT: stateFAIL,
joiningR: stateFAIL,
joinZWNJ: stateFAIL,
joinZWJ: stateFAIL,
2024-02-18 10:42:21 +00:00
joinVirama: stateFAIL,
},
}
// validateLabel validates the criteria from Section 4.1. Item 1, 4, and 6 are
2024-02-18 10:42:21 +00:00
// already implicitly satisfied by the overall implementation.
2024-02-18 10:42:21 +00:00
func (p *Profile) validateLabel(s string) error {
2024-02-18 10:42:21 +00:00
if s == "" {
2024-02-18 10:42:21 +00:00
if p.verifyDNSLength {
2024-02-18 10:42:21 +00:00
return &labelError{s, "A4"}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if p.bidirule != nil && !p.bidirule(s) {
2024-02-18 10:42:21 +00:00
return &labelError{s, "B"}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if p.checkHyphens {
2024-02-18 10:42:21 +00:00
if len(s) > 4 && s[2] == '-' && s[3] == '-' {
2024-02-18 10:42:21 +00:00
return &labelError{s, "V2"}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if s[0] == '-' || s[len(s)-1] == '-' {
2024-02-18 10:42:21 +00:00
return &labelError{s, "V3"}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !p.checkJoiners {
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
trie := p.trie // p.checkJoiners is only set if trie is set.
2024-02-18 10:42:21 +00:00
// TODO: merge the use of this in the trie.
2024-02-18 10:42:21 +00:00
v, sz := trie.lookupString(s)
2024-02-18 10:42:21 +00:00
x := info(v)
2024-02-18 10:42:21 +00:00
if x.isModifier() {
2024-02-18 10:42:21 +00:00
return &labelError{s, "V5"}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
// Quickly return in the absence of zero-width (non) joiners.
2024-02-18 10:42:21 +00:00
if strings.Index(s, zwj) == -1 && strings.Index(s, zwnj) == -1 {
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
st := stateStart
2024-02-18 10:42:21 +00:00
for i := 0; ; {
2024-02-18 10:42:21 +00:00
jt := x.joinType()
2024-02-18 10:42:21 +00:00
if s[i:i+sz] == zwj {
2024-02-18 10:42:21 +00:00
jt = joinZWJ
2024-02-18 10:42:21 +00:00
} else if s[i:i+sz] == zwnj {
2024-02-18 10:42:21 +00:00
jt = joinZWNJ
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
st = joinStates[st][jt]
2024-02-18 10:42:21 +00:00
if x.isViramaModifier() {
2024-02-18 10:42:21 +00:00
st = joinStates[st][joinVirama]
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if i += sz; i == len(s) {
2024-02-18 10:42:21 +00:00
break
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
v, sz = trie.lookupString(s[i:])
2024-02-18 10:42:21 +00:00
x = info(v)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if st == stateFAIL || st == stateAfter {
2024-02-18 10:42:21 +00:00
return &labelError{s, "C"}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}
func ascii(s string) bool {
2024-02-18 10:42:21 +00:00
for i := 0; i < len(s); i++ {
2024-02-18 10:42:21 +00:00
if s[i] >= utf8.RuneSelf {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}