mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-18 18:04:17 +00:00
An install source typed as a URL can carry a token in its userinfo or query, and operations.log stored it as typed, so anyone reading the log or the dashboard's log view could see the credential (#878). Redact in oplog.Write, which every CLI and dashboard writer goes through: the source, oldSource and newSource args lose their userinfo, query and fragment, and the message loses the same parts where an error echoes them. The redaction reuses update's redactRepoURL, moved to install.RedactRepoURL. It now also covers git+https:// and ssh:// passwords, and a source typed without a scheme such as user:token@host/owner/repo, which install clones over HTTPS. logInstallOp logged args[0] as the source, which is a flag when one comes first (install --into team <url> logged "--into"), so the message was never redacted. It now logs the parsed source. Fixes #878
368 lines
12 KiB
Go
368 lines
12 KiB
Go
package install
|
|
|
|
import (
|
|
"fmt"
|
|
"net/url"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
// Platform represents a git hosting platform.
|
|
type Platform int
|
|
|
|
const (
|
|
PlatformUnknown Platform = iota
|
|
PlatformGitHub // github.com and GitHub Enterprise
|
|
PlatformGitLab // gitlab.com and self-hosted GitLab
|
|
PlatformBitbucket // bitbucket.org
|
|
PlatformAzureDevOps // dev.azure.com and visualstudio.com
|
|
PlatformCNB // cnb.cool and self-hosted CNB instances
|
|
PlatformGitea // gitea.com and self-hosted Gitea instances
|
|
)
|
|
|
|
// extractHost returns the hostname from a clone URL.
|
|
// Supports HTTPS (https://host/...) and SSH (git@host:...) formats.
|
|
func extractHost(cloneURL string) string {
|
|
s := strings.TrimSpace(cloneURL)
|
|
if s == "" {
|
|
return ""
|
|
}
|
|
|
|
// SSH: git@host:owner/repo.git
|
|
if strings.Contains(s, "@") && strings.Contains(s, ":") && !strings.Contains(s, "://") {
|
|
at := strings.Index(s, "@")
|
|
colon := strings.Index(s[at:], ":")
|
|
if colon > 0 {
|
|
return s[at+1 : at+colon]
|
|
}
|
|
}
|
|
|
|
// HTTPS or file://
|
|
u, err := url.Parse(s)
|
|
if err != nil || u.Host == "" {
|
|
return ""
|
|
}
|
|
return u.Hostname()
|
|
}
|
|
|
|
// detectPlatform identifies the git hosting platform from a clone URL.
|
|
func detectPlatform(cloneURL string) Platform {
|
|
host := strings.ToLower(extractHost(cloneURL))
|
|
if host == "" {
|
|
return PlatformUnknown
|
|
}
|
|
if isGitHubLikeHost(host) {
|
|
return PlatformGitHub
|
|
}
|
|
if strings.Contains(host, "gitlab") {
|
|
return PlatformGitLab
|
|
}
|
|
if strings.Contains(host, "bitbucket") {
|
|
return PlatformBitbucket
|
|
}
|
|
if host == "dev.azure.com" || host == "ssh.dev.azure.com" || strings.HasSuffix(host, ".visualstudio.com") {
|
|
return PlatformAzureDevOps
|
|
}
|
|
if strings.Contains(host, "cnb.cool") {
|
|
return PlatformCNB
|
|
}
|
|
if strings.Contains(host, "gitea") {
|
|
return PlatformGitea
|
|
}
|
|
return PlatformUnknown
|
|
}
|
|
|
|
// resolveToken looks up HTTPS credentials. Provider tokens require a trusted
|
|
// host; platform-name heuristics are only for parsing, never authorization.
|
|
// SKILLSHARE_GIT_TOKEN remains an explicit fallback for any HTTPS source.
|
|
func resolveToken(cloneURL string) (token, username string) {
|
|
return resolveTokenWithOptions(cloneURL, nil, nil)
|
|
}
|
|
|
|
func resolveTokenWithOptions(cloneURL string, cnbHosts, giteaHosts []string) (token, username string) {
|
|
if !isHTTPS(cloneURL) {
|
|
return "", ""
|
|
}
|
|
|
|
platform := credentialPlatform(cloneURL, cnbHosts, giteaHosts)
|
|
switch platform {
|
|
case PlatformGitHub:
|
|
if t := os.Getenv("GITHUB_TOKEN"); t != "" {
|
|
return t, "x-access-token"
|
|
}
|
|
if t := os.Getenv("GH_TOKEN"); t != "" {
|
|
return t, "x-access-token"
|
|
}
|
|
case PlatformGitLab:
|
|
if t := os.Getenv("GITLAB_TOKEN"); t != "" {
|
|
return t, "oauth2"
|
|
}
|
|
case PlatformBitbucket:
|
|
if t := os.Getenv("BITBUCKET_TOKEN"); t != "" {
|
|
if u := os.Getenv("BITBUCKET_USERNAME"); u != "" {
|
|
return t, u
|
|
}
|
|
return t, "x-token-auth"
|
|
}
|
|
case PlatformAzureDevOps:
|
|
if t := os.Getenv("AZURE_DEVOPS_TOKEN"); t != "" {
|
|
return t, "x-access-token"
|
|
}
|
|
case PlatformCNB:
|
|
if t := os.Getenv("CNB_TOKEN"); t != "" {
|
|
return t, "cnb"
|
|
}
|
|
case PlatformGitea:
|
|
if t := os.Getenv("GITEA_TOKEN"); t != "" {
|
|
return t, "x-access-token"
|
|
}
|
|
}
|
|
|
|
// Generic fallback — use platform-appropriate username, or preserve
|
|
// existing URL username (e.g. https://myuser@host/... authenticates as myuser).
|
|
if t := os.Getenv("SKILLSHARE_GIT_TOKEN"); t != "" {
|
|
if u := urlUsername(cloneURL); u != "" {
|
|
return t, u
|
|
}
|
|
switch detectPlatformWithOptions(cloneURL, cnbHosts, giteaHosts) {
|
|
case PlatformGitLab:
|
|
return t, "oauth2"
|
|
case PlatformBitbucket:
|
|
return t, "x-token-auth"
|
|
default:
|
|
return t, "x-access-token"
|
|
}
|
|
}
|
|
return "", ""
|
|
}
|
|
|
|
// credentialPlatform binds provider credentials to their public HTTPS origin,
|
|
// or an explicitly configured CNB/Gitea host, not a provider-looking name.
|
|
func credentialPlatform(rawURL string, cnbHosts, giteaHosts []string) Platform {
|
|
u, err := url.Parse(strings.TrimSpace(rawURL))
|
|
if err != nil || !strings.EqualFold(u.Scheme, "https") || (u.Port() != "" && u.Port() != "443") {
|
|
return PlatformUnknown
|
|
}
|
|
host := strings.ToLower(u.Hostname())
|
|
switch host {
|
|
case "github.com", "api.github.com", "raw.githubusercontent.com":
|
|
return PlatformGitHub
|
|
case "gitlab.com":
|
|
return PlatformGitLab
|
|
case "bitbucket.org":
|
|
return PlatformBitbucket
|
|
case "dev.azure.com":
|
|
return PlatformAzureDevOps
|
|
case "cnb.cool":
|
|
return PlatformCNB
|
|
case "gitea.com":
|
|
return PlatformGitea
|
|
}
|
|
if strings.HasSuffix(host, ".visualstudio.com") {
|
|
return PlatformAzureDevOps
|
|
}
|
|
if hostMatchesAny(host, cnbHosts) {
|
|
return PlatformCNB
|
|
}
|
|
if hostMatchesAny(host, giteaHosts) {
|
|
return PlatformGitea
|
|
}
|
|
return PlatformUnknown
|
|
}
|
|
|
|
// authEnv returns environment variables that give git the token through a
|
|
// credential helper set via GIT_CONFIG_COUNT/KEY/VALUE (Git 2.31+). The token
|
|
// itself travels only in SKILLSHARE_GIT_AUTH_PASSWORD, never in config or argv.
|
|
// If GIT_CONFIG_COUNT is already set in the environment, the new entries
|
|
// are appended at the next available index to avoid overriding existing
|
|
// git config entries (e.g. from CI pipelines).
|
|
// Returns nil for SSH/file URLs or when no token is available.
|
|
func authEnv(cloneURL string) []string {
|
|
return authEnvWithOptions(cloneURL, nil, nil)
|
|
}
|
|
|
|
func authEnvWithOptions(cloneURL string, cnbHosts, giteaHosts []string) []string {
|
|
token, username := resolveTokenWithOptions(cloneURL, cnbHosts, giteaHosts)
|
|
if token == "" {
|
|
return nil
|
|
}
|
|
|
|
host := extractHost(cloneURL)
|
|
if host == "" {
|
|
return nil
|
|
}
|
|
|
|
// Not an insteadOf URL rewrite: git passes the rewritten URL to
|
|
// git-remote-https as an argument, visible in ps (#565). Not an extraHeader:
|
|
// it is sent before any 401, so a stale token breaks public repos.
|
|
// The empty entry clears the user's helpers for this host, so git never
|
|
// asks their keychain or erases from it.
|
|
base := existingConfigCount()
|
|
key := fmt.Sprintf("credential.https://%s.helper", host)
|
|
return []string{
|
|
fmt.Sprintf("GIT_CONFIG_COUNT=%d", base+2),
|
|
fmt.Sprintf("GIT_CONFIG_KEY_%d=%s", base, key),
|
|
fmt.Sprintf("GIT_CONFIG_VALUE_%d=", base),
|
|
fmt.Sprintf("GIT_CONFIG_KEY_%d=%s", base+1, key),
|
|
fmt.Sprintf("GIT_CONFIG_VALUE_%d=%s", base+1, gitCredentialHelper),
|
|
gitAuthUsernameEnv + "=" + username,
|
|
gitAuthPasswordEnv + "=" + token,
|
|
}
|
|
}
|
|
|
|
const (
|
|
gitAuthUsernameEnv = "SKILLSHARE_GIT_AUTH_USERNAME"
|
|
gitAuthPasswordEnv = "SKILLSHARE_GIT_AUTH_PASSWORD"
|
|
// printf is a shell builtin, so the token never reaches a process argv.
|
|
gitCredentialHelper = `!f() { if [ "$1" = get ]; then printf 'username=%s\npassword=%s\n' "$` + gitAuthUsernameEnv + `" "$` + gitAuthPasswordEnv + `"; fi; }; f`
|
|
)
|
|
|
|
// AuthEnvForURL returns git auth injection env for HTTPS clone/pull URLs.
|
|
// Returns nil when no token is available or URL is not HTTPS.
|
|
func AuthEnvForURL(cloneURL string, opts ParseOptions) []string {
|
|
return authEnvWithOptions(cloneURL, opts.CNBHosts, opts.GiteaHosts)
|
|
}
|
|
|
|
// ResolveTokenForURL returns token credentials resolved from env vars for the
|
|
// provided HTTPS URL. Returns empty strings when no token is available.
|
|
func ResolveTokenForURL(cloneURL string, opts ParseOptions) (token, username string) {
|
|
return resolveTokenWithOptions(cloneURL, opts.CNBHosts, opts.GiteaHosts)
|
|
}
|
|
|
|
// DetectPlatformForURL identifies the git hosting platform for a URL.
|
|
func DetectPlatformForURL(cloneURL string) Platform {
|
|
return detectPlatform(cloneURL)
|
|
}
|
|
|
|
func detectPlatformWithOptions(cloneURL string, cnbHosts, giteaHosts []string) Platform {
|
|
host := strings.ToLower(extractHost(cloneURL))
|
|
if host == "" {
|
|
return PlatformUnknown
|
|
}
|
|
return detectPlatformFromHost(host, cnbHosts, giteaHosts)
|
|
}
|
|
|
|
func (s *Source) authEnv() []string {
|
|
if s == nil {
|
|
return nil
|
|
}
|
|
return authEnvWithOptions(s.CloneURL, s.CNBHosts, s.GiteaHosts)
|
|
}
|
|
|
|
func (s *Source) platform() Platform {
|
|
if s == nil {
|
|
return PlatformUnknown
|
|
}
|
|
return detectPlatformWithOptions(s.CloneURL, s.CNBHosts, s.GiteaHosts)
|
|
}
|
|
|
|
// existingConfigCount returns the current GIT_CONFIG_COUNT from the
|
|
// environment, or 0 if unset/invalid.
|
|
func existingConfigCount() int {
|
|
s := os.Getenv("GIT_CONFIG_COUNT")
|
|
if s == "" {
|
|
return 0
|
|
}
|
|
n, err := strconv.Atoi(s)
|
|
if err != nil || n < 0 {
|
|
return 0
|
|
}
|
|
return n
|
|
}
|
|
|
|
// sanitizeTokens replaces any known credential values found in text with
|
|
// [REDACTED]. This prevents tokens and associated usernames from leaking
|
|
// in error messages.
|
|
func sanitizeTokens(text string) string {
|
|
vars := []string{
|
|
"GITHUB_TOKEN", "GH_TOKEN", "GITLAB_TOKEN", "BITBUCKET_TOKEN",
|
|
"AZURE_DEVOPS_TOKEN", "SKILLSHARE_GIT_TOKEN", "BITBUCKET_USERNAME",
|
|
"CNB_TOKEN", "GITEA_TOKEN",
|
|
}
|
|
for _, v := range vars {
|
|
if t := os.Getenv(v); t != "" {
|
|
text = strings.ReplaceAll(text, t, "[REDACTED]")
|
|
}
|
|
}
|
|
return text
|
|
}
|
|
|
|
// RedactRepoURL returns repoURL without its userinfo, query and fragment, plus
|
|
// a replacer that removes those parts from text echoing the URL. Userinfo is
|
|
// taken up to the last "@" before the first "/", so a "?" or "#" inside a
|
|
// password cannot hide it while an "@" in the path stays. A source without a
|
|
// scheme, such as "user:token@host/owner/repo", is cloned over HTTPS (see
|
|
// expandGitHubShorthand) and redacted as such; scp-style URLs and local paths
|
|
// come back unchanged.
|
|
func RedactRepoURL(repoURL string) (string, *strings.Replacer) {
|
|
scheme, rest, ok := strings.Cut(repoURL, "://")
|
|
if !ok {
|
|
if isLocalPath(repoURL) || gitSSHPattern.MatchString(repoURL) {
|
|
return repoURL, strings.NewReplacer()
|
|
}
|
|
clean, redact := RedactRepoURL("https://" + repoURL)
|
|
return strings.TrimPrefix(clean, "https://"), redact
|
|
}
|
|
var pairs []string
|
|
host, path := rest, ""
|
|
if i := strings.IndexByte(rest, '/'); i >= 0 {
|
|
host, path = rest[:i], rest[i:]
|
|
}
|
|
if at := strings.LastIndex(host, "@"); at > 0 {
|
|
userinfo := host[:at]
|
|
host = host[at+1:]
|
|
pairs = append(pairs, userinfo+"@", "")
|
|
if _, password, ok := strings.Cut(userinfo, ":"); ok && password != "" {
|
|
pairs = append(pairs, password, "[REDACTED]")
|
|
}
|
|
}
|
|
rest = host + path
|
|
if i := strings.IndexByte(rest, '#'); i >= 0 {
|
|
if len(rest)-i > 1 {
|
|
pairs = append(pairs, rest[i:], "")
|
|
}
|
|
rest = rest[:i]
|
|
}
|
|
if i := strings.IndexByte(rest, '?'); i >= 0 {
|
|
if query := rest[i+1:]; query != "" {
|
|
pairs = append(pairs, rest[i:], "", query, "[REDACTED]")
|
|
}
|
|
rest = rest[:i]
|
|
}
|
|
return scheme + "://" + rest, strings.NewReplacer(pairs...)
|
|
}
|
|
|
|
// urlUsername extracts the username from an HTTPS URL, if present.
|
|
// Returns "" for URLs without userinfo (e.g. https://github.com/...).
|
|
func urlUsername(cloneURL string) string {
|
|
u, err := url.Parse(strings.TrimSpace(cloneURL))
|
|
if err != nil || u.User == nil {
|
|
return ""
|
|
}
|
|
return u.User.Username()
|
|
}
|
|
|
|
// isHTTPS returns true if the URL uses the HTTPS scheme.
|
|
func isHTTPS(cloneURL string) bool {
|
|
return strings.HasPrefix(strings.ToLower(strings.TrimSpace(cloneURL)), "https://")
|
|
}
|
|
|
|
// IsAuthError returns true when git stderr indicates an authentication failure.
|
|
func IsAuthError(output string) bool {
|
|
return strings.Contains(output, "Authentication failed") ||
|
|
strings.Contains(output, "Access denied") ||
|
|
strings.Contains(output, "could not read Username") ||
|
|
strings.Contains(output, "terminal prompts disabled")
|
|
}
|
|
|
|
// IsSSLError returns true when git stderr indicates a TLS/SSL certificate failure.
|
|
func IsSSLError(output string) bool {
|
|
lower := strings.ToLower(output)
|
|
return strings.Contains(lower, "ssl certificate problem") ||
|
|
strings.Contains(lower, "unable to get local issuer certificate") ||
|
|
strings.Contains(lower, "self signed certificate") ||
|
|
strings.Contains(lower, "certificate verify failed") ||
|
|
strings.Contains(lower, "certificate verification failed")
|
|
}
|