@@ -389,8 +389,11 @@ func (r *Resolver) LookupPort(ctx context.Context, network, service string) (por
389
389
// LookupCNAME does not return an error if host does not
390
390
// contain DNS "CNAME" records, as long as host resolves to
391
391
// address records.
392
+ //
393
+ // The returned canonical name is validated to be a properly
394
+ // formatted presentation-format domain name.
392
395
func LookupCNAME (host string ) (cname string , err error ) {
393
- return DefaultResolver .lookupCNAME (context .Background (), host )
396
+ return DefaultResolver .LookupCNAME (context .Background (), host )
394
397
}
395
398
396
399
// LookupCNAME returns the canonical name for the given host.
@@ -403,8 +406,18 @@ func LookupCNAME(host string) (cname string, err error) {
403
406
// LookupCNAME does not return an error if host does not
404
407
// contain DNS "CNAME" records, as long as host resolves to
405
408
// address records.
406
- func (r * Resolver ) LookupCNAME (ctx context.Context , host string ) (cname string , err error ) {
407
- return r .lookupCNAME (ctx , host )
409
+ //
410
+ // The returned canonical name is validated to be a properly
411
+ // formatted presentation-format domain name.
412
+ func (r * Resolver ) LookupCNAME (ctx context.Context , host string ) (string , error ) {
413
+ cname , err := r .lookupCNAME (ctx , host )
414
+ if err != nil {
415
+ return "" , err
416
+ }
417
+ if ! isDomainName (cname ) {
418
+ return "" , & DNSError {Err : "CNAME target is invalid" , Name : host }
419
+ }
420
+ return cname , nil
408
421
}
409
422
410
423
// LookupSRV tries to resolve an SRV query of the given service,
@@ -416,8 +429,11 @@ func (r *Resolver) LookupCNAME(ctx context.Context, host string) (cname string,
416
429
// That is, it looks up _service._proto.name. To accommodate services
417
430
// publishing SRV records under non-standard names, if both service
418
431
// and proto are empty strings, LookupSRV looks up name directly.
432
+ //
433
+ // The returned service names are validated to be properly
434
+ // formatted presentation-format domain names.
419
435
func LookupSRV (service , proto , name string ) (cname string , addrs []* SRV , err error ) {
420
- return DefaultResolver .lookupSRV (context .Background (), service , proto , name )
436
+ return DefaultResolver .LookupSRV (context .Background (), service , proto , name )
421
437
}
422
438
423
439
// LookupSRV tries to resolve an SRV query of the given service,
@@ -429,28 +445,82 @@ func LookupSRV(service, proto, name string) (cname string, addrs []*SRV, err err
429
445
// That is, it looks up _service._proto.name. To accommodate services
430
446
// publishing SRV records under non-standard names, if both service
431
447
// and proto are empty strings, LookupSRV looks up name directly.
432
- func (r * Resolver ) LookupSRV (ctx context.Context , service , proto , name string ) (cname string , addrs []* SRV , err error ) {
433
- return r .lookupSRV (ctx , service , proto , name )
448
+ //
449
+ // The returned service names are validated to be properly
450
+ // formatted presentation-format domain names.
451
+ func (r * Resolver ) LookupSRV (ctx context.Context , service , proto , name string ) (string , []* SRV , error ) {
452
+ cname , addrs , err := r .lookupSRV (ctx , service , proto , name )
453
+ if err != nil {
454
+ return "" , nil , err
455
+ }
456
+ if cname != "" && ! isDomainName (cname ) {
457
+ return "" , nil , & DNSError {Err : "SRV header name is invalid" , Name : name }
458
+ }
459
+ for _ , addr := range addrs {
460
+ if addr == nil {
461
+ continue
462
+ }
463
+ if ! isDomainName (addr .Target ) {
464
+ return "" , nil , & DNSError {Err : "SRV target is invalid" , Name : name }
465
+ }
466
+ }
467
+ return cname , addrs , nil
434
468
}
435
469
436
470
// LookupMX returns the DNS MX records for the given domain name sorted by preference.
471
+ //
472
+ // The returned mail server names are validated to be properly
473
+ // formatted presentation-format domain names.
437
474
func LookupMX (name string ) ([]* MX , error ) {
438
- return DefaultResolver .lookupMX (context .Background (), name )
475
+ return DefaultResolver .LookupMX (context .Background (), name )
439
476
}
440
477
441
478
// LookupMX returns the DNS MX records for the given domain name sorted by preference.
479
+ //
480
+ // The returned mail server names are validated to be properly
481
+ // formatted presentation-format domain names.
442
482
func (r * Resolver ) LookupMX (ctx context.Context , name string ) ([]* MX , error ) {
443
- return r .lookupMX (ctx , name )
483
+ records , err := r .lookupMX (ctx , name )
484
+ if err != nil {
485
+ return nil , err
486
+ }
487
+ for _ , mx := range records {
488
+ if mx == nil {
489
+ continue
490
+ }
491
+ if ! isDomainName (mx .Host ) {
492
+ return nil , & DNSError {Err : "MX target is invalid" , Name : name }
493
+ }
494
+ }
495
+ return records , nil
444
496
}
445
497
446
498
// LookupNS returns the DNS NS records for the given domain name.
499
+ //
500
+ // The returned name server names are validated to be properly
501
+ // formatted presentation-format domain names.
447
502
func LookupNS (name string ) ([]* NS , error ) {
448
- return DefaultResolver .lookupNS (context .Background (), name )
503
+ return DefaultResolver .LookupNS (context .Background (), name )
449
504
}
450
505
451
506
// LookupNS returns the DNS NS records for the given domain name.
507
+ //
508
+ // The returned name server names are validated to be properly
509
+ // formatted presentation-format domain names.
452
510
func (r * Resolver ) LookupNS (ctx context.Context , name string ) ([]* NS , error ) {
453
- return r .lookupNS (ctx , name )
511
+ records , err := r .lookupNS (ctx , name )
512
+ if err != nil {
513
+ return nil , err
514
+ }
515
+ for _ , ns := range records {
516
+ if ns == nil {
517
+ continue
518
+ }
519
+ if ! isDomainName (ns .Host ) {
520
+ return nil , & DNSError {Err : "NS target is invalid" , Name : name }
521
+ }
522
+ }
523
+ return records , nil
454
524
}
455
525
456
526
// LookupTXT returns the DNS TXT records for the given domain name.
@@ -466,14 +536,29 @@ func (r *Resolver) LookupTXT(ctx context.Context, name string) ([]string, error)
466
536
// LookupAddr performs a reverse lookup for the given address, returning a list
467
537
// of names mapping to that address.
468
538
//
539
+ // The returned names are validated to be properly formatted presentation-format
540
+ // domain names.
541
+ //
469
542
// When using the host C library resolver, at most one result will be
470
543
// returned. To bypass the host resolver, use a custom Resolver.
471
544
func LookupAddr (addr string ) (names []string , err error ) {
472
- return DefaultResolver .lookupAddr (context .Background (), addr )
545
+ return DefaultResolver .LookupAddr (context .Background (), addr )
473
546
}
474
547
475
548
// LookupAddr performs a reverse lookup for the given address, returning a list
476
549
// of names mapping to that address.
477
- func (r * Resolver ) LookupAddr (ctx context.Context , addr string ) (names []string , err error ) {
478
- return r .lookupAddr (ctx , addr )
550
+ //
551
+ // The returned names are validated to be properly formatted presentation-format
552
+ // domain names.
553
+ func (r * Resolver ) LookupAddr (ctx context.Context , addr string ) ([]string , error ) {
554
+ names , err := r .lookupAddr (ctx , addr )
555
+ if err != nil {
556
+ return nil , err
557
+ }
558
+ for _ , name := range names {
559
+ if ! isDomainName (name ) {
560
+ return nil , & DNSError {Err : "PTR target is invalid" , Name : addr }
561
+ }
562
+ }
563
+ return names , nil
479
564
}
0 commit comments