@@ -396,10 +396,13 @@ func (r *Resolver) LookupPort(ctx context.Context, network, service string) (por
396396// contain DNS "CNAME" records, as long as host resolves to
397397// address records.
398398//
399+ // The returned canonical name is validated to be a properly
400+ // formatted presentation-format domain name.
401+ //
399402// LookupCNAME uses context.Background internally; to specify the context, use
400403// Resolver.LookupCNAME.
401404func LookupCNAME (host string ) (cname string , err error ) {
402- return DefaultResolver .lookupCNAME (context .Background (), host )
405+ return DefaultResolver .LookupCNAME (context .Background (), host )
403406}
404407
405408// LookupCNAME returns the canonical name for the given host.
@@ -412,8 +415,18 @@ func LookupCNAME(host string) (cname string, err error) {
412415// LookupCNAME does not return an error if host does not
413416// contain DNS "CNAME" records, as long as host resolves to
414417// 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
417430}
418431
419432// 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,
425438// That is, it looks up _service._proto.name. To accommodate services
426439// publishing SRV records under non-standard names, if both service
427440// 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.
428444func 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 )
430446}
431447
432448// 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
438454// That is, it looks up _service._proto.name. To accommodate services
439455// publishing SRV records under non-standard names, if both service
440456// 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
443477}
444478
445479// LookupMX returns the DNS MX records for the given domain name sorted by preference.
446480//
481+ // The returned mail server names are validated to be properly
482+ // formatted presentation-format domain names.
483+ //
447484// LookupMX uses context.Background internally; to specify the context, use
448485// Resolver.LookupMX.
449486func LookupMX (name string ) ([]* MX , error ) {
450- return DefaultResolver .lookupMX (context .Background (), name )
487+ return DefaultResolver .LookupMX (context .Background (), name )
451488}
452489
453490// 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.
454494func (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
456508}
457509
458510// LookupNS returns the DNS NS records for the given domain name.
459511//
512+ // The returned name server names are validated to be properly
513+ // formatted presentation-format domain names.
514+ //
460515// LookupNS uses context.Background internally; to specify the context, use
461516// Resolver.LookupNS.
462517func LookupNS (name string ) ([]* NS , error ) {
463- return DefaultResolver .lookupNS (context .Background (), name )
518+ return DefaultResolver .LookupNS (context .Background (), name )
464519}
465520
466521// 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.
467525func (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
469539}
470540
471541// 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)
484554// LookupAddr performs a reverse lookup for the given address, returning a list
485555// of names mapping to that address.
486556//
557+ // The returned names are validated to be properly formatted presentation-format
558+ // domain names.
559+ //
487560// When using the host C library resolver, at most one result will be
488561// returned. To bypass the host resolver, use a custom Resolver.
489562//
490563// LookupAddr uses context.Background internally; to specify the context, use
491564// Resolver.LookupAddr.
492565func LookupAddr (addr string ) (names []string , err error ) {
493- return DefaultResolver .lookupAddr (context .Background (), addr )
566+ return DefaultResolver .LookupAddr (context .Background (), addr )
494567}
495568
496569// LookupAddr performs a reverse lookup for the given address, returning a list
497570// 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
500585}
0 commit comments