@@ -788,6 +788,9 @@ func (e *CommandCustodianExecutor) runAWSEndpointDiagnostics(ctx context.Context
788788
789789 var diagnosticsErr error
790790 for _ , endpoint := range endpoints {
791+ if err := ctx .Err (); err != nil {
792+ return errors .Join (diagnosticsErr , err )
793+ }
791794 lookupCtx , cancel := context .WithTimeout (ctx , 5 * time .Second )
792795 lookupStarted := time .Now ()
793796 ips , err := lookupHost (lookupCtx , endpoint .Host )
@@ -895,9 +898,13 @@ func parseNetworkDiagnosticEndpoint(value string) (networkDiagnosticEndpoint, er
895898 if port == "" {
896899 port = "443"
897900 }
898- if _ , err := strconv .Atoi (port ); err != nil {
901+ portNumber , err := strconv .Atoi (port )
902+ if err != nil {
899903 return networkDiagnosticEndpoint {}, fmt .Errorf ("network diagnostic endpoint %q has invalid port %q: %w" , original , port , err )
900904 }
905+ if portNumber < 1 || portNumber > 65535 {
906+ return networkDiagnosticEndpoint {}, fmt .Errorf ("network diagnostic endpoint %q has invalid port %q: must be between 1 and 65535" , original , port )
907+ }
901908 return networkDiagnosticEndpoint {
902909 Host : strings .ToLower (host ),
903910 Port : port ,
@@ -934,19 +941,30 @@ type tlsProbeResult struct {
934941}
935942
936943func defaultTLSProbeEndpoint (ctx context.Context , endpoint networkDiagnosticEndpoint ) (tlsProbeResult , error ) {
937- dialer := & net.Dialer {Timeout : 5 * time .Second }
944+ if err := ctx .Err (); err != nil {
945+ return tlsProbeResult {}, err
946+ }
947+ netDialer := & net.Dialer {Timeout : 5 * time .Second }
938948 if deadline , ok := ctx .Deadline (); ok {
939- if remaining := time .Until (deadline ); remaining > 0 && remaining < dialer .Timeout {
940- dialer .Timeout = remaining
949+ if remaining := time .Until (deadline ); remaining > 0 && remaining < netDialer .Timeout {
950+ netDialer .Timeout = remaining
941951 }
942952 }
943- conn , err := tls .DialWithDialer (dialer , "tcp" , net .JoinHostPort (endpoint .Host , endpoint .Port ), & tls.Config {ServerName : endpoint .ServerName , MinVersion : tls .VersionTLS12 })
953+ tlsDialer := & tls.Dialer {
954+ NetDialer : netDialer ,
955+ Config : & tls.Config {ServerName : endpoint .ServerName , MinVersion : tls .VersionTLS12 },
956+ }
957+ conn , err := tlsDialer .DialContext (ctx , "tcp" , net .JoinHostPort (endpoint .Host , endpoint .Port ))
944958 if err != nil {
945959 return tlsProbeResult {}, err
946960 }
947961 defer conn .Close ()
948962
949- state := conn .ConnectionState ()
963+ tlsConn , ok := conn .(* tls.Conn )
964+ if ! ok {
965+ return tlsProbeResult {}, errors .New ("TLS dial returned non-TLS connection" )
966+ }
967+ state := tlsConn .ConnectionState ()
950968 return tlsProbeResult {
951969 RemoteAddr : conn .RemoteAddr ().String (),
952970 TLSVersion : tlsVersionString (state .Version ),
0 commit comments