Removed regex, added timeout.

This commit is contained in:
empathetic-alligator 2014-12-16 20:47:39 -05:00
parent 3c466dc88e
commit a1455a8eba

View File

@ -293,32 +293,35 @@ func (s *Server) Whitelist(fingerprint string) error {
return nil return nil
} }
var pubKeyRegex = regexp.MustCompile(`ssh-rsa ([A-Za-z0-9\+=\/]+)\s*`) // Client for getting github pub keys
var timeout = time.Duration(10 * time.Second)
var client = http.Client{
Timeout: timeout,
}
// Returns an array of public keys for the given github user URL // Returns an array of public keys for the given github user URL
func getGithubPubKeys(url string) ([]ssh.PublicKey, error) { func getGithubPubKeys(url string) ([]ssh.PublicKey, error) {
timeout := time.Duration(10 * time.Second)
client := http.Client{
Timeout: timeout,
}
resp, err := client.Get("http://" + url + ".keys") resp, err := client.Get("http://" + url + ".keys")
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer resp.Body.Close() defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body) body, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
return nil, err return nil, err
} }
bodyStr := string(body) bodyStr := string(body)
keys := pubKeyRegex.FindAllStringSubmatch(bodyStr, -1) pubs := []ssh.PublicKey{}
pubs := make([]ssh.PublicKey, 0, 3) for _, key := range strings.SplitN(bodyStr, "\n", -1) {
for _, key := range keys { splitKey := strings.SplitN(key, " ", -1)
if(len(key) < 2) {
// In case of malformated key
if len(splitKey) < 2 {
continue continue
} }
bodyDecoded, err := base64.StdEncoding.DecodeString(key[1]) bodyDecoded, err := base64.StdEncoding.DecodeString(splitKey[1])
if err != nil { if err != nil {
return nil, err return nil, err
} }