Skip to content

Commit

Permalink
Fix HTTP proxy authentication to support both preemptive and challeng…
Browse files Browse the repository at this point in the history
…e-response auth (#134)
  • Loading branch information
lhpalacio authored Sep 3, 2024
1 parent 5b7f822 commit 4a564b5
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,23 @@ func (s *HTTPServer) authenticate(req *http.Request) (int, error) {
}

auth := req.Header.Get(proxyAuthHeaderKey)
if auth != "" {
enc := strings.TrimPrefix(auth, "Basic ")
str, err := base64.StdEncoding.DecodeString(enc)
if err != nil {
return http.StatusNotAcceptable, fmt.Errorf("decode username and password failed: %w", err)
}
pairs := bytes.SplitN(str, []byte(":"), 2)
if len(pairs) != 2 {
return http.StatusLengthRequired, fmt.Errorf("username and password format invalid")
}
if s.auth.Valid(string(pairs[0]), string(pairs[1])) {
return 0, nil
}
return http.StatusUnauthorized, fmt.Errorf("username and password not matching")
if auth == "" {
return http.StatusProxyAuthRequired, fmt.Errorf(http.StatusText(http.StatusProxyAuthRequired))

Check failure on line 35 in http.go

View workflow job for this annotation

GitHub Actions / lint

printf: non-constant format string in call to fmt.Errorf (govet)
}

return http.StatusProxyAuthRequired, fmt.Errorf(http.StatusText(http.StatusProxyAuthRequired))
enc := strings.TrimPrefix(auth, "Basic ")
str, err := base64.StdEncoding.DecodeString(enc)
if err != nil {
return http.StatusNotAcceptable, fmt.Errorf("decode username and password failed: %w", err)
}
pairs := bytes.SplitN(str, []byte(":"), 2)
if len(pairs) != 2 {
return http.StatusLengthRequired, fmt.Errorf("username and password format invalid")
}
if s.auth.Valid(string(pairs[0]), string(pairs[1])) {
return 0, nil
}
return http.StatusUnauthorized, fmt.Errorf("username and password not matching")
}

func (s *HTTPServer) handleConn(req *http.Request, conn net.Conn) (peer net.Conn, err error) {
Expand Down Expand Up @@ -103,7 +103,11 @@ func (s *HTTPServer) serve(conn net.Conn) {

code, err := s.authenticate(req)
if err != nil {
_ = responseWith(req, code).Write(conn)
resp := responseWith(req, code)
if code == http.StatusProxyAuthRequired {
resp.Header.Set("Proxy-Authenticate", "Basic realm=\"Proxy\"")
}
_ = resp.Write(conn)
log.Println(err)
return
}
Expand Down

0 comments on commit 4a564b5

Please sign in to comment.