fix(agentgh): require https for creds, pin gh to github.com, map signal exits
This commit is contained in:
+12
-3
@@ -17,6 +17,7 @@ import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"syscall"
|
||||
|
||||
"git.unkin.net/unkin/agent-tools/internal/agent"
|
||||
|
||||
@@ -126,7 +127,7 @@ func credentialGet(stdin io.Reader, stdout io.Writer, load loader) error {
|
||||
if err := sc.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
if req["host"] != githubHost {
|
||||
if req["protocol"] != "https" || req["host"] != githubHost {
|
||||
return nil
|
||||
}
|
||||
tok, err := token(load)
|
||||
@@ -172,6 +173,11 @@ func newGHCmd(load loader) *cobra.Command {
|
||||
}
|
||||
}
|
||||
|
||||
var strippedEnv = map[string]bool{
|
||||
"GH_TOKEN": true, "GITHUB_TOKEN": true, "GH_HOST": true,
|
||||
"GH_ENTERPRISE_TOKEN": true, "GITHUB_ENTERPRISE_TOKEN": true,
|
||||
}
|
||||
|
||||
// runGH passes the token through the child env only, never argv.
|
||||
func runGH(args []string, tok string, stdin io.Reader, stdout, stderr io.Writer) error {
|
||||
bin, err := exec.LookPath("gh")
|
||||
@@ -181,14 +187,17 @@ func runGH(args []string, tok string, stdin io.Reader, stdout, stderr io.Writer)
|
||||
c := exec.Command(bin, args...)
|
||||
c.Stdin, c.Stdout, c.Stderr = stdin, stdout, stderr
|
||||
for _, kv := range os.Environ() {
|
||||
if !strings.HasPrefix(kv, "GH_TOKEN=") && !strings.HasPrefix(kv, "GITHUB_TOKEN=") {
|
||||
if k, _, _ := strings.Cut(kv, "="); !strippedEnv[k] {
|
||||
c.Env = append(c.Env, kv)
|
||||
}
|
||||
}
|
||||
c.Env = append(c.Env, "GH_TOKEN="+tok)
|
||||
c.Env = append(c.Env, "GH_TOKEN="+tok, "GH_HOST="+githubHost)
|
||||
err = c.Run()
|
||||
var ee *exec.ExitError
|
||||
if errors.As(err, &ee) {
|
||||
if ws, ok := ee.Sys().(syscall.WaitStatus); ok && ws.Signaled() {
|
||||
return exitCode(128 + int(ws.Signal()))
|
||||
}
|
||||
return exitCode(ee.ExitCode())
|
||||
}
|
||||
return err
|
||||
|
||||
@@ -29,6 +29,8 @@ func TestCredentialGetHostFiltering(t *testing.T) {
|
||||
"protocol=https\nhost=github.com.evil.example\n\n",
|
||||
"protocol=https\nhost=api.github.com\n\n",
|
||||
"protocol=https\n\n",
|
||||
"protocol=http\nhost=github.com\n\n",
|
||||
"host=github.com\n\n",
|
||||
} {
|
||||
var out bytes.Buffer
|
||||
if err := credentialGet(strings.NewReader(in), &out, load); err != nil || out.Len() != 0 {
|
||||
@@ -133,16 +135,18 @@ func fakeGH(t *testing.T, script string) string {
|
||||
}
|
||||
|
||||
func TestRunGHPassesTokenInEnvOnly(t *testing.T) {
|
||||
dir := fakeGH(t, `printf '%s\n' "$@" > "$(dirname "$0")/argv"; echo "GH_TOKEN=$GH_TOKEN GITHUB_TOKEN=$GITHUB_TOKEN"; cat; exit 7`)
|
||||
t.Setenv("GH_TOKEN", "stale")
|
||||
t.Setenv("GITHUB_TOKEN", "stale")
|
||||
dir := fakeGH(t, `printf '%s\n' "$@" > "$(dirname "$0")/argv"; echo "GH_TOKEN=$GH_TOKEN GITHUB_TOKEN=$GITHUB_TOKEN GH_HOST=$GH_HOST GH_ENTERPRISE_TOKEN=$GH_ENTERPRISE_TOKEN GITHUB_ENTERPRISE_TOKEN=$GITHUB_ENTERPRISE_TOKEN"; cat; exit 7`)
|
||||
for _, k := range []string{"GH_TOKEN", "GITHUB_TOKEN", "GH_ENTERPRISE_TOKEN", "GITHUB_ENTERPRISE_TOKEN"} {
|
||||
t.Setenv(k, "stale")
|
||||
}
|
||||
t.Setenv("GH_HOST", "ghe.example")
|
||||
var out bytes.Buffer
|
||||
err := runGH([]string{"pr", "list", "--repo", "o/r"}, "ghs_secret", strings.NewReader("piped\n"), &out, &out)
|
||||
var code exitCode
|
||||
if !errors.As(err, &code) || code != 7 {
|
||||
t.Errorf("err = %v, want exit code 7", err)
|
||||
}
|
||||
if out.String() != "GH_TOKEN=ghs_secret GITHUB_TOKEN=\npiped\n" {
|
||||
if out.String() != "GH_TOKEN=ghs_secret GITHUB_TOKEN= GH_HOST=github.com GH_ENTERPRISE_TOKEN= GITHUB_ENTERPRISE_TOKEN=\npiped\n" {
|
||||
t.Errorf("child saw %q", out.String())
|
||||
}
|
||||
argv, _ := os.ReadFile(filepath.Join(dir, "argv"))
|
||||
@@ -159,3 +163,12 @@ func TestGHCommandPassesFlagsThroughWithApp(t *testing.T) {
|
||||
t.Errorf("out=%q err=%v", out, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunGHSignalExitCode(t *testing.T) {
|
||||
fakeGH(t, `kill -TERM $$`)
|
||||
err := runGH(nil, "tok", strings.NewReader(""), &bytes.Buffer{}, &bytes.Buffer{})
|
||||
var code exitCode
|
||||
if !errors.As(err, &code) || code != 128+15 {
|
||||
t.Errorf("err = %v, want exit code 143", err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user