diff --git a/internal/dashboard/dashboard.go b/internal/dashboard/dashboard.go index 3c1ac5a..e60a016 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" @@ -911,6 +912,30 @@ 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. 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 { + 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(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 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 { f := connectForm{Preset: id, Auth: policy.AuthBearer} @@ -1020,6 +1045,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..b4d295b 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,64 @@ 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") + } + // Opaque IDs of other providers that merely contain such words are fine. + 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", "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) + } + } + 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) + } + // 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 e4bd6ea..b5e2ca4 100644 --- a/internal/dashboard/services.go +++ b/internal/dashboard/services.go @@ -224,6 +224,12 @@ 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 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 + } if next.ClientSecretKey != "" { stored, err := b.storedNames() if err != nil {