@@ -396,10 +396,13 @@ func (r *Resolver) LookupPort(ctx context.Context, network, service string) (por
396
396
// contain DNS "CNAME" records, as long as host resolves to
397
397
// address records.
398
398
//
399
+ // The returned canonical name is validated to be a properly
400
+ // formatted presentation-format domain name.
401
+ //
399
402
// LookupCNAME uses context.Background internally; to specify the context, use
400
403
// Resolver.LookupCNAME.
401
404
func LookupCNAME (host string ) (cname string , err error ) {
402
- return DefaultResolver .lookupCNAME (context .Background (), host )
405
+ return DefaultResolver .LookupCNAME (context .Background (), host )
403
406
}
404
407
405
408
// LookupCNAME returns the canonical name for the given host.
@@ -412,8 +415,18 @@ func LookupCNAME(host string) (cname string, err error) {
412
415
// LookupCNAME does not return an error if host does not
413
416
// contain DNS "CNAME" records, as long as host resolves to
414
417
// address records.
415
- func (r * Resolver ) LookupCNAME (ctx context.Context , host string ) (cname string , err error ) {
416
- return r .lookupCNAME (ctx , host )
418
+ //
419
+ // The returned canonical name is validated to be a properly
420
+ // formatted presentation-format domain name.
421
+ func (r * Resolver ) LookupCNAME (ctx context.Context , host string ) (string , error ) {
422
+ cname , err := r .lookupCNAME (ctx , host )
423
+ if err != nil {
424
+ return "" , err
425
+ }
426
+ if ! isDomainName (cname ) {
427
+ return "" , & DNSError {Err : "CNAME target is invalid" , Name : host }
428
+ }
429
+ return cname , nil
417
430
}
418
431
419
432
// LookupSRV tries to resolve an SRV query of the given service,
@@ -425,8 +438,11 @@ func (r *Resolver) LookupCNAME(ctx context.Context, host string) (cname string,
425
438
// That is, it looks up _service._proto.name. To accommodate services
426
439
// publishing SRV records under non-standard names, if both service
427
440
// and proto are empty strings, LookupSRV looks up name directly.
441
+ //
442
+ // The returned service names are validated to be properly
443
+ // formatted presentation-format domain names.
428
444
func LookupSRV (service , proto , name string ) (cname string , addrs []* SRV , err error ) {
429
- return DefaultResolver .lookupSRV (context .Background (), service , proto , name )
445
+ return DefaultResolver .LookupSRV (context .Background (), service , proto , name )
430
446
}
431
447
432
448
// LookupSRV tries to resolve an SRV query of the given service,
@@ -438,34 +454,88 @@ func LookupSRV(service, proto, name string) (cname string, addrs []*SRV, err err
438
454
// That is, it looks up _service._proto.name. To accommodate services
439
455
// publishing SRV records under non-standard names, if both service
440
456
// and proto are empty strings, LookupSRV looks up name directly.
441
- func (r * Resolver ) LookupSRV (ctx context.Context , service , proto , name string ) (cname string , addrs []* SRV , err error ) {
442
- return r .lookupSRV (ctx , service , proto , name )
457
+ //
458
+ // The returned service names are validated to be properly
459
+ // formatted presentation-format domain names.
460
+ func (r * Resolver ) LookupSRV (ctx context.Context , service , proto , name string ) (string , []* SRV , error ) {
461
+ cname , addrs , err := r .lookupSRV (ctx , service , proto , name )
462
+ if err != nil {
463
+ return "" , nil , err
464
+ }
465
+ if cname != "" && ! isDomainName (cname ) {
466
+ return "" , nil , & DNSError {Err : "SRV header name is invalid" , Name : name }
467
+ }
468
+ for _ , addr := range addrs {
469
+ if addr == nil {
470
+ continue
471
+ }
472
+ if ! isDomainName (addr .Target ) {
473
+ return "" , nil , & DNSError {Err : "SRV target is invalid" , Name : name }
474
+ }
475
+ }
476
+ return cname , addrs , nil
443
477
}
444
478
445
479
// LookupMX returns the DNS MX records for the given domain name sorted by preference.
446
480
//
481
+ // The returned mail server names are validated to be properly
482
+ // formatted presentation-format domain names.
483
+ //
447
484
// LookupMX uses context.Background internally; to specify the context, use
448
485
// Resolver.LookupMX.
449
486
func LookupMX (name string ) ([]* MX , error ) {
450
- return DefaultResolver .lookupMX (context .Background (), name )
487
+ return DefaultResolver .LookupMX (context .Background (), name )
451
488
}
452
489
453
490
// LookupMX returns the DNS MX records for the given domain name sorted by preference.
491
+ //
492
+ // The returned mail server names are validated to be properly
493
+ // formatted presentation-format domain names.
454
494
func (r * Resolver ) LookupMX (ctx context.Context , name string ) ([]* MX , error ) {
455
- return r .lookupMX (ctx , name )
495
+ records , err := r .lookupMX (ctx , name )
496
+ if err != nil {
497
+ return nil , err
498
+ }
499
+ for _ , mx := range records {
500
+ if mx == nil {
501
+ continue
502
+ }
503
+ if ! isDomainName (mx .Host ) {
504
+ return nil , & DNSError {Err : "MX target is invalid" , Name : name }
505
+ }
506
+ }
507
+ return records , nil
456
508
}
457
509
458
510
// LookupNS returns the DNS NS records for the given domain name.
459
511
//
512
+ // The returned name server names are validated to be properly
513
+ // formatted presentation-format domain names.
514
+ //
460
515
// LookupNS uses context.Background internally; to specify the context, use
461
516
// Resolver.LookupNS.
462
517
func LookupNS (name string ) ([]* NS , error ) {
463
- return DefaultResolver .lookupNS (context .Background (), name )
518
+ return DefaultResolver .LookupNS (context .Background (), name )
464
519
}
465
520
466
521
// LookupNS returns the DNS NS records for the given domain name.
522
+ //
523
+ // The returned name server names are validated to be properly
524
+ // formatted presentation-format domain names.
467
525
func (r * Resolver ) LookupNS (ctx context.Context , name string ) ([]* NS , error ) {
468
- return r .lookupNS (ctx , name )
526
+ records , err := r .lookupNS (ctx , name )
527
+ if err != nil {
528
+ return nil , err
529
+ }
530
+ for _ , ns := range records {
531
+ if ns == nil {
532
+ continue
533
+ }
534
+ if ! isDomainName (ns .Host ) {
535
+ return nil , & DNSError {Err : "NS target is invalid" , Name : name }
536
+ }
537
+ }
538
+ return records , nil
469
539
}
470
540
471
541
// LookupTXT returns the DNS TXT records for the given domain name.
@@ -484,17 +554,32 @@ func (r *Resolver) LookupTXT(ctx context.Context, name string) ([]string, error)
484
554
// LookupAddr performs a reverse lookup for the given address, returning a list
485
555
// of names mapping to that address.
486
556
//
557
+ // The returned names are validated to be properly formatted presentation-format
558
+ // domain names.
559
+ //
487
560
// When using the host C library resolver, at most one result will be
488
561
// returned. To bypass the host resolver, use a custom Resolver.
489
562
//
490
563
// LookupAddr uses context.Background internally; to specify the context, use
491
564
// Resolver.LookupAddr.
492
565
func LookupAddr (addr string ) (names []string , err error ) {
493
- return DefaultResolver .lookupAddr (context .Background (), addr )
566
+ return DefaultResolver .LookupAddr (context .Background (), addr )
494
567
}
495
568
496
569
// LookupAddr performs a reverse lookup for the given address, returning a list
497
570
// of names mapping to that address.
498
- func (r * Resolver ) LookupAddr (ctx context.Context , addr string ) (names []string , err error ) {
499
- return r .lookupAddr (ctx , addr )
571
+ //
572
+ // The returned names are validated to be properly formatted presentation-format
573
+ // domain names.
574
+ func (r * Resolver ) LookupAddr (ctx context.Context , addr string ) ([]string , error ) {
575
+ names , err := r .lookupAddr (ctx , addr )
576
+ if err != nil {
577
+ return nil , err
578
+ }
579
+ for _ , name := range names {
580
+ if ! isDomainName (name ) {
581
+ return nil , & DNSError {Err : "PTR target is invalid" , Name : addr }
582
+ }
583
+ }
584
+ return names , nil
500
585
}
0 commit comments