Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -1085,6 +1085,18 @@ described through `describeRejectedOrigin` so a password or token in the URL
never reaches stderr. Never build a credential-bearing client with a nil
`CheckRedirect`.

Transfers to server-chosen URLs (presigned object-store URLs, the ti fs download
redirect, Starter export files, and `ti update` release metadata and artifacts)
carry no credential header, so they may cross origins, but they must never
travel in cleartext. Build the request with `transport.NewSecureRequest` and
send it with `transport.DoSecure`: the URL and every redirect must be `https`,
or plain `http` only to a loopback host, never with user info, and a redirect
from `https` to `http` is refused even to loopback. Never put the raw transfer
URL in an error, because its query carries the presigned signature; a refused
URL is named only by `transport.Origin`, and net/http's own error for a redirect
whose `Location` does not parse is dropped (`transport.IsLocationParseError`)
because it repeats the raw `Location` value.

Do not store secrets in logs, telemetry, generated docs examples, or test
fixtures.

Expand Down
37 changes: 31 additions & 6 deletions cmd/ti-telemetry-migrate/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,55 @@ package main
import (
"context"
"database/sql"
"errors"
"flag"
"io"
"log/slog"
"os"
"strings"
"time"

_ "github.com/go-sql-driver/mysql"
"github.com/tidbcloud/ti-cli/internal/telemetrybackend"
"github.com/tidbcloud/ti-cli/internal/telemetrybackend/migrations"
)

func main() {
logger := slog.New(slog.NewJSONHandler(os.Stderr, nil))
os.Exit(run(os.Args[1:], os.Getenv, logger))
}

func run(args []string, getenv func(string) string, logger *slog.Logger) int {
flags := flag.NewFlagSet("ti-telemetry-migrate", flag.ContinueOnError)
flags.SetOutput(io.Discard)
var targetVersion int64
flag.Int64Var(&targetVersion, "to-version", 0, "apply migrations through this version")
flag.Parse()
flags.Int64Var(&targetVersion, "to-version", 0, "apply migrations through this version")
if err := flags.Parse(args); err != nil {
if errors.Is(err, flag.ErrHelp) {
flags.SetOutput(os.Stderr)
flags.PrintDefaults()
return 0
}
logger.Error("invalid arguments", "error", err.Error())
return 2
}

dsn := strings.TrimSpace(os.Getenv("TIDB_DSN"))
dsn := strings.TrimSpace(getenv("TIDB_DSN"))
if dsn == "" {
logger.Error("TIDB_DSN is required")
os.Exit(1)
return 1
}
// Same rule as the API server: in production (also when
// TELEMETRY_ENVIRONMENT is unset or not a known local/test name) the DSN
// must use verified TLS.
if err := telemetrybackend.ValidateTiDBDSN(getenv("TELEMETRY_ENVIRONMENT"), dsn); err != nil {
logger.Error("refusing TIDB_DSN", "error", err.Error())
return 1
}
db, err := sql.Open("mysql", dsn)
if err != nil {
logger.Error("open TiDB connection failed")
os.Exit(1)
return 1
}
defer db.Close()
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
Expand All @@ -39,7 +63,8 @@ func main() {
}
if err != nil {
logger.Error("apply telemetry schema migrations failed")
os.Exit(1)
return 1
}
logger.Info("telemetry schema migrations applied")
return 0
}
56 changes: 56 additions & 0 deletions cmd/ti-telemetry-migrate/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package main

import (
"bytes"
"log/slog"
"strings"
"testing"
)

func TestRunRefusesProductionDSNWithoutVerifiedTLS(t *testing.T) {
for _, tc := range []struct {
name string
environment string
}{
{"unset environment defaults to production", ""},
{"explicit production", "production"},
{"misspelled production", "prod"},
} {
t.Run(tc.name, func(t *testing.T) {
env := map[string]string{
"TIDB_DSN": "user:secret-password@tcp(203.0.113.10:4000)/telemetry?tls=skip-verify",
"TELEMETRY_ENVIRONMENT": tc.environment,
}
var logs bytes.Buffer
code := run(nil, func(key string) string { return env[key] }, slog.New(slog.NewJSONHandler(&logs, nil)))
if code != 1 {
t.Fatalf("run exit code = %d, want 1", code)
}
if !strings.Contains(logs.String(), "verified TLS") {
t.Fatalf("logs = %s, want a verified TLS refusal", logs.String())
}
if strings.Contains(logs.String(), "secret-password") {
t.Fatalf("logs leak the DSN password: %s", logs.String())
}
if strings.Contains(logs.String(), "migrations failed") {
t.Fatalf("run tried to migrate before refusing the DSN: %s", logs.String())
}
})
}
}

func TestRunRequiresDSN(t *testing.T) {
var logs bytes.Buffer
code := run(nil, func(string) string { return "" }, slog.New(slog.NewJSONHandler(&logs, nil)))
if code != 1 || !strings.Contains(logs.String(), "TIDB_DSN is required") {
t.Fatalf("run exit code = %d, logs = %s", code, logs.String())
}
}

