From 4bbe61844eec1bb0ef4af862cf43fde6e54d8a8b Mon Sep 17 00:00:00 2001 From: Jackson Hoyt Date: Tue, 17 Nov 2026 10:08:05 -0500 Subject: [PATCH 1/2] Allow string app IDs matching [A-Za-z0-9_.] The credentials file previously required the app id to be exactly "100". App ids may now be any non-empty string of letters, digits, '_' or '.'. Hyphens remain disallowed because the OAuth state parameter is {appID}-{redirectURL} and is split on the first hyphen. Co-Authored-By: Claude Opus 5.5 --- README.md | 2 ++ doc/spec/oss-architecture.md | 6 ++--- internal/config.go | 24 +++++++++++++---- internal/config_test.go | 52 +++++++++++++++++++++++++++++++++--- 4 files changed, 73 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 0991b60..384b3d6 100644 --- a/README.md +++ b/README.md @@ -168,6 +168,8 @@ idTokenNameKey: "name" idTokenEmailKey: "email" ``` +The `id` may contain only letters, digits, `_` and `.` (e.g. `"100"`, `"sheets_read"`, `"onedrive.v2"`). Hyphens are not allowed because the OAuth `state` parameter uses `-` as a separator. Clients discover the id via the app list endpoint and pass it back on authorize/token/refresh requests. + #### User Identity Keys After a successful token exchange, the proxy calls `testingUrl` to verify the token works and retrieve the user's identity. The four key fields tell the proxy where to find the user's name and email in the provider's response: diff --git a/doc/spec/oss-architecture.md b/doc/spec/oss-architecture.md index f24d8e2..e45f825 100644 --- a/doc/spec/oss-architecture.md +++ b/doc/spec/oss-architecture.md @@ -317,7 +317,7 @@ The page renders localized text (based on `Accept-Language`) and includes a Java ### Behavioral Notes -**State parameter format:** The state value is `{appID}-{redirectURL}`. It is split on the first hyphen, so app IDs must not contain hyphens. The desktop client receives this state from the OAuth provider callback and passes it back to `/v1/token`. +**State parameter format:** The state value is `{appID}-{redirectURL}`. It is split on the first hyphen; app IDs are restricted to `[A-Za-z0-9_.]`, so they never contain hyphens. The desktop client receives this state from the OAuth provider callback and passes it back to `/v1/token`. **Scope validation:** Token exchange verifies that all scopes configured in `creds.yaml` are present in the provider's response. If the provider returns fewer scopes (e.g., user denied a permission), the exchange fails with an error listing the missing scopes. @@ -428,7 +428,7 @@ idTokenEmailKey: "email" | Field | Required | Description | |-------|----------|-------------| -| `id` | Yes | App identifier (must be "100") | +| `id` | Yes | App identifier — letters, digits, `_` and `.` only (e.g. `"100"`, `"sheets_read"`); no hyphens | | `type` | Yes | App type label (e.g., "sheets", "onedrive") | | `clientId` | Yes | OAuth2 client ID | | `clientSecret` | Yes | OAuth2 client secret (kept server-side) | @@ -448,7 +448,7 @@ idTokenEmailKey: "email" | `idTokenNameKey` | No | JWT claim key for name (fallback if userinfo unavailable) | | `idTokenEmailKey` | No | JWT claim key for email (fallback if userinfo unavailable) | -Currently only a single app configuration (ID "100") is supported per instance. +Currently only a single app configuration is supported per instance. Its `id` may contain letters, digits, `_` and `.`. ## 7. Security Model diff --git a/internal/config.go b/internal/config.go index 169121e..e57b672 100644 --- a/internal/config.go +++ b/internal/config.go @@ -170,13 +170,10 @@ func validateApps(apps map[string]AppConfig) error { if len(apps) != 1 { return fmt.Errorf("exactly one app must be defined in creds.yaml, got %d", len(apps)) } - if _, ok := apps["100"]; !ok { - return fmt.Errorf("app with id '100' must be defined in creds.yaml") - } for _, app := range apps { - if strings.Contains(app.ID, "-") { - return fmt.Errorf("app id cannot contain hyphens: %s", app.ID) + if !isValidAppID(app.ID) { + return fmt.Errorf("app id must contain only letters, digits, '_' or '.': %q", app.ID) } if err := validateAppFields(app); err != nil { @@ -191,6 +188,23 @@ func validateApps(apps map[string]AppConfig) error { return nil } +// isValidAppID reports whether id is a non-empty string matching +// [A-Za-z0-9_.]+. Hyphens are excluded because the OAuth state format +// ({appID}-{redirectURL}) is split on the first hyphen. +func isValidAppID(id string) bool { + if id == "" { + return false + } + for _, c := range id { + switch { + case c >= 'a' && c <= 'z', c >= 'A' && c <= 'Z', c >= '0' && c <= '9', c == '_', c == '.': + default: + return false + } + } + return true +} + func validateAppFields(app AppConfig) error { val := reflect.ValueOf(app) typ := reflect.TypeFor[AppConfig]() diff --git a/internal/config_test.go b/internal/config_test.go index 853903c..777313a 100644 --- a/internal/config_test.go +++ b/internal/config_test.go @@ -3,6 +3,9 @@ package proxy import ( + "os" + "path/filepath" + "strings" "testing" "github.com/stretchr/testify/require" @@ -139,15 +142,58 @@ func TestValidateApps_NoDashes(t *testing.T) { r.Error(err) } -func TestValidateApps_RequiresID100(t *testing.T) { +func TestValidateApps_AcceptsArbitraryNumericID(t *testing.T) { r := require.New(t) apps := map[string]AppConfig{ - "200": fullTestApp("200"), + "4217": fullTestApp("4217"), } + r.NoError(validateApps(apps)) +} + +func TestValidateApps_AcceptsAlphanumericUnderscoreDotID(t *testing.T) { + r := require.New(t) + + id := "Sheets_read.v2" + apps := map[string]AppConfig{id: fullTestApp(id)} + r.NoError(validateApps(apps)) +} + +func TestValidateApps_RejectsSpaceInID(t *testing.T) { + r := require.New(t) + + id := "sheets read" + apps := map[string]AppConfig{id: fullTestApp(id)} + err := validateApps(apps) + r.Error(err) + r.Contains(err.Error(), "letters, digits") +} + +func TestValidateApps_RejectsNonASCIIID(t *testing.T) { + r := require.New(t) + + id := "café" + apps := map[string]AppConfig{id: fullTestApp(id)} err := validateApps(apps) r.Error(err) - r.Contains(err.Error(), "id '100'") + r.Contains(err.Error(), "letters, digits") +} + +func TestLoadApps_UnquotedNumericID(t *testing.T) { + r := require.New(t) + + orig, err := os.ReadFile("testdata/creds.yaml") + r.NoError(err) + content := strings.Replace(string(orig), `id: "100"`, "id: 4217", 1) + r.NotEqual(string(orig), content) + + path := filepath.Join(t.TempDir(), "creds.yaml") + r.NoError(os.WriteFile(path, []byte(content), 0o600)) + + apps, err := LoadApps(path, zap.NewNop()) + r.NoError(err) + r.Contains(apps, "4217") + r.Equal("4217", apps["4217"].ID) } func TestValidateApps_RequiresEmailScope(t *testing.T) { From 4c910bd4b764cf721aec887ef53b6001d5872901 Mon Sep 17 00:00:00 2001 From: Jackson Hoyt Date: Tue, 17 Nov 2026 11:36:07 -0500 Subject: [PATCH 2/2] Validate app IDs with a regex; consolidate ID tests Address review feedback: - Replace the hand-written character loop with a single compiled appIDPattern regex, and reference it in the error message so the rule is stated in one place. - Replace the one-off ID tests with an accepted-IDs test (including pure-numeric "100" and the README examples) and a rejected-IDs test covering hyphens, whitespace, slashes, punctuation, URL delimiters and non-ASCII characters. Co-Authored-By: Claude Opus 5.5 --- internal/config.go | 27 ++++++------------ internal/config_test.go | 62 +++++++++++++++++------------------------ 2 files changed, 33 insertions(+), 56 deletions(-) diff --git a/internal/config.go b/internal/config.go index e57b672..0759306 100644 --- a/internal/config.go +++ b/internal/config.go @@ -8,6 +8,7 @@ import ( "io" "os" "reflect" + "regexp" "slices" "strings" "time" @@ -166,14 +167,19 @@ func LoadApps(filePath string, logger *zap.Logger) (map[string]AppConfig, error) return apps, nil } +// appIDPattern restricts app IDs to letters, digits, '_' and '.'. Hyphens are +// excluded because the OAuth state format ({appID}-{redirectURL}) is split on +// the first hyphen. +var appIDPattern = regexp.MustCompile(`^[A-Za-z0-9_.]+$`) + func validateApps(apps map[string]AppConfig) error { if len(apps) != 1 { return fmt.Errorf("exactly one app must be defined in creds.yaml, got %d", len(apps)) } for _, app := range apps { - if !isValidAppID(app.ID) { - return fmt.Errorf("app id must contain only letters, digits, '_' or '.': %q", app.ID) + if !appIDPattern.MatchString(app.ID) { + return fmt.Errorf("app id %q must match %s", app.ID, appIDPattern) } if err := validateAppFields(app); err != nil { @@ -188,23 +194,6 @@ func validateApps(apps map[string]AppConfig) error { return nil } -// isValidAppID reports whether id is a non-empty string matching -// [A-Za-z0-9_.]+. Hyphens are excluded because the OAuth state format -// ({appID}-{redirectURL}) is split on the first hyphen. -func isValidAppID(id string) bool { - if id == "" { - return false - } - for _, c := range id { - switch { - case c >= 'a' && c <= 'z', c >= 'A' && c <= 'Z', c >= '0' && c <= '9', c == '_', c == '.': - default: - return false - } - } - return true -} - func validateAppFields(app AppConfig) error { val := reflect.ValueOf(app) typ := reflect.TypeFor[AppConfig]() diff --git a/internal/config_test.go b/internal/config_test.go index 777313a..cf3c006 100644 --- a/internal/config_test.go +++ b/internal/config_test.go @@ -132,51 +132,39 @@ func TestLoadApps_MissingFile(t *testing.T) { r.Contains(err.Error(), "read creds file") } -func TestValidateApps_NoDashes(t *testing.T) { - r := require.New(t) - - apps := map[string]AppConfig{ - "bad-id": {ID: "bad-id"}, - } - err := validateApps(apps) - r.Error(err) -} - -func TestValidateApps_AcceptsArbitraryNumericID(t *testing.T) { - r := require.New(t) - - apps := map[string]AppConfig{ - "4217": fullTestApp("4217"), - } - r.NoError(validateApps(apps)) -} - -func TestValidateApps_AcceptsAlphanumericUnderscoreDotID(t *testing.T) { - r := require.New(t) - - id := "Sheets_read.v2" +func requireValidAppID(t *testing.T, id string) { + t.Helper() apps := map[string]AppConfig{id: fullTestApp(id)} - r.NoError(validateApps(apps)) + require.NoError(t, validateApps(apps), "expected app id %q to be accepted", id) } -func TestValidateApps_RejectsSpaceInID(t *testing.T) { - r := require.New(t) - - id := "sheets read" +func requireInvalidAppID(t *testing.T, id string) { + t.Helper() apps := map[string]AppConfig{id: fullTestApp(id)} err := validateApps(apps) - r.Error(err) - r.Contains(err.Error(), "letters, digits") + require.Error(t, err, "expected app id %q to be rejected", id) + require.Contains(t, err.Error(), appIDPattern.String()) } -func TestValidateApps_RejectsNonASCIIID(t *testing.T) { - r := require.New(t) +func TestValidateApps_AcceptsAllowedAppIDs(t *testing.T) { + requireValidAppID(t, "100") // pure numeric, the historical default + requireValidAppID(t, "0") // single character + requireValidAppID(t, "sheets_read") // README example + requireValidAppID(t, "onedrive.v2") // README example + requireValidAppID(t, "Sheets_read.v2") // mixed case with every allowed symbol + requireValidAppID(t, "_.") // symbols only +} - id := "café" - apps := map[string]AppConfig{id: fullTestApp(id)} - err := validateApps(apps) - r.Error(err) - r.Contains(err.Error(), "letters, digits") +func TestValidateApps_RejectsDisallowedAppIDs(t *testing.T) { + requireInvalidAppID(t, "my-app") // hyphen breaks the {appID}-{redirectURL} state format + requireInvalidAppID(t, "-100") // leading hyphen + requireInvalidAppID(t, "sheets read") // space + requireInvalidAppID(t, " 100") // leading whitespace + requireInvalidAppID(t, "100\n") // trailing newline + requireInvalidAppID(t, "sheets/read") // slash + requireInvalidAppID(t, "sheets!") // punctuation outside the allowed set + requireInvalidAppID(t, "a&b=c") // URL query delimiters + requireInvalidAppID(t, "café") // non-ASCII letter } func TestLoadApps_UnquotedNumericID(t *testing.T) {