|
| 1 | +package jobs |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +// TestSplitPGPassword pins the SEC-WORKER FINDING-1 + FINDING-2 fix: |
| 9 | +// pg_dump call sites move the Postgres password from process argv into |
| 10 | +// PGPASSWORD env. The helper must: |
| 11 | +// 1. Strip the password from a typical userinfo URL. |
| 12 | +// 2. Pass through unchanged when there is no password (cert auth, |
| 13 | +// no user, malformed URL with fail-open). |
| 14 | +// 3. Never leak the literal password in the returned URL. |
| 15 | +func TestSplitPGPassword(t *testing.T) { |
| 16 | + cases := []struct { |
| 17 | + name string |
| 18 | + in string |
| 19 | + wantURL string |
| 20 | + wantPW string |
| 21 | + wantErr bool |
| 22 | + }{ |
| 23 | + { |
| 24 | + name: "userpass", |
| 25 | + in: "postgres://doadmin:abc123@host:25060/db?sslmode=require", |
| 26 | + wantURL: "postgres://doadmin@host:25060/db?sslmode=require", |
| 27 | + wantPW: "abc123", |
| 28 | + }, |
| 29 | + { |
| 30 | + name: "user_only_no_password", |
| 31 | + in: "postgres://doadmin@host:25060/db?sslmode=require", |
| 32 | + wantURL: "postgres://doadmin@host:25060/db?sslmode=require", |
| 33 | + wantPW: "", |
| 34 | + }, |
| 35 | + { |
| 36 | + name: "no_userinfo", |
| 37 | + in: "postgres://host:25060/db", |
| 38 | + wantURL: "postgres://host:25060/db", |
| 39 | + wantPW: "", |
| 40 | + }, |
| 41 | + { |
| 42 | + name: "empty", |
| 43 | + in: "", |
| 44 | + wantURL: "", |
| 45 | + wantPW: "", |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "percent_encoded_password", |
| 49 | + in: "postgres://u:p%40ss%40word@h:5432/db", |
| 50 | + wantURL: "postgres://u@h:5432/db", |
| 51 | + wantPW: "p@ss@word", // url.Userinfo.Password() decodes |
| 52 | + }, |
| 53 | + { |
| 54 | + name: "malformed_url_fail_open", |
| 55 | + in: "::::not a url", |
| 56 | + wantURL: "::::not a url", |
| 57 | + wantPW: "", |
| 58 | + wantErr: true, |
| 59 | + }, |
| 60 | + } |
| 61 | + for _, c := range cases { |
| 62 | + t.Run(c.name, func(t *testing.T) { |
| 63 | + gotURL, gotPW, err := splitPGPassword(c.in) |
| 64 | + if (err != nil) != c.wantErr { |
| 65 | + t.Fatalf("err = %v, wantErr = %v", err, c.wantErr) |
| 66 | + } |
| 67 | + if gotURL != c.wantURL { |
| 68 | + t.Errorf("URL\n got: %q\n want: %q", gotURL, c.wantURL) |
| 69 | + } |
| 70 | + if gotPW != c.wantPW { |
| 71 | + t.Errorf("password\n got: %q\n want: %q", gotPW, c.wantPW) |
| 72 | + } |
| 73 | + }) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +// TestSplitPGPassword_NoLeak: the literal password substring must never |
| 78 | +// appear in the returned URL (this is THE point of the fix). |
| 79 | +func TestSplitPGPassword_NoLeak(t *testing.T) { |
| 80 | + const secret = "ZZZ_NEVER_IN_URL_ZZZ" |
| 81 | + in := "postgres://admin:" + secret + "@host:5432/db?sslmode=require" |
| 82 | + gotURL, gotPW, err := splitPGPassword(in) |
| 83 | + if err != nil { |
| 84 | + t.Fatalf("splitPGPassword(%q) error: %v", in, err) |
| 85 | + } |
| 86 | + if strings.Contains(gotURL, secret) { |
| 87 | + t.Errorf("returned URL %q still contains the password %q", gotURL, secret) |
| 88 | + } |
| 89 | + if gotPW != secret { |
| 90 | + t.Errorf("password\n got: %q\n want: %q", gotPW, secret) |
| 91 | + } |
| 92 | +} |
0 commit comments