func TestRunRejectsUnknownFlag(t *testing.T) {
var logs bytes.Buffer
code := run([]string{"--unknown"}, func(string) string { return "" }, slog.New(slog.NewJSONHandler(&logs, nil)))
if code != 2 {
t.Fatalf("run exit code = %d, want 2", code)
}
}
2 changes: 1 addition & 1 deletion docs/telemetry-backend-design.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ TELEMETRY_TRUSTED_PROXY_CIDRS=172.16.0.0/12
TIDB_DSN=tdc_telemetry:password@tcp(gateway01.us-east-1.prod.aws.tidbcloud.com:4000)/tdc_telemetry?tls=true&parseTime=true
```

`TIDB_DSN` is an application credential. It must remain only in the server-side `.env`, must never be stored in GitHub repository or Environment secrets, must never be committed to git, and must never be logged. GitHub may store only deployment transport credentials such as the SSH host, username, and key. For TiDB Cloud, the DSN must enable TLS with certificate and identity verification. `TELEMETRY_ENVIRONMENT=production` enables this strict TLS validation.
`TIDB_DSN` is an application credential. It must remain only in the server-side `.env`, must never be stored in GitHub repository or Environment secrets, must never be committed to git, and must never be logged. GitHub may store only deployment transport credentials such as the SSH host, username, and key. For TiDB Cloud, the DSN must enable TLS with certificate and identity verification. Both the API server and `ti-telemetry-migrate` enforce this strict TLS validation. Every `TELEMETRY_ENVIRONMENT` value counts as production, an unset one included, except the local and test names `development`, `local`, `test` and `telemetry-e2e`, so a misspelling such as `prod` still gets the strict rule.

Deployments upgraded from the former dual-sink implementation may still have `POSTHOG_API_HOST` and `POSTHOG_PROJECT_TOKEN` in their server-local `.env`. The backend ignores those variables; operators should remove them after deployment. This change stops future forwarding but does not delete historical data already held by an external service.

Expand Down
167 changes: 167 additions & 0 deletions e2e/installer_https_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
package e2e

import (
"encoding/pem"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"sync/atomic"
"testing"
)

// installShellDownload returns install.sh's error() and download() functions
// followed by a call `download "$1" "$2"`, so the real download code runs alone.
func installShellDownload(t *testing.T) string {
t.Helper()
data, err := os.ReadFile(filepath.Join("..", "scripts", "install.sh"))
if err != nil {
t.Fatal(err)
}
script := string(data)
var errorLine string
for _, line := range strings.Split(script, "\n") {
if strings.HasPrefix(line, "error() {") {
errorLine = line
break
}
}
start := strings.Index(script, "\ndownload() {\n")
if errorLine == "" || start < 0 {
t.Fatal("install.sh no longer defines error() and download()")
}
end := strings.Index(script[start+1:], "\n}\n")
if end < 0 {
t.Fatal("install.sh download() is not terminated")
}
body := script[start+1 : start+1+end+3]
return "set -eu\nRED=''\nRESET=''\n" + errorLine + "\n" + body + "download \"$1\" \"$2\"\n"
}

func TestUnixInstallerDownloadsStayOnHTTPS(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("shell installer is not supported on Windows")
}
curl, err := exec.LookPath("curl")
if err != nil {
t.Skip("curl not found")
}
var plainHits atomic.Int32
plain := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
plainHits.Add(1)
_, _ = w.Write([]byte("tampered"))
}))
defer plain.Close()
secure := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/redirect" {
http.Redirect(w, r, plain.URL+"/ti.tar.gz", http.StatusFound)
return
}
_, _ = w.Write([]byte("genuine"))
}))
defer secure.Close()

dir := t.TempDir()
caFile := filepath.Join(dir, "ca.pem")
caPEM := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: secure.Certificate().Raw})
if err := os.WriteFile(caFile, caPEM, 0o600); err != nil {
t.Fatal(err)
}
script := installShellDownload(t)
download := func(rawURL, out string) ([]byte, error) {
cmd := exec.Command("sh", "-c", script, "sh", rawURL, out)
cmd.Env = []string{
"PATH=" + filepath.Dir(curl) + ":/usr/bin:/bin",
"CURL_CA_BUNDLE=" + caFile,
"SSL_CERT_FILE=" + caFile,
}
return cmd.CombinedOutput()
}

refused := func(name, rawURL string) {
t.Helper()
out := filepath.Join(dir, strings.ReplaceAll(name, " ", "-"))
if output, err := download(rawURL, out); err == nil {
t.Fatalf("%s: download succeeded:\n%s", name, output)
}
if data, _ := os.ReadFile(out); string(data) == "tampered" {
t.Fatalf("%s: download wrote the cleartext bytes", name)
}
if hits := plainHits.Load(); hits != 0 {
t.Fatalf("%s: the plain http server was reached %d times", name, hits)
}
}
// Needs no TLS, so it runs even where curl can't be pointed at the test CA.
refused("plain http", plain.URL+"/ti.tar.gz")

okFile := filepath.Join(dir, "ok")
if output, err := download(secure.URL+"/ti.tar.gz", okFile); err != nil {
if os.Getenv("CI") != "" {
t.Fatalf("curl does not trust the test certificate: %v\n%s", err, output)
}
t.Skipf("curl does not trust the test certificate here: %v\n%s", err, output)
}
if data, _ := os.ReadFile(okFile); string(data) != "genuine" {
t.Fatalf("https download wrote %q", data)
}
refused("https redirect to http", secure.URL+"/redirect")
}

func TestUnixInstallerRequiresCurl(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("shell installer is not supported on Windows")
}
sh, err := exec.LookPath("sh")
if err != nil {
t.Skip("sh not found")
}
// A PATH with only sh: no curl, and no wget fallback either.
bin := t.TempDir()
if err := os.Symlink(sh, filepath.Join(bin, "sh")); err != nil {
t.Fatal(err)
}
cmd := exec.Command(filepath.Join(bin, "sh"), "-c", installShellDownload(t), "sh", "https://example.invalid/ti.tar.gz", filepath.Join(t.TempDir(), "out"))
cmd.Env = []string{"PATH=" + bin}
output, err := cmd.CombinedOutput()
if err == nil || !strings.Contains(string(output), "curl is required") {
t.Fatalf("download without curl = %v\n%s", err, output)
}
}

func TestInstallersRefuseDownloadsThatLeaveHTTPS(t *testing.T) {
shell, err := os.ReadFile(filepath.Join("..", "scripts", "install.sh"))
if err != nil {
t.Fatal(err)
}
if strings.Count(string(shell), "curl -f") != strings.Count(string(shell), "--proto '=https' --proto-redir '=https'") {
t.Fatal("every curl download in install.sh must use --proto '=https' --proto-redir '=https'")
}
if strings.Contains(string(shell), "wget -") {
t.Fatal("install.sh must not download with wget: it can't refuse an https-to-http redirect")
}

powerShell, err := os.ReadFile(filepath.Join("..", "scripts", "install.ps1"))
if err != nil {
t.Fatal(err)
}
ps := string(powerShell)
if got := strings.Count(ps, "Invoke-WebRequest"); got != 1 {
t.Fatalf("install.ps1 calls Invoke-WebRequest %d times; every download must go through Save-HttpsDownload", got)
}
if !strings.Contains(ps, `$FinalUri.Scheme -ne "https"`) {
t.Fatal("install.ps1 must check that the final download URL is https")
}
for _, call := range []string{
"Save-HttpsDownload $ArchiveUrl $ArchivePath",
"Save-HttpsDownload $ChecksumsUrl $ChecksumsPath",
"Save-HttpsDownload $CompanionUrl $CompanionPath",
"Save-HttpsDownload $CompanionChecksumsUrl $CompanionChecksumsPath",
} {
if !strings.Contains(ps, call) {
t.Fatalf("install.ps1 is missing %q", call)
}
}
}
1 change: 1 addition & 0 deletions e2e/installer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ url=''
while [ "$#" -gt 0 ]; do
case "$1" in
-o) out="$2"; shift 2 ;;
--proto|--proto-redir) shift 2 ;;
-*) shift ;;
*) url="$1"; shift ;;
esac
Expand Down
4 changes: 3 additions & 1 deletion e2e/telemetry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ func telemetryMigratorBinary(t *testing.T) string {
func runTelemetryMigrator(t *testing.T, ctx context.Context, binary, dsn string, args ...string) {
t.Helper()
command := exec.CommandContext(ctx, binary, args...)
command.Env = append(os.Environ(), "TIDB_DSN="+dsn)
// Match the API server below: the e2e TiDB is not on verified TLS, and the
// migrator, like the API, treats an unset environment as production.
command.Env = append(os.Environ(), "TIDB_DSN="+dsn, "TELEMETRY_ENVIRONMENT=telemetry-e2e")
output, err := command.CombinedOutput()
if err != nil {
t.Fatalf("run telemetry migrator: %v\n%s", err, output)
Expand Down
11 changes: 11 additions & 0 deletions internal/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,17 @@ func (c *Client) do(req *http.Request) (*http.Response, error) {
// requestError maps a failed round trip to an API error. A same-origin
// redirect loop is reported as such rather than as a network problem.
func requestError(req *http.Request, err error) error {
// net/http reports a redirect whose Location does not parse with the raw
// Location value in the message. A ti fs download redirect carries a presigned
// signature there, so that cause is dropped rather than shown.
if apitransport.IsLocationParseError(err) {
return &Error{
Code: "api.invalid_redirect",
Category: "api",
ExitCode: 1,
Message: fmt.Sprintf("API request failed: %s sent a redirect with an invalid Location header", req.URL.Host),
}
}
if errors.Is(err, apitransport.ErrTooManyRedirects) {
return &Error{
Code: "api.redirect_loop",
Expand Down
Loading