1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package idna
20
21 import (
22 "fmt"
23 "strings"
24 "unicode/utf8"
25
26 "golang.org/x/text/secure/bidirule"
27 "golang.org/x/text/unicode/norm"
28 )
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 func ToASCII(s string) (string, error) {
47 return Punycode.process(s, true)
48 }
49
50
51 func ToUnicode(s string) (string, error) {
52 return Punycode.process(s, false)
53 }
54
55
56 type Option func(*options)
57
58
59
60
61
62
63 func Transitional(transitional bool) Option {
64 return func(o *options) { o.transitional = transitional }
65 }
66
67
68
69
70
71 func VerifyDNSLength(verify bool) Option {
72 return func(o *options) { o.verifyDNSLength = verify }
73 }
74
75
76
77 func RemoveLeadingDots(remove bool) Option {
78 return func(o *options) { o.removeLeadingDots = remove }
79 }
80
81
82
83
84
85
86 func ValidateLabels(enable bool) Option {
87 return func(o *options) {
88
89
90 if o.mapping == nil && enable {
91 o.mapping = normalize
92 }
93 o.trie = trie
94 o.checkJoiners = enable
95 o.checkHyphens = enable
96 if enable {
97 o.fromPuny = validateFromPunycode
98 } else {
99 o.fromPuny = nil
100 }
101 }
102 }
103
104
105
106
107
108
109 func CheckHyphens(enable bool) Option {
110 return func(o *options) { o.checkHyphens = enable }
111 }
112
113
114
115
116
117 func CheckJoiners(enable bool) Option {
118 return func(o *options) {
119 o.trie = trie
120 o.checkJoiners = enable
121 }
122 }
123
124
125
126
127
128
129
130
131
132
133
134 func StrictDomainName(use bool) Option {
135 return func(o *options) { o.useSTD3Rules = use }
136 }
137
138
139
140
141
142
143
144
145 func BidiRule() Option {
146 return func(o *options) { o.bidirule = bidirule.ValidString }
147 }
148
149
150
151 func ValidateForRegistration() Option {
152 return func(o *options) {
153 o.mapping = validateRegistration
154 StrictDomainName(true)(o)
155 ValidateLabels(true)(o)
156 VerifyDNSLength(true)(o)
157 BidiRule()(o)
158 }
159 }
160
161
162
163
164
165
166
167
168
169 func MapForLookup() Option {
170 return func(o *options) {
171 o.mapping = validateAndMap
172 StrictDomainName(true)(o)
173 ValidateLabels(true)(o)
174 RemoveLeadingDots(true)(o)
175 }
176 }
177
178 type options struct {
179 transitional bool
180 useSTD3Rules bool
181 checkHyphens bool
182 checkJoiners bool
183 verifyDNSLength bool
184 removeLeadingDots bool
185
186 trie *idnaTrie
187
188
189 fromPuny func(p *Profile, s string) error
190
191
192
193 mapping func(p *Profile, s string) (string, error)
194
195
196
197 bidirule func(s string) bool
198 }
199
200
201 type Profile struct {
202 options
203 }
204
205 func apply(o *options, opts []Option) {
206 for _, f := range opts {
207 f(o)
208 }
209 }
210
211
212
213
214
215
216
217
218
219 func New(o ...Option) *Profile {
220 p := &Profile{}
221 apply(&p.options, o)
222 return p
223 }
224
225
226
227
228
229 func (p *Profile) ToASCII(s string) (string, error) {
230 return p.process(s, true)
231 }
232
233
234
235
236
237 func (p *Profile) ToUnicode(s string) (string, error) {
238 pp := *p
239 pp.transitional = false
240 return pp.process(s, false)
241 }
242
243
244
245 func (p *Profile) String() string {
246 s := ""
247 if p.transitional {
248 s = "Transitional"
249 } else {
250 s = "NonTransitional"
251 }
252 if p.useSTD3Rules {
253 s += ":UseSTD3Rules"
254 }
255 if p.checkHyphens {
256 s += ":CheckHyphens"
257 }
258 if p.checkJoiners {
259 s += ":CheckJoiners"
260 }
261 if p.verifyDNSLength {
262 s += ":VerifyDNSLength"
263 }
264 return s
265 }
266
267 var (
268
269
270 Punycode *Profile = punycode
271
272
273
274
275 Lookup *Profile = lookup
276
277
278
279 Display *Profile = display
280
281
282
283 Registration *Profile = registration
284
285 punycode = &Profile{}
286 lookup = &Profile{options{
287 transitional: true,
288 removeLeadingDots: true,
289 useSTD3Rules: true,
290 checkHyphens: true,
291 checkJoiners: true,
292 trie: trie,
293 fromPuny: validateFromPunycode,
294 mapping: validateAndMap,
295 bidirule: bidirule.ValidString,
296 }}
297 display = &Profile{options{
298 useSTD3Rules: true,
299 removeLeadingDots: true,
300 checkHyphens: true,
301 checkJoiners: true,
302 trie: trie,
303 fromPuny: validateFromPunycode,
304 mapping: validateAndMap,
305 bidirule: bidirule.ValidString,
306 }}
307 registration = &Profile{options{
308 useSTD3Rules: true,
309 verifyDNSLength: true,
310 checkHyphens: true,
311 checkJoiners: true,
312 trie: trie,
313 fromPuny: validateFromPunycode,
314 mapping: validateRegistration,
315 bidirule: bidirule.ValidString,
316 }}
317
318
319
320
321 )
322
323 type labelError struct{ label, code_ string }
324
325 func (e labelError) code() string { return e.code_ }
326 func (e labelError) Error() string {
327 return fmt.Sprintf("idna: invalid label %q", e.label)
328 }
329
330 type runeError rune
331
332 func (e runeError) code() string { return "P1" }
333 func (e runeError) Error() string {
334 return fmt.Sprintf("idna: disallowed rune %U", e)
335 }
336
337
338
339 func (p *Profile) process(s string, toASCII bool) (string, error) {
340 var err error
341 if p.mapping != nil {
342 s, err = p.mapping(p, s)
343 }
344
345 if p.removeLeadingDots {
346 for ; len(s) > 0 && s[0] == '.'; s = s[1:] {
347 }
348 }
349
350
351 if err == nil && p.verifyDNSLength && s == "" {
352 err = &labelError{s, "A4"}
353 }
354 labels := labelIter{orig: s}
355 for ; !labels.done(); labels.next() {
356 label := labels.label()
357 if label == "" {
358
359
360 if err == nil && p.verifyDNSLength {
361 err = &labelError{s, "A4"}
362 }
363 continue
364 }
365 if strings.HasPrefix(label, acePrefix) {
366 u, err2 := decode(label[len(acePrefix):])
367 if err2 != nil {
368 if err == nil {
369 err = err2
370 }
371
372 continue
373 }
374 labels.set(u)
375 if err == nil && p.fromPuny != nil {
376 err = p.fromPuny(p, u)
377 }
378 if err == nil {
379
380
381
382 err = p.validateLabel(u)
383 }
384 } else if err == nil {
385 err = p.validateLabel(label)
386 }
387 }
388 if toASCII {
389 for labels.reset(); !labels.done(); labels.next() {
390 label := labels.label()
391 if !ascii(label) {
392 a, err2 := encode(acePrefix, label)
393 if err == nil {
394 err = err2
395 }
396 label = a
397 labels.set(a)
398 }
399 n := len(label)
400 if p.verifyDNSLength && err == nil && (n == 0 || n > 63) {
401 err = &labelError{label, "A4"}
402 }
403 }
404 }
405 s = labels.result()
406 if toASCII && p.verifyDNSLength && err == nil {
407
408 n := len(s)
409 if n > 0 && s[n-1] == '.' {
410 n--
411 }
412 if len(s) < 1 || n > 253 {
413 err = &labelError{s, "A4"}
414 }
415 }
416 return s, err
417 }
418
419 func normalize(p *Profile, s string) (string, error) {
420 return norm.NFC.String(s), nil
421 }
422
423 func validateRegistration(p *Profile, s string) (string, error) {
424 if !norm.NFC.IsNormalString(s) {
425 return s, &labelError{s, "V1"}
426 }
427 for i := 0; i < len(s); {
428 v, sz := trie.lookupString(s[i:])
429
430 switch p.simplify(info(v).category()) {
431
432
433 case valid, deviation:
434 case disallowed, mapped, unknown, ignored:
435 r, _ := utf8.DecodeRuneInString(s[i:])
436 return s, runeError(r)
437 }
438 i += sz
439 }
440 return s, nil
441 }
442
443 func validateAndMap(p *Profile, s string) (string, error) {
444 var (
445 err error
446 b []byte
447 k int
448 )
449 for i := 0; i < len(s); {
450 v, sz := trie.lookupString(s[i:])
451 start := i
452 i += sz
453
454 switch p.simplify(info(v).category()) {
455 case valid:
456 continue
457 case disallowed:
458 if err == nil {
459 r, _ := utf8.DecodeRuneInString(s[start:])
460 err = runeError(r)
461 }
462 continue
463 case mapped, deviation:
464 b = append(b, s[k:start]...)
465 b = info(v).appendMapping(b, s[start:i])
466 case ignored:
467 b = append(b, s[k:start]...)
468
469 case unknown:
470 b = append(b, s[k:start]...)
471 b = append(b, "\ufffd"...)
472 }
473 k = i
474 }
475 if k == 0 {
476
477 s = norm.NFC.String(s)
478 } else {
479 b = append(b, s[k:]...)
480 if norm.NFC.QuickSpan(b) != len(b) {
481 b = norm.NFC.Bytes(b)
482 }
483
484 s = string(b)
485 }
486 return s, err
487 }
488
489
490 type labelIter struct {
491 orig string
492 slice []string
493 curStart int
494 curEnd int
495 i int
496 }
497
498 func (l *labelIter) reset() {
499 l.curStart = 0
500 l.curEnd = 0
501 l.i = 0
502 }
503
504 func (l *labelIter) done() bool {
505 return l.curStart >= len(l.orig)
506 }
507
508 func (l *labelIter) result() string {
509 if l.slice != nil {
510 return strings.Join(l.slice, ".")
511 }
512 return l.orig
513 }
514
515 func (l *labelIter) label() string {
516 if l.slice != nil {
517 return l.slice[l.i]
518 }
519 p := strings.IndexByte(l.orig[l.curStart:], '.')
520 l.curEnd = l.curStart + p
521 if p == -1 {
522 l.curEnd = len(l.orig)
523 }
524 return l.orig[l.curStart:l.curEnd]
525 }
526
527
528 func (l *labelIter) next() {
529 l.i++
530 if l.slice != nil {
531 if l.i >= len(l.slice) || l.i == len(l.slice)-1 && l.slice[l.i] == "" {
532 l.curStart = len(l.orig)
533 }
534 } else {
535 l.curStart = l.curEnd + 1
536 if l.curStart == len(l.orig)-1 && l.orig[l.curStart] == '.' {
537 l.curStart = len(l.orig)
538 }
539 }
540 }
541
542 func (l *labelIter) set(s string) {
543 if l.slice == nil {
544 l.slice = strings.Split(l.orig, ".")
545 }
546 l.slice[l.i] = s
547 }
548
549
550 const acePrefix = "xn--"
551
552 func (p *Profile) simplify(cat category) category {
553 switch cat {
554 case disallowedSTD3Mapped:
555 if p.useSTD3Rules {
556 cat = disallowed
557 } else {
558 cat = mapped
559 }
560 case disallowedSTD3Valid:
561 if p.useSTD3Rules {
562 cat = disallowed
563 } else {
564 cat = valid
565 }
566 case deviation:
567 if !p.transitional {
568 cat = valid
569 }
570 case validNV8, validXV8:
571
572 cat = valid
573 }
574 return cat
575 }
576
577 func validateFromPunycode(p *Profile, s string) error {
578 if !norm.NFC.IsNormalString(s) {
579 return &labelError{s, "V1"}
580 }
581 for i := 0; i < len(s); {
582 v, sz := trie.lookupString(s[i:])
583 if c := p.simplify(info(v).category()); c != valid && c != deviation {
584 return &labelError{s, "V6"}
585 }
586 i += sz
587 }
588 return nil
589 }
590
591 const (
592 zwnj = "\u200c"
593 zwj = "\u200d"
594 )
595
596 type joinState int8
597
598 const (
599 stateStart joinState = iota
600 stateVirama
601 stateBefore
602 stateBeforeVirama
603 stateAfter
604 stateFAIL
605 )
606
607 var joinStates = [][numJoinTypes]joinState{
608 stateStart: {
609 joiningL: stateBefore,
610 joiningD: stateBefore,
611 joinZWNJ: stateFAIL,
612 joinZWJ: stateFAIL,
613 joinVirama: stateVirama,
614 },
615 stateVirama: {
616 joiningL: stateBefore,
617 joiningD: stateBefore,
618 },
619 stateBefore: {
620 joiningL: stateBefore,
621 joiningD: stateBefore,
622 joiningT: stateBefore,
623 joinZWNJ: stateAfter,
624 joinZWJ: stateFAIL,
625 joinVirama: stateBeforeVirama,
626 },
627 stateBeforeVirama: {
628 joiningL: stateBefore,
629 joiningD: stateBefore,
630 joiningT: stateBefore,
631 },
632 stateAfter: {
633 joiningL: stateFAIL,
634 joiningD: stateBefore,
635 joiningT: stateAfter,
636 joiningR: stateStart,
637 joinZWNJ: stateFAIL,
638 joinZWJ: stateFAIL,
639 joinVirama: stateAfter,
640 },
641 stateFAIL: {
642 0: stateFAIL,
643 joiningL: stateFAIL,
644 joiningD: stateFAIL,
645 joiningT: stateFAIL,
646 joiningR: stateFAIL,
647 joinZWNJ: stateFAIL,
648 joinZWJ: stateFAIL,
649 joinVirama: stateFAIL,
650 },
651 }
652
653
654
655 func (p *Profile) validateLabel(s string) error {
656 if s == "" {
657 if p.verifyDNSLength {
658 return &labelError{s, "A4"}
659 }
660 return nil
661 }
662 if p.bidirule != nil && !p.bidirule(s) {
663 return &labelError{s, "B"}
664 }
665 if p.checkHyphens {
666 if len(s) > 4 && s[2] == '-' && s[3] == '-' {
667 return &labelError{s, "V2"}
668 }
669 if s[0] == '-' || s[len(s)-1] == '-' {
670 return &labelError{s, "V3"}
671 }
672 }
673 if !p.checkJoiners {
674 return nil
675 }
676 trie := p.trie
677
678 v, sz := trie.lookupString(s)
679 x := info(v)
680 if x.isModifier() {
681 return &labelError{s, "V5"}
682 }
683
684 if strings.Index(s, zwj) == -1 && strings.Index(s, zwnj) == -1 {
685 return nil
686 }
687 st := stateStart
688 for i := 0; ; {
689 jt := x.joinType()
690 if s[i:i+sz] == zwj {
691 jt = joinZWJ
692 } else if s[i:i+sz] == zwnj {
693 jt = joinZWNJ
694 }
695 st = joinStates[st][jt]
696 if x.isViramaModifier() {
697 st = joinStates[st][joinVirama]
698 }
699 if i += sz; i == len(s) {
700 break
701 }
702 v, sz = trie.lookupString(s[i:])
703 x = info(v)
704 }
705 if st == stateFAIL || st == stateAfter {
706 return &labelError{s, "C"}
707 }
708 return nil
709 }
710
711 func ascii(s string) bool {
712 for i := 0; i < len(s); i++ {
713 if s[i] >= utf8.RuneSelf {
714 return false
715 }
716 }
717 return true
718 }
719
View as plain text