From b9fd6325ea57df260c4db88a5333b665b681c944 Mon Sep 17 00:00:00 2001 From: jerryfane Date: Tue, 10 Nov 2026 16:05:18 +0100 Subject: [PATCH 1/3] Refuse a placeholder OAuth client ID on connect and edit (146) A saved PASTE_YOUR_CLIENT_ID_HERE made Google answer invalid_client. Connect and the service edit form now refuse a client ID containing PASTE, YOUR, CLIENT_ID or angle brackets (uppercase only; Google's IDs are lowercase), and, for oauth2.googleapis.com or accounts.google.com, one not ending in .apps.googleusercontent.com. Not checked on config load, so a running config still starts. --- internal/dashboard/dashboard.go | 26 ++++++++++++++++ internal/dashboard/oauth2_connect_test.go | 37 +++++++++++++++++++++-- internal/dashboard/services.go | 4 +++ 3 files changed, 65 insertions(+), 2 deletions(-) diff --git a/internal/dashboard/dashboard.go b/internal/dashboard/dashboard.go index 3c1ac5a..c2a8c9e 100644 --- a/internal/dashboard/dashboard.go +++ b/internal/dashboard/dashboard.go @@ -911,6 +911,26 @@ func storedKeyRef(stored map[string]bool, field, key string) string { return "" } +// clientIDProblem returns the message to show if an OAuth client ID is a +// template placeholder left in (Jarvis 447635: a saved "PASTE_YOUR_CLIENT_ID_HERE" +// made Google answer invalid_client), or, for Google's token endpoint, not a +// Google client ID. Uppercase only: real IDs (Google's are lowercase) never +// match. Checked on connect and edit, not on load, so a running config still +// starts. +func clientIDProblem(o *policy.OAuth2) string { + id := o.ClientID + for _, p := range []string{"PASTE", "YOUR", "CLIENT_ID", "<", ">"} { + if strings.Contains(id, p) { + return "The OAuth client ID looks like a placeholder. Copy the real one from the provider (Google: Clients > your client > Client ID)." + } + } + if u, err := url.Parse(o.TokenURL); err == nil && (u.Hostname() == "oauth2.googleapis.com" || u.Hostname() == "accounts.google.com") && + !strings.HasSuffix(id, ".apps.googleusercontent.com") { + return "A Google OAuth client ID ends in .apps.googleusercontent.com. Copy it from Google Cloud: Clients > your client > Client ID." + } + return "" +} + // connectFormFor fills the form from a preset for key name. func (b *Backend) connectFormFor(name, id string) connectForm { f := connectForm{Preset: id, Auth: policy.AuthBearer} @@ -1020,6 +1040,12 @@ func (b *Backend) connectKey(w http.ResponseWriter, r *http.Request, sid string) again(http.StatusBadRequest, msg) return } + if f.OAuth2 != nil { + if msg := clientIDProblem(f.OAuth2); msg != "" { + again(http.StatusBadRequest, msg) + return + } + } if f.OAuth2 != nil && f.OAuth2.ClientSecretKey != "" { // Same rule as extra headers: a stored key's name, never a value, // and never repeated in the message. diff --git a/internal/dashboard/oauth2_connect_test.go b/internal/dashboard/oauth2_connect_test.go index 43410ab..f561cc2 100644 --- a/internal/dashboard/oauth2_connect_test.go +++ b/internal/dashboard/oauth2_connect_test.go @@ -1,6 +1,7 @@ package dashboard import ( + "html" "io" "net/http" "net/http/httptest" @@ -144,7 +145,7 @@ func TestEditOAuthServiceFixesAWrongClientID(t *testing.T) { e := newEnv(t) g, sends := fakeGoogle(t) e.request("POST", "/keys", url.Values{"name": {"GMAIL_SECRET"}, "value": {gmailSecret}, "password": {password}}) - pasted := `{"service":"gm","base":"` + g.URL + `","auth":"oauth2","oauth2":{"token_url":"` + g.URL + `/token","client_id":"CLIENT_ID","client_secret_key":"GMAIL_SECRET"}}` + pasted := `{"service":"gm","base":"` + g.URL + `","auth":"oauth2","oauth2":{"token_url":"` + g.URL + `/token","client_id":"wrong.apps.googleusercontent.com","client_secret_key":"GMAIL_SECRET"}}` e.addAndConnect("GMAIL_REFRESH", gmailRefresh, url.Values{"pasted": {pasted}}) cfg := e.proxy.Config() access := cfg.AccessList @@ -165,7 +166,7 @@ func TestEditOAuthServiceFixesAWrongClientID(t *testing.T) { t.Fatalf("send with a wrong client ID: %d, want 503", code) } page := e.request("GET", "/services/gm", nil).Body.String() - if !strings.Contains(page, `name="client_id" value="CLIENT_ID"`) { + if !strings.Contains(page, `name="client_id" value="wrong.apps.googleusercontent.com"`) { t.Fatal("the edit form does not show the client ID") } w := e.edit("gm", url.Values{"base": {g.URL}, "auth": {"oauth2"}, "client_id": {"fixture.apps.googleusercontent.com"}, "token_url": {g.URL + "/token"}, "client_secret_key": {"GMAIL_SECRET"}}) @@ -194,3 +195,35 @@ func TestEditOAuthServiceFixesAWrongClientID(t *testing.T) { t.Fatal("a secret is shown or logged") } } + +// Jarvis 447635: a template placeholder saved as the client ID made Google +// answer invalid_client. Connect and edit refuse a placeholder, and a +// non-Google client ID for Google's token endpoint; a real one is accepted. +func TestOAuthPlaceholderClientIDRefused(t *testing.T) { + e := newEnv(t) + e.request("POST", "/keys", url.Values{"name": {"GM_SECRET"}, "value": {gmailSecret}, "password": {password}}) + e.request("POST", "/keys", url.Values{"name": {"GM_REFRESH"}, "value": {gmailRefresh}, "password": {password}}) + connect := func(service, clientID string) *httptest.ResponseRecorder { + pasted := `{"service":"` + service + `","base":"https://gmail.googleapis.com","auth":"oauth2","oauth2":{"token_url":"https://oauth2.googleapis.com/token","client_id":"` + clientID + `","client_secret_key":"GM_SECRET"}}` + return e.request("POST", "/keys/GM_REFRESH/connect", url.Values{"pasted": {pasted}, "password": {password}}) + } + for id, want := range map[string]string{ + "PASTE_YOUR_CLIENT_ID_HERE": "placeholder", + "123-abc.apps.example.com": "ends in .apps.googleusercontent.com", + ".apps.googleusercontent.com": "placeholder", + } { + if w := connect("gm", id); w.Code != http.StatusBadRequest || !strings.Contains(html.UnescapeString(w.Body.String()), want) { + t.Errorf("connect %s: %d, want 400 with %q", id, w.Code, want) + } + } + if _, ok := e.proxy.Config().Services["gm"]; ok { + t.Fatal("a placeholder client ID was saved") + } + if w := connect("gm", "123-abc.apps.googleusercontent.com"); w.Code != http.StatusSeeOther { + t.Fatalf("real client ID: %d %s", w.Code, w.Body.String()) + } + w := e.edit("gm", url.Values{"base": {"https://gmail.googleapis.com"}, "auth": {"oauth2"}, "client_id": {"PASTE_YOUR_CLIENT_ID_HERE"}, "token_url": {"https://oauth2.googleapis.com/token"}, "client_secret_key": {"GM_SECRET"}}) + if w.Code != http.StatusBadRequest || e.proxy.Config().Services["gm"].OAuth2.ClientID != "123-abc.apps.googleusercontent.com" { + t.Fatalf("edit to a placeholder: %d, client ID now %q", w.Code, e.proxy.Config().Services["gm"].OAuth2.ClientID) + } +} diff --git a/internal/dashboard/services.go b/internal/dashboard/services.go index e4bd6ea..a2078a3 100644 --- a/internal/dashboard/services.go +++ b/internal/dashboard/services.go @@ -224,6 +224,10 @@ func (b *Backend) saveService(w http.ResponseWriter, r *http.Request, sid string case !pasted: next.ClientID, next.TokenURL, next.ClientSecretKey = f.ClientID, f.TokenURL, f.ClientSecretKey } + if msg := clientIDProblem(&next); msg != "" { + again(http.StatusBadRequest, msg) + return + } if next.ClientSecretKey != "" { stored, err := b.storedNames() if err != nil { From 6d208da114fe568cfd18ee9006a02f92d03d5c1b Mon Sep 17 00:00:00 2001 From: jerryfane Date: Tue, 10 Nov 2026 16:13:31 +0100 Subject: [PATCH 2/3] Placeholder check: whole template shapes only; edit checks a changed ID only (#147 review F1) REPORTING_CLIENT_ID or WORKYOURWAY are valid opaque IDs elsewhere. Match template values instead: starts with PASTE_/YOUR_/ENTER_/INSERT_/REPLACE_, ends in _HERE, is just client_id, or uses <...>, {{ or ${ syntax. An edit that keeps the client ID and token URL is not refused for them. --- internal/dashboard/dashboard.go | 22 ++++++++++------- internal/dashboard/oauth2_connect_test.go | 29 +++++++++++++++++++++++ internal/dashboard/services.go | 4 +++- 3 files changed, 45 insertions(+), 10 deletions(-) diff --git a/internal/dashboard/dashboard.go b/internal/dashboard/dashboard.go index c2a8c9e..2f71832 100644 --- a/internal/dashboard/dashboard.go +++ b/internal/dashboard/dashboard.go @@ -14,6 +14,7 @@ import ( "net/http" "net/url" "os" + "regexp" "slices" "sort" "strings" @@ -914,23 +915,26 @@ func storedKeyRef(stored map[string]bool, field, key string) string { // clientIDProblem returns the message to show if an OAuth client ID is a // template placeholder left in (Jarvis 447635: a saved "PASTE_YOUR_CLIENT_ID_HERE" // made Google answer invalid_client), or, for Google's token endpoint, not a -// Google client ID. Uppercase only: real IDs (Google's are lowercase) never -// match. Checked on connect and edit, not on load, so a running config still -// starts. +// Google client ID. Only whole template shapes count, so an opaque ID that +// merely contains such a word (REPORTING_CLIENT_ID) passes. Checked on +// connect and on an edit that changes the ID, never on load, so a running +// config still starts. func clientIDProblem(o *policy.OAuth2) string { - id := o.ClientID - for _, p := range []string{"PASTE", "YOUR", "CLIENT_ID", "<", ">"} { - if strings.Contains(id, p) { - return "The OAuth client ID looks like a placeholder. Copy the real one from the provider (Google: Clients > your client > Client ID)." - } + if placeholderID.MatchString(o.ClientID) { + return "The OAuth client ID looks like a placeholder. Copy the real one from the provider (Google: Clients > your client > Client ID)." } if u, err := url.Parse(o.TokenURL); err == nil && (u.Hostname() == "oauth2.googleapis.com" || u.Hostname() == "accounts.google.com") && - !strings.HasSuffix(id, ".apps.googleusercontent.com") { + !strings.HasSuffix(o.ClientID, ".apps.googleusercontent.com") { return "A Google OAuth client ID ends in .apps.googleusercontent.com. Copy it from Google Cloud: Clients > your client > Client ID." } return "" } +// placeholderID is a template value: one that starts with an instruction +// (PASTE_, YOUR_, ENTER_, INSERT_, REPLACE_), ends in _HERE, is just the +// field name, or uses <...>, {{...}} or ${...} placeholder syntax. +var placeholderID = regexp.MustCompile(`(?i)^(paste|your|enter|insert|replace)[_-]|[_-]here$|^client[_-]?id$|<[^>]*>|\{\{|\$\{`) + // connectFormFor fills the form from a preset for key name. func (b *Backend) connectFormFor(name, id string) connectForm { f := connectForm{Preset: id, Auth: policy.AuthBearer} diff --git a/internal/dashboard/oauth2_connect_test.go b/internal/dashboard/oauth2_connect_test.go index f561cc2..39c6aee 100644 --- a/internal/dashboard/oauth2_connect_test.go +++ b/internal/dashboard/oauth2_connect_test.go @@ -219,6 +219,17 @@ func TestOAuthPlaceholderClientIDRefused(t *testing.T) { if _, ok := e.proxy.Config().Services["gm"]; ok { t.Fatal("a placeholder client ID was saved") } + // Opaque IDs of other providers that merely contain such words are fine. + for _, id := range []string{"REPORTING_CLIENT_ID", "WORKYOURWAY", "your.app.client"} { + if got := clientIDProblem(&policy.OAuth2{ClientID: id, TokenURL: "https://auth.example.com/token"}); got != "" { + t.Errorf("%s refused: %s", id, got) + } + } + for _, id := range []string{"YOUR_CLIENT_ID", "client_id", "PASTE-ID-HERE", "${CLIENT}", "{{client_id}}"} { + if clientIDProblem(&policy.OAuth2{ClientID: id, TokenURL: "https://auth.example.com/token"}) == "" { + t.Errorf("%s accepted", id) + } + } if w := connect("gm", "123-abc.apps.googleusercontent.com"); w.Code != http.StatusSeeOther { t.Fatalf("real client ID: %d %s", w.Code, w.Body.String()) } @@ -226,4 +237,22 @@ func TestOAuthPlaceholderClientIDRefused(t *testing.T) { if w.Code != http.StatusBadRequest || e.proxy.Config().Services["gm"].OAuth2.ClientID != "123-abc.apps.googleusercontent.com" { t.Fatalf("edit to a placeholder: %d, client ID now %q", w.Code, e.proxy.Config().Services["gm"].OAuth2.ClientID) } + // A config that already holds one (saved before this check) still edits + // its other fields; only a changed client ID is checked. + cfg := e.proxy.Config() + access := cfg.AccessList + svc := access.Services["gm"] + o := *svc.OAuth2 + o.ClientID = "PASTE_YOUR_CLIENT_ID_HERE" + svc.OAuth2 = &o + access.Services["gm"] = svc + next, err := policy.SaveAccess(cfg.Rules, access) + if err != nil { + t.Fatal(err) + } + e.proxy.Swap(next, mustKeys(t, e)) + w = e.edit("gm", url.Values{"base": {"https://gmail.googleapis.com"}, "auth": {"oauth2"}, "test_path": {"/gmail/v1/users/me/profile"}, "client_id": {"PASTE_YOUR_CLIENT_ID_HERE"}, "token_url": {"https://oauth2.googleapis.com/token"}, "client_secret_key": {"GM_SECRET"}}) + if w.Header().Get("Location") != "/services/gm?done=saved" { + t.Fatalf("unrelated edit of an existing placeholder: %d %s", w.Code, w.Body.String()) + } } diff --git a/internal/dashboard/services.go b/internal/dashboard/services.go index a2078a3..b5e2ca4 100644 --- a/internal/dashboard/services.go +++ b/internal/dashboard/services.go @@ -224,7 +224,9 @@ func (b *Backend) saveService(w http.ResponseWriter, r *http.Request, sid string case !pasted: next.ClientID, next.TokenURL, next.ClientSecretKey = f.ClientID, f.TokenURL, f.ClientSecretKey } - if msg := clientIDProblem(&next); msg != "" { + if next.ClientID == cur.OAuth2.ClientID && next.TokenURL == cur.OAuth2.TokenURL { + // Unchanged: an edit of another field is not refused for it. + } else if msg := clientIDProblem(&next); msg != "" { again(http.StatusBadRequest, msg) return } From 8406ee452de7db8c7cebb26f526d2cc399579f35 Mon Sep 17 00:00:00 2001 From: jerryfane Date: Tue, 10 Nov 2026 16:21:12 +0100 Subject: [PATCH 3/3] Placeholder check: complete template values only (#147 review F1, round 2) enter_47f9e2 or app_here are opaque IDs. Refuse only a whole template for the field itself (optional PASTE_/ENTER_/INSERT_/PUT_/REPLACE_, optional YOUR_, optional OAUTH_/GOOGLE_, CLIENT_ID, optional _HERE) or explicit <...>, {{ or ${ syntax. --- internal/dashboard/dashboard.go | 9 +++++---- internal/dashboard/oauth2_connect_test.go | 4 ++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/internal/dashboard/dashboard.go b/internal/dashboard/dashboard.go index 2f71832..e60a016 100644 --- a/internal/dashboard/dashboard.go +++ b/internal/dashboard/dashboard.go @@ -930,10 +930,11 @@ func clientIDProblem(o *policy.OAuth2) string { return "" } -// placeholderID is a template value: one that starts with an instruction -// (PASTE_, YOUR_, ENTER_, INSERT_, REPLACE_), ends in _HERE, is just the -// field name, or uses <...>, {{...}} or ${...} placeholder syntax. -var placeholderID = regexp.MustCompile(`(?i)^(paste|your|enter|insert|replace)[_-]|[_-]here$|^client[_-]?id$|<[^>]*>|\{\{|\$\{`) +// placeholderID is a complete template value for the field itself, such as +// PASTE_YOUR_CLIENT_ID_HERE, YOUR_CLIENT_ID or client_id, or explicit +// placeholder syntax: <...>, {{...}} or ${...}. Nothing else, so an opaque ID +// (REPORTING_CLIENT_ID, enter_47f9e2) is never refused. +var placeholderID = regexp.MustCompile(`(?i)^((paste|enter|insert|put|replace)[_-])?(your[_-])?((oauth2?|google)[_-])?client[_-]?id([_-]here)?$|<[^<>]+>|\{\{|\$\{`) // connectFormFor fills the form from a preset for key name. func (b *Backend) connectFormFor(name, id string) connectForm { diff --git a/internal/dashboard/oauth2_connect_test.go b/internal/dashboard/oauth2_connect_test.go index 39c6aee..b4d295b 100644 --- a/internal/dashboard/oauth2_connect_test.go +++ b/internal/dashboard/oauth2_connect_test.go @@ -220,12 +220,12 @@ func TestOAuthPlaceholderClientIDRefused(t *testing.T) { t.Fatal("a placeholder client ID was saved") } // Opaque IDs of other providers that merely contain such words are fine. - for _, id := range []string{"REPORTING_CLIENT_ID", "WORKYOURWAY", "your.app.client"} { + for _, id := range []string{"REPORTING_CLIENT_ID", "WORKYOURWAY", "your.app.client", "enter_47f9e2", "app_here", "client_id_7f3"} { if got := clientIDProblem(&policy.OAuth2{ClientID: id, TokenURL: "https://auth.example.com/token"}); got != "" { t.Errorf("%s refused: %s", id, got) } } - for _, id := range []string{"YOUR_CLIENT_ID", "client_id", "PASTE-ID-HERE", "${CLIENT}", "{{client_id}}"} { + for _, id := range []string{"YOUR_CLIENT_ID", "client_id", "Enter-Your-OAuth-Client-ID-Here", "YOUR_GOOGLE_CLIENT_ID", "${CLIENT}", "{{client_id}}"} { if clientIDProblem(&policy.OAuth2{ClientID: id, TokenURL: "https://auth.example.com/token"}) == "" { t.Errorf("%s accepted", id) }