-
Notifications
You must be signed in to change notification settings - Fork 17.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmd/go/internal/web: merge internal/web2 into web
The cmd/go/internal/web package was forked in order to support direct HTTPS fetches from widely-used hosting providers,¹ but direct fetches were subsequently dropped in CL 107657. The forked web2 package, with its GitHub-specific diagnostics and .netrc support, remained in use for module proxy support, but was not used for the initial '?go-get=1' path resolution, so the .netrc file was only used to fetch from already-resolved module protocol servers. This CL moves the .netrc support into its own (new) package, cmd/go/internal/auth, and consolidates the web and web2 packages back into just web. As a result, fetches via the web package now support .netrc, and fetches that previously used web2 now enforce the same security policies as web (such as prohibiting HTTPS-to-HTTP redirects). ¹golang/vgo@63138cb Fixes #29591 Fixes #29888 Fixes #30610 Updates #26232 Change-Id: Ia3a13526e443679cf14a72a1f3db96f336ce5e73 Reviewed-on: https://go-review.googlesource.com/c/go/+/170879 Run-TryBot: Russ Cox <[email protected]> Reviewed-by: Russ Cox <[email protected]> Reviewed-by: Jay Conrod <[email protected]>
- Loading branch information
Showing
20 changed files
with
509 additions
and
879 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright 2019 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
// Package auth provides access to user-provided authentication credentials. | ||
package auth | ||
|
||
import "net/http" | ||
|
||
// AddCredentials fills in the user's credentials for req, if any. | ||
// The return value reports whether any matching credentials were found. | ||
func AddCredentials(req *http.Request) (added bool) { | ||
// TODO(golang.org/issue/26232): Support arbitrary user-provided credentials. | ||
netrcOnce.Do(readNetrc) | ||
for _, l := range netrc { | ||
if l.machine == req.URL.Host { | ||
req.SetBasicAuth(l.login, l.password) | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
// Copyright 2019 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package auth | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"runtime" | ||
"strings" | ||
"sync" | ||
) | ||
|
||
type netrcLine struct { | ||
machine string | ||
login string | ||
password string | ||
} | ||
|
||
var ( | ||
netrcOnce sync.Once | ||
netrc []netrcLine | ||
netrcErr error | ||
) | ||
|
||
func parseNetrc(data string) []netrcLine { | ||
// See https://www.gnu.org/software/inetutils/manual/html_node/The-_002enetrc-file.html | ||
// for documentation on the .netrc format. | ||
var nrc []netrcLine | ||
var l netrcLine | ||
inMacro := false | ||
for _, line := range strings.Split(data, "\n") { | ||
if inMacro { | ||
if line == "" { | ||
inMacro = false | ||
} | ||
continue | ||
} | ||
|
||
f := strings.Fields(line) | ||
i := 0 | ||
for ; i < len(f)-1; i += 2 { | ||
// Reset at each "machine" token. | ||
// “The auto-login process searches the .netrc file for a machine token | ||
// that matches […]. Once a match is made, the subsequent .netrc tokens | ||
// are processed, stopping when the end of file is reached or another | ||
// machine or a default token is encountered.” | ||
switch f[i] { | ||
case "machine": | ||
l = netrcLine{machine: f[i+1]} | ||
case "default": | ||
break | ||
case "login": | ||
l.login = f[i+1] | ||
case "password": | ||
l.password = f[i+1] | ||
case "macdef": | ||
// “A macro is defined with the specified name; its contents begin with | ||
// the next .netrc line and continue until a null line (consecutive | ||
// new-line characters) is encountered.” | ||
inMacro = true | ||
} | ||
if l.machine != "" && l.login != "" && l.password != "" { | ||
nrc = append(nrc, l) | ||
l = netrcLine{} | ||
} | ||
} | ||
|
||
if i < len(f) && f[i] == "default" { | ||
// “There can be only one default token, and it must be after all machine tokens.” | ||
break | ||
} | ||
} | ||
|
||
return nrc | ||
} | ||
|
||
func netrcPath() (string, error) { | ||
if env := os.Getenv("NETRC"); env != "" { | ||
return env, nil | ||
} | ||
dir, err := os.UserHomeDir() | ||
if err != nil { | ||
return "", err | ||
} | ||
base := ".netrc" | ||
if runtime.GOOS == "windows" { | ||
base = "_netrc" | ||
} | ||
return filepath.Join(dir, base), nil | ||
} | ||
|
||
func readNetrc() { | ||
path, err := netrcPath() | ||
if err != nil { | ||
netrcErr = err | ||
return | ||
} | ||
|
||
data, err := ioutil.ReadFile(path) | ||
if err != nil { | ||
if !os.IsNotExist(err) { | ||
netrcErr = err | ||
} | ||
return | ||
} | ||
|
||
netrc = parseNetrc(string(data)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.